Commit 36444962 authored by Stefan Behnel's avatar Stefan Behnel

enable direct assignments to C arrays by transforming to full slice assignment

parent fd565517
......@@ -1860,7 +1860,6 @@ class NameNode(AtomicExprNode):
def is_lvalue(self):
return (
self.entry.is_variable and
not self.entry.type.is_array and
not self.entry.is_readonly
) or (
self.entry.is_cfunction and
......@@ -11247,7 +11246,7 @@ class CoerceFromPyTypeNode(CoercionNode):
return self
def is_ephemeral(self):
return self.type.is_ptr and self.arg.is_ephemeral()
return (self.type.is_ptr and not self.type.is_array) and self.arg.is_ephemeral()
def generate_result_code(self, code):
code.putln(self.type.from_py_call_code(
......
......@@ -4750,14 +4750,20 @@ class SingleAssignmentNode(AssignmentNode):
self.lhs.memslice_broadcast = True
self.rhs.memslice_broadcast = True
is_index_node = isinstance(self.lhs, ExprNodes.IndexNode)
if (is_index_node and not self.rhs.type.is_memoryviewslice and
(self.lhs.memslice_slice or self.lhs.is_memslice_copy) and
(self.lhs.type.dtype.assignable_from(self.rhs.type) or
self.rhs.type.is_pyobject)):
if (self.lhs.is_subscript and not self.rhs.type.is_memoryviewslice and
(self.lhs.memslice_slice or self.lhs.is_memslice_copy) and
(self.lhs.type.dtype.assignable_from(self.rhs.type) or
self.rhs.type.is_pyobject)):
# scalar slice assignment
self.lhs.is_memslice_scalar_assignment = True
dtype = self.lhs.type.dtype
elif self.lhs.type.is_array:
if not isinstance(self.lhs, (ExprNodes.IndexNode, ExprNodes.SliceIndexNode)):
# cannot assign to C array, only to its full slice
self.lhs = ExprNodes.SliceIndexNode(
self.lhs.pos, base=self.lhs, start=None, stop=None)
self.lhs = self.lhs.analyse_target_types(env)
dtype = self.lhs.type
else:
dtype = self.lhs.type
......
......@@ -96,6 +96,21 @@ def to_int_array(x):
Traceback (most recent call last):
IndexError: too many values found during array assignment, expected 3
"""
cdef int[3] v = x
return v[0], v[1], v[2]
def to_int_array_slice(x):
"""
>>> to_int_array_slice([1, 2, 3])
(1, 2, 3)
>>> to_int_array_slice([1, 2])
Traceback (most recent call last):
IndexError: not enough values found during array assignment, expected 3, got 2
>>> to_int_array_slice([1, 2, 3, 4])
Traceback (most recent call last):
IndexError: too many values found during array assignment, expected 3
"""
cdef int[3] v
v[:] = x[:3]
assert v[0] == x[0]
......
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