code simplification in len(char*) optimisation
authorStefan Behnel <scoder@users.berlios.de>
Sun, 21 Mar 2010 19:24:18 +0000 (20:24 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 21 Mar 2010 19:24:18 +0000 (20:24 +0100)
Cython/Compiler/Optimize.py

index a0a82ba8f7169e8366420d8ab4e7742b26117000..c122c4c15aeec006c37d74e37a167e2ad1b8fc1b 100644 (file)
@@ -1232,20 +1232,15 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
             arg = arg.arg
         if not arg.type.is_string:
             return node
-        if not node.type.is_numeric:
-            # this optimisation only works when we already replaced
-            # len() by PyObject_Length() which returns a Py_ssize_t
-            # instead of a Python object, so we can return a plain
-            # size_t instead without caring about Python object
-            # conversion etc.
-            return node
-        node = ExprNodes.PythonCapiCallNode(
+        new_node = ExprNodes.PythonCapiCallNode(
             node.pos, "strlen", self.Pyx_strlen_func_type,
             args = [arg],
             is_temp = node.is_temp,
             utility_code = include_string_h_utility_code
             )
-        return node
+        if node.type not in (PyrexTypes.c_size_t_type, PyrexTypes.c_py_ssize_t_type):
+            new_node = new_node.coerce_to(node.type, self.env_stack[-1])
+        return new_node
 
     Pyx_Type_func_type = PyrexTypes.CFuncType(
         Builtin.type_type, [