bpo-36710: Use tstate in pylifecycle.c (GH-14249)

In pylifecycle.c: pass tstate argument, rather than interp argument,
to functions.
This commit is contained in:
Victor Stinner 2019-06-20 00:05:23 +02:00 committed by GitHub
parent 35068bd059
commit b45d259bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 121 additions and 109 deletions

View File

@ -21,7 +21,7 @@ extern int _Py_SetFileSystemEncoding(
const char *encoding, const char *encoding,
const char *errors); const char *errors);
extern void _Py_ClearFileSystemEncoding(void); extern void _Py_ClearFileSystemEncoding(void);
extern PyStatus _PyUnicode_InitEncodings(PyInterpreterState *interp); extern PyStatus _PyUnicode_InitEncodings(PyThreadState *tstate);
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
extern int _PyUnicode_EnableLegacyWindowsFSEncoding(void); extern int _PyUnicode_EnableLegacyWindowsFSEncoding(void);
#endif #endif
@ -37,20 +37,20 @@ extern int _PyStructSequence_Init(void);
extern int _PyLong_Init(void); extern int _PyLong_Init(void);
extern PyStatus _PyFaulthandler_Init(int enable); extern PyStatus _PyFaulthandler_Init(int enable);
extern int _PyTraceMalloc_Init(int enable); extern int _PyTraceMalloc_Init(int enable);
extern PyObject * _PyBuiltin_Init(void); extern PyObject * _PyBuiltin_Init(PyThreadState *tstate);
extern PyStatus _PySys_Create( extern PyStatus _PySys_Create(
_PyRuntimeState *runtime, _PyRuntimeState *runtime,
PyInterpreterState *interp, PyThreadState *tstate,
PyObject **sysmod_p); PyObject **sysmod_p);
extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict); extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
extern int _PySys_InitMain( extern int _PySys_InitMain(
_PyRuntimeState *runtime, _PyRuntimeState *runtime,
PyThreadState *tstate); PyThreadState *tstate);
extern PyStatus _PyImport_Init(PyInterpreterState *interp); extern PyStatus _PyImport_Init(PyThreadState *tstate);
extern PyStatus _PyExc_Init(void); extern PyStatus _PyExc_Init(void);
extern PyStatus _PyErr_Init(void); extern PyStatus _PyErr_Init(void);
extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod); extern PyStatus _PyBuiltins_AddExceptions(PyObject * bltinmod);
extern PyStatus _PyImportHooks_Init(void); extern PyStatus _PyImportHooks_Init(PyThreadState *tstate);
extern int _PyFloat_Init(void); extern int _PyFloat_Init(void);
extern PyStatus _Py_HashRandomization_Init(const PyConfig *); extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
@ -88,7 +88,6 @@ extern void _PyWarnings_Fini(PyInterpreterState *interp);
extern void _PyGILState_Init( extern void _PyGILState_Init(
_PyRuntimeState *runtime, _PyRuntimeState *runtime,
PyInterpreterState *interp,
PyThreadState *tstate); PyThreadState *tstate);
extern void _PyGILState_Fini(_PyRuntimeState *runtime); extern void _PyGILState_Fini(_PyRuntimeState *runtime);

View File

@ -310,7 +310,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
/* Used by PyImport_Cleanup() */ /* Used by _PyImport_Cleanup() */
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp); extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);

View File

