make len(Py_UNICODE) efficient
authorStefan Behnel <scoder@users.berlios.de>
Mon, 17 May 2010 11:23:31 +0000 (13:23 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Mon, 17 May 2010 11:23:31 +0000 (13:23 +0200)
Cython/Compiler/Optimize.py
tests/run/py_unicode_type.pyx

index 3ec676235b7749125a2ee55e602ab4b511a6f766..e874a75b29585a38b96921bdbea37ac0b7f68fbd 100644 (file)
@@ -1540,6 +1540,9 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
                 node.pos, cfunc_name, self.PyObject_Size_func_type,
                 args = [arg],
                 is_temp = node.is_temp)
+        elif arg.type is PyrexTypes.c_py_unicode_type:
+            return ExprNodes.IntNode(node.pos, value='1', constant_result=1,
+                                     type=node.type)
         else:
             return node
         if node.type not in (PyrexTypes.c_size_t_type, PyrexTypes.c_py_ssize_t_type):
index 5a93cc73c1c8dd18c024994b31ccd0f16f82a5f0..c7283144cd77bfe9fc9fa1e68333dc650abf759a 100644 (file)
@@ -119,3 +119,13 @@ def unicode_methods(Py_UNICODE uchar):
         uchar.upper(),
         uchar.title(),
         ]
+
+@cython.test_assert_path_exists('//IntNode')
+@cython.test_fail_if_path_exists('//SimpleCallNode',
+                                 '//PythonCapiCallNode')
+def len_uchar(Py_UNICODE uchar):
+    """
+    >>> len_uchar(ord('A'))
+    1
+    """
+    return len(uchar)