Commit 11a35a32 authored by Stefan Behnel's avatar Stefan Behnel

fix bytes iteration behaviour: Cython should be consistent whether it knows...

fix bytes iteration behaviour: Cython should be consistent whether it knows the iterable type or not
parent 9020634e
......@@ -248,6 +248,11 @@ class IterationTransform(Visitor.VisitorTransform):
unpack_func_type = self.PyUnicode_AS_UNICODE_func_type
len_func_type = self.PyUnicode_GET_SIZE_func_type
elif slice_node.type is Builtin.bytes_type:
target_type = node.target.type
if not target_type.is_int:
# bytes iteration returns bytes objects in Py2, but
# integers in Py3
return node
unpack_func = "PyBytes_AS_STRING"
unpack_func_type = self.PyBytes_AS_STRING_func_type
len_func = "PyBytes_GET_SIZE"
......
......@@ -49,21 +49,43 @@ def for_char_in_bytes(bytes s):
else:
return 'X'
#### Py2 and Py3 behave differently here: Py2->bytes, Py3->integer
##
## @cython.test_assert_path_exists("//ForFromStatNode")
## @cython.test_fail_if_path_exists("//ForInStatNode")
## def for_obj_in_bytes_slice(bytes s):
## """
## >>> for_obj_in_bytes_slice(bytes_abc)
## 'X'
## >>> for_obj_in_bytes_slice(bytes_ABC)
## 'B'
## >>> for_obj_in_bytes_slice(bytes_abc_null)
## 'X'
## >>> for_obj_in_bytes_slice(bytes_ABC_null)
## 'B'
## """
## for c in s[1:-1]:
## if c == b'B':
## return 'B'
## else:
## return 'X'
@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
def for_obj_in_bytes_slice(bytes s):
def for_char_in_bytes_slice(bytes s):
"""
>>> for_obj_in_bytes_slice(bytes_abc)
>>> for_char_in_bytes_slice(bytes_abc)
'X'
>>> for_obj_in_bytes_slice(bytes_ABC)
>>> for_char_in_bytes_slice(bytes_ABC)
'B'
>>> for_obj_in_bytes_slice(bytes_abc_null)
>>> for_char_in_bytes_slice(bytes_abc_null)
'X'
>>> for_obj_in_bytes_slice(bytes_ABC_null)
>>> for_char_in_bytes_slice(bytes_ABC_null)
'B'
"""
cdef char c
for c in s[1:-1]:
if c == b'B':
if c == c'B':
return 'B'
else:
return 'X'
......@@ -89,20 +111,22 @@ def for_char_in_enumerate_bytes(bytes s):
else:
return 'X'
@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
def for_pyvar_in_char_ptr(char* c_string):
"""
>>> for_pyvar_in_char_ptr( (bytes_abc+bytes_ABC) * 2 )
[True, True, True, False, False, False, True, True, True, False]
>>> for_pyvar_in_char_ptr( bytes_abc_null * 2 )
[True, False, True, False, True, True, False, True, False, True]
"""
in_test = []
cdef object c
for c in c_string[:10]:
in_test.append( c in b'abc' )
return in_test
#### Py2 and Py3 behave differently here: Py2->bytes, Py3->integer
##
## @cython.test_assert_path_exists("//ForFromStatNode")
## @cython.test_fail_if_path_exists("//ForInStatNode")
## def for_pyvar_in_char_ptr(char* c_string):
## """
## >>> for_pyvar_in_char_ptr( (bytes_abc+bytes_ABC) * 2 )
## [True, True, True, False, False, False, True, True, True, False]
## >>> for_pyvar_in_char_ptr( bytes_abc_null * 2 )
## [True, False, True, False, True, True, False, True, False, True]
## """
## in_test = []
## cdef object c
## for c in c_string[:10]:
## in_test.append( c in b'abc' )
## return in_test
@cython.test_assert_path_exists("//ForFromStatNode")
@cython.test_fail_if_path_exists("//ForInStatNode")
......
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