From: Craig Citro Date: Wed, 7 Jul 2010 07:47:07 +0000 (-0700) Subject: Fix type inference on unary operators. X-Git-Tag: 0.13.beta0~32 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=2fccef53500c6e66e607cdc9afbb7ee6e3f42f7f;p=cython.git Fix type inference on unary operators. --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 7dbaf917..60e79b4e 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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) diff --git a/tests/run/type_inference.pyx b/tests/run/type_inference.pyx index d9d7293d..a05848f5 100644 --- a/tests/run/type_inference.pyx +++ b/tests/run/type_inference.pyx @@ -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()