fix indexing for inferred Py_UNICODE 'strings'
authorStefan Behnel <scoder@users.berlios.de>
Mon, 17 May 2010 14:43:08 +0000 (16:43 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Mon, 17 May 2010 14:43:08 +0000 (16:43 +0200)
Cython/Compiler/ExprNodes.py
tests/run/py_unicode_type.pyx

index 3eac2bb39c705c0ec7cb5757762d4be3b0ed8057..429e8d398bdc0e72e3a1b5bda8030d9039838e15 100755 (executable)
@@ -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
index f0f9b3e833b2b49dc9ba4b4b7f8007839fd28069..3febe4e5659f7d4fbaf378fa87e961804f275e9f 100644 (file)
@@ -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()