From ae84b380b25e676ce93bc3d88364e5fab8c72cae Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 8 Sep 2010 07:18:49 +0200 Subject: [PATCH] partial optimisation for ord(Py_UNICODE) - better optimisation that returns a C integer result requires type analysis refactoring --- Cython/Compiler/Optimize.py | 11 +++++++++++ tests/run/builtin_ord.pyx | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/run/builtin_ord.pyx diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 3f03d571..79370bda 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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 index 00000000..38c20a3c --- /dev/null +++ b/tests/run/builtin_ord.pyx @@ -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 -- 2.26.2