fix #480: float() as a type cast for function return values
authorStefan Behnel <scoder@users.berlios.de>
Sun, 24 Jan 2010 15:29:01 +0000 (16:29 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 24 Jan 2010 15:29:01 +0000 (16:29 +0100)
Cython/Compiler/Optimize.py
tests/run/float_len_T480.pyx

index 989ca2d80b5592d644c623abbda29bef432d27fc..d7fdf39bdcd392b8be156e8839e356038a07a636 100644 (file)
@@ -960,13 +960,15 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
                 if func_arg.type == node.type:
                     return func_arg
                 elif node.type.assignable_from(func_arg.type) or func_arg.type.is_float:
-                    return ExprNodes.CastNode(func_arg, node.type)
+                    return ExprNodes.TypecastNode(
+                        node.pos, operand=func_arg, type=node.type)
         elif function.name == 'float':
             if func_arg.type.is_float or node.type.is_float:
                 if func_arg.type == node.type:
                     return func_arg
                 elif node.type.assignable_from(func_arg.type) or func_arg.type.is_float:
-                    return ExprNodes.CastNode(func_arg, node.type)
+                    return ExprNodes.TypecastNode(
+                        node.pos, operand=func_arg, type=node.type)
         return node
 
     ### dispatch to specific optimisers
@@ -1115,7 +1117,8 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
         if func_arg.type is PyrexTypes.c_double_type:
             return func_arg
         elif node.type.assignable_from(func_arg.type) or func_arg.type.is_numeric:
-            return ExprNodes.CastNode(func_arg, node.type)
+            return ExprNodes.TypecastNode(
+                node.pos, operand=func_arg, type=node.type)
         return ExprNodes.PythonCapiCallNode(
             node.pos, "__Pyx_PyObject_AsDouble",
             self.PyObject_AsDouble_func_type,
index 7020570edd4bfd82a0d5b68d2f68b036508cbfb2..d5651a2014c3eb61fbff22fbea3de1d1db977d36 100644 (file)
@@ -1,9 +1,23 @@
 def f(x):
     return x
 
-def float_len(x):
+def len_f(x):
     """
-    >>> float_len([1,2,3])
+    >>> len_f([1,2,3])
+    3
+    """
+    return len(f(x))
+
+def float_len_f(x):
+    """
+    >>> float_len_f([1,2,3])
+    3.0
+    """
+    return float(len(f(x)))
+
+def cast_len_f(x):
+    """
+    >>> cast_len_f([1,2,3])
     3.0
     """
-    float(len(f(x)))
+    return <double>len(f(x))