fix calling bool() and float() without arguments
authorStefan Behnel <scoder@users.berlios.de>
Thu, 15 Apr 2010 21:18:08 +0000 (23:18 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 15 Apr 2010 21:18:08 +0000 (23:18 +0200)
Cython/Compiler/Optimize.py
tests/run/empty_builtin_constructors.pyx [new file with mode: 0644]

index 1a574f5f50999fe6dd97abbf3c7fa9d52800731b..625f10e11ad65fbf77058863ffee97b9c4d28b17 100644 (file)
@@ -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 (file)
index 0000000..c205128
--- /dev/null
@@ -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()