Commit e1324fc1 authored by Stefan Behnel's avatar Stefan Behnel

fix calling bool() and float() without arguments

parent 20d9cddc
......@@ -1249,8 +1249,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
"""
# Note: this requires the float() function to be typed as
# returning a C 'double'
if len(pos_args) != 1:
self._error_wrong_arg_count('float', node, pos_args, 1)
if len(pos_args) == 0:
return ExprNode.FloatNode(
node, value="0.0", constant_result=0.0
).coerce_to(Builtin.float_type, self.current_env())
elif len(pos_args) != 1:
self._error_wrong_arg_count('float', node, pos_args, '0 or 1')
return node
func_arg = pos_args[0]
if isinstance(func_arg, ExprNodes.CoerceToPyTypeNode):
......@@ -1271,8 +1275,12 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
def _handle_simple_function_bool(self, node, pos_args):
"""Transform bool(x) into a type coercion to a boolean.
"""
if len(pos_args) != 1:
self._error_wrong_arg_count('bool', node, pos_args, 1)
if len(pos_args) == 0:
return ExprNodes.BoolNode(
node.pos, value=False, constant_result=False
).coerce_to(Builtin.bool_type, self.current_env())
elif len(pos_args) != 1:
self._error_wrong_arg_count('bool', node, pos_args, '0 or 1')
return node
return pos_args[0].coerce_to_boolean(
self.current_env()).coerce_to_pyobject(self.current_env())
......
cimport cython
import sys
IS_PY3 = sys.version_info[0] >= 3
def _bool():
"""
>>> _bool() == bool()
True
"""
return bool()
def _int():
"""
>>> _int() == int()
True
"""
return int()
def _long():
"""
>>> IS_PY3 or _long() == long()
True
"""
return long()
def _float():
"""
>>> _float() == float()
True
"""
return float()
def _complex():
"""
>>> _complex() == complex()
True
"""
return complex()
def _bytes():
"""
>>> IS_PY3 and _bytes() == bytes() or _bytes() == str()
True
"""
return bytes()
def _str():
"""
>>> _str() == str()
True
"""
return str()
def _unicode():
"""
>>> IS_PY3 and _unicode() == str() or _unicode() == unicode()
True
"""
return unicode()
def _tuple():
"""
>>> _tuple() == tuple()
True
"""
return tuple()
def _list():
"""
>>> _list() == list()
True
"""
return list()
def _dict():
"""
>>> _dict() == dict()
True
"""
return dict()
py_set = cython.set
def _set():
"""
>>> _set() == py_set()
True
"""
return set()
py_frozenset = cython.frozenset
def _frozenset():
"""
>>> _frozenset() == py_frozenset()
True
"""
return frozenset()
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