From 9d5aaeaba8d73bb9de3275c305ef170fe493c3af Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 17 May 2010 16:43:08 +0200 Subject: [PATCH] fix indexing for inferred Py_UNICODE 'strings' --- Cython/Compiler/ExprNodes.py | 9 +++++++++ tests/run/py_unicode_type.pyx | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 3eac2bb3..429e8d39 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1983,6 +1983,15 @@ class IndexNode(ExprNode): elif not skip_child_analysis: self.index.analyse_types(env) self.original_index_type = self.index.type + if base_type is PyrexTypes.c_py_unicode_type: + # we infer Py_UNICODE for unicode strings in some + # cases, but indexing must still work for them + if self.index.constant_result in (0, -1): + # FIXME: we know that this node is redundant - + # currently, this needs to get handled in Optimize.py + pass + self.base = self.base.coerce_to_pyobject(env) + base_type = self.base.type if base_type.is_pyobject: if self.index.type.is_int: if (not setting diff --git a/tests/run/py_unicode_type.pyx b/tests/run/py_unicode_type.pyx index f0f9b3e8..3febe4e5 100644 --- a/tests/run/py_unicode_type.pyx +++ b/tests/run/py_unicode_type.pyx @@ -130,6 +130,18 @@ def len_uchar(Py_UNICODE uchar): """ return len(uchar) +def index_uchar(Py_UNICODE uchar, Py_ssize_t i): + """ + >>> index_uchar(ord('A'), 0) == ('A', 'A', 'A') + True + >>> index_uchar(ord('A'), -1) == ('A', 'A', 'A') + True + >>> index_uchar(ord('A'), 1) + Traceback (most recent call last): + IndexError: string index out of range + """ + return uchar[0], uchar[-1], uchar[i] + mixed_ustring = u'AbcDefGhIjKlmnoP' lower_ustring = mixed_ustring.lower() upper_ustring = mixed_ustring.lower() -- 2.26.2