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

Reallow capture of memoryview arguments (GH-4929)

It was actually OK in def functions. It only looks very dodgy:
```
__Pyx_XDEC_MEMVIEW(closure->arg)
```
This gets called twice and `INC` gets called once. However this is
actually OK since XDEC really means "clear"

Fixes https://github.com/cython/cython/issues/4798
for 0.29.x (completely I think)
parent 67c44be9
...@@ -3237,14 +3237,6 @@ class DefNode(FuncDefNode): ...@@ -3237,14 +3237,6 @@ class DefNode(FuncDefNode):
# Move arguments into closure if required # Move arguments into closure if required
def put_into_closure(entry): def put_into_closure(entry):
if entry.in_closure: 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)) code.putln('%s = %s;' % (entry.cname, entry.original_cname))
if entry.xdecref_cleanup: if entry.xdecref_cleanup:
# mostly applies to the starstar arg - this can sometimes be NULL # mostly applies to the starstar arg - this can sometimes be NULL
......
...@@ -2556,6 +2556,25 @@ def test_const_buffer(const int[:] a): ...@@ -2556,6 +2556,25 @@ def test_const_buffer(const int[:] a):
print(a[0]) print(a[0])
print(c[-1]) print(c[-1])
@testcase
def test_arg_in_closure(int [:] a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> inner = test_arg_in_closure(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
"""
def inner():
return (a[0], a[1])
return inner
cdef arg_in_closure_cdef(int [:] a): cdef arg_in_closure_cdef(int [:] a):
def inner(): def inner():
return (a[0], a[1]) return (a[0], a[1])
......
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