Commit 1e12bf46 authored by Stefan Behnel's avatar Stefan Behnel

when indexing 'str', it's safe to infer 'str' for the result

parent f76ca90f
......@@ -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
......
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment