aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Python/pylifecycle.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.ru>2022-02-10 16:44:30 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:44:30 +0300
commit2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch)
tree012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Python/pylifecycle.c
parent6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff)
downloadydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Python/pylifecycle.c')
-rw-r--r--contrib/tools/python3/src/Python/pylifecycle.c2512
1 files changed, 1256 insertions, 1256 deletions
diff --git a/contrib/tools/python3/src/Python/pylifecycle.c b/contrib/tools/python3/src/Python/pylifecycle.c
index 7f591179ac4..8a348e9a636 100644
--- a/contrib/tools/python3/src/Python/pylifecycle.c
+++ b/contrib/tools/python3/src/Python/pylifecycle.c
@@ -3,42 +3,42 @@
#include "Python.h"
#include "Python-ast.h"
-#undef Yield /* undefine macro conflicting with <winbase.h> */
-
-#include "pycore_ceval.h" // _PyEval_FiniGIL()
-#include "pycore_context.h" // _PyContext_Init()
-#include "pycore_fileutils.h" // _Py_ResetForceASCII()
-#include "pycore_import.h" // _PyImport_Cleanup()
-#include "pycore_initconfig.h" // _PyStatus_OK()
-#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
-#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
-#include "pycore_pyerrors.h" // _PyErr_Occurred()
-#include "pycore_pylifecycle.h" // _PyErr_Print()
-#include "pycore_pystate.h" // _PyThreadState_GET()
-#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
-#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
-
-#include "grammar.h" // PyGrammar_RemoveAccelerators()
-#include <locale.h> // setlocale()
-
+#undef Yield /* undefine macro conflicting with <winbase.h> */
+
+#include "pycore_ceval.h" // _PyEval_FiniGIL()
+#include "pycore_context.h" // _PyContext_Init()
+#include "pycore_fileutils.h" // _Py_ResetForceASCII()
+#include "pycore_import.h" // _PyImport_Cleanup()
+#include "pycore_initconfig.h" // _PyStatus_OK()
+#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
+#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
+#include "pycore_pyerrors.h" // _PyErr_Occurred()
+#include "pycore_pylifecycle.h" // _PyErr_Print()
+#include "pycore_pystate.h" // _PyThreadState_GET()
+#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
+#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
+
+#include "grammar.h" // PyGrammar_RemoveAccelerators()
+#include <locale.h> // setlocale()
+
#ifdef HAVE_SIGNAL_H
-# include <signal.h> // SIG_IGN
+# include <signal.h> // SIG_IGN
#endif
#ifdef HAVE_LANGINFO_H
-# include <langinfo.h> // nl_langinfo(CODESET)
+# include <langinfo.h> // nl_langinfo(CODESET)
#endif
#ifdef MS_WINDOWS
-# undef BYTE
-# include "windows.h"
+# undef BYTE
+# include "windows.h"
- extern PyTypeObject PyWindowsConsoleIO_Type;
-# define PyWindowsConsoleIO_Check(op) \
- (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
+ extern PyTypeObject PyWindowsConsoleIO_Type;
+# define PyWindowsConsoleIO_Check(op) \
+ (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
#endif
-
+
_Py_IDENTIFIER(flush);
_Py_IDENTIFIER(name);
_Py_IDENTIFIER(stdin);
@@ -52,20 +52,20 @@ extern "C" {
extern grammar _PyParser_Grammar; /* From graminit.c */
-/* Forward declarations */
-static PyStatus add_main_module(PyInterpreterState *interp);
-static PyStatus init_import_site(void);
-static PyStatus init_set_builtins_open(void);
-static PyStatus init_sys_streams(PyThreadState *tstate);
-static void call_py_exitfuncs(PyThreadState *tstate);
-static void wait_for_thread_shutdown(PyThreadState *tstate);
-static void call_ll_exitfuncs(_PyRuntimeState *runtime);
+/* Forward declarations */
+static PyStatus add_main_module(PyInterpreterState *interp);
+static PyStatus init_import_site(void);
+static PyStatus init_set_builtins_open(void);
+static PyStatus init_sys_streams(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;
+int _Py_UnhandledKeyboardInterrupt = 0;
_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
-static int runtime_initialized = 0;
+static int runtime_initialized = 0;
-PyStatus
+PyStatus
_PyRuntime_Initialize(void)
{
/* XXX We only initialize once in the process, which aligns with
@@ -74,10 +74,10 @@ _PyRuntime_Initialize(void)
every Py_Initialize() call, but doing so breaks the runtime.
This is because the runtime state is not properly finalized
currently. */
- if (runtime_initialized) {
- return _PyStatus_OK();
+ if (runtime_initialized) {
+ return _PyStatus_OK();
}
- runtime_initialized = 1;
+ runtime_initialized = 1;
return _PyRuntimeState_Init(&_PyRuntime);
}
@@ -86,13 +86,13 @@ void
_PyRuntime_Finalize(void)
{
_PyRuntimeState_Fini(&_PyRuntime);
- runtime_initialized = 0;
+ runtime_initialized = 0;
}
int
_Py_IsFinalizing(void)
{
- return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
+ return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
}
/* Hack to force loading of object files */
@@ -138,78 +138,78 @@ Py_IsInitialized(void)
*/
-static PyStatus
-init_importlib(PyThreadState *tstate, PyObject *sysmod)
+static PyStatus
+init_importlib(PyThreadState *tstate, PyObject *sysmod)
{
PyObject *importlib;
PyObject *impmod;
PyObject *value;
- PyInterpreterState *interp = tstate->interp;
- int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
+ PyInterpreterState *interp = tstate->interp;
+ int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
/* Import _importlib through its frozen version, _frozen_importlib. */
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
- return _PyStatus_ERR("can't import _frozen_importlib");
+ return _PyStatus_ERR("can't import _frozen_importlib");
}
- else if (verbose) {
+ else if (verbose) {
PySys_FormatStderr("import _frozen_importlib # frozen\n");
}
importlib = PyImport_AddModule("_frozen_importlib");
if (importlib == NULL) {
- return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
+ return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
}
interp->importlib = importlib;
Py_INCREF(interp->importlib);
interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
if (interp->import_func == NULL)
- return _PyStatus_ERR("__import__ not found");
+ return _PyStatus_ERR("__import__ not found");
Py_INCREF(interp->import_func);
/* Import the _imp module */
impmod = PyInit__imp();
if (impmod == NULL) {
- return _PyStatus_ERR("can't import _imp");
+ return _PyStatus_ERR("can't import _imp");
}
- else if (verbose) {
+ else if (verbose) {
PySys_FormatStderr("import _imp # builtin\n");
}
if (_PyImport_SetModuleString("_imp", impmod) < 0) {
- return _PyStatus_ERR("can't save _imp to sys.modules");
+ return _PyStatus_ERR("can't save _imp to sys.modules");
}
/* Install importlib as the implementation of import */
value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
if (value == NULL) {
- _PyErr_Print(tstate);
- return _PyStatus_ERR("importlib install failed");
+ _PyErr_Print(tstate);
+ return _PyStatus_ERR("importlib install failed");
}
Py_DECREF(value);
Py_DECREF(impmod);
- return _PyStatus_OK();
+ return _PyStatus_OK();
}
-static PyStatus
-init_importlib_external(PyThreadState *tstate)
+static PyStatus
+init_importlib_external(PyThreadState *tstate)
{
PyObject *value;
- value = PyObject_CallMethod(tstate->interp->importlib,
+ value = PyObject_CallMethod(tstate->interp->importlib,
"_install_external_importers", "");
if (value == NULL) {
- _PyErr_Print(tstate);
- return _PyStatus_ERR("external importer setup failed");
+ _PyErr_Print(tstate);
+ return _PyStatus_ERR("external importer setup failed");
}
Py_DECREF(value);
value = PyImport_ImportModule("__res");
if (value == NULL) {
PyErr_Print();
- return _PyStatus_ERR("can't import __res");
+ return _PyStatus_ERR("can't import __res");
}
Py_DECREF(value);
- return _PyImportZip_Init(tstate);
+ return _PyImportZip_Init(tstate);
}
/* Helper functions to better handle the legacy C locale
@@ -233,18 +233,18 @@ init_importlib_external(PyThreadState *tstate)
*/
int
-_Py_LegacyLocaleDetected(int warn)
+_Py_LegacyLocaleDetected(int warn)
{
#ifndef MS_WINDOWS
- if (!warn) {
- const char *locale_override = getenv("LC_ALL");
- if (locale_override != NULL && *locale_override != '\0') {
- /* Don't coerce C locale if the LC_ALL environment variable
- is set */
- return 0;
- }
- }
-
+ if (!warn) {
+ const char *locale_override = getenv("LC_ALL");
+ if (locale_override != NULL && *locale_override != '\0') {
+ /* Don't coerce C locale if the LC_ALL environment variable
+ is set */
+ return 0;
+ }
+ }
+
/* On non-Windows systems, the C locale is considered a legacy locale */
/* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
* the POSIX locale as a simple alias for the C locale, so
@@ -258,7 +258,7 @@ _Py_LegacyLocaleDetected(int warn)
#endif
}
-#ifndef MS_WINDOWS
+#ifndef MS_WINDOWS
static const char *_C_LOCALE_WARNING =
"Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
"encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
@@ -266,14 +266,14 @@ static const char *_C_LOCALE_WARNING =
"locales is recommended.\n";
static void
-emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
+emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
{
- const PyPreConfig *preconfig = &runtime->preconfig;
- if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
- PySys_FormatStderr("%s", _C_LOCALE_WARNING);
+ const PyPreConfig *preconfig = &runtime->preconfig;
+ if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
+ PySys_FormatStderr("%s", _C_LOCALE_WARNING);
}
}
-#endif /* !defined(MS_WINDOWS) */
+#endif /* !defined(MS_WINDOWS) */
typedef struct _CandidateLocale {
const char *locale_name; /* The locale to try as a coercion target */
@@ -286,18 +286,18 @@ static _LocaleCoercionTarget _TARGET_LOCALES[] = {
{NULL}
};
-
-int
-_Py_IsLocaleCoercionTarget(const char *ctype_loc)
+
+int
+_Py_IsLocaleCoercionTarget(const char *ctype_loc)
{
- const _LocaleCoercionTarget *target = NULL;
- for (target = _TARGET_LOCALES; target->locale_name; target++) {
- if (strcmp(ctype_loc, target->locale_name) == 0) {
- return 1;
+ const _LocaleCoercionTarget *target = NULL;
+ for (target = _TARGET_LOCALES; target->locale_name; target++) {
+ if (strcmp(ctype_loc, target->locale_name) == 0) {
+ return 1;
}
- }
- return 0;
-}
+ }
+ return 0;
+}
#ifdef PY_COERCE_C_LOCALE
@@ -305,8 +305,8 @@ static const char C_LOCALE_COERCION_WARNING[] =
"Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
"or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
-static int
-_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
+static int
+_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
{
const char *newloc = target->locale_name;
@@ -317,28 +317,28 @@ _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
if (setenv("LC_CTYPE", newloc, 1)) {
fprintf(stderr,
"Error setting LC_CTYPE, skipping C locale coercion\n");
- return 0;
+ return 0;
}
- if (warn) {
+ if (warn) {
fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
}
/* Reconfigure with the overridden environment variables */
_Py_SetLocaleFromEnv(LC_ALL);
- return 1;
+ return 1;
}
#endif
-int
-_Py_CoerceLegacyLocale(int warn)
+int
+_Py_CoerceLegacyLocale(int warn)
{
- int coerced = 0;
+ int coerced = 0;
#ifdef PY_COERCE_C_LOCALE
char *oldloc = NULL;
oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
if (oldloc == NULL) {
- return coerced;
+ return coerced;
}
const char *locale_override = getenv("LC_ALL");
@@ -349,7 +349,7 @@ _Py_CoerceLegacyLocale(int warn)
const char *new_locale = setlocale(LC_CTYPE,
target->locale_name);
if (new_locale != NULL) {
-#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
+#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
/* Also ensure that nl_langinfo works in this locale */
char *codeset = nl_langinfo(CODESET);
if (!codeset || *codeset == '\0') {
@@ -360,7 +360,7 @@ _Py_CoerceLegacyLocale(int warn)
}
#endif
/* Successfully configured locale, so make it the default */
- coerced = _coerce_default_locale_settings(warn, target);
+ coerced = _coerce_default_locale_settings(warn, target);
goto done;
}
}
@@ -372,7 +372,7 @@ _Py_CoerceLegacyLocale(int warn)
done:
PyMem_RawFree(oldloc);
#endif
- return coerced;
+ return coerced;
}
/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
@@ -441,7 +441,7 @@ _Py_SetLocaleFromEnv(int category)
/* Global initializations. Can be undone by Py_Finalize(). Don't
call this twice without an intervening Py_Finalize() call.
- Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
+ Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
must have a corresponding call to Py_Finalize.
Locking: you must hold the interpreter lock while calling these APIs.
@@ -450,56 +450,56 @@ _Py_SetLocaleFromEnv(int category)
*/
-static PyStatus
-pyinit_core_reconfigure(_PyRuntimeState *runtime,
- PyThreadState **tstate_p,
- const PyConfig *config)
+static PyStatus
+pyinit_core_reconfigure(_PyRuntimeState *runtime,
+ PyThreadState **tstate_p,
+ const PyConfig *config)
{
- PyStatus status;
- PyThreadState *tstate = _PyThreadState_GET();
- if (!tstate) {
- return _PyStatus_ERR("failed to read thread state");
+ PyStatus status;
+ PyThreadState *tstate = _PyThreadState_GET();
+ if (!tstate) {
+ return _PyStatus_ERR("failed to read thread state");
}
- *tstate_p = tstate;
+ *tstate_p = tstate;
- PyInterpreterState *interp = tstate->interp;
- if (interp == NULL) {
- return _PyStatus_ERR("can't make main interpreter");
+ PyInterpreterState *interp = tstate->interp;
+ if (interp == NULL) {
+ return _PyStatus_ERR("can't make main interpreter");
}
- status = _PyConfig_Write(config, runtime);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ status = _PyConfig_Write(config, runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
- status = _PyInterpreterState_SetConfig(interp, config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- config = _PyInterpreterState_GetConfig(interp);
+ status = _PyInterpreterState_SetConfig(interp, config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ config = _PyInterpreterState_GetConfig(interp);
- if (config->_install_importlib) {
- status = _PyConfig_WritePathConfig(config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ if (config->_install_importlib) {
+ status = _PyConfig_WritePathConfig(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
}
- return _PyStatus_OK();
-}
+ return _PyStatus_OK();
+}
-static PyStatus
-pycore_init_runtime(_PyRuntimeState *runtime,
- const PyConfig *config)
-{
- if (runtime->initialized) {
- return _PyStatus_ERR("main interpreter already initialized");
+static PyStatus
+pycore_init_runtime(_PyRuntimeState *runtime,
+ const PyConfig *config)
+{
+ if (runtime->initialized) {
+ return _PyStatus_ERR("main interpreter already initialized");
}
- PyStatus status = _PyConfig_Write(config, runtime);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ PyStatus status = _PyConfig_Write(config, runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
/* Py_Finalize leaves _Py_Finalizing set in order to help daemon
* threads behave a little more gracefully at interpreter shutdown.
@@ -510,676 +510,676 @@ pycore_init_runtime(_PyRuntimeState *runtime,
* threads still hanging around from a previous Py_Initialize/Finalize
* pair :(
*/
- _PyRuntimeState_SetFinalizing(runtime, NULL);
-
- status = _Py_HashRandomization_Init(config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- status = _PyInterpreterState_Enable(runtime);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- return _PyStatus_OK();
-}
-
-
-static PyStatus
-init_interp_create_gil(PyThreadState *tstate)
-{
- PyStatus status;
-
- /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
- only called here. */
- _PyEval_FiniGIL(tstate);
-
- /* Auto-thread-state API */
- status = _PyGILState_Init(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- /* Create the GIL and take it */
- status = _PyEval_InitGIL(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- return _PyStatus_OK();
-}
-
-
-static PyStatus
-pycore_create_interpreter(_PyRuntimeState *runtime,
- const PyConfig *config,
- PyThreadState **tstate_p)
-{
- PyInterpreterState *interp = PyInterpreterState_New();
+ _PyRuntimeState_SetFinalizing(runtime, NULL);
+
+ status = _Py_HashRandomization_Init(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyInterpreterState_Enable(runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ return _PyStatus_OK();
+}
+
+
+static PyStatus
+init_interp_create_gil(PyThreadState *tstate)
+{
+ PyStatus status;
+
+ /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
+ only called here. */
+ _PyEval_FiniGIL(tstate);
+
+ /* Auto-thread-state API */
+ status = _PyGILState_Init(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ /* Create the GIL and take it */
+ status = _PyEval_InitGIL(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ return _PyStatus_OK();
+}
+
+
+static PyStatus
+pycore_create_interpreter(_PyRuntimeState *runtime,
+ const PyConfig *config,
+ PyThreadState **tstate_p)
+{
+ PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
- return _PyStatus_ERR("can't make main interpreter");
+ return _PyStatus_ERR("can't make main interpreter");
}
- PyStatus status = _PyInterpreterState_SetConfig(interp, config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ PyStatus status = _PyInterpreterState_SetConfig(interp, config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
PyThreadState *tstate = PyThreadState_New(interp);
- if (tstate == NULL) {
- return _PyStatus_ERR("can't make first thread");
- }
+ if (tstate == NULL) {
+ return _PyStatus_ERR("can't make first thread");
+ }
(void) PyThreadState_Swap(tstate);
- status = init_interp_create_gil(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- *tstate_p = tstate;
- return _PyStatus_OK();
-}
-
-
-static PyStatus
-pycore_init_types(PyThreadState *tstate)
-{
- PyStatus status;
- int is_main_interp = _Py_IsMainInterpreter(tstate);
-
- status = _PyGC_Init(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- if (is_main_interp) {
- status = _PyTypes_Init();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- }
-
-
- if (!_PyLong_Init(tstate)) {
- return _PyStatus_ERR("can't init longs");
- }
-
- if (is_main_interp) {
- status = _PyUnicode_Init();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- }
-
- status = _PyExc_Init();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- if (is_main_interp) {
- if (!_PyFloat_Init()) {
- return _PyStatus_ERR("can't init float");
- }
-
- if (_PyStructSequence_Init() < 0) {
- return _PyStatus_ERR("can't initialize structseq");
- }
- }
-
- status = _PyErr_Init();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- if (is_main_interp) {
- if (!_PyContext_Init()) {
- return _PyStatus_ERR("can't init context");
- }
- }
-
- return _PyStatus_OK();
-}
-
-
-static PyStatus
-pycore_init_builtins(PyThreadState *tstate)
-{
- assert(!_PyErr_Occurred(tstate));
-
- PyObject *bimod = _PyBuiltin_Init(tstate);
- if (bimod == NULL) {
- goto error;
- }
-
- PyInterpreterState *interp = tstate->interp;
- if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
- goto error;
- }
-
- PyObject *builtins_dict = PyModule_GetDict(bimod);
- if (builtins_dict == NULL) {
- goto error;
- }
- Py_INCREF(builtins_dict);
- interp->builtins = builtins_dict;
-
- PyStatus status = _PyBuiltins_AddExceptions(bimod);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- interp->builtins_copy = PyDict_Copy(interp->builtins);
- if (interp->builtins_copy == NULL) {
- goto error;
- }
- Py_DECREF(bimod);
-
- assert(!_PyErr_Occurred(tstate));
-
- return _PyStatus_OK();
-
-error:
- Py_XDECREF(bimod);
- return _PyStatus_ERR("can't initialize builtins module");
-}
-
-
-static PyStatus
-pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
-{
- assert(!_PyErr_Occurred(tstate));
-
- PyStatus status = _PyImportHooks_Init(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
- if (_Py_IsMainInterpreter(tstate)) {
- /* Initialize _warnings. */
- status = _PyWarnings_InitState(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- if (config->_install_importlib) {
- status = _PyConfig_WritePathConfig(config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- }
- }
+ status = init_interp_create_gil(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ *tstate_p = tstate;
+ return _PyStatus_OK();
+}
+
+
+static PyStatus
+pycore_init_types(PyThreadState *tstate)
+{
+ PyStatus status;
+ int is_main_interp = _Py_IsMainInterpreter(tstate);
+
+ status = _PyGC_Init(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ if (is_main_interp) {
+ status = _PyTypes_Init();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+
+
+ if (!_PyLong_Init(tstate)) {
+ return _PyStatus_ERR("can't init longs");
+ }
+
+ if (is_main_interp) {
+ status = _PyUnicode_Init();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+
+ status = _PyExc_Init();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ if (is_main_interp) {
+ if (!_PyFloat_Init()) {
+ return _PyStatus_ERR("can't init float");
+ }
+
+ if (_PyStructSequence_Init() < 0) {
+ return _PyStatus_ERR("can't initialize structseq");
+ }
+ }
+
+ status = _PyErr_Init();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ if (is_main_interp) {
+ if (!_PyContext_Init()) {
+ return _PyStatus_ERR("can't init context");
+ }
+ }
+
+ return _PyStatus_OK();
+}
+
+
+static PyStatus
+pycore_init_builtins(PyThreadState *tstate)
+{
+ assert(!_PyErr_Occurred(tstate));
+
+ PyObject *bimod = _PyBuiltin_Init(tstate);
+ if (bimod == NULL) {
+ goto error;
+ }
+
+ PyInterpreterState *interp = tstate->interp;
+ if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
+ goto error;
+ }
+
+ PyObject *builtins_dict = PyModule_GetDict(bimod);
+ if (builtins_dict == NULL) {
+ goto error;
+ }
+ Py_INCREF(builtins_dict);
+ interp->builtins = builtins_dict;
+
+ PyStatus status = _PyBuiltins_AddExceptions(bimod);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ interp->builtins_copy = PyDict_Copy(interp->builtins);
+ if (interp->builtins_copy == NULL) {
+ goto error;
+ }
+ Py_DECREF(bimod);
+
+ assert(!_PyErr_Occurred(tstate));
+
+ return _PyStatus_OK();
+
+error:
+ Py_XDECREF(bimod);
+ return _PyStatus_ERR("can't initialize builtins module");
+}
+
+
+static PyStatus
+pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
+{
+ assert(!_PyErr_Occurred(tstate));
+
+ PyStatus status = _PyImportHooks_Init(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+ if (_Py_IsMainInterpreter(tstate)) {
+ /* Initialize _warnings. */
+ status = _PyWarnings_InitState(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ if (config->_install_importlib) {
+ status = _PyConfig_WritePathConfig(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+ }
/* This call sets up builtin and frozen import support */
- if (config->_install_importlib) {
- status = init_importlib(tstate, sysmod);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ if (config->_install_importlib) {
+ status = init_importlib(tstate, sysmod);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
}
-
- assert(!_PyErr_Occurred(tstate));
-
- return _PyStatus_OK();
-}
-
-
-static PyStatus
-pycore_interp_init(PyThreadState *tstate)
-{
- PyStatus status;
- PyObject *sysmod = NULL;
-
- status = pycore_init_types(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- status = _PySys_Create(tstate, &sysmod);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- status = pycore_init_builtins(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- status = pycore_init_import_warnings(tstate, sysmod);
-
-done:
- /* sys.modules['sys'] contains a strong reference to the module */
- Py_XDECREF(sysmod);
- return status;
-}
-
-
-static PyStatus
-pyinit_config(_PyRuntimeState *runtime,
- PyThreadState **tstate_p,
- const PyConfig *config)
-{
- PyStatus status = pycore_init_runtime(runtime, config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- PyThreadState *tstate;
- status = pycore_create_interpreter(runtime, config, &tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- *tstate_p = tstate;
-
- status = pycore_interp_init(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
+
+ assert(!_PyErr_Occurred(tstate));
+
+ return _PyStatus_OK();
+}
+
+
+static PyStatus
+pycore_interp_init(PyThreadState *tstate)
+{
+ PyStatus status;
+ PyObject *sysmod = NULL;
+
+ status = pycore_init_types(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
+ status = _PySys_Create(tstate, &sysmod);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
+ status = pycore_init_builtins(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
+ status = pycore_init_import_warnings(tstate, sysmod);
+
+done:
+ /* sys.modules['sys'] contains a strong reference to the module */
+ Py_XDECREF(sysmod);
+ return status;
+}
+
+
+static PyStatus
+pyinit_config(_PyRuntimeState *runtime,
+ PyThreadState **tstate_p,
+ const PyConfig *config)
+{
+ PyStatus status = pycore_init_runtime(runtime, config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ PyThreadState *tstate;
+ status = pycore_create_interpreter(runtime, config, &tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ *tstate_p = tstate;
+
+ status = pycore_interp_init(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
/* Only when we get here is the runtime core fully initialized */
- runtime->core_initialized = 1;
- return _PyStatus_OK();
-}
-
-
-PyStatus
-_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
-{
- PyStatus status;
-
- if (src_config == NULL) {
- return _PyStatus_ERR("preinitialization config is NULL");
- }
-
- status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- _PyRuntimeState *runtime = &_PyRuntime;
-
- if (runtime->preinitialized) {
- /* If it's already configured: ignored the new configuration */
- return _PyStatus_OK();
- }
-
- /* Note: preinitialized remains 1 on error, it is only set to 0
- at exit on success. */
- runtime->preinitializing = 1;
-
- PyPreConfig config;
-
- status = _PyPreConfig_InitFromPreConfig(&config, src_config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- status = _PyPreConfig_Read(&config, args);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- status = _PyPreConfig_Write(&config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- runtime->preinitializing = 0;
- runtime->preinitialized = 1;
- return _PyStatus_OK();
-}
-
-
-PyStatus
-Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
-{
- _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
- return _Py_PreInitializeFromPyArgv(src_config, &args);
-}
-
-
-PyStatus
-Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
-{
- _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
- return _Py_PreInitializeFromPyArgv(src_config, &args);
-}
-
-
-PyStatus
-Py_PreInitialize(const PyPreConfig *src_config)
-{
- return _Py_PreInitializeFromPyArgv(src_config, NULL);
-}
-
-
-PyStatus
-_Py_PreInitializeFromConfig(const PyConfig *config,
- const _PyArgv *args)
-{
- assert(config != NULL);
-
- PyStatus status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- _PyRuntimeState *runtime = &_PyRuntime;
-
- if (runtime->preinitialized) {
- /* Already initialized: do nothing */
- return _PyStatus_OK();
- }
-
- PyPreConfig preconfig;
-
- _PyPreConfig_InitFromConfig(&preconfig, config);
-
- if (!config->parse_argv) {
- return Py_PreInitialize(&preconfig);
- }
- else if (args == NULL) {
- _PyArgv config_args = {
- .use_bytes_argv = 0,
- .argc = config->argv.length,
- .wchar_argv = config->argv.items};
- return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
- }
+ runtime->core_initialized = 1;
+ return _PyStatus_OK();
+}
+
+
+PyStatus
+_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
+{
+ PyStatus status;
+
+ if (src_config == NULL) {
+ return _PyStatus_ERR("preinitialization config is NULL");
+ }
+
+ status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+
+ if (runtime->preinitialized) {
+ /* If it's already configured: ignored the new configuration */
+ return _PyStatus_OK();
+ }
+
+ /* Note: preinitialized remains 1 on error, it is only set to 0
+ at exit on success. */
+ runtime->preinitializing = 1;
+
+ PyPreConfig config;
+
+ status = _PyPreConfig_InitFromPreConfig(&config, src_config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyPreConfig_Read(&config, args);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ status = _PyPreConfig_Write(&config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ runtime->preinitializing = 0;
+ runtime->preinitialized = 1;
+ return _PyStatus_OK();
+}
+
+
+PyStatus
+Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
+{
+ _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
+ return _Py_PreInitializeFromPyArgv(src_config, &args);
+}
+
+
+PyStatus
+Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
+{
+ _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
+ return _Py_PreInitializeFromPyArgv(src_config, &args);
+}
+
+
+PyStatus
+Py_PreInitialize(const PyPreConfig *src_config)
+{
+ return _Py_PreInitializeFromPyArgv(src_config, NULL);
+}
+
+
+PyStatus
+_Py_PreInitializeFromConfig(const PyConfig *config,
+ const _PyArgv *args)
+{
+ assert(config != NULL);
+
+ PyStatus status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+
+ if (runtime->preinitialized) {
+ /* Already initialized: do nothing */
+ return _PyStatus_OK();
+ }
+
+ PyPreConfig preconfig;
+
+ _PyPreConfig_InitFromConfig(&preconfig, config);
+
+ if (!config->parse_argv) {
+ return Py_PreInitialize(&preconfig);
+ }
+ else if (args == NULL) {
+ _PyArgv config_args = {
+ .use_bytes_argv = 0,
+ .argc = config->argv.length,
+ .wchar_argv = config->argv.items};
+ return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
+ }
else {
- return _Py_PreInitializeFromPyArgv(&preconfig, args);
- }
-}
-
-
-/* Begin interpreter initialization
- *
- * On return, the first thread and interpreter state have been created,
- * but the compiler, signal handling, multithreading and
- * multiple interpreter support, and codec infrastructure are not yet
- * available.
- *
- * The import system will support builtin and frozen modules only.
- * The only supported io is writing to sys.stderr
- *
- * If any operation invoked by this function fails, a fatal error is
- * issued and the function does not return.
- *
- * Any code invoked from this function should *not* assume it has access
- * to the Python C API (unless the API is explicitly listed as being
- * safe to call without calling Py_Initialize first)
- */
-static PyStatus
-pyinit_core(_PyRuntimeState *runtime,
- const PyConfig *src_config,
- PyThreadState **tstate_p)
-{
- PyStatus status;
-
- status = _Py_PreInitializeFromConfig(src_config, NULL);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- PyConfig config;
- _PyConfig_InitCompatConfig(&config);
-
- status = _PyConfig_Copy(&config, src_config);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- status = PyConfig_Read(&config);
- if (_PyStatus_EXCEPTION(status)) {
- goto done;
- }
-
- if (!runtime->core_initialized) {
- status = pyinit_config(runtime, tstate_p, &config);
- }
- else {
- status = pyinit_core_reconfigure(runtime, tstate_p, &config);
- }
- if (_PyStatus_EXCEPTION(status)) {
+ return _Py_PreInitializeFromPyArgv(&preconfig, args);
+ }
+}
+
+
+/* Begin interpreter initialization
+ *
+ * On return, the first thread and interpreter state have been created,
+ * but the compiler, signal handling, multithreading and
+ * multiple interpreter support, and codec infrastructure are not yet
+ * available.
+ *
+ * The import system will support builtin and frozen modules only.
+ * The only supported io is writing to sys.stderr
+ *
+ * If any operation invoked by this function fails, a fatal error is
+ * issued and the function does not return.
+ *
+ * Any code invoked from this function should *not* assume it has access
+ * to the Python C API (unless the API is explicitly listed as being
+ * safe to call without calling Py_Initialize first)
+ */
+static PyStatus
+pyinit_core(_PyRuntimeState *runtime,
+ const PyConfig *src_config,
+ PyThreadState **tstate_p)
+{
+ PyStatus status;
+
+ status = _Py_PreInitializeFromConfig(src_config, NULL);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
+ PyConfig config;
+ _PyConfig_InitCompatConfig(&config);
+
+ status = _PyConfig_Copy(&config, src_config);
+ if (_PyStatus_EXCEPTION(status)) {
goto done;
}
+ status = PyConfig_Read(&config);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
+ if (!runtime->core_initialized) {
+ status = pyinit_config(runtime, tstate_p, &config);
+ }
+ else {
+ status = pyinit_core_reconfigure(runtime, tstate_p, &config);
+ }
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
done:
- PyConfig_Clear(&config);
- return status;
+ PyConfig_Clear(&config);
+ return status;
}
-
+
/* Py_Initialize() has already been called: update the main interpreter
configuration. Example of bpo-34008: Py_Main() called after
Py_Initialize(). */
-static PyStatus
-_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
+static PyStatus
+_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
{
- const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
-
- PyObject *argv = _PyWideStringList_AsList(&config->argv);
- if (argv == NULL) {
- return _PyStatus_NO_MEMORY(); \
- }
-
- int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);
- Py_DECREF(argv);
- if (res < 0) {
- return _PyStatus_ERR("fail to set sys.argv");
+ const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+
+ PyObject *argv = _PyWideStringList_AsList(&config->argv);
+ if (argv == NULL) {
+ return _PyStatus_NO_MEMORY(); \
}
- return _PyStatus_OK();
+
+ int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);
+ Py_DECREF(argv);
+ if (res < 0) {
+ return _PyStatus_ERR("fail to set sys.argv");
+ }
+ return _PyStatus_OK();
}
-
-static PyStatus
-init_interp_main(PyThreadState *tstate)
+
+static PyStatus
+init_interp_main(PyThreadState *tstate)
{
- assert(!_PyErr_Occurred(tstate));
+ assert(!_PyErr_Occurred(tstate));
- PyStatus status;
- int is_main_interp = _Py_IsMainInterpreter(tstate);
- PyInterpreterState *interp = tstate->interp;
- const PyConfig *config = _PyInterpreterState_GetConfig(interp);
+ PyStatus status;
+ int is_main_interp = _Py_IsMainInterpreter(tstate);
+ PyInterpreterState *interp = tstate->interp;
+ const PyConfig *config = _PyInterpreterState_GetConfig(interp);
- if (!config->_install_importlib) {
+ if (!config->_install_importlib) {
/* Special mode for freeze_importlib: run with no import system
*
* This means anything which needs support from extension modules
* or pure Python code in the standard library won't work.
*/
- if (is_main_interp) {
- interp->runtime->initialized = 1;
- }
- return _PyStatus_OK();
+ if (is_main_interp) {
+ interp->runtime->initialized = 1;
+ }
+ return _PyStatus_OK();
}
- if (is_main_interp) {
- if (_PyTime_Init() < 0) {
- return _PyStatus_ERR("can't initialize time");
- }
+ if (is_main_interp) {
+ if (_PyTime_Init() < 0) {
+ return _PyStatus_ERR("can't initialize time");
+ }
}
- if (_PySys_InitMain(tstate) < 0) {
- return _PyStatus_ERR("can't finish initializing sys");
+ if (_PySys_InitMain(tstate) < 0) {
+ return _PyStatus_ERR("can't finish initializing sys");
}
- status = init_importlib_external(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ status = init_importlib_external(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- if (is_main_interp) {
- /* initialize the faulthandler module */
- status = _PyFaulthandler_Init(config->faulthandler);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ if (is_main_interp) {
+ /* initialize the faulthandler module */
+ status = _PyFaulthandler_Init(config->faulthandler);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
}
- status = _PyUnicode_InitEncodings(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ status = _PyUnicode_InitEncodings(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- if (is_main_interp) {
- if (_PySignal_Init(config->install_signal_handlers) < 0) {
- return _PyStatus_ERR("can't initialize signals");
- }
-
- if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
- return _PyStatus_ERR("can't initialize tracemalloc");
- }
+ if (is_main_interp) {
+ if (_PySignal_Init(config->install_signal_handlers) < 0) {
+ return _PyStatus_ERR("can't initialize signals");
+ }
+
+ if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
+ return _PyStatus_ERR("can't initialize tracemalloc");
+ }
}
- status = init_sys_streams(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
+ status = init_sys_streams(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
- status = init_set_builtins_open();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ status = init_set_builtins_open();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- status = add_main_module(interp);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
- if (is_main_interp) {
- /* Initialize warnings. */
- PyObject *warnoptions = PySys_GetObject("warnoptions");
- if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
- {
- PyObject *warnings_module = PyImport_ImportModule("warnings");
- if (warnings_module == NULL) {
- fprintf(stderr, "'import warnings' failed; traceback:\n");
- _PyErr_Print(tstate);
- }
- Py_XDECREF(warnings_module);
- }
-
- interp->runtime->initialized = 1;
+ status = add_main_module(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
- if (config->site_import) {
- status = init_import_site();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ if (is_main_interp) {
+ /* Initialize warnings. */
+ PyObject *warnoptions = PySys_GetObject("warnoptions");
+ if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
+ {
+ PyObject *warnings_module = PyImport_ImportModule("warnings");
+ if (warnings_module == NULL) {
+ fprintf(stderr, "'import warnings' failed; traceback:\n");
+ _PyErr_Print(tstate);
+ }
+ Py_XDECREF(warnings_module);
}
+
+ interp->runtime->initialized = 1;
}
- if (is_main_interp) {
-#ifndef MS_WINDOWS
- emit_stderr_warning_for_legacy_locale(interp->runtime);
-#endif
- }
-
- assert(!_PyErr_Occurred(tstate));
-
- return _PyStatus_OK();
-}
-
-
-/* Update interpreter state based on supplied configuration settings
- *
- * After calling this function, most of the restrictions on the interpreter
- * are lifted. The only remaining incomplete settings are those related
- * to the main module (sys.argv[0], __main__ metadata)
- *
- * Calling this when the interpreter is not initializing, is already
- * initialized or without a valid current thread state is a fatal error.
- * Other errors should be reported as normal Python exceptions with a
- * non-zero return code.
- */
-static PyStatus
-pyinit_main(PyThreadState *tstate)
-{
- PyInterpreterState *interp = tstate->interp;
- if (!interp->runtime->core_initialized) {
- return _PyStatus_ERR("runtime core not initialized");
- }
-
- if (interp->runtime->initialized) {
- return _Py_ReconfigureMainInterpreter(tstate);
- }
-
- PyStatus status = init_interp_main(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- return _PyStatus_OK();
-}
-
-
-PyStatus
-_Py_InitializeMain(void)
-{
- PyStatus status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- _PyRuntimeState *runtime = &_PyRuntime;
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
- return pyinit_main(tstate);
-}
-
-
-PyStatus
-Py_InitializeFromConfig(const PyConfig *config)
-{
- if (config == NULL) {
- return _PyStatus_ERR("initialization config is NULL");
- }
-
- PyStatus status;
-
- status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- _PyRuntimeState *runtime = &_PyRuntime;
-
- PyThreadState *tstate = NULL;
- status = pyinit_core(runtime, config, &tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- config = _PyInterpreterState_GetConfig(tstate->interp);
-
- if (config->_init_main) {
- status = pyinit_main(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
+ if (config->site_import) {
+ status = init_import_site();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
}
-
- return _PyStatus_OK();
+
+ if (is_main_interp) {
+#ifndef MS_WINDOWS
+ emit_stderr_warning_for_legacy_locale(interp->runtime);
+#endif
+ }
+
+ assert(!_PyErr_Occurred(tstate));
+
+ return _PyStatus_OK();
+}
+
+
+/* Update interpreter state based on supplied configuration settings
+ *
+ * After calling this function, most of the restrictions on the interpreter
+ * are lifted. The only remaining incomplete settings are those related
+ * to the main module (sys.argv[0], __main__ metadata)
+ *
+ * Calling this when the interpreter is not initializing, is already
+ * initialized or without a valid current thread state is a fatal error.
+ * Other errors should be reported as normal Python exceptions with a
+ * non-zero return code.
+ */
+static PyStatus
+pyinit_main(PyThreadState *tstate)
+{
+ PyInterpreterState *interp = tstate->interp;
+ if (!interp->runtime->core_initialized) {
+ return _PyStatus_ERR("runtime core not initialized");
+ }
+
+ if (interp->runtime->initialized) {
+ return _Py_ReconfigureMainInterpreter(tstate);
+ }
+
+ PyStatus status = init_interp_main(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ return _PyStatus_OK();
+}
+
+
+PyStatus
+_Py_InitializeMain(void)
+{
+ PyStatus status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ return pyinit_main(tstate);
+}
+
+
+PyStatus
+Py_InitializeFromConfig(const PyConfig *config)
+{
+ if (config == NULL) {
+ return _PyStatus_ERR("initialization config is NULL");
+ }
+
+ PyStatus status;
+
+ status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+
+ PyThreadState *tstate = NULL;
+ status = pyinit_core(runtime, config, &tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ config = _PyInterpreterState_GetConfig(tstate->interp);
+
+ if (config->_init_main) {
+ status = pyinit_main(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+
+ return _PyStatus_OK();
}
void
Py_InitializeEx(int install_sigs)
{
- PyStatus status;
-
- status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- Py_ExitStatusException(status);
- }
- _PyRuntimeState *runtime = &_PyRuntime;
-
- if (runtime->initialized) {
+ PyStatus status;
+
+ status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ Py_ExitStatusException(status);
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+
+ if (runtime->initialized) {
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
return;
}
- PyConfig config;
- _PyConfig_InitCompatConfig(&config);
-
+ PyConfig config;
+ _PyConfig_InitCompatConfig(&config);
+
config.install_signal_handlers = install_sigs;
- status = Py_InitializeFromConfig(&config);
- if (_PyStatus_EXCEPTION(status)) {
- Py_ExitStatusException(status);
+ status = Py_InitializeFromConfig(&config);
+ if (_PyStatus_EXCEPTION(status)) {
+ Py_ExitStatusException(status);
}
}
@@ -1217,7 +1217,7 @@ flush_std_files(void)
int status = 0;
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
- tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
+ tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
if (tmp == NULL) {
PyErr_WriteUnraisable(fout);
status = -1;
@@ -1227,7 +1227,7 @@ flush_std_files(void)
}
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
- tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
+ tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
if (tmp == NULL) {
PyErr_Clear();
status = -1;
@@ -1253,161 +1253,161 @@ flush_std_files(void)
*/
-
-static void
-finalize_interp_types(PyThreadState *tstate, int is_main_interp)
-{
- if (is_main_interp) {
- /* Sundry finalizers */
- _PyAST_Fini();
- _PyFrame_Fini();
- _PyTuple_Fini();
- _PyList_Fini();
- _PySet_Fini();
- _PyBytes_Fini();
- }
-
- _PyLong_Fini(tstate);
-
- if (is_main_interp) {
- _PyFloat_Fini();
- _PyDict_Fini();
- _PySlice_Fini();
- }
-
- _PyWarnings_Fini(tstate->interp);
-
- if (is_main_interp) {
- _Py_HashRandomization_Fini();
- _PyArg_Fini();
- _PyAsyncGen_Fini();
- _PyContext_Fini();
- }
-
- /* Cleanup Unicode implementation */
- _PyUnicode_Fini(tstate);
-
- if (is_main_interp) {
- _Py_ClearFileSystemEncoding();
- }
-}
-
-
-static void
-finalize_interp_clear(PyThreadState *tstate)
-{
- int is_main_interp = _Py_IsMainInterpreter(tstate);
-
- /* Clear interpreter state and all thread states */
- PyInterpreterState_Clear(tstate->interp);
-
- /* Trigger a GC collection on subinterpreters*/
- if (!is_main_interp) {
- _PyGC_CollectNoFail();
- }
-
- /* Clear all loghooks */
- /* Both _PySys_Audit function and users still need PyObject, such as tuple.
- Call _PySys_ClearAuditHooks when PyObject available. */
- if (is_main_interp) {
- _PySys_ClearAuditHooks(tstate);
- }
-
- finalize_interp_types(tstate, is_main_interp);
-
- if (is_main_interp) {
- /* XXX Still allocated:
- - various static ad-hoc pointers to interned strings
- - int and float free list blocks
- - whatever various modules and libraries allocate
- */
-
- PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
-
- _PyExc_Fini();
- }
-
- _PyGC_Fini(tstate);
-}
-
-
-static void
-finalize_interp_delete(PyThreadState *tstate)
-{
- if (_Py_IsMainInterpreter(tstate)) {
- /* Cleanup auto-thread-state */
- _PyGILState_Fini(tstate);
- }
-
- /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
- fail when it is being awaited by another running daemon thread (see
- bpo-9901). Instead pycore_create_interpreter() destroys the previously
- created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
- called multiple times. */
-
- PyInterpreterState_Delete(tstate->interp);
-}
-
-
+
+static void
+finalize_interp_types(PyThreadState *tstate, int is_main_interp)
+{
+ if (is_main_interp) {
+ /* Sundry finalizers */
+ _PyAST_Fini();
+ _PyFrame_Fini();
+ _PyTuple_Fini();
+ _PyList_Fini();
+ _PySet_Fini();
+ _PyBytes_Fini();
+ }
+
+ _PyLong_Fini(tstate);
+
+ if (is_main_interp) {
+ _PyFloat_Fini();
+ _PyDict_Fini();
+ _PySlice_Fini();
+ }
+
+ _PyWarnings_Fini(tstate->interp);
+
+ if (is_main_interp) {
+ _Py_HashRandomization_Fini();
+ _PyArg_Fini();
+ _PyAsyncGen_Fini();
+ _PyContext_Fini();
+ }
+
+ /* Cleanup Unicode implementation */
+ _PyUnicode_Fini(tstate);
+
+ if (is_main_interp) {
+ _Py_ClearFileSystemEncoding();
+ }
+}
+
+
+static void
+finalize_interp_clear(PyThreadState *tstate)
+{
+ int is_main_interp = _Py_IsMainInterpreter(tstate);
+
+ /* Clear interpreter state and all thread states */
+ PyInterpreterState_Clear(tstate->interp);
+
+ /* Trigger a GC collection on subinterpreters*/
+ if (!is_main_interp) {
+ _PyGC_CollectNoFail();
+ }
+
+ /* Clear all loghooks */
+ /* Both _PySys_Audit function and users still need PyObject, such as tuple.
+ Call _PySys_ClearAuditHooks when PyObject available. */
+ if (is_main_interp) {
+ _PySys_ClearAuditHooks(tstate);
+ }
+
+ finalize_interp_types(tstate, is_main_interp);
+
+ if (is_main_interp) {
+ /* XXX Still allocated:
+ - various static ad-hoc pointers to interned strings
+ - int and float free list blocks
+ - whatever various modules and libraries allocate
+ */
+
+ PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
+
+ _PyExc_Fini();
+ }
+
+ _PyGC_Fini(tstate);
+}
+
+
+static void
+finalize_interp_delete(PyThreadState *tstate)
+{
+ if (_Py_IsMainInterpreter(tstate)) {
+ /* Cleanup auto-thread-state */
+ _PyGILState_Fini(tstate);
+ }
+
+ /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
+ fail when it is being awaited by another running daemon thread (see
+ bpo-9901). Instead pycore_create_interpreter() destroys the previously
+ created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
+ called multiple times. */
+
+ PyInterpreterState_Delete(tstate->interp);
+}
+
+
int
Py_FinalizeEx(void)
{
int status = 0;
- _PyRuntimeState *runtime = &_PyRuntime;
- if (!runtime->initialized) {
+ _PyRuntimeState *runtime = &_PyRuntime;
+ if (!runtime->initialized) {
return status;
- }
-
- /* Get current thread state and interpreter pointer */
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
-
- // Wrap up existing "threading"-module-created, non-daemon threads.
- wait_for_thread_shutdown(tstate);
+ }
- // Make any remaining pending calls.
- _Py_FinishPendingCalls(tstate);
+ /* Get current thread state and interpreter pointer */
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+
+ // Wrap up existing "threading"-module-created, non-daemon threads.
+ wait_for_thread_shutdown(tstate);
+ // Make any remaining pending calls.
+ _Py_FinishPendingCalls(tstate);
+
/* 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
* expects Py_IsInitialized() to return true. So don't say the
- * runtime is uninitialized until after the exit funcs have run.
+ * runtime is uninitialized until after the exit funcs have run.
* Note that Threading.py uses an exit func to do a join on all the
* threads created thru it, so this also protects pending imports in
* the threads created via Threading.
*/
- call_py_exitfuncs(tstate);
+ call_py_exitfuncs(tstate);
/* Copy the core config, PyInterpreterState_Delete() free
the core config memory */
#ifdef Py_REF_DEBUG
- int show_ref_count = tstate->interp->config.show_ref_count;
+ int show_ref_count = tstate->interp->config.show_ref_count;
#endif
#ifdef Py_TRACE_REFS
- int dump_refs = tstate->interp->config.dump_refs;
+ int dump_refs = tstate->interp->config.dump_refs;
#endif
#ifdef WITH_PYMALLOC
- int malloc_stats = tstate->interp->config.malloc_stats;
+ int malloc_stats = tstate->interp->config.malloc_stats;
#endif
- /* Remaining daemon threads will automatically exit
- when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
- _PyRuntimeState_SetFinalizing(runtime, tstate);
- runtime->initialized = 0;
- runtime->core_initialized = 0;
-
- /* Destroy the state of all threads of the interpreter, except of the
- current thread. In practice, only daemon threads should still be alive,
- except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
- Clear frames of other threads to call objects destructors. Destructors
- will be called in the current Python thread. Since
- _PyRuntimeState_SetFinalizing() has been called, no other Python thread
- can take the GIL at this point: if they try, they will exit
- immediately. */
- _PyThreadState_DeleteExcept(runtime, tstate);
-
+ /* Remaining daemon threads will automatically exit
+ when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
+ _PyRuntimeState_SetFinalizing(runtime, tstate);
+ runtime->initialized = 0;
+ runtime->core_initialized = 0;
+
+ /* Destroy the state of all threads of the interpreter, except of the
+ current thread. In practice, only daemon threads should still be alive,
+ except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
+ Clear frames of other threads to call objects destructors. Destructors
+ will be called in the current Python thread. Since
+ _PyRuntimeState_SetFinalizing() has been called, no other Python thread
+ can take the GIL at this point: if they try, they will exit
+ immediately. */
+ _PyThreadState_DeleteExcept(runtime, tstate);
+
/* Flush sys.stdout and sys.stderr */
if (flush_std_files() < 0) {
status = -1;
@@ -1431,11 +1431,11 @@ Py_FinalizeEx(void)
_PyGC_CollectIfEnabled();
/* Destroy all modules */
- _PyImport_Cleanup(tstate);
-
- /* Print debug stats if any */
- _PyEval_Fini();
+ _PyImport_Cleanup(tstate);
+ /* Print debug stats if any */
+ _PyEval_Fini();
+
/* Flush sys.stdout and sys.stderr (again, in case more was printed) */
if (flush_std_files() < 0) {
status = -1;
@@ -1493,8 +1493,8 @@ Py_FinalizeEx(void)
}
#endif /* Py_TRACE_REFS */
- finalize_interp_clear(tstate);
- finalize_interp_delete(tstate);
+ finalize_interp_clear(tstate);
+ finalize_interp_delete(tstate);
#ifdef Py_TRACE_REFS
/* Display addresses (& refcnts) of all objects still alive.
@@ -1511,7 +1511,7 @@ Py_FinalizeEx(void)
}
#endif
- call_ll_exitfuncs(runtime);
+ call_ll_exitfuncs(runtime);
_PyRuntime_Finalize();
return status;
@@ -1523,7 +1523,7 @@ Py_Finalize(void)
Py_FinalizeEx();
}
-
+
/* Create and initialize a new interpreter and thread, and return the
new thread. This requires that Py_Initialize() has been called
first.
@@ -1537,107 +1537,107 @@ Py_Finalize(void)
*/
-static PyStatus
-new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
+static PyStatus
+new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
{
- PyStatus status;
+ PyStatus status;
- status = _PyRuntime_Initialize();
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- _PyRuntimeState *runtime = &_PyRuntime;
-
- if (!runtime->initialized) {
- return _PyStatus_ERR("Py_Initialize must be called first");
+ status = _PyRuntime_Initialize();
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
}
+ _PyRuntimeState *runtime = &_PyRuntime;
+ if (!runtime->initialized) {
+ return _PyStatus_ERR("Py_Initialize must be called first");
+ }
+
/* Issue #10915, #15751: The GIL API doesn't work with multiple
interpreters: disable PyGILState_Check(). */
- runtime->gilstate.check_enabled = 0;
+ runtime->gilstate.check_enabled = 0;
- PyInterpreterState *interp = PyInterpreterState_New();
+ PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
*tstate_p = NULL;
- return _PyStatus_OK();
+ return _PyStatus_OK();
}
- PyThreadState *tstate = PyThreadState_New(interp);
+ PyThreadState *tstate = PyThreadState_New(interp);
if (tstate == NULL) {
PyInterpreterState_Delete(interp);
*tstate_p = NULL;
- return _PyStatus_OK();
+ return _PyStatus_OK();
}
- PyThreadState *save_tstate = PyThreadState_Swap(tstate);
+ PyThreadState *save_tstate = PyThreadState_Swap(tstate);
/* Copy the current interpreter config into the new interpreter */
- const PyConfig *config;
+ const PyConfig *config;
if (save_tstate != NULL) {
- config = _PyInterpreterState_GetConfig(save_tstate->interp);
- }
- else
- {
+ config = _PyInterpreterState_GetConfig(save_tstate->interp);
+ }
+ else
+ {
/* No current thread state, copy from the main interpreter */
PyInterpreterState *main_interp = PyInterpreterState_Main();
- config = _PyInterpreterState_GetConfig(main_interp);
- }
-
- status = _PyInterpreterState_SetConfig(interp, config);
- if (_PyStatus_EXCEPTION(status)) {
- goto error;
+ config = _PyInterpreterState_GetConfig(main_interp);
}
- interp->config._isolated_interpreter = isolated_subinterpreter;
- status = init_interp_create_gil(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- goto error;
+ status = _PyInterpreterState_SetConfig(interp, config);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto error;
}
-
- status = pycore_interp_init(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- goto error;
+ interp->config._isolated_interpreter = isolated_subinterpreter;
+
+ status = init_interp_create_gil(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto error;
}
- status = init_interp_main(tstate);
- if (_PyStatus_EXCEPTION(status)) {
- goto error;
+ status = pycore_interp_init(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto error;
+ }
+
+ status = init_interp_main(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto error;
}
*tstate_p = tstate;
- return _PyStatus_OK();
-
-error:
- *tstate_p = NULL;
+ return _PyStatus_OK();
+error:
+ *tstate_p = NULL;
+
/* Oops, it didn't work. Undo it all. */
PyErr_PrintEx(0);
PyThreadState_Clear(tstate);
PyThreadState_Delete(tstate);
PyInterpreterState_Delete(interp);
- PyThreadState_Swap(save_tstate);
+ PyThreadState_Swap(save_tstate);
- return status;
+ return status;
}
PyThreadState *
-_Py_NewInterpreter(int isolated_subinterpreter)
+_Py_NewInterpreter(int isolated_subinterpreter)
{
- PyThreadState *tstate = NULL;
- PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
- if (_PyStatus_EXCEPTION(status)) {
- Py_ExitStatusException(status);
+ PyThreadState *tstate = NULL;
+ PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
+ if (_PyStatus_EXCEPTION(status)) {
+ Py_ExitStatusException(status);
}
return tstate;
}
-PyThreadState *
-Py_NewInterpreter(void)
-{
- return _Py_NewInterpreter(0);
-}
-
+PyThreadState *
+Py_NewInterpreter(void)
+{
+ return _Py_NewInterpreter(0);
+}
+
/* Delete an interpreter and its last thread. This requires that the
given thread state is current, that the thread has no remaining
frames, and that it is its interpreter's only remaining thread.
@@ -1655,53 +1655,53 @@ Py_EndInterpreter(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
- if (tstate != _PyThreadState_GET()) {
- Py_FatalError("thread is not current");
- }
- if (tstate->frame != NULL) {
- Py_FatalError("thread still has a frame");
- }
- interp->finalizing = 1;
+ if (tstate != _PyThreadState_GET()) {
+ Py_FatalError("thread is not current");
+ }
+ if (tstate->frame != NULL) {
+ Py_FatalError("thread still has a frame");
+ }
+ interp->finalizing = 1;
- // Wrap up existing "threading"-module-created, non-daemon threads.
- wait_for_thread_shutdown(tstate);
+ // Wrap up existing "threading"-module-created, non-daemon threads.
+ wait_for_thread_shutdown(tstate);
- call_py_exitfuncs(tstate);
+ call_py_exitfuncs(tstate);
- if (tstate != interp->tstate_head || tstate->next != NULL) {
- Py_FatalError("not the last thread");
- }
+ if (tstate != interp->tstate_head || tstate->next != NULL) {
+ Py_FatalError("not the last thread");
+ }
- _PyImport_Cleanup(tstate);
- finalize_interp_clear(tstate);
- finalize_interp_delete(tstate);
+ _PyImport_Cleanup(tstate);
+ finalize_interp_clear(tstate);
+ finalize_interp_delete(tstate);
}
/* Add the __main__ module */
-static PyStatus
+static PyStatus
add_main_module(PyInterpreterState *interp)
{
PyObject *m, *d, *loader, *ann_dict;
m = PyImport_AddModule("__main__");
if (m == NULL)
- return _PyStatus_ERR("can't create __main__ module");
+ return _PyStatus_ERR("can't create __main__ module");
d = PyModule_GetDict(m);
ann_dict = PyDict_New();
if ((ann_dict == NULL) ||
(PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
- return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
+ return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
}
Py_DECREF(ann_dict);
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
PyObject *bimod = PyImport_ImportModule("builtins");
if (bimod == NULL) {
- return _PyStatus_ERR("Failed to retrieve builtins module");
+ return _PyStatus_ERR("Failed to retrieve builtins module");
}
if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
- return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
+ return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
}
Py_DECREF(bimod);
}
@@ -1717,28 +1717,28 @@ add_main_module(PyInterpreterState *interp)
PyObject *loader = PyObject_GetAttrString(interp->importlib,
"BuiltinImporter");
if (loader == NULL) {
- return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
+ return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
}
if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
- return _PyStatus_ERR("Failed to initialize __main__.__loader__");
+ return _PyStatus_ERR("Failed to initialize __main__.__loader__");
}
Py_DECREF(loader);
}
- return _PyStatus_OK();
+ return _PyStatus_OK();
}
/* Import the site module (not into __main__ though) */
-static PyStatus
-init_import_site(void)
+static PyStatus
+init_import_site(void)
{
PyObject *m;
m = PyImport_ImportModule("site");
if (m == NULL) {
- return _PyStatus_ERR("Failed to import the site module");
+ return _PyStatus_ERR("Failed to import the site module");
}
Py_DECREF(m);
- return _PyStatus_OK();
+ return _PyStatus_OK();
}
/* Check if a file descriptor is valid or not.
@@ -1746,42 +1746,42 @@ init_import_site(void)
static int
is_valid_fd(int fd)
{
-/* dup() is faster than fstat(): fstat() can require input/output operations,
- whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
- startup. Problem: dup() doesn't check if the file descriptor is valid on
- some platforms.
-
- bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
- side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
- EBADF. FreeBSD has similar issue (bpo-32849).
-
- Only use dup() on platforms where dup() is enough to detect invalid FD in
- corner cases: on Linux and Windows (bpo-32849). */
-#if defined(__linux__) || defined(MS_WINDOWS)
- if (fd < 0) {
- return 0;
- }
+/* dup() is faster than fstat(): fstat() can require input/output operations,
+ whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
+ startup. Problem: dup() doesn't check if the file descriptor is valid on
+ some platforms.
+
+ bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
+ side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
+ EBADF. FreeBSD has similar issue (bpo-32849).
+
+ Only use dup() on platforms where dup() is enough to detect invalid FD in
+ corner cases: on Linux and Windows (bpo-32849). */
+#if defined(__linux__) || defined(MS_WINDOWS)
+ if (fd < 0) {
+ return 0;
+ }
int fd2;
-
+
_Py_BEGIN_SUPPRESS_IPH
fd2 = dup(fd);
- if (fd2 >= 0) {
+ if (fd2 >= 0) {
close(fd2);
- }
+ }
_Py_END_SUPPRESS_IPH
-
- return (fd2 >= 0);
-#else
- struct stat st;
- return (fstat(fd, &st) == 0);
+
+ return (fd2 >= 0);
+#else
+ struct stat st;
+ return (fstat(fd, &st) == 0);
#endif
}
/* returns Py_None if the fd is not valid */
static PyObject*
-create_stdio(const PyConfig *config, PyObject* io,
+create_stdio(const PyConfig *config, PyObject* io,
int fd, int write_mode, const char* name,
- const wchar_t* encoding, const wchar_t* errors)
+ const wchar_t* encoding, const wchar_t* errors)
{
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
const char* mode;
@@ -1792,7 +1792,7 @@ create_stdio(const PyConfig *config, PyObject* io,
_Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(TextIOWrapper);
_Py_IDENTIFIER(mode);
- const int buffered_stdio = config->buffered_stdio;
+ const int buffered_stdio = config->buffered_stdio;
if (!is_valid_fd(fd))
Py_RETURN_NONE;
@@ -1802,7 +1802,7 @@ create_stdio(const PyConfig *config, PyObject* io,
depends on the presence of a read1() method which only exists on
buffered streams.
*/
- if (!buffered_stdio && write_mode)
+ if (!buffered_stdio && write_mode)
buffering = 0;
else
buffering = -1;
@@ -1810,10 +1810,10 @@ create_stdio(const PyConfig *config, PyObject* io,
mode = "wb";
else
mode = "rb";
- buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
+ buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
fd, mode, buffering,
Py_None, Py_None, /* encoding, errors */
- Py_None, Py_False); /* newline, closefd */
+ Py_None, Py_False); /* newline, closefd */
if (buf == NULL)
goto error;
@@ -1831,24 +1831,24 @@ create_stdio(const PyConfig *config, PyObject* io,
#ifdef MS_WINDOWS
/* Windows console IO is always UTF-8 encoded */
if (PyWindowsConsoleIO_Check(raw))
- encoding = L"utf-8";
+ encoding = L"utf-8";
#endif
text = PyUnicode_FromString(name);
if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
goto error;
- res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
+ res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
if (res == NULL)
goto error;
isatty = PyObject_IsTrue(res);
Py_DECREF(res);
if (isatty == -1)
goto error;
- if (!buffered_stdio)
+ if (!buffered_stdio)
write_through = Py_True;
else
write_through = Py_False;
- if (buffered_stdio && (isatty || fd == fileno(stderr)))
+ if (buffered_stdio && (isatty || fd == fileno(stderr)))
line_buffering = Py_True;
else
line_buffering = Py_False;
@@ -1867,25 +1867,25 @@ create_stdio(const PyConfig *config, PyObject* io,
newline = "\n";
#endif
- PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
- if (encoding_str == NULL) {
- Py_CLEAR(buf);
- goto error;
- }
-
- PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
- if (errors_str == NULL) {
- Py_CLEAR(buf);
- Py_CLEAR(encoding_str);
- goto error;
- }
-
- stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
- buf, encoding_str, errors_str,
+ PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
+ if (encoding_str == NULL) {
+ Py_CLEAR(buf);
+ goto error;
+ }
+
+ PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
+ if (errors_str == NULL) {
+ Py_CLEAR(buf);
+ Py_CLEAR(encoding_str);
+ goto error;
+ }
+
+ stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
+ buf, encoding_str, errors_str,
newline, line_buffering, write_through);
Py_CLEAR(buf);
- Py_CLEAR(encoding_str);
- Py_CLEAR(errors_str);
+ Py_CLEAR(encoding_str);
+ Py_CLEAR(errors_str);
if (stream == NULL)
goto error;
@@ -1915,69 +1915,69 @@ error:
return NULL;
}
-/* Set builtins.open to io.OpenWrapper */
-static PyStatus
-init_set_builtins_open(void)
+/* Set builtins.open to io.OpenWrapper */
+static PyStatus
+init_set_builtins_open(void)
{
PyObject *iomod = NULL, *wrapper;
PyObject *bimod = NULL;
- PyStatus res = _PyStatus_OK();
-
- if (!(iomod = PyImport_ImportModule("io"))) {
- goto error;
- }
-
- if (!(bimod = PyImport_ImportModule("builtins"))) {
- goto error;
- }
-
- if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
- goto error;
- }
-
- /* Set builtins.open */
- if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
- Py_DECREF(wrapper);
- goto error;
- }
- Py_DECREF(wrapper);
- goto done;
-
-error:
- res = _PyStatus_ERR("can't initialize io.open");
-
-done:
- Py_XDECREF(bimod);
- Py_XDECREF(iomod);
- return res;
-}
-
-
-/* Initialize sys.stdin, stdout, stderr and builtins.open */
-static PyStatus
-init_sys_streams(PyThreadState *tstate)
-{
- PyObject *iomod = NULL;
+ PyStatus res = _PyStatus_OK();
+
+ if (!(iomod = PyImport_ImportModule("io"))) {
+ goto error;
+ }
+
+ if (!(bimod = PyImport_ImportModule("builtins"))) {
+ goto error;
+ }
+
+ if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
+ goto error;
+ }
+
+ /* Set builtins.open */
+ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
+ Py_DECREF(wrapper);
+ goto error;
+ }
+ Py_DECREF(wrapper);
+ goto done;
+
+error:
+ res = _PyStatus_ERR("can't initialize io.open");
+
+done:
+ Py_XDECREF(bimod);
+ Py_XDECREF(iomod);
+ return res;
+}
+
+
+/* Initialize sys.stdin, stdout, stderr and builtins.open */
+static PyStatus
+init_sys_streams(PyThreadState *tstate)
+{
+ PyObject *iomod = NULL;
PyObject *m;
PyObject *std = NULL;
int fd;
PyObject * encoding_attr;
- PyStatus res = _PyStatus_OK();
- const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
-
- /* Check that stdin is not a directory
- Using shell redirection, you can redirect stdin to a directory,
- crashing the Python interpreter. Catch this common mistake here
- and output a useful error message. Note that under MS Windows,
- the shell already prevents that. */
-#ifndef MS_WINDOWS
- struct _Py_stat_struct sb;
- if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
- S_ISDIR(sb.st_mode)) {
- return _PyStatus_ERR("<stdin> is a directory, cannot continue");
- }
-#endif
-
+ PyStatus res = _PyStatus_OK();
+ const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+
+ /* Check that stdin is not a directory
+ Using shell redirection, you can redirect stdin to a directory,
+ crashing the Python interpreter. Catch this common mistake here
+ and output a useful error message. Note that under MS Windows,
+ the shell already prevents that. */
+#ifndef MS_WINDOWS
+ struct _Py_stat_struct sb;
+ if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
+ S_ISDIR(sb.st_mode)) {
+ return _PyStatus_ERR("<stdin> is a directory, cannot continue");
+ }
+#endif
+
/* Hack to avoid a nasty recursion issue when Python is invoked
in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
@@ -2000,9 +2000,9 @@ init_sys_streams(PyThreadState *tstate)
* and fileno() may point to an invalid file descriptor. For example
* GUI apps don't have valid standard streams by default.
*/
- std = create_stdio(config, iomod, fd, 0, "<stdin>",
- config->stdio_encoding,
- config->stdio_errors);
+ std = create_stdio(config, iomod, fd, 0, "<stdin>",
+ config->stdio_encoding,
+ config->stdio_errors);
if (std == NULL)
goto error;
PySys_SetObject("__stdin__", std);
@@ -2011,9 +2011,9 @@ init_sys_streams(PyThreadState *tstate)
/* Set sys.stdout */
fd = fileno(stdout);
- std = create_stdio(config, iomod, fd, 1, "<stdout>",
- config->stdio_encoding,
- config->stdio_errors);
+ std = create_stdio(config, iomod, fd, 1, "<stdout>",
+ config->stdio_encoding,
+ config->stdio_errors);
if (std == NULL)
goto error;
PySys_SetObject("__stdout__", std);
@@ -2023,9 +2023,9 @@ init_sys_streams(PyThreadState *tstate)
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
/* Set sys.stderr, replaces the preliminary stderr */
fd = fileno(stderr);
- std = create_stdio(config, iomod, fd, 1, "<stderr>",
- config->stdio_encoding,
- L"backslashreplace");
+ std = create_stdio(config, iomod, fd, 1, "<stderr>",
+ config->stdio_encoding,
+ L"backslashreplace");
if (std == NULL)
goto error;
@@ -2040,7 +2040,7 @@ init_sys_streams(PyThreadState *tstate)
}
Py_DECREF(encoding_attr);
}
- _PyErr_Clear(tstate); /* 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);
@@ -2056,24 +2056,24 @@ init_sys_streams(PyThreadState *tstate)
goto done;
error:
- res = _PyStatus_ERR("can't initialize sys standard streams");
+ res = _PyStatus_ERR("can't initialize sys standard streams");
done:
- _Py_ClearStandardStreamEncoding();
+ _Py_ClearStandardStreamEncoding();
Py_XDECREF(iomod);
return res;
}
static void
-_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
- PyThreadState *tstate)
+_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
+ PyThreadState *tstate)
{
fputc('\n', stderr);
fflush(stderr);
/* display the current Python stack */
- _Py_DumpTracebackThreads(fd, interp, tstate);
+ _Py_DumpTracebackThreads(fd, interp, tstate);
}
/* Print the current exception (if an exception is set) with its traceback,
@@ -2085,13 +2085,13 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
Return 1 if the traceback was displayed, 0 otherwise. */
static int
-_Py_FatalError_PrintExc(PyThreadState *tstate)
+_Py_FatalError_PrintExc(PyThreadState *tstate)
{
PyObject *ferr, *res;
PyObject *exception, *v, *tb;
int has_tb;
- _PyErr_Fetch(tstate, &exception, &v, &tb);
+ _PyErr_Fetch(tstate, &exception, &v, &tb);
if (exception == NULL) {
/* No current exception */
return 0;
@@ -2104,7 +2104,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
return 0;
}
- _PyErr_NormalizeException(tstate, &exception, &v, &tb);
+ _PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) {
tb = Py_None;
Py_INCREF(tb);
@@ -2122,13 +2122,13 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
Py_XDECREF(tb);
/* sys.stderr may be buffered: call sys.stderr.flush() */
- res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
- if (res == NULL) {
- _PyErr_Clear(tstate);
- }
- else {
+ res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
+ if (res == NULL) {
+ _PyErr_Clear(tstate);
+ }
+ else {
Py_DECREF(res);
- }
+ }
return has_tb;
}
@@ -2170,108 +2170,108 @@ fatal_output_debug(const char *msg)
}
#endif
-
-static void
-fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
-{
- fprintf(stream, "Python runtime state: ");
- PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
- if (finalizing) {
- fprintf(stream, "finalizing (tstate=%p)", finalizing);
- }
- else if (runtime->initialized) {
- fprintf(stream, "initialized");
- }
- else if (runtime->core_initialized) {
- fprintf(stream, "core initialized");
- }
- else if (runtime->preinitialized) {
- fprintf(stream, "preinitialized");
- }
- else if (runtime->preinitializing) {
- fprintf(stream, "preinitializing");
- }
- else {
- fprintf(stream, "unknown");
- }
- fprintf(stream, "\n");
- fflush(stream);
-}
-
-
-static inline void _Py_NO_RETURN
-fatal_error_exit(int status)
-{
- if (status < 0) {
-#if defined(MS_WINDOWS) && defined(_DEBUG)
- DebugBreak();
-#endif
- abort();
- }
- else {
- exit(status);
- }
-}
-
-
+
+static void
+fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
+{
+ fprintf(stream, "Python runtime state: ");
+ PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
+ if (finalizing) {
+ fprintf(stream, "finalizing (tstate=%p)", finalizing);
+ }
+ else if (runtime->initialized) {
+ fprintf(stream, "initialized");
+ }
+ else if (runtime->core_initialized) {
+ fprintf(stream, "core initialized");
+ }
+ else if (runtime->preinitialized) {
+ fprintf(stream, "preinitialized");
+ }
+ else if (runtime->preinitializing) {
+ fprintf(stream, "preinitializing");
+ }
+ else {
+ fprintf(stream, "unknown");
+ }
+ fprintf(stream, "\n");
+ fflush(stream);
+}
+
+
+static inline void _Py_NO_RETURN
+fatal_error_exit(int status)
+{
+ if (status < 0) {
+#if defined(MS_WINDOWS) && defined(_DEBUG)
+ DebugBreak();
+#endif
+ abort();
+ }
+ else {
+ exit(status);
+ }
+}
+
+
static void _Py_NO_RETURN
-fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
- int status)
+fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
+ int status)
{
- const int fd = fileno(stream);
+ const int fd = fileno(stream);
static int reentrant = 0;
if (reentrant) {
/* Py_FatalError() caused a second fatal error.
Example: flush_std_files() raises a recursion error. */
- fatal_error_exit(status);
+ fatal_error_exit(status);
}
reentrant = 1;
- if (header) {
- fprintf(stream, "Fatal Python error: ");
- if (prefix) {
- fputs(prefix, stream);
- fputs(": ", stream);
- }
- if (msg) {
- fputs(msg, stream);
- }
- else {
- fprintf(stream, "<message not set>");
- }
- fputs("\n", stream);
- fflush(stream);
- }
-
- _PyRuntimeState *runtime = &_PyRuntime;
- fatal_error_dump_runtime(stream, runtime);
-
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
- PyInterpreterState *interp = NULL;
- if (tstate != NULL) {
- interp = tstate->interp;
- }
-
+ if (header) {
+ fprintf(stream, "Fatal Python error: ");
+ if (prefix) {
+ fputs(prefix, stream);
+ fputs(": ", stream);
+ }
+ if (msg) {
+ fputs(msg, stream);
+ }
+ else {
+ fprintf(stream, "<message not set>");
+ }
+ fputs("\n", stream);
+ fflush(stream);
+ }
+
+ _PyRuntimeState *runtime = &_PyRuntime;
+ fatal_error_dump_runtime(stream, runtime);
+
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ PyInterpreterState *interp = NULL;
+ if (tstate != NULL) {
+ interp = tstate->interp;
+ }
+
/* Check if the current thread has a Python thread state
- and holds the GIL.
-
- tss_tstate is NULL if Py_FatalError() is called from a C thread which
- has no Python thread state.
-
- tss_tstate != tstate if the current Python thread does not hold the GIL.
- */
+ and holds the GIL.
+
+ tss_tstate is NULL if Py_FatalError() is called from a C thread which
+ has no Python thread state.
+
+ tss_tstate != tstate if the current Python thread does not hold the GIL.
+ */
PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
- int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
+ int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
if (has_tstate_and_gil) {
/* If an exception is set, print the exception with its traceback */
- if (!_Py_FatalError_PrintExc(tss_tstate)) {
+ if (!_Py_FatalError_PrintExc(tss_tstate)) {
/* No exception is set, or an exception is set without traceback */
- _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
+ _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
}
}
else {
- _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
+ _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
}
/* The main purpose of faulthandler is to display the traceback.
@@ -2290,72 +2290,72 @@ fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
fatal_output_debug(msg);
#endif /* MS_WINDOWS */
- fatal_error_exit(status);
+ fatal_error_exit(status);
}
-
-#undef Py_FatalError
-
+
+#undef Py_FatalError
+
void _Py_NO_RETURN
Py_FatalError(const char *msg)
{
- fatal_error(stderr, 1, NULL, msg, -1);
+ fatal_error(stderr, 1, NULL, msg, -1);
}
-
+
void _Py_NO_RETURN
-_Py_FatalErrorFunc(const char *func, const char *msg)
-{
- fatal_error(stderr, 1, func, msg, -1);
-}
-
-
-void _Py_NO_RETURN
-_Py_FatalErrorFormat(const char *func, const char *format, ...)
-{
- static int reentrant = 0;
- if (reentrant) {
- /* _Py_FatalErrorFormat() caused a second fatal error */
- fatal_error_exit(-1);
- }
- reentrant = 1;
-
- FILE *stream = stderr;
- fprintf(stream, "Fatal Python error: ");
- if (func) {
- fputs(func, stream);
- fputs(": ", stream);
- }
- fflush(stream);
-
- va_list vargs;
-#ifdef HAVE_STDARG_PROTOTYPES
- va_start(vargs, format);
-#else
- va_start(vargs);
-#endif
- vfprintf(stream, format, vargs);
- va_end(vargs);
-
- fputs("\n", stream);
- fflush(stream);
-
- fatal_error(stream, 0, NULL, NULL, -1);
-}
-
-
-void _Py_NO_RETURN
-Py_ExitStatusException(PyStatus status)
-{
- if (_PyStatus_IS_EXIT(status)) {
- exit(status.exitcode);
- }
- else if (_PyStatus_IS_ERROR(status)) {
- fatal_error(stderr, 1, status.func, status.err_msg, 1);
- }
- else {
- Py_FatalError("Py_ExitStatusException() must not be called on success");
- }
+_Py_FatalErrorFunc(const char *func, const char *msg)
+{
+ fatal_error(stderr, 1, func, msg, -1);
+}
+
+
+void _Py_NO_RETURN
+_Py_FatalErrorFormat(const char *func, const char *format, ...)
+{
+ static int reentrant = 0;
+ if (reentrant) {
+ /* _Py_FatalErrorFormat() caused a second fatal error */
+ fatal_error_exit(-1);
+ }
+ reentrant = 1;
+
+ FILE *stream = stderr;
+ fprintf(stream, "Fatal Python error: ");
+ if (func) {
+ fputs(func, stream);
+ fputs(": ", stream);
+ }
+ fflush(stream);
+
+ va_list vargs;
+#ifdef HAVE_STDARG_PROTOTYPES
+ va_start(vargs, format);
+#else
+ va_start(vargs);
+#endif
+ vfprintf(stream, format, vargs);
+ va_end(vargs);
+
+ fputs("\n", stream);
+ fflush(stream);
+
+ fatal_error(stream, 0, NULL, NULL, -1);
+}
+
+
+void _Py_NO_RETURN
+Py_ExitStatusException(PyStatus status)
+{
+ if (_PyStatus_IS_EXIT(status)) {
+ exit(status.exitcode);
+ }
+ else if (_PyStatus_IS_ERROR(status)) {
+ fatal_error(stderr, 1, status.func, status.err_msg, 1);
+ }
+ else {
+ Py_FatalError("Py_ExitStatusException() must not be called on success");
+ }
}
/* Clean up and exit */
@@ -2363,7 +2363,7 @@ Py_ExitStatusException(PyStatus status)
/* For the atexit module. */
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
{
- PyInterpreterState *is = _PyInterpreterState_GET();
+ PyInterpreterState *is = _PyInterpreterState_GET();
/* Guard against API misuse (see bpo-17852) */
assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
@@ -2373,14 +2373,14 @@ void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
}
static void
-call_py_exitfuncs(PyThreadState *tstate)
+call_py_exitfuncs(PyThreadState *tstate)
{
- PyInterpreterState *interp = tstate->interp;
- if (interp->pyexitfunc == NULL)
+ PyInterpreterState *interp = tstate->interp;
+ if (interp->pyexitfunc == NULL)
return;
- (*interp->pyexitfunc)(interp->pyexitmodule);
- _PyErr_Clear(tstate);
+ (*interp->pyexitfunc)(interp->pyexitmodule);
+ _PyErr_Clear(tstate);
}
/* Wait until threading._shutdown completes, provided
@@ -2388,19 +2388,19 @@ call_py_exitfuncs(PyThreadState *tstate)
The shutdown routine will wait until all non-daemon
"threading" threads have completed. */
static void
-wait_for_thread_shutdown(PyThreadState *tstate)
+wait_for_thread_shutdown(PyThreadState *tstate)
{
_Py_IDENTIFIER(_shutdown);
PyObject *result;
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) {
- if (_PyErr_Occurred(tstate)) {
- PyErr_WriteUnraisable(NULL);
- }
- /* else: threading not imported */
+ if (_PyErr_Occurred(tstate)) {
+ PyErr_WriteUnraisable(NULL);
+ }
+ /* else: threading not imported */
return;
}
- result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
+ result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
if (result == NULL) {
PyErr_WriteUnraisable(threading);
}
@@ -2420,22 +2420,22 @@ int Py_AtExit(void (*func)(void))
}
static void
-call_ll_exitfuncs(_PyRuntimeState *runtime)
+call_ll_exitfuncs(_PyRuntimeState *runtime)
{
- while (runtime->nexitfuncs > 0) {
- /* pop last function from the list */
- runtime->nexitfuncs--;
- void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
- runtime->exitfuncs[runtime->nexitfuncs] = NULL;
-
- exitfunc();
- }
+ while (runtime->nexitfuncs > 0) {
+ /* pop last function from the list */
+ runtime->nexitfuncs--;
+ void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
+ runtime->exitfuncs[runtime->nexitfuncs] = NULL;
+ exitfunc();
+ }
+
fflush(stdout);
fflush(stderr);
}
-void _Py_NO_RETURN
+void _Py_NO_RETURN
Py_Exit(int sts)
{
if (Py_FinalizeEx() < 0) {
@@ -2451,8 +2451,8 @@ Py_Exit(int sts)
* All of the code in this function must only use async-signal-safe functions,
* listed at `man 7 signal` or
* http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
- *
- * If this function is updated, update also _posix_spawn() of subprocess.py.
+ *
+ * If this function is updated, update also _posix_spawn() of subprocess.py.
*/
void
_Py_RestoreSignals(void)