Commit e4ef0c1e authored by da-woods's avatar da-woods Committed by GitHub

Error on memoryview argument capture on 0.29.x (GH-4849)

I don't believe it's easy to fix
https://github.com/cython/cython/issues/4798 on 0.29.x Therefore,
generate an error message that explains two possible workarounds.

This at least makes sure that people don't end up with mysterious crashes.
parent 22f4444a
......@@ -2637,6 +2637,9 @@ class CFuncDefNode(FuncDefNode):
def put_into_closure(entry):
if entry.in_closure and not arg.default:
code.putln('%s = %s;' % (entry.cname, entry.original_cname))
if entry.type.is_memoryviewslice:
code.put_incref_memoryviewslice(entry.cname, have_gil=True)
else:
code.put_var_incref(entry)
code.put_var_giveref(entry)
for arg in self.args:
......@@ -3234,6 +3237,14 @@ class DefNode(FuncDefNode):
# Move arguments into closure if required
def put_into_closure(entry):
if entry.in_closure:
if entry.type.is_memoryviewslice:
error(
self.pos,
"Referring to a memoryview typed argument directly in a nested closure function "
"is not supported in Cython 0.x. "
"Either upgrade to Cython 3, or assign the argument to a local variable "
"and use that in the nested function."
)
code.putln('%s = %s;' % (entry.cname, entry.original_cname))
if entry.xdecref_cleanup:
# mostly applies to the starstar arg - this can sometimes be NULL
......
......@@ -2549,3 +2549,23 @@ def test_const_buffer(const int[:] a):
cdef const int[:] c = a
print(a[0])
print(c[-1])
cdef arg_in_closure_cdef(int [:] a):
def inner():
return (a[0], a[1])
return inner
def test_arg_in_closure_cdef(a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> inner = test_arg_in_closure_cdef(A)
acquired A
>>> inner()
(0, 1)
The assignment below is just to avoid printing what was collected
>>> del inner; ignore_me = gc.collect()
released A
"""
return arg_in_closure_cdef(a)
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