@ -15792,8 +15792,10 @@ init_fs_encoding(PyInterpreterState *interp)
PyStatus PyStatus
_PyUnicode_InitEncodings(PyInterpreterState *interp) _PyUnicode_InitEncodings(PyThreadState *tstate)
{ {
PyInterpreterState *interp = tstate->interp;
PyStatus status = init_fs_encoding(interp); PyStatus status = init_fs_encoding(interp);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;

View File

@ -2769,11 +2769,11 @@ static struct PyModuleDef builtinsmodule = {
PyObject * PyObject *
_PyBuiltin_Init(void) _PyBuiltin_Init(PyThreadState *tstate)
{ {
PyObject *mod, *dict, *debug; PyObject *mod, *dict, *debug;
const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; const PyConfig *config = &tstate->interp->config;
if (PyType_Ready(&PyFilter_Type) < 0 || if (PyType_Ready(&PyFilter_Type) < 0 ||
PyType_Ready(&PyMap_Type) < 0 || PyType_Ready(&PyMap_Type) < 0 ||

View File

@ -48,8 +48,9 @@ module _imp
/* Initialize things */ /* Initialize things */
PyStatus PyStatus
_PyImport_Init(PyInterpreterState *interp) _PyImport_Init(PyThreadState *tstate)
{ {
PyInterpreterState *interp = tstate->interp;
interp->builtins_copy = PyDict_Copy(interp->builtins); interp->builtins_copy = PyDict_Copy(interp->builtins);
if (interp->builtins_copy == NULL) { if (interp->builtins_copy == NULL) {
return _PyStatus_ERR("Can't backup builtins dict"); return _PyStatus_ERR("Can't backup builtins dict");
@ -58,7 +59,7 @@ _PyImport_Init(PyInterpreterState *interp)
} }
PyStatus PyStatus
_PyImportHooks_Init(void) _PyImportHooks_Init(PyThreadState *tstate)
{ {
PyObject *v, *path_hooks = NULL; PyObject *v, *path_hooks = NULL;
int err = 0; int err = 0;
@ -89,7 +90,7 @@ _PyImportHooks_Init(void)
return _PyStatus_OK(); return _PyStatus_OK();
error: error:
PyErr_Print(); _PyErr_Print(tstate);
return _PyStatus_ERR("initializing sys.meta_path, sys.path_hooks, " return _PyStatus_ERR("initializing sys.meta_path, sys.path_hooks, "
"or path_importer_cache failed"); "or path_importer_cache failed");
} }
@ -554,7 +555,7 @@ _PyImport_Cleanup(PyThreadState *tstate)
} }
Py_XDECREF(dict); Py_XDECREF(dict);
/* Clear module dict copies stored in the interpreter state */ /* Clear module dict copies stored in the interpreter state */
_PyInterpreterState_ClearModules(tstate->interp); _PyInterpreterState_ClearModules(interp);
/* Collect references */ /* Collect references */
_PyGC_CollectNoFail(); _PyGC_CollectNoFail();
/* Dump GC stats before it's too late, since it uses the warnings /* Dump GC stats before it's too late, since it uses the warnings

View File

@ -11,6 +11,7 @@
#include "pycore_fileutils.h" #include "pycore_fileutils.h"
#include "pycore_hamt.h" #include "pycore_hamt.h"
#include "pycore_pathconfig.h" #include "pycore_pathconfig.h"
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h" #include "pycore_pylifecycle.h"
#include "pycore_pymem.h" #include "pycore_pymem.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
@ -60,13 +61,13 @@ extern "C" {
extern grammar _PyParser_Grammar; /* From graminit.c */ extern grammar _PyParser_Grammar; /* From graminit.c */
/* Forward */ /* Forward declarations */
static PyStatus add_main_module(PyInterpreterState *interp); static PyStatus add_main_module(PyInterpreterState *interp);
static PyStatus init_import_size(void); static PyStatus init_import_site(void);
static PyStatus init_sys_streams(PyInterpreterState *interp); static PyStatus init_sys_streams(PyThreadState *tstate);
static PyStatus init_signals(void); static PyStatus init_signals(PyThreadState *tstate);
static void call_py_exitfuncs(PyInterpreterState *); static void call_py_exitfuncs(PyThreadState *tstate);
static void wait_for_thread_shutdown(void); static void wait_for_thread_shutdown(PyThreadState *tstate);
static void call_ll_exitfuncs(_PyRuntimeState *runtime); static void call_ll_exitfuncs(_PyRuntimeState *runtime);
int _Py_UnhandledKeyboardInterrupt = 0; int _Py_UnhandledKeyboardInterrupt = 0;
@ -147,11 +148,12 @@ Py_IsInitialized(void)
*/ */
static PyStatus static PyStatus
init_importlib(PyInterpreterState *interp, PyObject *sysmod) init_importlib(PyThreadState *tstate, PyObject *sysmod)
{ {
PyObject *importlib; PyObject *importlib;
PyObject *impmod; PyObject *impmod;
PyObject *value; PyObject *value;
PyInterpreterState *interp = tstate->interp;
int verbose = interp->config.verbose; int verbose = interp->config.verbose;
/* Import _importlib through its frozen version, _frozen_importlib. */ /* Import _importlib through its frozen version, _frozen_importlib. */
@ -188,7 +190,7 @@ init_importlib(PyInterpreterState *interp, PyObject *sysmod)
/* Install importlib as the implementation of import */ /* Install importlib as the implementation of import */
value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
if (value == NULL) { if (value == NULL) {
PyErr_Print(); _PyErr_Print(tstate);
return _PyStatus_ERR("importlib install failed"); return _PyStatus_ERR("importlib install failed");
} }
Py_DECREF(value); Py_DECREF(value);
@ -204,7 +206,7 @@ init_importlib_external(PyThreadState *tstate)
value = PyObject_CallMethod(tstate->interp->importlib, value = PyObject_CallMethod(tstate->interp->importlib,
"_install_external_importers", ""); "_install_external_importers", "");
if (value == NULL) { if (value == NULL) {
PyErr_Print(); _PyErr_Print(tstate);
return _PyStatus_ERR("external importer setup failed"); return _PyStatus_ERR("external importer setup failed");
} }
Py_DECREF(value); Py_DECREF(value);
@ -449,7 +451,7 @@ _Py_SetLocaleFromEnv(int category)
static PyStatus static PyStatus
pyinit_core_reconfigure(_PyRuntimeState *runtime, pyinit_core_reconfigure(_PyRuntimeState *runtime,
PyInterpreterState **interp_p, PyThreadState **tstate_p,
const PyConfig *config) const PyConfig *config)
{ {
PyStatus status; PyStatus status;
@ -457,12 +459,12 @@ pyinit_core_reconfigure(_PyRuntimeState *runtime,
if (!tstate) { if (!tstate) {
return _PyStatus_ERR("failed to read thread state"); return _PyStatus_ERR("failed to read thread state");
} }
*tstate_p = tstate;
PyInterpreterState *interp = tstate->interp; PyInterpreterState *interp = tstate->interp;
if (interp == NULL) { if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter"); return _PyStatus_ERR("can't make main interpreter");
} }
*interp_p = interp;
_PyConfig_Write(config, runtime); _PyConfig_Write(config, runtime);
@ -519,23 +521,22 @@ pycore_init_runtime(_PyRuntimeState *runtime,
static PyStatus static PyStatus
pycore_create_interpreter(_PyRuntimeState *runtime, pycore_create_interpreter(_PyRuntimeState *runtime,
const PyConfig *config, const PyConfig *config,
PyInterpreterState **interp_p) PyThreadState **tstate_p)
{ {
PyInterpreterState *interp = PyInterpreterState_New(); PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) { if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter"); return _PyStatus_ERR("can't make main interpreter");
} }
*interp_p = interp;
PyStatus status = _PyConfig_Copy(&interp->config, config); PyStatus status = _PyConfig_Copy(&interp->config, config);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
config = &interp->config;
PyThreadState *tstate = PyThreadState_New(interp); PyThreadState *tstate = PyThreadState_New(interp);
if (tstate == NULL) if (tstate == NULL) {
return _PyStatus_ERR("can't make first thread"); return _PyStatus_ERR("can't make first thread");
}
(void) PyThreadState_Swap(tstate); (void) PyThreadState_Swap(tstate);
/* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
@ -546,11 +547,12 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
_PyEval_FiniThreads(&runtime->ceval); _PyEval_FiniThreads(&runtime->ceval);
/* Auto-thread-state API */ /* Auto-thread-state API */
_PyGILState_Init(runtime, interp, tstate); _PyGILState_Init(runtime, tstate);
/* Create the GIL */ /* Create the GIL */
PyEval_InitThreads(); PyEval_InitThreads();
*tstate_p = tstate;
return _PyStatus_OK(); return _PyStatus_OK();
} }
@ -599,9 +601,11 @@ pycore_init_types(void)
static PyStatus static PyStatus
pycore_init_builtins(PyInterpreterState *interp) pycore_init_builtins(PyThreadState *tstate)
{ {
PyObject *bimod = _PyBuiltin_Init(); PyInterpreterState *interp = tstate->interp;
PyObject *bimod = _PyBuiltin_Init(tstate);
if (bimod == NULL) { if (bimod == NULL) {
return _PyStatus_ERR("can't initialize builtins modules"); return _PyStatus_ERR("can't initialize builtins modules");
} }
@ -622,14 +626,16 @@ pycore_init_builtins(PyInterpreterState *interp)
static PyStatus static PyStatus
pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod) pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
{ {
PyStatus status = _PyImport_Init(interp); const PyConfig *config = &tstate->interp->config;
PyStatus status = _PyImport_Init(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = _PyImportHooks_Init(); status = _PyImportHooks_Init(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -639,16 +645,16 @@ pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod)
return _PyStatus_ERR("can't initialize warnings"); return _PyStatus_ERR("can't initialize warnings");
} }
if (interp->config._install_importlib) { if (config->_install_importlib) {
status = _PyConfig_SetPathConfig(&interp->config); status = _PyConfig_SetPathConfig(config);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
} }
/* This call sets up builtin and frozen import support */ /* This call sets up builtin and frozen import support */
if (interp->config._install_importlib) { if (config->_install_importlib) {
status = init_importlib(interp, sysmod); status = init_importlib(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -659,11 +665,9 @@ pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod)
static PyStatus static PyStatus
pyinit_config(_PyRuntimeState *runtime, pyinit_config(_PyRuntimeState *runtime,
PyInterpreterState **interp_p, PyThreadState **tstate_p,
const PyConfig *config) const PyConfig *config)
{ {
PyInterpreterState *interp;
_PyConfig_Write(config, runtime); _PyConfig_Write(config, runtime);
PyStatus status = pycore_init_runtime(runtime, config); PyStatus status = pycore_init_runtime(runtime, config);
@ -671,12 +675,13 @@ pyinit_config(_PyRuntimeState *runtime,
return status; return status;
} }
status = pycore_create_interpreter(runtime, config, &interp); PyThreadState *tstate;
status = pycore_create_interpreter(runtime, config, &tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
config = &interp->config; config = &tstate->interp->config;
*interp_p = interp; *tstate_p = tstate;
status = pycore_init_types(); status = pycore_init_types();
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
@ -684,17 +689,17 @@ pyinit_config(_PyRuntimeState *runtime,
} }
PyObject *sysmod; PyObject *sysmod;
status = _PySys_Create(runtime, interp, &sysmod); status = _PySys_Create(runtime, tstate, &sysmod);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = pycore_init_builtins(interp); status = pycore_init_builtins(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = pycore_init_import_warnings(interp, sysmod); status = pycore_init_import_warnings(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -822,7 +827,7 @@ _Py_PreInitializeFromConfig(const PyConfig *config,
static PyStatus static PyStatus
pyinit_core(_PyRuntimeState *runtime, pyinit_core(_PyRuntimeState *runtime,
const PyConfig *src_config, const PyConfig *src_config,
PyInterpreterState **interp_p) PyThreadState **tstate_p)
{ {
PyStatus status; PyStatus status;
@ -845,10 +850,10 @@ pyinit_core(_PyRuntimeState *runtime,
} }
if (!runtime->core_initialized) { if (!runtime->core_initialized) {
status = pyinit_config(runtime, interp_p, &config); status = pyinit_config(runtime, tstate_p, &config);
} }
else { else {
status = pyinit_core_reconfigure(runtime, interp_p, &config); status = pyinit_core_reconfigure(runtime, tstate_p, &config);
} }
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
goto done; goto done;
@ -893,14 +898,14 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
* non-zero return code. * non-zero return code.
*/ */
static PyStatus static PyStatus
pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp) pyinit_main(_PyRuntimeState *runtime, PyThreadState *tstate)
{ {
if (!runtime->core_initialized) { if (!runtime->core_initialized) {
return _PyStatus_ERR("runtime core not initialized"); return _PyStatus_ERR("runtime core not initialized");
} }
/* Configure the main interpreter */ /* Configure the main interpreter */
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyInterpreterState *interp = tstate->interp;
PyConfig *config = &interp->config; PyConfig *config = &interp->config;
if (runtime->initialized) { if (runtime->initialized) {
@ -936,13 +941,13 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
return status; return status;
} }
status = _PyUnicode_InitEncodings(interp); status = _PyUnicode_InitEncodings(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
if (config->install_signal_handlers) { if (config->install_signal_handlers) {
status = init_signals(); status = init_signals(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -957,7 +962,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
return status; return status;
} }
status = init_sys_streams(interp); status = init_sys_streams(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -969,7 +974,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
PyObject *warnings_module = PyImport_ImportModule("warnings"); PyObject *warnings_module = PyImport_ImportModule("warnings");
if (warnings_module == NULL) { if (warnings_module == NULL) {
fprintf(stderr, "'import warnings' failed; traceback:\n"); fprintf(stderr, "'import warnings' failed; traceback:\n");
PyErr_Print(); _PyErr_Print(tstate);
} }
Py_XDECREF(warnings_module); Py_XDECREF(warnings_module);
} }
@ -977,7 +982,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
runtime->initialized = 1; runtime->initialized = 1;
if (config->site_import) { if (config->site_import) {
status = init_import_size(); /* Module site */ status = init_import_site();
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -999,9 +1004,8 @@ _Py_InitializeMain(void)
return status; return status;
} }
_PyRuntimeState *runtime = &_PyRuntime; _PyRuntimeState *runtime = &_PyRuntime;
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp; PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
return pyinit_main(runtime, tstate);
return pyinit_main(runtime, interp);
} }
@ -1020,15 +1024,15 @@ Py_InitializeFromConfig(const PyConfig *config)
} }
_PyRuntimeState *runtime = &_PyRuntime; _PyRuntimeState *runtime = &_PyRuntime;
PyInterpreterState *interp = NULL; PyThreadState *tstate = NULL;
status = pyinit_core(runtime, config, &interp); status = pyinit_core(runtime, config, &tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
config = &interp->config; config = &tstate->interp->config;
if (config->_init_main) { if (config->_init_main) {
status = pyinit_main(runtime, interp); status = pyinit_main(runtime, tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -1148,16 +1152,16 @@ Py_FinalizeEx(void)
return status; return status;
} }
// Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown();
// Make any remaining pending calls.
_Py_FinishPendingCalls(runtime);
/* Get current thread state and interpreter pointer */ /* Get current thread state and interpreter pointer */
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
PyInterpreterState *interp = tstate->interp; PyInterpreterState *interp = tstate->interp;
// Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown(tstate);
// Make any remaining pending calls.
_Py_FinishPendingCalls(runtime);
/* The interpreter is still entirely intact at this point, and the /* The interpreter is still entirely intact at this point, and the
* exit funcs may be relying on that. In particular, if some thread * exit funcs may be relying on that. In particular, if some thread
* or exit func is still waiting to do an import, the import machinery * or exit func is still waiting to do an import, the import machinery
@ -1168,7 +1172,7 @@ Py_FinalizeEx(void)
* the threads created via Threading. * the threads created via Threading.
*/ */
call_py_exitfuncs(interp); call_py_exitfuncs(tstate);
/* Copy the core config, PyInterpreterState_Delete() free /* Copy the core config, PyInterpreterState_Delete() free
the core config memory */ the core config memory */
@ -1462,7 +1466,7 @@ new_interpreter(PyThreadState **tstate_p)
return _PyStatus_ERR("can't finish initializing sys"); return _PyStatus_ERR("can't finish initializing sys");
} }
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto handle_error; goto handle_error;
} }
@ -1473,7 +1477,7 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_error; goto handle_error;
Py_INCREF(interp->builtins); Py_INCREF(interp->builtins);
} }
else if (PyErr_Occurred()) { else if (_PyErr_Occurred(tstate)) {
goto handle_error; goto handle_error;
} }
@ -1488,12 +1492,12 @@ new_interpreter(PyThreadState **tstate_p)
return status; return status;
} }
status = _PyImportHooks_Init(); status = _PyImportHooks_Init(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = init_importlib(interp, sysmod); status = init_importlib(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -1503,12 +1507,12 @@ new_interpreter(PyThreadState **tstate_p)
return status; return status;
} }
status = _PyUnicode_InitEncodings(interp); status = _PyUnicode_InitEncodings(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
status = init_sys_streams(interp); status = init_sys_streams(tstate);
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
@ -1519,14 +1523,14 @@ new_interpreter(PyThreadState **tstate_p)
} }
if (config->site_import) { if (config->site_import) {
status = init_import_size(); status = init_import_site();
if (_PyStatus_EXCEPTION(status)) { if (_PyStatus_EXCEPTION(status)) {
return status; return status;
} }
} }
} }
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
goto handle_error; goto handle_error;
} }
@ -1575,19 +1579,22 @@ Py_EndInterpreter(PyThreadState *tstate)
{ {
PyInterpreterState *interp = tstate->interp; PyInterpreterState *interp = tstate->interp;
if (tstate != _PyThreadState_GET()) if (tstate != _PyThreadState_GET()) {
Py_FatalError("Py_EndInterpreter: thread is not current"); Py_FatalError("Py_EndInterpreter: thread is not current");
if (tstate->frame != NULL) }
if (tstate->frame != NULL) {
Py_FatalError("Py_EndInterpreter: thread still has a frame"); Py_FatalError("Py_EndInterpreter: thread still has a frame");
}
interp->finalizing = 1; interp->finalizing = 1;
// Wrap up existing "threading"-module-created, non-daemon threads. // Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown(); wait_for_thread_shutdown(tstate);
call_py_exitfuncs(interp); call_py_exitfuncs(tstate);
if (tstate != interp->tstate_head || tstate->next != NULL) if (tstate != interp->tstate_head || tstate->next != NULL) {
Py_FatalError("Py_EndInterpreter: not the last thread"); Py_FatalError("Py_EndInterpreter: not the last thread");
}
_PyImport_Cleanup(tstate); _PyImport_Cleanup(tstate);
PyInterpreterState_Clear(interp); PyInterpreterState_Clear(interp);
@ -1648,7 +1655,7 @@ add_main_module(PyInterpreterState *interp)
/* Import the site module (not into __main__ though) */ /* Import the site module (not into __main__ though) */
static PyStatus static PyStatus
init_import_size(void) init_import_site(void)
{ {
PyObject *m; PyObject *m;
m = PyImport_ImportModule("site"); m = PyImport_ImportModule("site");
@ -1835,7 +1842,7 @@ error:
/* Initialize sys.stdin, stdout, stderr and builtins.open */ /* Initialize sys.stdin, stdout, stderr and builtins.open */
static PyStatus static PyStatus
init_sys_streams(PyInterpreterState *interp) init_sys_streams(PyThreadState *tstate)
{ {
PyObject *iomod = NULL, *wrapper; PyObject *iomod = NULL, *wrapper;
PyObject *bimod = NULL; PyObject *bimod = NULL;
@ -1844,7 +1851,7 @@ init_sys_streams(PyInterpreterState *interp)
int fd; int fd;
PyObject * encoding_attr; PyObject * encoding_attr;
PyStatus res = _PyStatus_OK(); PyStatus res = _PyStatus_OK();
PyConfig *config = &interp->config; const PyConfig *config = &tstate->interp->config;
/* Check that stdin is not a directory /* Check that stdin is not a directory
Using shell redirection, you can redirect stdin to a directory, Using shell redirection, you can redirect stdin to a directory,
@ -1935,7 +1942,7 @@ init_sys_streams(PyInterpreterState *interp)
} }
Py_DECREF(encoding_attr); Py_DECREF(encoding_attr);
} }
PyErr_Clear(); /* Not a fatal error if codec isn't available */ _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
if (PySys_SetObject("__stderr__", std) < 0) { if (PySys_SetObject("__stderr__", std) < 0) {
Py_DECREF(std); Py_DECREF(std);
@ -1983,11 +1990,12 @@ _Py_FatalError_DumpTracebacks(int fd)
static int static int
_Py_FatalError_PrintExc(int fd) _Py_FatalError_PrintExc(int fd)
{ {
PyThreadState *tstate = _PyThreadState_GET();
PyObject *ferr, *res; PyObject *ferr, *res;
PyObject *exception, *v, *tb; PyObject *exception, *v, *tb;
int has_tb; int has_tb;
PyErr_Fetch(&exception, &v, &tb); _PyErr_Fetch(tstate, &exception, &v, &tb);
if (exception == NULL) { if (exception == NULL) {
/* No current exception */ /* No current exception */
return 0; return 0;
@ -2000,7 +2008,7 @@ _Py_FatalError_PrintExc(int fd)
return 0; return 0;
} }
PyErr_NormalizeException(&exception, &v, &tb); _PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) { if (tb == NULL) {
tb = Py_None; tb = Py_None;
Py_INCREF(tb); Py_INCREF(tb);
@ -2019,10 +2027,12 @@ _Py_FatalError_PrintExc(int fd)
/* sys.stderr may be buffered: call sys.stderr.flush() */ /* sys.stderr may be buffered: call sys.stderr.flush() */
res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
if (res == NULL) if (res == NULL) {
PyErr_Clear(); _PyErr_Clear(tstate);
else }
else {
Py_DECREF(res); Py_DECREF(res);
}
return has_tb; return has_tb;
} }
@ -2173,7 +2183,7 @@ Py_ExitStatusException(PyStatus status)
/* For the atexit module. */ /* For the atexit module. */
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module) void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
{ {
PyInterpreterState *is = _PyInterpreterState_Get(); PyInterpreterState *is = _PyInterpreterState_GET_UNSAFE();
/* Guard against API misuse (see bpo-17852) */ /* Guard against API misuse (see bpo-17852) */
assert(is->pyexitfunc == NULL || is->pyexitfunc == func); assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
@ -2183,13 +2193,14 @@ void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
} }
static void static void
call_py_exitfuncs(PyInterpreterState *istate) call_py_exitfuncs(PyThreadState *tstate)
{ {
if (istate->pyexitfunc == NULL) PyInterpreterState *interp = tstate->interp;
if (interp->pyexitfunc == NULL)
return; return;
(*istate->pyexitfunc)(istate->pyexitmodule); (*interp->pyexitfunc)(interp->pyexitmodule);
PyErr_Clear(); _PyErr_Clear(tstate);
} }
/* Wait until threading._shutdown completes, provided /* Wait until threading._shutdown completes, provided
@ -2197,13 +2208,13 @@ call_py_exitfuncs(PyInterpreterState *istate)
The shutdown routine will wait until all non-daemon The shutdown routine will wait until all non-daemon
"threading" threads have completed. */ "threading" threads have completed. */
static void static void
wait_for_thread_shutdown(void) wait_for_thread_shutdown(PyThreadState *tstate)
{ {
_Py_IDENTIFIER(_shutdown); _Py_IDENTIFIER(_shutdown);
PyObject *result; PyObject *result;
PyObject *threading = _PyImport_GetModuleId(&PyId_threading); PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) { if (threading == NULL) {
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
/* else: threading not imported */ /* else: threading not imported */
@ -2255,7 +2266,7 @@ Py_Exit(int sts)
} }
static PyStatus static PyStatus
init_signals(void) init_signals(PyThreadState *tstate)
{ {
#ifdef SIGPIPE #ifdef SIGPIPE
PyOS_setsig(SIGPIPE, SIG_IGN); PyOS_setsig(SIGPIPE, SIG_IGN);
@ -2267,7 +2278,7 @@ init_signals(void)
PyOS_setsig(SIGXFSZ, SIG_IGN); PyOS_setsig(SIGXFSZ, SIG_IGN);
#endif #endif
PyOS_InitInterrupts(); /* May imply initsignal() */ PyOS_InitInterrupts(); /* May imply initsignal() */
if (PyErr_Occurred()) { if (_PyErr_Occurred(tstate)) {
return _PyStatus_ERR("can't import signal"); return _PyStatus_ERR("can't import signal");
} }
return _PyStatus_OK(); return _PyStatus_OK();

View File

@ -1143,19 +1143,18 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
Py_Initialize/Py_FinalizeEx Py_Initialize/Py_FinalizeEx
*/ */
void void
_PyGILState_Init(_PyRuntimeState *runtime, _PyGILState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
PyInterpreterState *interp, PyThreadState *tstate)
{ {
/* must init with valid states */ /* must init with valid states */
assert(interp != NULL);
assert(tstate != NULL); assert(tstate != NULL);
assert(tstate->interp != NULL);
struct _gilstate_runtime_state *gilstate = &runtime->gilstate; struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) { if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Py_FatalError("Could not allocate TSS entry"); Py_FatalError("Could not allocate TSS entry");
} }
gilstate->autoInterpreterState = interp; gilstate->autoInterpreterState = tstate->interp;
assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL); assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
assert(tstate->gilstate_counter == 0); assert(tstate->gilstate_counter == 0);

View File

@ -3036,10 +3036,10 @@ error:
/* Create sys module without all attributes: _PySys_InitMain() should be called /* Create sys module without all attributes: _PySys_InitMain() should be called
later to add remaining attributes. */ later to add remaining attributes. */
PyStatus PyStatus
_PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp, _PySys_Create(_PyRuntimeState *runtime, PyThreadState *tstate,
PyObject **sysmod_p) PyObject **sysmod_p)
{ {
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyInterpreterState *interp = tstate->interp;
PyObject *modules = PyDict_New(); PyObject *modules = PyDict_New();
if (modules == NULL) { if (modules == NULL) {