diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-06-07 07:06:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-07 07:06:40 (GMT) |
commit | 1e4fa91104a03c44baa241326102cbec42d387f1 (patch) | |
tree | 54316b04170a217f1917769c680f598c7b31a33e /Modules | |
parent | 90ee51f1cd9e63cfe0f402392612f50fc1bea747 (diff) | |
download | cpython-1e4fa91104a03c44baa241326102cbec42d387f1.zip cpython-1e4fa91104a03c44baa241326102cbec42d387f1.tar.gz cpython-1e4fa91104a03c44baa241326102cbec42d387f1.tar.bz2 |
bpo-40724: Support setting buffer slots from type specs (GH-20648) (GH-20683)
This is not part of the limited API but makes the buffer slots available for type specs.
(cherry picked from commit f7c4e236429606e1c982cacf24e10fc86ef4462f)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 101d549..d6a90b8 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -6298,6 +6298,47 @@ static PyType_Spec HeapCTypeSubclass_spec = { HeapCTypeSubclass_slots }; +PyDoc_STRVAR(heapctypewithbuffer__doc__, +"Heap type with buffer support.\n\n" +"The buffer is set to [b'1', b'2', b'3', b'4']"); + +typedef struct { + HeapCTypeObject base; + char buffer[4]; +} HeapCTypeWithBufferObject; + +static int +heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject *self, Py_buffer *view, int flags) +{ + self->buffer[0] = '1'; + self->buffer[1] = '2'; + self->buffer[2] = '3'; + self->buffer[3] = '4'; + return PyBuffer_FillInfo( + view, (PyObject*)self, (void *)self->buffer, 4, 1, flags); +} + +static int +heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject *self, Py_buffer *view) +{ + assert(view->obj == (void*) self); +} + +static PyType_Slot HeapCTypeWithBuffer_slots[] = { + {Py_bf_getbuffer, heapctypewithbuffer_getbuffer}, + {Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer}, + {Py_tp_doc, (char*)heapctypewithbuffer__doc__}, + {0, 0}, +}; + +static PyType_Spec HeapCTypeWithBuffer_spec = { + "_testcapi.HeapCTypeWithBuffer", + sizeof(HeapCTypeWithBufferObject), + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + HeapCTypeWithBuffer_slots +}; + PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" "__class__ is set to plain HeapCTypeSubclass during finalization.\n" @@ -6775,6 +6816,12 @@ PyInit__testcapi(void) } PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref); + PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec); + if (HeapCTypeWithBuffer == NULL) { + return NULL; + } + PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer); + PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); if (subclass_with_finalizer_bases == NULL) { return NULL; |