From: Stefan Behnel Date: Sun, 24 Jan 2010 15:29:01 +0000 (+0100) Subject: fix #480: float() as a type cast for function return values X-Git-Tag: 0.12.1~12 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=2520038ecf08b231a4f728a97ba8658c1b14fce5;p=cython.git fix #480: float() as a type cast for function return values --- diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 989ca2d8..d7fdf39b 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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, diff --git a/tests/run/float_len_T480.pyx b/tests/run/float_len_T480.pyx index 7020570e..d5651a20 100644 --- a/tests/run/float_len_T480.pyx +++ b/tests/run/float_len_T480.pyx @@ -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 len(f(x))