Commit 95c699d3 authored by Stefan Behnel's avatar Stefan Behnel

enable array assignments from pointers

parent a0e2b420
......@@ -4070,10 +4070,7 @@ class SliceIndexNode(ExprNode):
array_length = rhs.type.size
self.generate_slice_guard_code(code, array_length)
else:
error(self.pos,
"Slice assignments from pointers are not yet supported.")
# FIXME: fix the array size according to start/stop
array_length = self.base.type.size
array_length = '%s - %s' % (self.stop_code(), start_offset)
def copy_carray(dst, src, item_type, start, count, depth):
var_name = Naming.quick_temp_cname
......
......@@ -2206,8 +2206,12 @@ class CArrayType(CPointerBaseType):
or other_type is error_type)
def assignable_from_resolved_type(self, src_type):
# Can't assign to a variable of an array type, except from Python containers
return src_type.is_pyobject
# C arrays are assigned by value, either Python containers or C arrays/pointers
if src_type.is_pyobject:
return True
if src_type.is_ptr or src_type.is_array:
return self.base_type.assignable_from(src_type.base_type)
return False
def element_ptr_type(self):
return c_ptr_type(self.base_type)
......
......@@ -149,3 +149,81 @@ def test_list(list l):
cdef int a[5]
a[:] = l
return (a[0], a[1], a[2], a[3], a[4])
def assign_all_from_pointer():
"""
>>> assign_all_from_pointer()
(1, 2, 3, 4, 5)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a = v
return (a[0], a[1], a[2], a[3], a[4])
def assign_full_from_pointer():
"""
>>> assign_full_from_pointer()
(1, 2, 3, 4, 5)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a[:] = v
return (a[0], a[1], a[2], a[3], a[4])
def assign_slice_end_from_pointer():
"""
>>> assign_slice_end_from_pointer()
(1, 2, 3, 4, 123)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a[4] = 123
a[:4] = v
return (a[0], a[1], a[2], a[3], a[4])
def assign_slice_start_from_pointer():
"""
>>> assign_slice_start_from_pointer()
(123, 234, 1, 2, 3)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a[0] = 123
a[1] = 234
a[2:] = v
return (a[0], a[1], a[2], a[3], a[4])
def assign_slice_start_end_from_pointer():
"""
>>> assign_slice_start_end_from_pointer()
(123, 234, 1, 2, 345)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a[0] = 123
a[1] = 234
a[4] = 345
a[2:4] = v
return (a[0], a[1], a[2], a[3], a[4])
'''
# FIXME: make this work:
def assign_slice_start_end_from_sliced_pointer():
"""
>>> assign_slice_start_end_from_sliced_pointer()
(123, 234, 3, 4, 345)
"""
cdef int *v = [1, 2, 3, 4, 5]
cdef int[5] a
a[0] = 123
a[1] = 234
a[4] = 345
a[2:4] = v[2:4]
return (a[0], a[1], a[2], a[3], a[4])
'''
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