gh-101819: Adapt _io types to heap types, batch 1 (GH-101949)

Adapt StringIO, TextIOWrapper, FileIO, Buffered*, and BytesIO types.

Automerge-Triggered-By: GH:erlend-aasland
This commit is contained in:
Erlend E. Aasland 2023-02-20 14:46:20 +01:00 committed by GitHub
parent 2713631041
commit c00faf7943
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 463 additions and 478 deletions

View file

@ -13,9 +13,9 @@
/*[clinic input]
module _io
class _io.StringIO "stringio *" "&PyStringIO_Type"
class _io.StringIO "stringio *" "clinic_state()->PyStringIO_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c17bc0f42165cd7d]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2693eada0658d470]*/
typedef struct {
PyObject_HEAD
@ -43,6 +43,7 @@ typedef struct {
PyObject *dict;
PyObject *weakreflist;
_PyIO_State *module_state;
} stringio;
static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs);
@ -401,7 +402,7 @@ stringio_iternext(stringio *self)
CHECK_CLOSED(self);
ENSURE_REALIZED(self);
if (Py_IS_TYPE(self, &PyStringIO_Type)) {
if (Py_IS_TYPE(self, self->module_state->PyStringIO_Type)) {
/* Skip method call overhead for speed */
line = _stringio_readline(self, -1);
}
@ -581,6 +582,7 @@ _io_StringIO_close_impl(stringio *self)
static int
stringio_traverse(stringio *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->dict);
return 0;
}
@ -595,6 +597,7 @@ stringio_clear(stringio *self)
static void
stringio_dealloc(stringio *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
self->ok = 0;
if (self->buf) {
@ -606,9 +609,11 @@ stringio_dealloc(stringio *self)
Py_CLEAR(self->writenl);
Py_CLEAR(self->decoder);
Py_CLEAR(self->dict);
if (self->weakreflist != NULL)
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) self);
Py_TYPE(self)->tp_free(self);
}
tp->tp_free(self);
Py_DECREF(tp);
}
static PyObject *
@ -745,7 +750,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
self->state = STATE_ACCUMULATING;
}
self->pos = 0;
self->module_state = find_io_state_by_def(Py_TYPE(self));
self->closed = 0;
self->ok = 1;
return 0;
@ -963,7 +968,9 @@ stringio_newlines(stringio *self, void *context)
return PyObject_GetAttr(self->decoder, &_Py_ID(newlines));
}
#define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
#include "clinic/stringio.c.h"
#undef clinic_state
static struct PyMethodDef stringio_methods[] = {
_IO_STRINGIO_CLOSE_METHODDEF
@ -997,44 +1004,30 @@ static PyGetSetDef stringio_getset[] = {
{NULL}
};
PyTypeObject PyStringIO_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_io.StringIO", /*tp_name*/
sizeof(stringio), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)stringio_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*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC, /*tp_flags*/
_io_StringIO___init____doc__, /*tp_doc*/
(traverseproc)stringio_traverse, /*tp_traverse*/
(inquiry)stringio_clear, /*tp_clear*/
0, /*tp_richcompare*/
offsetof(stringio, weakreflist), /*tp_weaklistoffset*/
0, /*tp_iter*/
(iternextfunc)stringio_iternext, /*tp_iternext*/
stringio_methods, /*tp_methods*/
0, /*tp_members*/
stringio_getset, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
offsetof(stringio, dict), /*tp_dictoffset*/
_io_StringIO___init__, /*tp_init*/
0, /*tp_alloc*/
stringio_new, /*tp_new*/
static struct PyMemberDef stringio_members[] = {
{"__weaklistoffset__", T_PYSSIZET, offsetof(stringio, weakreflist), READONLY},
{"__dictoffset__", T_PYSSIZET, offsetof(stringio, dict), READONLY},
{NULL},
};
static PyType_Slot stringio_slots[] = {
{Py_tp_dealloc, stringio_dealloc},
{Py_tp_doc, (void *)_io_StringIO___init____doc__},
{Py_tp_traverse, stringio_traverse},
{Py_tp_clear, stringio_clear},
{Py_tp_iternext, stringio_iternext},
{Py_tp_methods, stringio_methods},
{Py_tp_members, stringio_members},
{Py_tp_getset, stringio_getset},
{Py_tp_init, _io_StringIO___init__},
{Py_tp_new, stringio_new},
{0, NULL},
};
PyType_Spec stringio_spec = {
.name = "_io.StringIO",
.basicsize = sizeof(stringio),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = stringio_slots,
};