partial optimisation for ord(Py_UNICODE) - better optimisation that returns a C integ...
authorStefan Behnel <scoder@users.berlios.de>
Wed, 8 Sep 2010 05:18:49 +0000 (07:18 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 8 Sep 2010 05:18:49 +0000 (07:18 +0200)
Cython/Compiler/Optimize.py
tests/run/builtin_ord.pyx [new file with mode: 0644]

index 3f03d5715ad2ec310038cc9ba78995a5656c9ce5..79370bda06ed11d760d2291fa3e954c9c8ace0e7 100644 (file)
@@ -1939,6 +1939,17 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
             test_node = UtilNodes.EvalWithTempExprNode(temp, test_node)
         return test_node
 
+    def _handle_simple_function_ord(self, node, pos_args):
+        """Unpack ord(Py_UNICODE).
+        """
+        if len(pos_args) != 1:
+            return node
+        arg = pos_args[0]
+        if isinstance(arg, ExprNodes.CoerceToPyTypeNode):
+            if arg.arg.type is PyrexTypes.c_py_unicode_type:
+                return arg.arg.coerce_to(node.type, self.current_env())
+        return node
+
     ### special methods
 
     Pyx_tp_new_func_type = PyrexTypes.CFuncType(
diff --git a/tests/run/builtin_ord.pyx b/tests/run/builtin_ord.pyx
new file mode 100644 (file)
index 0000000..38c20a3
--- /dev/null
@@ -0,0 +1,18 @@
+
+cimport cython
+
+ustring_with_a = u'abcdefg'
+ustring_without_a = u'bcdefg'
+
+@cython.test_fail_if_path_exists('//SimpleCallNode')
+def unicode_for_loop_ord(unicode s):
+    """
+    >>> unicode_for_loop_ord(ustring_with_a)
+    True
+    >>> unicode_for_loop_ord(ustring_without_a)
+    False
+    """
+    for c in s:
+        if ord(c) == u'a':
+            return True
+    return False