pypy/module/cpyext/test/test_memoryobject.py::AppTestPyBuffer::()::test_releasebuffer
self = <pypy.interpreter.typedef.W_ObjectObjectUserDictWeakrefable object at 0x000001243deeb328>
def test_releasebuffer(self):
module = self.import_extension('foo', [
("create_test", "METH_NOARGS",
"""
PyObject *obj;
obj = PyObject_New(PyObject, (PyTypeObject*)type);
return obj;
"""),
("get_cnt", "METH_NOARGS",
'return PyLong_FromLong(cnt);'),
("get_dealloc_cnt", "METH_NOARGS",
'return PyLong_FromLong(dealloc_cnt);'),
],
prologue="""
static float test_data = 42.f;
static int cnt=0;
static int dealloc_cnt=0;
static PyHeapTypeObject * type=NULL;
void dealloc(PyObject *self) {
dealloc_cnt++;
}
int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
cnt ++;
memset(view, 0, sizeof(Py_buffer));
view->obj = obj;
/* see the CPython docs for why we need this incref:
https://docs.python.org/3.5/c-api/typeobj.html#c.PyBufferProcs.bf_getbuffer */
Py_INCREF(obj);
view->ndim = 0;
view->buf = (void *) &test_data;
view->itemsize = sizeof(float);
view->len = 1;
view->strides = NULL;
view->shape = NULL;
view->format = "f";
return 0;
}
void releasebuffer(PyObject *obj, Py_buffer *view) {
cnt --;
}
""", more_init="""
type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
type->ht_type.tp_name = "Test";
type->ht_type.tp_basicsize = sizeof(PyObject);
type->ht_name = PyUnicode_FromString("Test");
type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HEAPTYPE;
type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
type->ht_type.tp_dealloc = dealloc;
type->ht_type.tp_as_buffer = &type->as_buffer;
type->as_buffer.bf_getbuffer = getbuffer;
type->as_buffer.bf_releasebuffer = releasebuffer;
if (PyType_Ready(&type->ht_type) < 0) INITERROR;
""", )
import gc
assert module.get_cnt() == 0
a = memoryview(module.create_test())
assert module.get_cnt() == 1
assert module.get_dealloc_cnt() == 0
del a
self.debug_collect()
assert module.get_cnt() == 0
> assert module.get_dealloc_cnt() == 1
E (application-level) AssertionError
[d:\pypy_stuff\buildbot64\slave\own-win-x86-64\build\pypy\module\cpyext\test\test_memoryobject.py:296]:69: AssertionError
---------- Captured stdout call ----------
source_0.c
Creating library d:\systemtemp\pytest\usession-py3.11-73\foo-4\foo.lib and object d:\systemtemp\pytest\usession-py3.11-73\foo-4\foo.exp
Generating code
Finished generating code
dealloc_trigger...
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x1243b74a370>
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x1243b74ac10>
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x1243b74b150>
dealloc_trigger DONE
---------- Captured stderr call ----------
<function PyModule_Create2 at 0x0000012439b0a548> DONE
<function PyUnicode_FromString at 0x0000012439b74ae8> DONE
<function PyType_Ready at 0x000001243829d8f8> DONE
<function PyLong_FromLong at 0x0000012439b46890> DONE
<function PyLong_FromLong at 0x0000012439b46890> DONE
<function PyLong_FromLong at 0x0000012439b46890> DONE
<function PyLong_FromLong at 0x0000012439b46890> DONE
<function PyLong_FromLong at 0x0000012439b46890> DONE
builder: own-win-x86-64 build #2292+
test: pypy/module/cpyext/test/test_memoryobject.py::AppTestPyBuffer::()::test_releasebuffer