Commit 78b216f5 authored by Stefan Behnel's avatar Stefan Behnel

fix '<int>bool(<int>x)' to always return 0 or 1 instead of plain '<int>x'

parent a6b0f405
...@@ -1860,8 +1860,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1860,8 +1860,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
self._error_wrong_arg_count('bool', node, pos_args, '0 or 1') self._error_wrong_arg_count('bool', node, pos_args, '0 or 1')
return node return node
else: else:
return pos_args[0].coerce_to_boolean( # => !!<bint>(x) to make sure it's exactly 0 or 1
self.current_env()).coerce_to_pyobject(self.current_env()) operand = pos_args[0].coerce_to_boolean(self.current_env())
operand = ExprNodes.NotNode(node.pos, operand = operand)
operand = ExprNodes.NotNode(node.pos, operand = operand)
# coerce back to Python object as that's the result we are expecting
return operand.coerce_to_pyobject(self.current_env())
### builtin functions ### builtin functions
......
...@@ -5,4 +5,13 @@ def test(): ...@@ -5,4 +5,13 @@ def test():
True True
""" """
cdef int x = 5 cdef int x = 5
print bool(x) return bool(x)
def test_bool_and_int():
"""
>>> test_bool_and_int()
1
"""
cdef int x = 5
cdef int b = bool(x)
return b
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment