From: Stefan Behnel Date: Sun, 6 Feb 2011 15:07:39 +0000 (+0100) Subject: when indexing 'str', it's safe to infer 'str' for the result X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1e12bf466d2d78b15cf829c6c988a6fb152fe2a7;p=cython.git when indexing 'str', it's safe to infer 'str' for the result --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 228a5b2f..07b0db22 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2074,6 +2074,9 @@ class IndexNode(ExprNode): # to receive it, throw it away, and potentially rebuild it # on a subsequent PyObject coercion. return PyrexTypes.c_py_ucs4_type + elif base_type is str_type: + # always returns str - Py2: bytes, Py3: unicode + return base_type elif isinstance(self.base, BytesNode): #if env.global_scope().context.language_level >= 3: # # infering 'char' can be made to work in Python 3 mode diff --git a/tests/run/type_inference.pyx b/tests/run/type_inference.pyx index e51e43e4..845bb146 100644 --- a/tests/run/type_inference.pyx +++ b/tests/run/type_inference.pyx @@ -94,7 +94,7 @@ def indexing(): b = b"abc" assert typeof(b) == "bytes object", typeof(b) b1 = b[1] - assert typeof(b1) == "Python object", typeof(b1) + assert typeof(b1) == "Python object", typeof(b1) # Py2: bytes, Py3: int u = u"xyz" assert typeof(u) == "unicode object", typeof(u) u1 = u[1] @@ -102,7 +102,7 @@ def indexing(): s = "xyz" assert typeof(s) == "str object", typeof(s) s1 = s[1] - assert typeof(s1) == "Python object", typeof(s1) + assert typeof(s1) == "str object", typeof(s1) L = [1,2,3] assert typeof(L) == "list object", typeof(L) L1 = L[1] @@ -296,6 +296,7 @@ def loop_over_bytes(): Python object """ cdef bytes bytes_string = b'abcdefg' + # bytes in Py2, int in Py3 for c in bytes_string: pass return typeof(c) @@ -303,9 +304,10 @@ def loop_over_bytes(): def loop_over_str(): """ >>> print( loop_over_str() ) - Python object + str object """ cdef str string = 'abcdefg' + # str (bytes) in Py2, str (unicode) in Py3 for c in string: pass return typeof(c) @@ -316,6 +318,7 @@ def loop_over_unicode(): Py_UCS4 """ cdef unicode ustring = u'abcdefg' + # Py_UCS4 can represent any Unicode character for uchar in ustring: pass return typeof(uchar)