Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,6 @@ static PyTypeObject* static_types[] = {
&PyBufferedIOBase_Type,
&PyRawIOBase_Type,
&PyTextIOBase_Type,

// PyRawIOBase_Type(PyIOBase_Type) subclasses
&_PyBytesIOBuffer_Type,
};


Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions Modules/_io/_iomodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
71 changes: 27 additions & 44 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,64 +1090,47 @@ 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;
}

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,
};
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down