"A fake environment for creating type test nodes etc."
nogil = False
+def unwrap_coerced_node(node, coercion_nodes=(ExprNodes.CoerceToPyTypeNode, ExprNodes.CoerceFromPyTypeNode)):
+ if isinstance(node, coercion_nodes):
+ return node.arg
+ return node
+
def unwrap_node(node):
while isinstance(node, UtilNodes.ResultRefNode):
node = node.expression
node, dict_obj=iterator, keys=True, values=False)
# C array (slice) iteration?
- plain_iterator = iterator
- if isinstance(iterator, ExprNodes.CoerceToPyTypeNode):
- plain_iterator = iterator.arg
+ plain_iterator = unwrap_coerced_node(iterator)
if isinstance(plain_iterator, ExprNodes.SliceIndexNode) and \
(plain_iterator.base.type.is_array or plain_iterator.base.type.is_ptr):
return self._transform_carray_iteration(node, plain_iterator)
- elif isinstance(plain_iterator, ExprNodes.IndexNode) and \
- isinstance(plain_iterator.index, (ExprNodes.SliceNode, ExprNodes.CoerceFromPyTypeNode)) and \
- (plain_iterator.base.type.is_array or plain_iterator.base.type.is_ptr):
- return self._transform_carray_iteration(node, plain_iterator)
- elif iterator.type.is_array:
+ if isinstance(plain_iterator, ExprNodes.IndexNode) and \
+ isinstance(plain_iterator.index, (ExprNodes.SliceNode, ExprNodes.CoerceFromPyTypeNode)):
+ iterator_base = unwrap_coerced_node(plain_iterator.base)
+ if iterator_base.type.is_array or iterator_base.type.is_ptr:
+ return self._transform_carray_iteration(node, plain_iterator)
+ if iterator.type.is_array:
return self._transform_carray_iteration(node, iterator)
- elif iterator.type in (Builtin.bytes_type, Builtin.unicode_type):
+ if iterator.type in (Builtin.bytes_type, Builtin.unicode_type):
return self._transform_string_iteration(node, iterator)
# the rest is based on function calls
return node
elif isinstance(slice_node, ExprNodes.IndexNode):
# slice_node.index must be a SliceNode
- slice_base = slice_node.base
- index = slice_node.index
- if isinstance(index, ExprNodes.CoerceFromPyTypeNode):
- index = index.arg
+ slice_base = unwrap_coerced_node(slice_node.base)
+ index = unwrap_coerced_node(slice_node.index)
start = index.start
stop = index.stop
step = index.step
stop = None
else:
stop = stop.coerce_to(PyrexTypes.c_py_ssize_t_type, self.current_scope)
+ if stop is None:
+ if neg_step:
+ stop = ExprNodes.IntNode(
+ slice_node.pos, value='-1', type=PyrexTypes.c_py_ssize_t_type, constant_result=-1)
+ else:
+ error(slice_node.pos, "C array iteration requires known step size and end index")
+ return node
ptr_type = slice_base.type
if ptr_type.is_array:
def slice_charptr_for_loop_c_step():
"""
>>> slice_charptr_for_loop_c_step()
- ['p', 't', 'q', 'C', 'B']
- ['p', 't', 'q', 'C', 'B']
+ Acba
+ ['A', 'c', 'b', 'a']
+ Acba
+ ['A', 'c', 'b', 'a']
+ bA
['b', 'A']
+ acB
['a', 'c', 'B']
+ acB
['a', 'c', 'B']
+ <BLANKLINE>
[]
+ ptqC
['p', 't', 'q', 'C']
+ pq
['p', 'q']
"""
+ cdef unicode ustring = cstring.decode('ASCII')
cdef char c
- print [ chr(c) for c in cstring[:3:-1] ]
- print [ chr(c) for c in cstring[None:3:-1] ]
+ print ustring[3::-1]
+ print [ chr(c) for c in cstring[3::-1] ]
+ print ustring[3:None:-1]
+ print [ chr(c) for c in cstring[3:None:-1] ]
+ print ustring[1:5:2]
print [ chr(c) for c in cstring[1:5:2] ]
+ print ustring[:5:2]
print [ chr(c) for c in cstring[:5:2] ]
+ print ustring[None:5:2]
print [ chr(c) for c in cstring[None:5:2] ]
+ print ustring[4:9:-1]
print [ chr(c) for c in cstring[4:9:-1] ]
+ print ustring[8:4:-1]
print [ chr(c) for c in cstring[8:4:-1] ]
+ print ustring[8:4:-2]
print [ chr(c) for c in cstring[8:4:-2] ]
@cython.test_assert_path_exists("//ForFromStatNode",