Fix an error with coercing C ints in boolean context.
authorCraig Citro <craigcitro@gmail.com>
Wed, 7 Jul 2010 08:11:24 +0000 (01:11 -0700)
committerCraig Citro <craigcitro@gmail.com>
Wed, 7 Jul 2010 08:11:24 +0000 (01:11 -0700)
Cython/Compiler/ExprNodes.py
tests/run/boolean_context.pyx [new file with mode: 0644]

index 852286f4cdeac810a70e9f09acc38e8df8b090b4..7239c635748d8bcf1c51aa1a0b2843f5801155f4 100755 (executable)
@@ -6268,7 +6268,12 @@ class CoerceToPyTypeNode(CoercionNode):
         return False
 
     def coerce_to_boolean(self, env):
-        return self.arg.coerce_to_boolean(env).coerce_to_temp(env)
+        arg_type = self.arg.type
+        if (arg_type == PyrexTypes.c_bint_type or
+            (arg_type.is_pyobject and arg_type.name == 'bool')):
+            return self.arg.coerce_to_temp(env)
+        else:
+            return CoerceToBooleanNode(self, env)
     
     def coerce_to_integer(self, env):
         # If not already some C integer type, coerce to longint.
diff --git a/tests/run/boolean_context.pyx b/tests/run/boolean_context.pyx
new file mode 100644 (file)
index 0000000..f6d59e1
--- /dev/null
@@ -0,0 +1,8 @@
+
+def test():
+    """
+    >>> test()
+    True
+    """
+    cdef int x = 5
+    print bool(x)