Commit 850c831d authored by Stefan Behnel's avatar Stefan Behnel

infer plain object for bytes_string[i] and str/unicode for indexed unicode/str objects

parent 8be7e855
...@@ -1892,12 +1892,12 @@ class IndexNode(ExprNode): ...@@ -1892,12 +1892,12 @@ class IndexNode(ExprNode):
return self.base.type_dependencies(env) return self.base.type_dependencies(env)
def infer_type(self, env): def infer_type(self, env):
if isinstance(self.base, StringNode): # FIXME: BytesNode? if isinstance(self.base, BytesNode):
return py_object_type return py_object_type
base_type = self.base.infer_type(env) base_type = self.base.infer_type(env)
if base_type.is_ptr or base_type.is_array: if base_type.is_ptr or base_type.is_array:
return base_type.base_type return base_type.base_type
elif base_type is Builtin.unicode_type and self.index.infer_type(env).is_int: elif base_type is unicode_type and self.index.infer_type(env).is_int:
# Py_UNICODE will automatically coerce to a unicode string # Py_UNICODE will automatically coerce to a unicode string
# if required, so this is safe. We only infer Py_UNICODE # if required, so this is safe. We only infer Py_UNICODE
# when the index is a C integer type. Otherwise, we may # when the index is a C integer type. Otherwise, we may
...@@ -1906,6 +1906,9 @@ class IndexNode(ExprNode): ...@@ -1906,6 +1906,9 @@ class IndexNode(ExprNode):
# to receive it, throw it away, and potentially rebuild it # to receive it, throw it away, and potentially rebuild it
# on a subsequent PyObject coercion. # on a subsequent PyObject coercion.
return PyrexTypes.c_py_unicode_type return PyrexTypes.c_py_unicode_type
elif base_type in (str_type, unicode_type):
# these types will always return themselves on Python indexing
return base_type
else: else:
# TODO: Handle buffers (hopefully without too much redundancy). # TODO: Handle buffers (hopefully without too much redundancy).
return py_object_type return py_object_type
......
...@@ -71,6 +71,27 @@ def slicing(): ...@@ -71,6 +71,27 @@ def slicing():
t1 = t[1:2] t1 = t[1:2]
assert typeof(t1) == "tuple object", typeof(t1) assert typeof(t1) == "tuple object", typeof(t1)
def indexing():
"""
>>> indexing()
"""
b = b"abc"
assert typeof(b) == "char *", typeof(b)
b1 = b[1]
assert typeof(b1) == "char", typeof(b1) # FIXME: bytes object ??
u = u"xyz"
assert typeof(u) == "unicode object", typeof(u)
u1 = u[1]
assert typeof(u1) == "Py_UNICODE", typeof(u1)
L = [1,2,3]
assert typeof(L) == "list object", typeof(L)
L1 = L[1]
assert typeof(L1) == "Python object", typeof(L1)
t = (4,5,6)
assert typeof(t) == "tuple object", typeof(t)
t1 = t[1]
assert typeof(t1) == "Python object", typeof(t1)
def multiple_assignments(): def multiple_assignments():
""" """
>>> multiple_assignments() >>> multiple_assignments()
...@@ -197,6 +218,15 @@ def loop_over_charptr(): ...@@ -197,6 +218,15 @@ def loop_over_charptr():
pass pass
return typeof(c) return typeof(c)
def loop_over_bytes_literal():
"""
>>> print( loop_over_bytes_literal() )
Python object
"""
for c in b'abcdefg':
pass
return typeof(c)
def loop_over_bytes(): def loop_over_bytes():
""" """
>>> print( loop_over_bytes() ) >>> print( loop_over_bytes() )
...@@ -207,6 +237,16 @@ def loop_over_bytes(): ...@@ -207,6 +237,16 @@ def loop_over_bytes():
pass pass
return typeof(c) return typeof(c)
def loop_over_str():
"""
>>> print( loop_over_str() )
str object
"""
cdef str string = 'abcdefg'
for c in string:
pass
return typeof(c)
def loop_over_unicode(): def loop_over_unicode():
""" """
>>> print( loop_over_unicode() ) >>> print( loop_over_unicode() )
......
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