| 1 |
/* Test PEP 688 - Buffers */ |
| 2 |
|
| 3 |
#include "parts.h" |
| 4 |
|
| 5 |
|
| 6 |
#include <stddef.h> // offsetof |
| 7 |
|
| 8 |
typedef struct { |
| 9 |
PyObject_HEAD |
| 10 |
PyObject *obj; |
| 11 |
Py_ssize_t references; |
| 12 |
} testBufObject; |
| 13 |
|
| 14 |
#define testBufObject_CAST(op) ((testBufObject *)(op)) |
| 15 |
|
| 16 |
static PyObject * |
| 17 |
testbuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 18 |
{ |
| 19 |
PyObject *obj = PyBytes_FromString("test"); |
| 20 |
if (obj == NULL) { |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
testBufObject *self = (testBufObject *)type->tp_alloc(type, 0); |
| 24 |
if (self == NULL) { |
| 25 |
Py_DECREF(obj); |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
self->obj = obj; |
| 29 |
self->references = 0; |
| 30 |
return (PyObject *)self; |
| 31 |
} |
| 32 |
|
| 33 |
static int |
| 34 |
testbuf_traverse(PyObject *op, visitproc visit, void *arg) |
| 35 |
{ |
| 36 |
testBufObject *self = testBufObject_CAST(op); |
| 37 |
Py_VISIT(self->obj); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
static int |
| 42 |
testbuf_clear(PyObject *op) |
| 43 |
{ |
| 44 |
testBufObject *self = testBufObject_CAST(op); |
| 45 |
Py_CLEAR(self->obj); |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
testbuf_dealloc(PyObject *op) |
| 51 |
{ |
| 52 |
testBufObject *self = testBufObject_CAST(op); |
| 53 |
PyObject_GC_UnTrack(self); |
| 54 |
Py_XDECREF(self->obj); |
| 55 |
Py_TYPE(self)->tp_free(self); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
testbuf_getbuf(PyObject *op, Py_buffer *view, int flags) |
| 60 |
{ |
| 61 |
testBufObject *self = testBufObject_CAST(op); |
| 62 |
int buf = PyObject_GetBuffer(self->obj, view, flags); |
| 63 |
if (buf == 0) { |
| 64 |
Py_SETREF(view->obj, Py_NewRef(self)); |
| 65 |
self->references++; |
| 66 |
} |
| 67 |
return buf; |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
testbuf_releasebuf(PyObject *op, Py_buffer *Py_UNUSED(view)) |
| 72 |
{ |
| 73 |
testBufObject *self = testBufObject_CAST(op); |
| 74 |
self->references--; |
| 75 |
assert(self->references >= 0); |
| 76 |
} |
| 77 |
|
| 78 |
static PyBufferProcs testbuf_as_buffer = { |
| 79 |
.bf_getbuffer = testbuf_getbuf, |
| 80 |
.bf_releasebuffer = testbuf_releasebuf, |
| 81 |
}; |
| 82 |
|
| 83 |
static struct PyMemberDef testbuf_members[] = { |
| 84 |
{"references", Py_T_PYSSIZET, offsetof(testBufObject, references), Py_READONLY}, |
| 85 |
{NULL}, |
| 86 |
}; |
| 87 |
|
| 88 |
static PyTypeObject testBufType = { |
| 89 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 90 |
.tp_name = "testBufType", |
| 91 |
.tp_basicsize = sizeof(testBufObject), |
| 92 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 93 |
.tp_new = testbuf_new, |
| 94 |
.tp_dealloc = testbuf_dealloc, |
| 95 |
.tp_traverse = testbuf_traverse, |
| 96 |
.tp_clear = testbuf_clear, |
| 97 |
.tp_as_buffer = &testbuf_as_buffer, |
| 98 |
.tp_members = testbuf_members |
| 99 |
}; |
| 100 |
|
| 101 |
int |
| 102 |
_PyTestCapi_Init_Buffer(PyObject *m) { |
| 103 |
if (PyType_Ready(&testBufType) < 0) { |
| 104 |
return -1; |
| 105 |
} |
| 106 |
if (PyModule_AddObjectRef(m, "testBuf", (PyObject *)&testBufType)) { |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |