#include "pycore_fileutils.h"
#include "pycore_hamt.h"
#include "pycore_pathconfig.h"
+#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
extern grammar _PyParser_Grammar; /* From graminit.c */
-/* Forward */
+/* Forward declarations */
static PyStatus add_main_module(PyInterpreterState *interp);
-static PyStatus init_import_size(void);
-static PyStatus init_sys_streams(PyInterpreterState *interp);
-static PyStatus init_signals(void);
-static void call_py_exitfuncs(PyInterpreterState *);
-static void wait_for_thread_shutdown(void);
+static PyStatus init_import_site(void);
+static PyStatus init_sys_streams(PyThreadState *tstate);
+static PyStatus init_signals(PyThreadState *tstate);
+static void call_py_exitfuncs(PyThreadState *tstate);
+static void wait_for_thread_shutdown(PyThreadState *tstate);
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
int _Py_UnhandledKeyboardInterrupt = 0;
*/
static PyStatus
-init_importlib(PyInterpreterState *interp, PyObject *sysmod)
+init_importlib(PyThreadState *tstate, PyObject *sysmod)
{
PyObject *importlib;
PyObject *impmod;
PyObject *value;
+ PyInterpreterState *interp = tstate->interp;
int verbose = interp->config.verbose;
/* Import _importlib through its frozen version, _frozen_importlib. */
/* Install importlib as the implementation of import */
value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
if (value == NULL) {
- PyErr_Print();
+ _PyErr_Print(tstate);
return _PyStatus_ERR("importlib install failed");
}
Py_DECREF(value);
value = PyObject_CallMethod(tstate->interp->importlib,
"_install_external_importers", "");
if (value == NULL) {
- PyErr_Print();
+ _PyErr_Print(tstate);
return _PyStatus_ERR("external importer setup failed");
}
Py_DECREF(value);
static PyStatus
pyinit_core_reconfigure(_PyRuntimeState *runtime,
- PyInterpreterState **interp_p,
+ PyThreadState **tstate_p,
const PyConfig *config)
{
PyStatus status;
if (!tstate) {
return _PyStatus_ERR("failed to read thread state");
}
+ *tstate_p = tstate;
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter");
}
- *interp_p = interp;
_PyConfig_Write(config, runtime);
static PyStatus
pycore_create_interpreter(_PyRuntimeState *runtime,
const PyConfig *config,
- PyInterpreterState **interp_p)
+ PyThreadState **tstate_p)
{
PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter");
}
- *interp_p = interp;
PyStatus status = _PyConfig_Copy(&interp->config, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- config = &interp->config;
PyThreadState *tstate = PyThreadState_New(interp);
- if (tstate == NULL)
+ if (tstate == NULL) {
return _PyStatus_ERR("can't make first thread");
+ }
(void) PyThreadState_Swap(tstate);
/* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
_PyEval_FiniThreads(&runtime->ceval);
/* Auto-thread-state API */
- _PyGILState_Init(runtime, interp, tstate);
+ _PyGILState_Init(runtime, tstate);
/* Create the GIL */
PyEval_InitThreads();
+ *tstate_p = tstate;
return _PyStatus_OK();
}
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) {
return _PyStatus_ERR("can't initialize builtins modules");
}
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)) {
return status;
}
- status = _PyImportHooks_Init();
+ status = _PyImportHooks_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_ERR("can't initialize warnings");
}
- if (interp->config._install_importlib) {
- status = _PyConfig_SetPathConfig(&interp->config);
+ if (config->_install_importlib) {
+ status = _PyConfig_SetPathConfig(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
/* This call sets up builtin and frozen import support */
- if (interp->config._install_importlib) {
- status = init_importlib(interp, sysmod);
+ if (config->_install_importlib) {
+ status = init_importlib(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
static PyStatus
pyinit_config(_PyRuntimeState *runtime,
- PyInterpreterState **interp_p,
+ PyThreadState **tstate_p,
const PyConfig *config)
{
- PyInterpreterState *interp;
-
_PyConfig_Write(config, runtime);
PyStatus status = pycore_init_runtime(runtime, config);
return status;
}
- status = pycore_create_interpreter(runtime, config, &interp);
+ PyThreadState *tstate;
+ status = pycore_create_interpreter(runtime, config, &tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- config = &interp->config;
- *interp_p = interp;
+ config = &tstate->interp->config;
+ *tstate_p = tstate;
status = pycore_init_types();
if (_PyStatus_EXCEPTION(status)) {
}
PyObject *sysmod;
- status = _PySys_Create(runtime, interp, &sysmod);
+ status = _PySys_Create(runtime, tstate, &sysmod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = pycore_init_builtins(interp);
+ status = pycore_init_builtins(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = pycore_init_import_warnings(interp, sysmod);
+ status = pycore_init_import_warnings(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
static PyStatus
pyinit_core(_PyRuntimeState *runtime,
const PyConfig *src_config,
- PyInterpreterState **interp_p)
+ PyThreadState **tstate_p)
{
PyStatus status;
}
if (!runtime->core_initialized) {
- status = pyinit_config(runtime, interp_p, &config);
+ status = pyinit_config(runtime, tstate_p, &config);
}
else {
- status = pyinit_core_reconfigure(runtime, interp_p, &config);
+ status = pyinit_core_reconfigure(runtime, tstate_p, &config);
}
if (_PyStatus_EXCEPTION(status)) {
goto done;
* non-zero return code.
*/
static PyStatus
-pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
+pyinit_main(_PyRuntimeState *runtime, PyThreadState *tstate)
{
if (!runtime->core_initialized) {
return _PyStatus_ERR("runtime core not initialized");
}
/* Configure the main interpreter */
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ PyInterpreterState *interp = tstate->interp;
PyConfig *config = &interp->config;
if (runtime->initialized) {
return status;
}
- status = _PyUnicode_InitEncodings(interp);
+ status = _PyUnicode_InitEncodings(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (config->install_signal_handlers) {
- status = init_signals();
+ status = init_signals(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return status;
}
- status = init_sys_streams(interp);
+ status = init_sys_streams(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
PyObject *warnings_module = PyImport_ImportModule("warnings");
if (warnings_module == NULL) {
fprintf(stderr, "'import warnings' failed; traceback:\n");
- PyErr_Print();
+ _PyErr_Print(tstate);
}
Py_XDECREF(warnings_module);
}
runtime->initialized = 1;
if (config->site_import) {
- status = init_import_size(); /* Module site */
+ status = init_import_site();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return status;
}
_PyRuntimeState *runtime = &_PyRuntime;
- PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
-
- return pyinit_main(runtime, interp);
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ return pyinit_main(runtime, tstate);
}
}
_PyRuntimeState *runtime = &_PyRuntime;
- PyInterpreterState *interp = NULL;
- status = pyinit_core(runtime, config, &interp);
+ PyThreadState *tstate = NULL;
+ status = pyinit_core(runtime, config, &tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- config = &interp->config;
+ config = &tstate->interp->config;
if (config->_init_main) {
- status = pyinit_main(runtime, interp);
+ status = pyinit_main(runtime, tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return status;
}
+ /* Get current thread state and interpreter pointer */
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ PyInterpreterState *interp = tstate->interp;
+
// Wrap up existing "threading"-module-created, non-daemon threads.
- wait_for_thread_shutdown();
+ wait_for_thread_shutdown(tstate);
// Make any remaining pending calls.
_Py_FinishPendingCalls(runtime);
- /* Get current thread state and interpreter pointer */
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
- PyInterpreterState *interp = tstate->interp;
-
/* The interpreter is still entirely intact at this point, and the
* 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
* the threads created via Threading.
*/
- call_py_exitfuncs(interp);
+ call_py_exitfuncs(tstate);
/* Copy the core config, PyInterpreterState_Delete() free
the core config memory */
return _PyStatus_ERR("can't finish initializing sys");
}
}
- else if (PyErr_Occurred()) {
+ else if (_PyErr_Occurred(tstate)) {
goto handle_error;
}
goto handle_error;
Py_INCREF(interp->builtins);
}
- else if (PyErr_Occurred()) {
+ else if (_PyErr_Occurred(tstate)) {
goto handle_error;
}
return status;
}
- status = _PyImportHooks_Init();
+ status = _PyImportHooks_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = init_importlib(interp, sysmod);
+ status = init_importlib(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return status;
}
- status = _PyUnicode_InitEncodings(interp);
+ status = _PyUnicode_InitEncodings(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
- status = init_sys_streams(interp);
+ status = init_sys_streams(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
if (config->site_import) {
- status = init_import_size();
+ status = init_import_site();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
}
- if (PyErr_Occurred()) {
+ if (_PyErr_Occurred(tstate)) {
goto handle_error;
}
{
PyInterpreterState *interp = tstate->interp;
- if (tstate != _PyThreadState_GET())
+ if (tstate != _PyThreadState_GET()) {
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");
+ }
interp->finalizing = 1;
// 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");
+ }
_PyImport_Cleanup(tstate);
PyInterpreterState_Clear(interp);
/* Import the site module (not into __main__ though) */
static PyStatus
-init_import_size(void)
+init_import_site(void)
{
PyObject *m;
m = PyImport_ImportModule("site");
/* Initialize sys.stdin, stdout, stderr and builtins.open */
static PyStatus
-init_sys_streams(PyInterpreterState *interp)
+init_sys_streams(PyThreadState *tstate)
{
PyObject *iomod = NULL, *wrapper;
PyObject *bimod = NULL;
int fd;
PyObject * encoding_attr;
PyStatus res = _PyStatus_OK();
- PyConfig *config = &interp->config;
+ const PyConfig *config = &tstate->interp->config;
/* Check that stdin is not a directory
Using shell redirection, you can redirect stdin to a directory,
}
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) {
Py_DECREF(std);
static int
_Py_FatalError_PrintExc(int fd)
{
+ PyThreadState *tstate = _PyThreadState_GET();
PyObject *ferr, *res;
PyObject *exception, *v, *tb;
int has_tb;
- PyErr_Fetch(&exception, &v, &tb);
+ _PyErr_Fetch(tstate, &exception, &v, &tb);
if (exception == NULL) {
/* No current exception */
return 0;
return 0;
}
- PyErr_NormalizeException(&exception, &v, &tb);
+ _PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) {
tb = Py_None;
Py_INCREF(tb);
/* sys.stderr may be buffered: call sys.stderr.flush() */
res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
- if (res == NULL)
- PyErr_Clear();
- else
+ if (res == NULL) {
+ _PyErr_Clear(tstate);
+ }
+ else {
Py_DECREF(res);
+ }
return has_tb;
}
/* For the atexit 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) */
assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
}
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;
- (*istate->pyexitfunc)(istate->pyexitmodule);
- PyErr_Clear();
+ (*interp->pyexitfunc)(interp->pyexitmodule);
+ _PyErr_Clear(tstate);
}
/* Wait until threading._shutdown completes, provided
The shutdown routine will wait until all non-daemon
"threading" threads have completed. */
static void
-wait_for_thread_shutdown(void)
+wait_for_thread_shutdown(PyThreadState *tstate)
{
_Py_IDENTIFIER(_shutdown);
PyObject *result;
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) {
- if (PyErr_Occurred()) {
+ if (_PyErr_Occurred(tstate)) {
PyErr_WriteUnraisable(NULL);
}
/* else: threading not imported */
}
static PyStatus
-init_signals(void)
+init_signals(PyThreadState *tstate)
{
#ifdef SIGPIPE
PyOS_setsig(SIGPIPE, SIG_IGN);
PyOS_setsig(SIGXFSZ, SIG_IGN);
#endif
PyOS_InitInterrupts(); /* May imply initsignal() */
- if (PyErr_Occurred()) {
+ if (_PyErr_Occurred(tstate)) {
return _PyStatus_ERR("can't import signal");
}
return _PyStatus_OK();