fixes and tests for enum in bool contexts and func args
authorLisandro Dalcin <dalcinl@gmail.com>
Wed, 14 Apr 2010 19:37:19 +0000 (16:37 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Wed, 14 Apr 2010 19:37:19 +0000 (16:37 -0300)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Nodes.py
tests/run/enumboolctx.pyx [new file with mode: 0644]

index cb4a688d6fa84ff728c777a47a3dc8470761a8e3..54d097addad24a9d988d724d235e1788dfe15221 100755 (executable)
@@ -593,7 +593,7 @@ class ExprNode(Node):
         if type.is_pyobject or type.is_ptr or type.is_float:
             return CoerceToBooleanNode(self, env)
         else:
-            if not type.is_int and not type.is_error:
+            if not (type.is_int or type.is_enum or type.is_error):
                 error(self.pos, 
                     "Type '%s' not acceptable as a boolean" % type)
             return self
index bad8e955e0dd90f9991e2ad8d1d2ba1c7a8b300b..b06eaf43d772f9adf06fd3474270417a078141d9 100644 (file)
@@ -2621,10 +2621,13 @@ class DefNode(FuncDefNode):
         func = new_type.from_py_function
         # copied from CoerceFromPyTypeNode
         if func:
-            code.putln("%s = %s(%s); %s" % (
-                arg.entry.cname,
-                func,
-                arg.hdr_cname,
+            lhs = arg.entry.cname
+            rhs = "%s(%s)" % (func, arg.hdr_cname)
+            if new_type.is_enum:
+                rhs = PyrexTypes.typecast(new_type, PyrexTypes.c_long_type, rhs)
+            code.putln("%s = %s; %s" % (
+                lhs, 
+                rhs,
                 code.error_goto_if(new_type.error_condition(arg.entry.cname), arg.pos)))
         else:
             error(arg.pos, 
diff --git a/tests/run/enumboolctx.pyx b/tests/run/enumboolctx.pyx
new file mode 100644 (file)
index 0000000..daa8497
--- /dev/null
@@ -0,0 +1,40 @@
+cdef public enum Truth:
+   FALSE=0
+   TRUE=1
+
+def enum_boolctx(Truth arg):
+    """
+    >>> enum_boolctx(FALSE)
+    False
+    >>> enum_boolctx(TRUE)
+    True
+    """
+    if arg:
+        return True
+    else:
+        return False
+
+cdef extern from *:
+    enum: FALSE_VALUE "(0)"
+    enum: TRUE_VALUE "(1)"
+
+def extern_enum_false():
+    """
+    >>> extern_enum_false()
+    """
+    if FALSE_VALUE:
+        raise ValueError
+
+def extern_enum_true():
+    """
+    >>> extern_enum_true()
+    """
+    if not TRUE_VALUE:
+        raise ValueError
+
+def extern_enum_false_true():
+    """
+    >>> extern_enum_false_true()
+    """
+    if not TRUE_VALUE or FALSE_VALUE:
+        raise ValueError