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-94673: Isolate the _io module to Each Interpreter #102663

Merged
merged 3 commits into from
Mar 21, 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
40 changes: 32 additions & 8 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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];
Expand Down
12 changes: 8 additions & 4 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
#include "opcode.h"

extern void _PyIO_Fini(void);
extern PyStatus _PyIO_InitTypes(PyInterpreterState *interp);
extern void _PyIO_FiniTypes(PyInterpreterState *interp);

#include <locale.h> // setlocale()
#include <stdlib.h> // getenv()
Expand Down Expand Up @@ -697,6 +698,11 @@ pycore_init_types(PyInterpreterState *interp)
return _PyStatus_ERR("failed to initialize an exception type");
}

status = _PyIO_InitTypes(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = _PyExc_InitGlobalObjects(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down Expand Up @@ -1695,9 +1701,7 @@ finalize_interp_clear(PyThreadState *tstate)
/* Clear interpreter state and all thread states */
_PyInterpreterState_Clear(tstate);

if (is_main_interp) {
_PyIO_Fini();
}
_PyIO_FiniTypes(tstate->interp);

/* Clear all loghooks */
/* Both _PySys_Audit function and users still need PyObject, such as tuple.
Expand Down