Commit ac88a9fa authored by Mark Florisson's avatar Mark Florisson

Support borrowed slices in cdef functions

parent e6d71f56
......@@ -1551,11 +1551,11 @@ class FuncDefNode(StatNode, BlockNode):
not entry.in_closure):
code.put_var_incref(entry)
# Note: defaults are always increffed. For def functions, we
# Note: defaults are always incref-ed. For def functions, we
# we aquire arguments from object converstion, so we have
# new references. If we are a cdef function, we need to
# incref our arguments
if is_cdef and entry.type.is_memoryviewslice:
elif is_cdef and entry.type.is_memoryviewslice and len(entry.cf_assignments) > 1:
code.put_incref_memoryviewslice(entry.cname,
have_gil=not lenv.nogil)
for entry in lenv.var_entries:
......@@ -1715,7 +1715,10 @@ class FuncDefNode(StatNode, BlockNode):
if ((acquire_gil or len(entry.cf_assignments) > 1) and
not entry.in_closure):
code.put_var_decref(entry)
if entry.type.is_memoryviewslice:
elif (entry.type.is_memoryviewslice and
(not is_cdef or len(entry.cf_assignments) > 1)):
# decref slices of def functions and acquired slices from cdef
# functions, but not borrowed slices from cdef functions.
code.put_xdecref_memoryviewslice(entry.cname,
have_gil=not lenv.nogil)
if self.needs_closure:
......
......@@ -1849,3 +1849,34 @@ cdef _test_slice_assignment_broadcast_strides(slice_1d src, slice_2d dst, slice_
for j in range(1, 3):
assert dst[i, j] == dst_f[i, j] == j - 1, (dst[i, j], dst_f[i, j], j - 1)
@testcase
def test_borrowed_slice():
"""
Test the difference between borrowed an non-borrowed slices. If you delete or assign
to a slice in a cdef function, it is not borrowed.
>>> test_borrowed_slice()
5
5
5
"""
cdef int i, carray[10]
for i in range(10):
carray[i] = i
_borrowed(carray)
_not_borrowed(carray)
_not_borrowed2(carray)
cdef _borrowed(int[:] m):
print m[5]
cdef _not_borrowed(int[:] m):
print m[5]
if object():
del m
cdef _not_borrowed2(int[:] m):
cdef int[10] carray
print m[5]
if object():
m = carray
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