diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-03-21 20:01:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 20:01:38 (GMT) |
commit | e6ecd3e6b437f3056e0a410a57c52e2639b56353 (patch) | |
tree | d4cfb7bcec5fa2abf2c0a98f74baa59c879c9905 /Modules/_io/_iomodule.c | |
parent | 8d015fa000db5775d477cd04dc574ba13721e278 (diff) | |
download | cpython-e6ecd3e6b437f3056e0a410a57c52e2639b56353.zip cpython-e6ecd3e6b437f3056e0a410a57c52e2639b56353.tar.gz cpython-e6ecd3e6b437f3056e0a410a57c52e2639b56353.tar.bz2 |
gh-94673: Isolate the _io module to Each Interpreter (gh-102663)
Aside from sys and builtins, _io is the only core builtin module that hasn't been ported to multi-phase init. We may do so later (e.g. gh-101948), but in the meantime we must at least take care of the module's static types properly. (This came up while working on gh-101660.)
https://github.com/python/cpython/issues/94673
Diffstat (limited to 'Modules/_io/_iomodule.c')
-rw-r--r-- | Modules/_io/_iomodule.c | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 1506755..5644cc0 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -11,6 +11,7 @@ #include "Python.h" #include "_iomodule.h" #include "pycore_pystate.h" // _PyInterpreterState_GET() +#include "pycore_initconfig.h" // _PyStatus_OK() #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> @@ -666,12 +667,40 @@ static PyTypeObject* static_types[] = { }; +PyStatus +_PyIO_InitTypes(PyInterpreterState *interp) +{ + if (!_Py_IsMainInterpreter(interp)) { + return _PyStatus_OK(); + } + + // Set type base classes +#ifdef HAVE_WINDOWS_CONSOLE_IO + PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; +#endif + + for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { + PyTypeObject *type = static_types[i]; + if (_PyStaticType_InitBuiltin(type) < 0) { + return _PyStatus_ERR("Can't initialize builtin type"); + } + } + + return _PyStatus_OK(); +} + void -_PyIO_Fini(void) +_PyIO_FiniTypes(PyInterpreterState *interp) { + if (!_Py_IsMainInterpreter(interp)) { + return; + } + + // Deallocate types in the reverse order to deallocate subclasses before + // their base classes. for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types) - 1; i >= 0; i--) { - PyTypeObject *exc = static_types[i]; - _PyStaticType_Dealloc(exc); + PyTypeObject *type = static_types[i]; + _PyStaticType_Dealloc(type); } } @@ -717,11 +746,6 @@ PyInit__io(void) goto fail; } - // Set type base classes -#ifdef HAVE_WINDOWS_CONSOLE_IO - PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type; -#endif - // Add types for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { PyTypeObject *type = static_types[i]; |