8000 FIX __releasebuffer__ in ReadonlyArrayWrapper · lorentzenchr/scikit-learn@e70770f · GitHub
[go: up one dir, main page]

Skip to content

Commit e70770f

Browse files
committed
FIX __releasebuffer__ in ReadonlyArrayWrapper
1 parent d9f351e commit e70770f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

sklearn/utils/_readonly_array_wrapper.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This way, we can use it on arrays that we don't touch.
1313

1414
from cpython cimport Py_buffer
1515
from cpython.buffer cimport PyObject_GetBuffer, PyBuffer_Release, PyBUF_WRITABLE
16+
from cpython.ref cimport Py_INCREF
1617

1718
import numpy as np
1819
cimport numpy as np
@@ -43,8 +44,15 @@ cdef class ReadonlyArrayWrapper:
4344
if request_for_writeable:
4445
# The following is a lie when self.wraps is readonly!
4546
buffer.readonly = False
47+
buffer.obj = self
4648

4749
def __releasebuffer__(self, Py_buffer *buffer):
50+
# restore the state when the buffer was created
51+
# because reassigning buffer.obj decrefs self, and the specification of
52+
# __releasebuffer__ ways we shouldn't do that
53+
Py_INCREF(self)
54+
buffer.obj = self.wraps
55+
buffer.readonly = True
4856
PyBuffer_Release(buffer)
4957

5058

0 commit comments

Comments
 (0)
0