[Python-checkins] gh-101819: Port _io.PyBytesIOBuffer_Type to heap type (#104264)

erlend-aasland webhook-mailer at python.org
Sun May 7 10:01:34 EDT 2023


https://github.com/python/cpython/commit/7a7eaff95c7a400449822bbabd94524b8f87299c
commit: 7a7eaff95c7a400449822bbabd94524b8f87299c
branch: main
author: Erlend E. Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-05-07T14:01:27Z
summary:

gh-101819: Port _io.PyBytesIOBuffer_Type to heap type (#104264)

files:
M Modules/_io/_iomodule.c
M Modules/_io/_iomodule.h
M Modules/_io/bytesio.c
M Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index b72b847c6634..c05407b5d618 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -671,9 +671,6 @@ static PyTypeObject* static_types[] = {
     &PyBufferedIOBase_Type,
     &PyRawIOBase_Type,
     &PyTextIOBase_Type,
-
-    // PyRawIOBase_Type(PyIOBase_Type) subclasses
-    &_PyBytesIOBuffer_Type,
 };
 
 
@@ -753,6 +750,7 @@ PyInit__io(void)
 
     // Base classes
     ADD_TYPE(m, state->PyIncrementalNewlineDecoder_Type, &nldecoder_spec, NULL);
+    ADD_TYPE(m, state->PyBytesIOBuffer_Type, &bytesiobuf_spec, NULL);
 
     // PyIOBase_Type subclasses
     state->PyRawIOBase_Type = (PyTypeObject *)Py_NewRef(&PyRawIOBase_Type);
@@ -771,7 +769,6 @@ PyInit__io(void)
              state->PyBufferedIOBase_Type);
 
     // PyRawIOBase_Type(PyIOBase_Type) subclasses
-    state->PyBytesIOBuffer_Type = (PyTypeObject *)Py_NewRef(&_PyBytesIOBuffer_Type);
     ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, state->PyRawIOBase_Type);
 #ifdef MS_WINDOWS
     ADD_TYPE(m, state->PyWindowsConsoleIO_Type, &winconsoleio_spec,
diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h
index 00e6a19db2b8..1bf301c9cf0a 100644
--- a/Modules/_io/_iomodule.h
+++ b/Modules/_io/_iomodule.h
@@ -20,6 +20,7 @@ extern PyType_Spec bufferedreader_spec;
 extern PyType_Spec bufferedrwpair_spec;
 extern PyType_Spec bufferedwriter_spec;
 extern PyType_Spec bytesio_spec;
+extern PyType_Spec bytesiobuf_spec;
 extern PyType_Spec fileio_spec;
 extern PyType_Spec nldecoder_spec;
 extern PyType_Spec stringio_spec;
@@ -194,5 +195,3 @@ extern _PyIO_State *_PyIO_get_module_state(void);
 #ifdef HAVE_WINDOWS_CONSOLE_IO
 extern char _PyIO_get_console_type(PyObject *);
 #endif
-
-extern Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type;
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 9c7a28357987..3fddfc2ed0bc 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -1090,9 +1090,17 @@ bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
     b->exports--;
 }
 
+static int
+bytesiobuf_clear(bytesiobuf *self)
+{
+    Py_CLEAR(self->source);
+    return 0;
+}
+
 static int
 bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
 {
+    Py_VISIT(Py_TYPE(self));
     Py_VISIT(self->source);
     return 0;
 }
@@ -1100,54 +1108,29 @@ bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
 static void
 bytesiobuf_dealloc(bytesiobuf *self)
 {
+    PyTypeObject *tp = Py_TYPE(self);
     /* bpo-31095: UnTrack is needed before calling any callbacks */
     PyObject_GC_UnTrack(self);
-    Py_CLEAR(self->source);
-    Py_TYPE(self)->tp_free(self);
+    (void)bytesiobuf_clear(self);
+    tp->tp_free(self);
+    Py_DECREF(tp);
 }
 
-static PyBufferProcs bytesiobuf_as_buffer = {
-    (getbufferproc) bytesiobuf_getbuffer,
-    (releasebufferproc) bytesiobuf_releasebuffer,
+static PyType_Slot bytesiobuf_slots[] = {
+    {Py_tp_dealloc, bytesiobuf_dealloc},
+    {Py_tp_traverse, bytesiobuf_traverse},
+    {Py_tp_clear, bytesiobuf_clear},
+
+    // Buffer protocol
+    {Py_bf_getbuffer, bytesiobuf_getbuffer},
+    {Py_bf_releasebuffer, bytesiobuf_releasebuffer},
+    {0, NULL},
 };
 
-Py_EXPORTED_SYMBOL PyTypeObject _PyBytesIOBuffer_Type = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    "_io._BytesIOBuffer",                      /*tp_name*/
-    sizeof(bytesiobuf),                        /*tp_basicsize*/
-    0,                                         /*tp_itemsize*/
-    (destructor)bytesiobuf_dealloc,            /*tp_dealloc*/
-    0,                                         /*tp_vectorcall_offset*/
-    0,                                         /*tp_getattr*/
-    0,                                         /*tp_setattr*/
-    0,                                         /*tp_as_async*/
-    0,                                         /*tp_repr*/
-    0,                                         /*tp_as_number*/
-    0,                                         /*tp_as_sequence*/
-    0,                                         /*tp_as_mapping*/
-    0,                                         /*tp_hash*/
-    0,                                         /*tp_call*/
-    0,                                         /*tp_str*/
-    0,                                         /*tp_getattro*/
-    0,                                         /*tp_setattro*/
-    &bytesiobuf_as_buffer,                     /*tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,   /*tp_flags*/
-    0,                                         /*tp_doc*/
-    (traverseproc)bytesiobuf_traverse,         /*tp_traverse*/
-    0,                                         /*tp_clear*/
-    0,                                         /*tp_richcompare*/
-    0,                                         /*tp_weaklistoffset*/
-    0,                                         /*tp_iter*/
-    0,                                         /*tp_iternext*/
-    0,                                         /*tp_methods*/
-    0,                                         /*tp_members*/
-    0,                                         /*tp_getset*/
-    0,                                         /*tp_base*/
-    0,                                         /*tp_dict*/
-    0,                                         /*tp_descr_get*/
-    0,                                         /*tp_descr_set*/
-    0,                                         /*tp_dictoffset*/
-    0,                                         /*tp_init*/
-    0,                                         /*tp_alloc*/
-    0,                                         /*tp_new*/
+PyType_Spec bytesiobuf_spec = {
+    .name = "_io._BytesIOBuffer",
+    .basicsize = sizeof(bytesiobuf),
+    .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+              Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
+    .slots = bytesiobuf_slots,
 };
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index ffe15152448a..453f63ec3f1c 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -318,7 +318,6 @@ Python/instrumentation.c	-	_PyInstrumentation_MISSING	-
 ## static types
 
 Modules/_io/bufferedio.c	-	PyBufferedIOBase_Type	-
-Modules/_io/bytesio.c	-	_PyBytesIOBuffer_Type	-
 Modules/_io/iobase.c	-	PyIOBase_Type	-
 Modules/_io/iobase.c	-	PyRawIOBase_Type	-
 Modules/_io/textio.c	-	PyTextIOBase_Type	-



More information about the Python-checkins mailing list