cython-devel

changeset 4007:77361d6754c0

fix '<int>bool(<int>x)' to always return 0 or 1 instead of plain '<int>x'
author Stefan Behnel <scoder@users.berlios.de>
date Thu Nov 25 07:03:10 2010 +0100 (2 years ago)
parents b1f04712264b
children b8653f818cc5
files Cython/Compiler/Optimize.py tests/run/boolean_context.pyx
line diff
1.1 --- a/Cython/Compiler/Optimize.py Wed Nov 24 23:00:04 2010 +0100 1.2 +++ b/Cython/Compiler/Optimize.py Thu Nov 25 07:03:10 2010 +0100 1.3 @@ -1860,8 +1860,12 @@ 1.4 self._error_wrong_arg_count('bool', node, pos_args, '0 or 1') 1.5 return node 1.6 else: 1.7 - return pos_args[0].coerce_to_boolean( 1.8 - self.current_env()).coerce_to_pyobject(self.current_env()) 1.9 + # => !!<bint>(x) to make sure it's exactly 0 or 1 1.10 + operand = pos_args[0].coerce_to_boolean(self.current_env()) 1.11 + operand = ExprNodes.NotNode(node.pos, operand = operand) 1.12 + operand = ExprNodes.NotNode(node.pos, operand = operand) 1.13 + # coerce back to Python object as that's the result we are expecting 1.14 + return operand.coerce_to_pyobject(self.current_env()) 1.15 1.16 ### builtin functions 1.17
2.1 --- a/tests/run/boolean_context.pyx Wed Nov 24 23:00:04 2010 +0100 2.2 +++ b/tests/run/boolean_context.pyx Thu Nov 25 07:03:10 2010 +0100 2.3 @@ -5,4 +5,13 @@ 2.4 True 2.5 """ 2.6 cdef int x = 5 2.7 - print bool(x) 2.8 + return bool(x) 2.9 + 2.10 +def test_bool_and_int(): 2.11 + """ 2.12 + >>> test_bool_and_int() 2.13 + 1 2.14 + """ 2.15 + cdef int x = 5 2.16 + cdef int b = bool(x) 2.17 + return b