From cabf946ed91b3f055a9dc339fd03e2d29c57fdc1 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 17 May 2010 13:23:31 +0200 Subject: [PATCH] make len(Py_UNICODE) efficient --- Cython/Compiler/Optimize.py | 3 +++ tests/run/py_unicode_type.pyx | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 3ec67623..e874a75b 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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): diff --git a/tests/run/py_unicode_type.pyx b/tests/run/py_unicode_type.pyx index 5a93cc73..c7283144 100644 --- a/tests/run/py_unicode_type.pyx +++ b/tests/run/py_unicode_type.pyx @@ -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) -- 2.26.2