From e1324fc19bca5a2a65ce506952506c575f1882cd Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Thu, 15 Apr 2010 23:18:08 +0200 Subject: [PATCH] fix calling bool() and float() without arguments --- Cython/Compiler/Optimize.py | 16 +++- tests/run/empty_builtin_constructors.pyx | 100 +++++++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 tests/run/empty_builtin_constructors.pyx diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 1a574f5f..625f10e1 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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()) diff --git a/tests/run/empty_builtin_constructors.pyx b/tests/run/empty_builtin_constructors.pyx new file mode 100644 index 00000000..c2051288 --- /dev/null +++ b/tests/run/empty_builtin_constructors.pyx @@ -0,0 +1,100 @@ + +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() -- 2.26.2