Fix type inference on unary operators.
authorCraig Citro <craigcitro@gmail.com>
Wed, 7 Jul 2010 07:47:07 +0000 (00:47 -0700)
committerCraig Citro <craigcitro@gmail.com>
Wed, 7 Jul 2010 07:47:07 +0000 (00:47 -0700)
Cython/Compiler/ExprNodes.py
tests/run/type_inference.pyx

index 7dbaf917b3357d0dd2be6d88a929025e47cd9ce4..60e79b4eab291de40bbc6dc5b71911345317bfa4 100755 (executable)
@@ -4343,7 +4343,11 @@ class UnopNode(ExprNode):
             self.compile_time_value_error(e)
     
     def infer_type(self, env):
-        return self.operand.infer_type(env)
+        operand_type = self.operand.infer_type(env)
+        if operand_type.is_pyobject:
+            return py_object_type
+        else:
+            return operand_type
 
     def analyse_types(self, env):
         self.operand.analyse_types(env)
index d9d7293d3fd1ce42cd030ad06828e4ff9abb33d3..a05848f5fb909b13c32acfae10a751621794a41f 100644 (file)
@@ -122,6 +122,27 @@ def arithmetic():
     d = 1 * 1.5 ** 2
     assert typeof(d) == "double", typeof(d)
 
+cdef class some_class:
+    pass
+
+def unary_operators():
+    """
+    >>> unary_operators()
+    """
+    cdef int x = 1
+    assert typeof(~x) == "int", typeof(~x)
+    cdef some_class obj
+    assert typeof(~obj) == "Python object", typeof(~obj)
+    a = int(1)
+    assert typeof(a) == "Python object", typeof(a)
+    b = not int(3)
+    assert typeof(b) == "int", typeof(b)
+    c = +int(3)
+    assert typeof(c) == "Python object", typeof(c)
+    d = -int(5)
+    assert typeof(d) == "Python object", typeof(d)
+    
+
 def builtin_type_operations():
     """
     >>> builtin_type_operations()