Author: Antonio Cuni <[email protected]>
Branch: fix-cpyext-releasebuffer
Changeset: r90438:6b28fd38f5bc
Date: 2017-03-01 14:30 +0100
http://bitbucket.org/pypy/pypy/changeset/6b28fd38f5bc/

Log:    (antocuni, rlamy, arigo around): fix test_releasebuffer by making
        sure that we do the correct amount of decrefs, so that the original
        object is correctly deallocated.

diff --git a/pypy/module/cpyext/memoryobject.py 
b/pypy/module/cpyext/memoryobject.py
--- a/pypy/module/cpyext/memoryobject.py
+++ b/pypy/module/cpyext/memoryobject.py
@@ -72,6 +72,8 @@
     format = 'B'
     if view.c_format:
         format = rffi.charp2str(view.c_format)
+    incref(space, view.c_obj) # we need this incref because
+                              # CPyBuffer.releasebuffer does a decref
     buf = CPyBuffer(space, view.c_buf, view.c_len, from_ref(space, view.c_obj),
                     format=format, shape=shape, strides=strides,
                     ndim=ndim, itemsize=view.c_itemsize,
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -14,7 +14,7 @@
     ssizessizeargfunc, ssizeobjargproc, iternextfunc, initproc, richcmpfunc,
     cmpfunc, hashfunc, descrgetfunc, descrsetfunc, objobjproc, objobjargproc,
     readbufferproc, getbufferproc, releasebufferproc, ssizessizeobjargproc)
-from pypy.module.cpyext.pyobject import make_ref, decref
+from pypy.module.cpyext.pyobject import make_ref, decref, as_pyobj
 from pypy.module.cpyext.pyerrors import PyErr_Occurred
 from pypy.module.cpyext.memoryobject import fill_Py_buffer
 from pypy.module.cpyext.state import State
@@ -333,7 +333,7 @@
         self.ptr = ptr
         self.size = size
         self.w_obj = w_obj # kept alive
-        self.pyobj = make_ref(space, w_obj)
+        self.pyobj = as_pyobj(space, w_obj)
         self.format = format
         if not shape:
             self.shape = [size]
@@ -350,6 +350,9 @@
 
     def releasebuffer(self):
         if self.pyobj:
+            # the CPython docs mandates that you do an incref whenever you
+            # call bf_getbuffer. This is the corresponding decref:
+            #   
https://docs.python.org/3.5/c-api/typeobj.html#c.PyBufferProcs.bf_getbuffer
             decref(self.space, self.pyobj)
             self.pyobj = lltype.nullptr(PyObject.TO)
         else:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to