gh-101819: Adapt _io.PyWindowsConsoleIO_Type to heap type (#104197)

This commit is contained in:
Erlend E. Aasland 2023-05-07 11:23:11 +02:00 committed by GitHub
parent 3952379655
commit cab1298a60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 70 deletions

View file

@ -321,7 +321,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
#ifdef HAVE_WINDOWS_CONSOLE_IO #ifdef HAVE_WINDOWS_CONSOLE_IO
const PyConfig *config = _Py_GetConfig(); const PyConfig *config = _Py_GetConfig();
if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') { if (!config->legacy_windows_stdio && _PyIO_get_console_type(path_or_fd) != '\0') {
RawIO_class = (PyObject *)&PyWindowsConsoleIO_Type; RawIO_class = (PyObject *)state->PyWindowsConsoleIO_Type;
encoding = "utf-8"; encoding = "utf-8";
} }
#endif #endif
@ -595,6 +595,9 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
Py_VISIT(state->PyStringIO_Type); Py_VISIT(state->PyStringIO_Type);
Py_VISIT(state->PyTextIOBase_Type); Py_VISIT(state->PyTextIOBase_Type);
Py_VISIT(state->PyTextIOWrapper_Type); Py_VISIT(state->PyTextIOWrapper_Type);
#ifdef HAVE_WINDOWS_CONSOLE_IO
Py_VISIT(state->PyWindowsConsoleIO_Type);
#endif
return 0; return 0;
} }
@ -619,6 +622,9 @@ iomodule_clear(PyObject *mod) {
Py_CLEAR(state->PyStringIO_Type); Py_CLEAR(state->PyStringIO_Type);
Py_CLEAR(state->PyTextIOBase_Type); Py_CLEAR(state->PyTextIOBase_Type);
Py_CLEAR(state->PyTextIOWrapper_Type); Py_CLEAR(state->PyTextIOWrapper_Type);
#ifdef HAVE_WINDOWS_CONSOLE_IO
Py_CLEAR(state->PyWindowsConsoleIO_Type);
#endif
return 0; return 0;
} }
@ -668,22 +674,12 @@ static PyTypeObject* static_types[] = {
// PyRawIOBase_Type(PyIOBase_Type) subclasses // PyRawIOBase_Type(PyIOBase_Type) subclasses
&_PyBytesIOBuffer_Type, &_PyBytesIOBuffer_Type,
#ifdef HAVE_WINDOWS_CONSOLE_IO
&PyWindowsConsoleIO_Type,
#endif
}; };
PyStatus PyStatus
_PyIO_InitTypes(PyInterpreterState *interp) _PyIO_InitTypes(PyInterpreterState *interp)
{ {
#ifdef HAVE_WINDOWS_CONSOLE_IO
if (_Py_IsMainInterpreter(interp)) {
// Set type base classes
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
}
#endif
for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
PyTypeObject *type = static_types[i]; PyTypeObject *type = static_types[i];
if (_PyStaticType_InitBuiltin(interp, type) < 0) { if (_PyStaticType_InitBuiltin(interp, type) < 0) {
@ -777,6 +773,10 @@ PyInit__io(void)
// PyRawIOBase_Type(PyIOBase_Type) subclasses // PyRawIOBase_Type(PyIOBase_Type) subclasses
state->PyBytesIOBuffer_Type = (PyTypeObject *)Py_NewRef(&_PyBytesIOBuffer_Type); state->PyBytesIOBuffer_Type = (PyTypeObject *)Py_NewRef(&_PyBytesIOBuffer_Type);
ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, state->PyRawIOBase_Type); ADD_TYPE(m, state->PyFileIO_Type, &fileio_spec, state->PyRawIOBase_Type);
#ifdef MS_WINDOWS
ADD_TYPE(m, state->PyWindowsConsoleIO_Type, &winconsoleio_spec,
state->PyRawIOBase_Type);
#endif
// PyTextIOBase_Type(PyIOBase_Type) subclasses // PyTextIOBase_Type(PyIOBase_Type) subclasses
ADD_TYPE(m, state->PyStringIO_Type, &stringio_spec, state->PyTextIOBase_Type); ADD_TYPE(m, state->PyStringIO_Type, &stringio_spec, state->PyTextIOBase_Type);

View file

@ -26,8 +26,8 @@ extern PyType_Spec stringio_spec;
extern PyType_Spec textiowrapper_spec; extern PyType_Spec textiowrapper_spec;
#ifdef HAVE_WINDOWS_CONSOLE_IO #ifdef HAVE_WINDOWS_CONSOLE_IO
extern PyTypeObject PyWindowsConsoleIO_Type; extern PyType_Spec winconsoleio_spec;
#endif /* HAVE_WINDOWS_CONSOLE_IO */ #endif
/* These functions are used as METH_NOARGS methods, are normally called /* These functions are used as METH_NOARGS methods, are normally called
* with args=NULL, and return a new reference. * with args=NULL, and return a new reference.
@ -157,6 +157,9 @@ typedef struct {
PyTypeObject *PyStringIO_Type; PyTypeObject *PyStringIO_Type;
PyTypeObject *PyTextIOBase_Type; PyTypeObject *PyTextIOBase_Type;
PyTypeObject *PyTextIOWrapper_Type; PyTypeObject *PyTextIOWrapper_Type;
#ifdef MS_WINDOWS
PyTypeObject *PyWindowsConsoleIO_Type;
#endif
} _PyIO_State; } _PyIO_State;
#define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod)) #define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod))

View file

@ -137,9 +137,9 @@ char _PyIO_get_console_type(PyObject *path_or_fd) {
/*[clinic input] /*[clinic input]
module _io module _io
class _io._WindowsConsoleIO "winconsoleio *" "&PyWindowsConsoleIO_Type" class _io._WindowsConsoleIO "winconsoleio *" "clinic_state()->PyWindowsConsoleIO_Type"
[clinic start generated code]*/ [clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e897fdc1fba4e131]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=05526e723011ab36]*/
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
@ -156,8 +156,6 @@ typedef struct {
wchar_t wbuf; wchar_t wbuf;
} winconsoleio; } winconsoleio;
PyTypeObject PyWindowsConsoleIO_Type;
int int
_PyWindowsConsoleIO_closed(PyObject *self) _PyWindowsConsoleIO_closed(PyObject *self)
{ {
@ -265,7 +263,10 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
int fd_is_own = 0; int fd_is_own = 0;
HANDLE handle = NULL; HANDLE handle = NULL;
assert(PyObject_TypeCheck(self, (PyTypeObject *)&PyWindowsConsoleIO_Type)); #ifdef Py_DEBUG
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
assert(PyObject_TypeCheck(self, state->PyWindowsConsoleIO_Type));
#endif
if (self->fd >= 0) { if (self->fd >= 0) {
if (self->closefd) { if (self->closefd) {
/* Have to close the existing file first. */ /* Have to close the existing file first. */
@ -417,6 +418,7 @@ done:
static int static int
winconsoleio_traverse(winconsoleio *self, visitproc visit, void *arg) winconsoleio_traverse(winconsoleio *self, visitproc visit, void *arg)
{ {
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->dict); Py_VISIT(self->dict);
return 0; return 0;
} }
@ -431,6 +433,7 @@ winconsoleio_clear(winconsoleio *self)
static void static void
winconsoleio_dealloc(winconsoleio *self) winconsoleio_dealloc(winconsoleio *self)
{ {
PyTypeObject *tp = Py_TYPE(self);
self->finalizing = 1; self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0) if (_PyIOBase_finalize((PyObject *) self) < 0)
return; return;
@ -438,7 +441,8 @@ winconsoleio_dealloc(winconsoleio *self)
if (self->weakreflist != NULL) if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self); PyObject_ClearWeakRefs((PyObject *) self);
Py_CLEAR(self->dict); Py_CLEAR(self->dict);
Py_TYPE(self)->tp_free((PyObject *)self); tp->tp_free((PyObject *)self);
Py_DECREF(tp);
} }
static PyObject * static PyObject *
@ -1078,7 +1082,9 @@ _io__WindowsConsoleIO_isatty_impl(winconsoleio *self)
Py_RETURN_TRUE; Py_RETURN_TRUE;
} }
#define clinic_state() (IO_STATE())
#include "clinic/winconsoleio.c.h" #include "clinic/winconsoleio.c.h"
#undef clinic_state
static PyMethodDef winconsoleio_methods[] = { static PyMethodDef winconsoleio_methods[] = {
_IO__WINDOWSCONSOLEIO_READ_METHODDEF _IO__WINDOWSCONSOLEIO_READ_METHODDEF
@ -1124,59 +1130,32 @@ static PyGetSetDef winconsoleio_getsetlist[] = {
static PyMemberDef winconsoleio_members[] = { static PyMemberDef winconsoleio_members[] = {
{"_blksize", T_UINT, offsetof(winconsoleio, blksize), 0}, {"_blksize", T_UINT, offsetof(winconsoleio, blksize), 0},
{"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), 0}, {"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), 0},
{"__weaklistoffset__", T_PYSSIZET, offsetof(winconsoleio, weakreflist), READONLY},
{"__dictoffset__", T_PYSSIZET, offsetof(winconsoleio, dict), READONLY},
{NULL} {NULL}
}; };
PyTypeObject PyWindowsConsoleIO_Type = { static PyType_Slot winconsoleio_slots[] = {
PyVarObject_HEAD_INIT(NULL, 0) {Py_tp_dealloc, winconsoleio_dealloc},
"_io._WindowsConsoleIO", {Py_tp_repr, winconsoleio_repr},
sizeof(winconsoleio), {Py_tp_getattro, PyObject_GenericGetAttr},
0, {Py_tp_doc, (void *)_io__WindowsConsoleIO___init____doc__},
(destructor)winconsoleio_dealloc, /* tp_dealloc */ {Py_tp_traverse, winconsoleio_traverse},
0, /* tp_vectorcall_offset */ {Py_tp_clear, winconsoleio_clear},
0, /* tp_getattr */ {Py_tp_methods, winconsoleio_methods},
0, /* tp_setattr */ {Py_tp_members, winconsoleio_members},
0, /* tp_as_async */ {Py_tp_getset, winconsoleio_getsetlist},
(reprfunc)winconsoleio_repr, /* tp_repr */ {Py_tp_init, _io__WindowsConsoleIO___init__},
0, /* tp_as_number */ {Py_tp_new, winconsoleio_new},
0, /* tp_as_sequence */ {0, NULL},
0, /* tp_as_mapping */ };
0, /* tp_hash */
0, /* tp_call */ PyType_Spec winconsoleio_spec = {
0, /* tp_str */ .name = "_io._WindowsConsoleIO",
PyObject_GenericGetAttr, /* tp_getattro */ .basicsize = sizeof(winconsoleio),
0, /* tp_setattro */ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
0, /* tp_as_buffer */ Py_TPFLAGS_IMMUTABLETYPE),
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE .slots = winconsoleio_slots,
| Py_TPFLAGS_HAVE_GC, /* tp_flags */
_io__WindowsConsoleIO___init____doc__, /* tp_doc */
(traverseproc)winconsoleio_traverse, /* tp_traverse */
(inquiry)winconsoleio_clear, /* tp_clear */
0, /* tp_richcompare */
offsetof(winconsoleio, weakreflist), /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
winconsoleio_methods, /* tp_methods */
winconsoleio_members, /* tp_members */
winconsoleio_getsetlist, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(winconsoleio, dict), /* tp_dictoffset */
_io__WindowsConsoleIO___init__, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
winconsoleio_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
0, /* tp_finalize */
}; };
#endif /* HAVE_WINDOWS_CONSOLE_IO */ #endif /* HAVE_WINDOWS_CONSOLE_IO */

View file

@ -322,7 +322,6 @@ Modules/_io/bytesio.c - _PyBytesIOBuffer_Type -
Modules/_io/iobase.c - PyIOBase_Type - Modules/_io/iobase.c - PyIOBase_Type -
Modules/_io/iobase.c - PyRawIOBase_Type - Modules/_io/iobase.c - PyRawIOBase_Type -
Modules/_io/textio.c - PyTextIOBase_Type - Modules/_io/textio.c - PyTextIOBase_Type -
Modules/_io/winconsoleio.c - PyWindowsConsoleIO_Type -
Modules/_testcapi/vectorcall.c - MethodDescriptorBase_Type - Modules/_testcapi/vectorcall.c - MethodDescriptorBase_Type -
Modules/_testcapi/vectorcall.c - MethodDescriptorDerived_Type - Modules/_testcapi/vectorcall.c - MethodDescriptorDerived_Type -
Modules/_testcapi/vectorcall.c - MethodDescriptorNopGet_Type - Modules/_testcapi/vectorcall.c - MethodDescriptorNopGet_Type -

Can't render this file because it has a wrong number of fields in line 4.