implement minor special case of indexing into an inferred 1-char Unicode string
authorStefan Behnel <stefan_ml@behnel.de>
Sat, 9 Nov 2013 13:57:57 +0000 (14:57 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Sat, 9 Nov 2013 13:57:57 +0000 (14:57 +0100)
--HG--
extra : amend_source : 068dca3eae20906b495571f1d80548dc5f99ed51

Cython/Compiler/ExprNodes.py

index c041053..3d91371 100644 (file)
@@ -3011,15 +3011,16 @@ class IndexNode(ExprNode):
             if base_type.is_unicode_char:
                 # we infer Py_UNICODE/Py_UCS4 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
+                if setting:
+                    warning(self.pos, "cannot assign to Unicode string index", level=1)
+                elif self.index.constant_result in (0, -1):
+                    # uchar[0] => uchar
+                    return self.base
                 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
+                    if (getting
                         and (base_type in (list_type, tuple_type, bytearray_type))
                         and (not self.index.type.signed
                              or not env.directives['wraparound']