Home - Summaries: (main) : (py3.11) : Everything - Nightly builds - Benchmarks - RPython - Builders - About

pypy/module/cpyext/test/test_memoryobject.py::AppTestPyBuffer::()::test_releasebuffer

self = <pypy.interpreter.typedef.W_ObjectObjectUserDictWeakrefable object at 0x000000012b8ced40>

    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

[/Users/matti/build-worker-arm64/own-macos-arm64/build/pypy/module/cpyext/test/test_memoryobject.py:296]:69: AssertionError
---------- Captured stdout call ----------
dealloc_trigger...
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x600002b982c0>
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x600002b98ac0>
deallocating PyObject * <C object Struct _object { c_ob_refcnt, c_ob_pypy_link, c_ob_type } at 0x600002b98340>
dealloc_trigger DONE
---------- Captured stderr call ----------
<function PyModule_Create2 at 0x0000000139681178>  DONE
<function PyUnicode_FromString at 0x00000001082c9e20>  DONE
<function PyType_Ready at 0x00000001396d6ea8>  DONE
<function PyLong_FromLong at 0x0000000119f4a3e0>  DONE
<function PyLong_FromLong at 0x0000000119f4a3e0>  DONE
<function PyLong_FromLong at 0x0000000119f4a3e0>  DONE
<function PyLong_FromLong at 0x0000000119f4a3e0>  DONE
<function PyLong_FromLong at 0x0000000119f4a3e0>  DONE
builder: own-macos-arm64 build #1263+
test: pypy/module/cpyext/test/test_memoryobject.py::AppTestPyBuffer::()::test_releasebuffer