blob: 1f826d7f6c445377e0e47de01b92eea494749f0d [file] [log] [blame]
Nick Coghland6009512014-11-20 21:39:37 +10001/* Python interpreter top-level routines, including init/exit */
2
3#include "Python.h"
4
5#include "Python-ast.h"
Victor Stinner3bb183d2018-11-22 18:38:38 +01006#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner4f98f462020-04-15 04:01:58 +02007
8#include "pycore_ceval.h" // _PyEval_FiniGIL()
9#include "pycore_context.h" // _PyContext_Init()
10#include "pycore_fileutils.h" // _Py_ResetForceASCII()
Victor Stinner4f98f462020-04-15 04:01:58 +020011#include "pycore_initconfig.h" // _PyStatus_OK()
12#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
13#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
14#include "pycore_pyerrors.h" // _PyErr_Occurred()
15#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere5014be2020-04-14 17:52:15 +020016#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner4f98f462020-04-15 04:01:58 +020017#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
18#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
19
Victor Stinner4f98f462020-04-15 04:01:58 +020020#include <locale.h> // setlocale()
Nick Coghland6009512014-11-20 21:39:37 +100021
22#ifdef HAVE_SIGNAL_H
Victor Stinner4f98f462020-04-15 04:01:58 +020023# include <signal.h> // SIG_IGN
Nick Coghland6009512014-11-20 21:39:37 +100024#endif
25
26#ifdef HAVE_LANGINFO_H
Victor Stinner4f98f462020-04-15 04:01:58 +020027# include <langinfo.h> // nl_langinfo(CODESET)
Nick Coghland6009512014-11-20 21:39:37 +100028#endif
29
30#ifdef MS_WINDOWS
Victor Stinner4f98f462020-04-15 04:01:58 +020031# undef BYTE
32# include "windows.h"
Steve Dower39294992016-08-30 21:22:36 -070033
Victor Stinner4f98f462020-04-15 04:01:58 +020034 extern PyTypeObject PyWindowsConsoleIO_Type;
35# define PyWindowsConsoleIO_Check(op) \
36 (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
Nick Coghland6009512014-11-20 21:39:37 +100037#endif
38
Victor Stinner4f98f462020-04-15 04:01:58 +020039
Nick Coghland6009512014-11-20 21:39:37 +100040_Py_IDENTIFIER(flush);
41_Py_IDENTIFIER(name);
42_Py_IDENTIFIER(stdin);
43_Py_IDENTIFIER(stdout);
44_Py_IDENTIFIER(stderr);
Eric Snow3f9eee62017-09-15 16:35:20 -060045_Py_IDENTIFIER(threading);
Nick Coghland6009512014-11-20 21:39:37 +100046
47#ifdef __cplusplus
48extern "C" {
49#endif
50
Nick Coghland6009512014-11-20 21:39:37 +100051
Victor Stinnerb45d2592019-06-20 00:05:23 +020052/* Forward declarations */
Victor Stinner331a6a52019-05-27 16:39:22 +020053static PyStatus add_main_module(PyInterpreterState *interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +020054static PyStatus init_import_site(void);
Andy Lester75cd5bf2020-03-12 02:49:05 -050055static PyStatus init_set_builtins_open(void);
Victor Stinnerb45d2592019-06-20 00:05:23 +020056static PyStatus init_sys_streams(PyThreadState *tstate);
57static PyStatus init_signals(PyThreadState *tstate);
58static void call_py_exitfuncs(PyThreadState *tstate);
59static void wait_for_thread_shutdown(PyThreadState *tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +020060static void call_ll_exitfuncs(_PyRuntimeState *runtime);
Nick Coghland6009512014-11-20 21:39:37 +100061
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080062int _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080063_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010064static int runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060065
Victor Stinner331a6a52019-05-27 16:39:22 +020066PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067_PyRuntime_Initialize(void)
68{
69 /* XXX We only initialize once in the process, which aligns with
70 the static initialization of the former globals now found in
71 _PyRuntime. However, _PyRuntime *should* be initialized with
72 every Py_Initialize() call, but doing so breaks the runtime.
73 This is because the runtime state is not properly finalized
74 currently. */
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010075 if (runtime_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +020076 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080077 }
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010078 runtime_initialized = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080079
80 return _PyRuntimeState_Init(&_PyRuntime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060081}
82
83void
84_PyRuntime_Finalize(void)
85{
86 _PyRuntimeState_Fini(&_PyRuntime);
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010087 runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060088}
89
90int
91_Py_IsFinalizing(void)
92{
Victor Stinner7b3c2522020-03-07 00:24:23 +010093 return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094}
95
Nick Coghland6009512014-11-20 21:39:37 +100096/* Hack to force loading of object files */
97int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
98 PyOS_mystrnicmp; /* Python/pystrcmp.o */
99
100/* PyModule_GetWarningsModule is no longer necessary as of 2.6
101since _warnings is builtin. This API should not be used. */
102PyObject *
103PyModule_GetWarningsModule(void)
104{
105 return PyImport_ImportModule("warnings");
106}
107
Eric Snowc7ec9982017-05-23 23:00:52 -0700108
Eric Snow1abcf672017-05-23 21:46:51 -0700109/* APIs to access the initialization flags
110 *
111 * Can be called prior to Py_Initialize.
112 */
Nick Coghland6009512014-11-20 21:39:37 +1000113
Eric Snow1abcf672017-05-23 21:46:51 -0700114int
115_Py_IsCoreInitialized(void)
116{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600117 return _PyRuntime.core_initialized;
Eric Snow1abcf672017-05-23 21:46:51 -0700118}
Nick Coghland6009512014-11-20 21:39:37 +1000119
120int
121Py_IsInitialized(void)
122{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600123 return _PyRuntime.initialized;
Nick Coghland6009512014-11-20 21:39:37 +1000124}
125
Nick Coghlan6ea41862017-06-11 13:16:15 +1000126
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000127/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
128 call this twice without an intervening Py_FinalizeEx() call. When
Nick Coghland6009512014-11-20 21:39:37 +1000129 initializations fail, a fatal error is issued and the function does
130 not return. On return, the first thread and interpreter state have
131 been created.
132
133 Locking: you must hold the interpreter lock while calling this.
134 (If the lock has not yet been initialized, that's equivalent to
135 having the lock, but you cannot use multiple threads.)
136
137*/
138
Victor Stinner331a6a52019-05-27 16:39:22 +0200139static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200140init_importlib(PyThreadState *tstate, PyObject *sysmod)
Nick Coghland6009512014-11-20 21:39:37 +1000141{
142 PyObject *importlib;
143 PyObject *impmod;
Nick Coghland6009512014-11-20 21:39:37 +1000144 PyObject *value;
Victor Stinnerb45d2592019-06-20 00:05:23 +0200145 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200146 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
Nick Coghland6009512014-11-20 21:39:37 +1000147
148 /* Import _importlib through its frozen version, _frozen_importlib. */
149 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200150 return _PyStatus_ERR("can't import _frozen_importlib");
Nick Coghland6009512014-11-20 21:39:37 +1000151 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200152 else if (verbose) {
Nick Coghland6009512014-11-20 21:39:37 +1000153 PySys_FormatStderr("import _frozen_importlib # frozen\n");
154 }
155 importlib = PyImport_AddModule("_frozen_importlib");
156 if (importlib == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200157 return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000158 }
159 interp->importlib = importlib;
160 Py_INCREF(interp->importlib);
161
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200162 interp->import_func = _PyDict_GetItemStringWithError(interp->builtins, "__import__");
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300163 if (interp->import_func == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +0200164 return _PyStatus_ERR("__import__ not found");
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300165 Py_INCREF(interp->import_func);
166
Victor Stinnercd6e6942015-09-18 09:11:57 +0200167 /* Import the _imp module */
Benjamin Petersonc65ef772018-01-29 11:33:57 -0800168 impmod = PyInit__imp();
Nick Coghland6009512014-11-20 21:39:37 +1000169 if (impmod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200170 return _PyStatus_ERR("can't import _imp");
Nick Coghland6009512014-11-20 21:39:37 +1000171 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200172 else if (verbose) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200173 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000174 }
Eric Snow3f9eee62017-09-15 16:35:20 -0600175 if (_PyImport_SetModuleString("_imp", impmod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200176 return _PyStatus_ERR("can't save _imp to sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000177 }
178
Victor Stinnercd6e6942015-09-18 09:11:57 +0200179 /* Install importlib as the implementation of import */
Nick Coghland6009512014-11-20 21:39:37 +1000180 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
181 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200182 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200183 return _PyStatus_ERR("importlib install failed");
Nick Coghland6009512014-11-20 21:39:37 +1000184 }
185 Py_DECREF(value);
186 Py_DECREF(impmod);
187
Victor Stinner331a6a52019-05-27 16:39:22 +0200188 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +1000189}
190
Victor Stinner331a6a52019-05-27 16:39:22 +0200191static PyStatus
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200192init_importlib_external(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -0700193{
194 PyObject *value;
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200195 value = PyObject_CallMethod(tstate->interp->importlib,
Eric Snow1abcf672017-05-23 21:46:51 -0700196 "_install_external_importers", "");
197 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200198 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200199 return _PyStatus_ERR("external importer setup failed");
Eric Snow1abcf672017-05-23 21:46:51 -0700200 }
Stéphane Wirtelab1cb802017-06-08 13:13:20 +0200201 Py_DECREF(value);
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200202 return _PyImportZip_Init(tstate);
Eric Snow1abcf672017-05-23 21:46:51 -0700203}
Nick Coghland6009512014-11-20 21:39:37 +1000204
Nick Coghlan6ea41862017-06-11 13:16:15 +1000205/* Helper functions to better handle the legacy C locale
206 *
207 * The legacy C locale assumes ASCII as the default text encoding, which
208 * causes problems not only for the CPython runtime, but also other
209 * components like GNU readline.
210 *
211 * Accordingly, when the CLI detects it, it attempts to coerce it to a
212 * more capable UTF-8 based alternative as follows:
213 *
214 * if (_Py_LegacyLocaleDetected()) {
215 * _Py_CoerceLegacyLocale();
216 * }
217 *
218 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
219 *
220 * Locale coercion also impacts the default error handler for the standard
221 * streams: while the usual default is "strict", the default for the legacy
222 * C locale and for any of the coercion target locales is "surrogateescape".
223 */
224
225int
Victor Stinner0f721472019-05-20 17:16:38 +0200226_Py_LegacyLocaleDetected(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000227{
228#ifndef MS_WINDOWS
Victor Stinner0f721472019-05-20 17:16:38 +0200229 if (!warn) {
230 const char *locale_override = getenv("LC_ALL");
231 if (locale_override != NULL && *locale_override != '\0') {
232 /* Don't coerce C locale if the LC_ALL environment variable
233 is set */
234 return 0;
235 }
236 }
237
Nick Coghlan6ea41862017-06-11 13:16:15 +1000238 /* On non-Windows systems, the C locale is considered a legacy locale */
Nick Coghlaneb817952017-06-18 12:29:42 +1000239 /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
240 * the POSIX locale as a simple alias for the C locale, so
241 * we may also want to check for that explicitly.
242 */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000243 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
244 return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
245#else
246 /* Windows uses code pages instead of locales, so no locale is legacy */
247 return 0;
248#endif
249}
250
Victor Stinnerb0051362019-11-22 17:52:42 +0100251#ifndef MS_WINDOWS
Nick Coghlaneb817952017-06-18 12:29:42 +1000252static const char *_C_LOCALE_WARNING =
253 "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
254 "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
255 "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
256 "locales is recommended.\n";
257
Nick Coghlaneb817952017-06-18 12:29:42 +1000258static void
Victor Stinner43125222019-04-24 18:23:53 +0200259emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
Nick Coghlaneb817952017-06-18 12:29:42 +1000260{
Victor Stinner331a6a52019-05-27 16:39:22 +0200261 const PyPreConfig *preconfig = &runtime->preconfig;
Victor Stinner0f721472019-05-20 17:16:38 +0200262 if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
Victor Stinnercf215042018-08-29 22:56:06 +0200263 PySys_FormatStderr("%s", _C_LOCALE_WARNING);
Nick Coghlaneb817952017-06-18 12:29:42 +1000264 }
265}
Victor Stinnerb0051362019-11-22 17:52:42 +0100266#endif /* !defined(MS_WINDOWS) */
Nick Coghlaneb817952017-06-18 12:29:42 +1000267
Nick Coghlan6ea41862017-06-11 13:16:15 +1000268typedef struct _CandidateLocale {
269 const char *locale_name; /* The locale to try as a coercion target */
270} _LocaleCoercionTarget;
271
272static _LocaleCoercionTarget _TARGET_LOCALES[] = {
273 {"C.UTF-8"},
274 {"C.utf8"},
Nick Coghlan18974c32017-06-30 00:48:14 +1000275 {"UTF-8"},
Nick Coghlan6ea41862017-06-11 13:16:15 +1000276 {NULL}
277};
278
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200279
280int
281_Py_IsLocaleCoercionTarget(const char *ctype_loc)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000282{
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200283 const _LocaleCoercionTarget *target = NULL;
284 for (target = _TARGET_LOCALES; target->locale_name; target++) {
285 if (strcmp(ctype_loc, target->locale_name) == 0) {
286 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000287 }
Victor Stinner124b9eb2018-08-29 01:29:06 +0200288 }
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200289 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000290}
291
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200292
Nick Coghlan6ea41862017-06-11 13:16:15 +1000293#ifdef PY_COERCE_C_LOCALE
Victor Stinner94540602017-12-16 04:54:22 +0100294static const char C_LOCALE_COERCION_WARNING[] =
Nick Coghlan6ea41862017-06-11 13:16:15 +1000295 "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
296 "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
297
Victor Stinner0f721472019-05-20 17:16:38 +0200298static int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200299_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000300{
301 const char *newloc = target->locale_name;
302
303 /* Reset locale back to currently configured defaults */
xdegaye1588be62017-11-12 12:45:59 +0100304 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000305
306 /* Set the relevant locale environment variable */
307 if (setenv("LC_CTYPE", newloc, 1)) {
308 fprintf(stderr,
309 "Error setting LC_CTYPE, skipping C locale coercion\n");
Victor Stinner0f721472019-05-20 17:16:38 +0200310 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000311 }
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200312 if (warn) {
Victor Stinner94540602017-12-16 04:54:22 +0100313 fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
Nick Coghlaneb817952017-06-18 12:29:42 +1000314 }
Nick Coghlan6ea41862017-06-11 13:16:15 +1000315
316 /* Reconfigure with the overridden environment variables */
xdegaye1588be62017-11-12 12:45:59 +0100317 _Py_SetLocaleFromEnv(LC_ALL);
Victor Stinner0f721472019-05-20 17:16:38 +0200318 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000319}
320#endif
321
Victor Stinner0f721472019-05-20 17:16:38 +0200322int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200323_Py_CoerceLegacyLocale(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000324{
Victor Stinner0f721472019-05-20 17:16:38 +0200325 int coerced = 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000326#ifdef PY_COERCE_C_LOCALE
Victor Stinner8ea09112018-09-03 17:05:18 +0200327 char *oldloc = NULL;
328
329 oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
330 if (oldloc == NULL) {
Victor Stinner0f721472019-05-20 17:16:38 +0200331 return coerced;
Victor Stinner8ea09112018-09-03 17:05:18 +0200332 }
333
Victor Stinner94540602017-12-16 04:54:22 +0100334 const char *locale_override = getenv("LC_ALL");
335 if (locale_override == NULL || *locale_override == '\0') {
336 /* LC_ALL is also not set (or is set to an empty string) */
337 const _LocaleCoercionTarget *target = NULL;
338 for (target = _TARGET_LOCALES; target->locale_name; target++) {
339 const char *new_locale = setlocale(LC_CTYPE,
340 target->locale_name);
341 if (new_locale != NULL) {
Victor Stinnere2510952019-05-02 11:28:57 -0400342#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94540602017-12-16 04:54:22 +0100343 /* Also ensure that nl_langinfo works in this locale */
344 char *codeset = nl_langinfo(CODESET);
345 if (!codeset || *codeset == '\0') {
346 /* CODESET is not set or empty, so skip coercion */
347 new_locale = NULL;
348 _Py_SetLocaleFromEnv(LC_CTYPE);
349 continue;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000350 }
Victor Stinner94540602017-12-16 04:54:22 +0100351#endif
352 /* Successfully configured locale, so make it the default */
Victor Stinner0f721472019-05-20 17:16:38 +0200353 coerced = _coerce_default_locale_settings(warn, target);
Victor Stinner8ea09112018-09-03 17:05:18 +0200354 goto done;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000355 }
356 }
357 }
358 /* No C locale warning here, as Py_Initialize will emit one later */
Victor Stinner8ea09112018-09-03 17:05:18 +0200359
360 setlocale(LC_CTYPE, oldloc);
361
362done:
363 PyMem_RawFree(oldloc);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000364#endif
Victor Stinner0f721472019-05-20 17:16:38 +0200365 return coerced;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000366}
367
xdegaye1588be62017-11-12 12:45:59 +0100368/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
369 * isolate the idiosyncrasies of different libc implementations. It reads the
370 * appropriate environment variable and uses its value to select the locale for
371 * 'category'. */
372char *
373_Py_SetLocaleFromEnv(int category)
374{
Victor Stinner353933e2018-11-23 13:08:26 +0100375 char *res;
xdegaye1588be62017-11-12 12:45:59 +0100376#ifdef __ANDROID__
377 const char *locale;
378 const char **pvar;
379#ifdef PY_COERCE_C_LOCALE
380 const char *coerce_c_locale;
381#endif
382 const char *utf8_locale = "C.UTF-8";
383 const char *env_var_set[] = {
384 "LC_ALL",
385 "LC_CTYPE",
386 "LANG",
387 NULL,
388 };
389
390 /* Android setlocale(category, "") doesn't check the environment variables
391 * and incorrectly sets the "C" locale at API 24 and older APIs. We only
392 * check the environment variables listed in env_var_set. */
393 for (pvar=env_var_set; *pvar; pvar++) {
394 locale = getenv(*pvar);
395 if (locale != NULL && *locale != '\0') {
396 if (strcmp(locale, utf8_locale) == 0 ||
397 strcmp(locale, "en_US.UTF-8") == 0) {
398 return setlocale(category, utf8_locale);
399 }
400 return setlocale(category, "C");
401 }
402 }
403
404 /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
405 * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
406 * Quote from POSIX section "8.2 Internationalization Variables":
407 * "4. If the LANG environment variable is not set or is set to the empty
408 * string, the implementation-defined default locale shall be used." */
409
410#ifdef PY_COERCE_C_LOCALE
411 coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
412 if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
413 /* Some other ported code may check the environment variables (e.g. in
414 * extension modules), so we make sure that they match the locale
415 * configuration */
416 if (setenv("LC_CTYPE", utf8_locale, 1)) {
417 fprintf(stderr, "Warning: failed setting the LC_CTYPE "
418 "environment variable to %s\n", utf8_locale);
419 }
420 }
421#endif
Victor Stinner353933e2018-11-23 13:08:26 +0100422 res = setlocale(category, utf8_locale);
423#else /* !defined(__ANDROID__) */
424 res = setlocale(category, "");
425#endif
426 _Py_ResetForceASCII();
427 return res;
xdegaye1588be62017-11-12 12:45:59 +0100428}
429
Nick Coghlan6ea41862017-06-11 13:16:15 +1000430
Eric Snow1abcf672017-05-23 21:46:51 -0700431/* Global initializations. Can be undone by Py_Finalize(). Don't
432 call this twice without an intervening Py_Finalize() call.
433
Victor Stinner331a6a52019-05-27 16:39:22 +0200434 Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
Eric Snow1abcf672017-05-23 21:46:51 -0700435 must have a corresponding call to Py_Finalize.
436
437 Locking: you must hold the interpreter lock while calling these APIs.
438 (If the lock has not yet been initialized, that's equivalent to
439 having the lock, but you cannot use multiple threads.)
440
441*/
442
Victor Stinner331a6a52019-05-27 16:39:22 +0200443static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200444pyinit_core_reconfigure(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200445 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200446 const PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200447{
Victor Stinner331a6a52019-05-27 16:39:22 +0200448 PyStatus status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100449 PyThreadState *tstate = _PyThreadState_GET();
450 if (!tstate) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200451 return _PyStatus_ERR("failed to read thread state");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100452 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200453 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100454
455 PyInterpreterState *interp = tstate->interp;
456 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200457 return _PyStatus_ERR("can't make main interpreter");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100458 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100459
Victor Stinnere81f6e62020-06-08 18:12:59 +0200460 status = _PyConfig_Write(config, runtime);
461 if (_PyStatus_EXCEPTION(status)) {
462 return status;
463 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200464
Victor Stinnerda7933e2020-04-13 03:04:28 +0200465 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200466 if (_PyStatus_EXCEPTION(status)) {
467 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200468 }
Victor Stinnerda7933e2020-04-13 03:04:28 +0200469 config = _PyInterpreterState_GetConfig(interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +0200470
Victor Stinner331a6a52019-05-27 16:39:22 +0200471 if (config->_install_importlib) {
Victor Stinner12f2f172019-09-26 15:51:50 +0200472 status = _PyConfig_WritePathConfig(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200473 if (_PyStatus_EXCEPTION(status)) {
474 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200475 }
476 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200477 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200478}
479
480
Victor Stinner331a6a52019-05-27 16:39:22 +0200481static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200482pycore_init_runtime(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200483 const PyConfig *config)
Nick Coghland6009512014-11-20 21:39:37 +1000484{
Victor Stinner43125222019-04-24 18:23:53 +0200485 if (runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200486 return _PyStatus_ERR("main interpreter already initialized");
Victor Stinner1dc6e392018-07-25 02:49:17 +0200487 }
Victor Stinnerda273412017-12-15 01:46:02 +0100488
Victor Stinnere81f6e62020-06-08 18:12:59 +0200489 PyStatus status = _PyConfig_Write(config, runtime);
490 if (_PyStatus_EXCEPTION(status)) {
491 return status;
492 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600493
Eric Snow1abcf672017-05-23 21:46:51 -0700494 /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
495 * threads behave a little more gracefully at interpreter shutdown.
496 * We clobber it here so the new interpreter can start with a clean
497 * slate.
498 *
499 * However, this may still lead to misbehaviour if there are daemon
500 * threads still hanging around from a previous Py_Initialize/Finalize
501 * pair :(
502 */
Victor Stinner7b3c2522020-03-07 00:24:23 +0100503 _PyRuntimeState_SetFinalizing(runtime, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600504
Victor Stinnere81f6e62020-06-08 18:12:59 +0200505 status = _Py_HashRandomization_Init(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200506 if (_PyStatus_EXCEPTION(status)) {
507 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800508 }
509
Victor Stinner331a6a52019-05-27 16:39:22 +0200510 status = _PyInterpreterState_Enable(runtime);
511 if (_PyStatus_EXCEPTION(status)) {
512 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800513 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200514 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100515}
Victor Stinnera7368ac2017-11-15 18:11:45 -0800516
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100517
Victor Stinner331a6a52019-05-27 16:39:22 +0200518static PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200519init_interp_create_gil(PyThreadState *tstate)
520{
521 PyStatus status;
522
523 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
524 only called here. */
525 _PyEval_FiniGIL(tstate);
526
527 /* Auto-thread-state API */
528 status = _PyGILState_Init(tstate);
529 if (_PyStatus_EXCEPTION(status)) {
530 return status;
531 }
532
533 /* Create the GIL and take it */
534 status = _PyEval_InitGIL(tstate);
535 if (_PyStatus_EXCEPTION(status)) {
536 return status;
537 }
538
539 return _PyStatus_OK();
540}
541
542
543static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200544pycore_create_interpreter(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200545 const PyConfig *config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200546 PyThreadState **tstate_p)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100547{
548 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnerda273412017-12-15 01:46:02 +0100549 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200550 return _PyStatus_ERR("can't make main interpreter");
Victor Stinnerda273412017-12-15 01:46:02 +0100551 }
552
Victor Stinnerda7933e2020-04-13 03:04:28 +0200553 PyStatus status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200554 if (_PyStatus_EXCEPTION(status)) {
555 return status;
Victor Stinnerda273412017-12-15 01:46:02 +0100556 }
Nick Coghland6009512014-11-20 21:39:37 +1000557
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200558 PyThreadState *tstate = PyThreadState_New(interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +0200559 if (tstate == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200560 return _PyStatus_ERR("can't make first thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +0200561 }
Nick Coghland6009512014-11-20 21:39:37 +1000562 (void) PyThreadState_Swap(tstate);
563
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200564 status = init_interp_create_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100565 if (_PyStatus_EXCEPTION(status)) {
566 return status;
567 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100568
Victor Stinnerb45d2592019-06-20 00:05:23 +0200569 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +0200570 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100571}
Nick Coghland6009512014-11-20 21:39:37 +1000572
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100573
Victor Stinner331a6a52019-05-27 16:39:22 +0200574static PyStatus
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100575pycore_init_types(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100576{
Victor Stinner444b39b2019-11-20 01:18:11 +0100577 PyStatus status;
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100578 int is_main_interp = _Py_IsMainInterpreter(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100579
Victor Stinner01b1cc12019-11-20 02:27:56 +0100580 status = _PyGC_Init(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100581 if (_PyStatus_EXCEPTION(status)) {
582 return status;
583 }
584
Victor Stinner0430dfa2020-06-24 15:21:54 +0200585 // Create the empty tuple singleton. It must be created before the first
586 // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
587 // for example.
588 status = _PyTuple_Init(tstate);
589 if (_PyStatus_EXCEPTION(status)) {
590 return status;
591 }
592
Victor Stinnere7e699e2019-11-20 12:08:13 +0100593 if (is_main_interp) {
594 status = _PyTypes_Init();
595 if (_PyStatus_EXCEPTION(status)) {
596 return status;
597 }
Victor Stinner630c8df2019-12-17 13:02:18 +0100598 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100599
Victor Stinner630c8df2019-12-17 13:02:18 +0100600 if (!_PyLong_Init(tstate)) {
601 return _PyStatus_ERR("can't init longs");
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100602 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100603
Victor Stinnerf363d0a2020-06-24 00:10:40 +0200604 status = _PyUnicode_Init(tstate);
605 if (_PyStatus_EXCEPTION(status)) {
606 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100607 }
608
Victor Stinner91698d82020-06-25 14:07:40 +0200609 status = _PyBytes_Init(tstate);
610 if (_PyStatus_EXCEPTION(status)) {
611 return status;
612 }
613
Victor Stinner281cce12020-06-23 22:55:46 +0200614 status = _PyExc_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200615 if (_PyStatus_EXCEPTION(status)) {
616 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100617 }
618
Victor Stinnere7e699e2019-11-20 12:08:13 +0100619 if (is_main_interp) {
620 if (!_PyFloat_Init()) {
621 return _PyStatus_ERR("can't init float");
622 }
Nick Coghland6009512014-11-20 21:39:37 +1000623
Victor Stinnere7e699e2019-11-20 12:08:13 +0100624 if (_PyStructSequence_Init() < 0) {
625 return _PyStatus_ERR("can't initialize structseq");
626 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100627 }
Victor Stinneref9d9b62019-05-22 11:28:22 +0200628
Victor Stinner331a6a52019-05-27 16:39:22 +0200629 status = _PyErr_Init();
630 if (_PyStatus_EXCEPTION(status)) {
631 return status;
Victor Stinneref9d9b62019-05-22 11:28:22 +0200632 }
633
Victor Stinnere7e699e2019-11-20 12:08:13 +0100634 if (is_main_interp) {
635 if (!_PyContext_Init()) {
636 return _PyStatus_ERR("can't init context");
637 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100638 }
639
Victor Stinner331a6a52019-05-27 16:39:22 +0200640 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100641}
642
643
Victor Stinner331a6a52019-05-27 16:39:22 +0200644static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200645pycore_init_builtins(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100646{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100647 assert(!_PyErr_Occurred(tstate));
648
Victor Stinnerb45d2592019-06-20 00:05:23 +0200649 PyObject *bimod = _PyBuiltin_Init(tstate);
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100650 if (bimod == NULL) {
Victor Stinner2582d462019-11-22 19:24:49 +0100651 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100652 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100653
Victor Stinner2582d462019-11-22 19:24:49 +0100654 PyInterpreterState *interp = tstate->interp;
655 if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
656 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100657 }
Victor Stinner2582d462019-11-22 19:24:49 +0100658
659 PyObject *builtins_dict = PyModule_GetDict(bimod);
660 if (builtins_dict == NULL) {
661 goto error;
662 }
663 Py_INCREF(builtins_dict);
664 interp->builtins = builtins_dict;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100665
Victor Stinner331a6a52019-05-27 16:39:22 +0200666 PyStatus status = _PyBuiltins_AddExceptions(bimod);
667 if (_PyStatus_EXCEPTION(status)) {
668 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100669 }
Victor Stinner2582d462019-11-22 19:24:49 +0100670
671 interp->builtins_copy = PyDict_Copy(interp->builtins);
672 if (interp->builtins_copy == NULL) {
673 goto error;
674 }
Pablo Galindob96c6b02019-12-04 11:19:59 +0000675 Py_DECREF(bimod);
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100676
677 assert(!_PyErr_Occurred(tstate));
678
Victor Stinner331a6a52019-05-27 16:39:22 +0200679 return _PyStatus_OK();
Victor Stinner2582d462019-11-22 19:24:49 +0100680
681error:
Pablo Galindob96c6b02019-12-04 11:19:59 +0000682 Py_XDECREF(bimod);
Victor Stinner2582d462019-11-22 19:24:49 +0100683 return _PyStatus_ERR("can't initialize builtins module");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100684}
685
686
Victor Stinner331a6a52019-05-27 16:39:22 +0200687static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200688pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100689{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100690 assert(!_PyErr_Occurred(tstate));
Victor Stinnerb45d2592019-06-20 00:05:23 +0200691
Victor Stinner2582d462019-11-22 19:24:49 +0100692 PyStatus status = _PyImportHooks_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200693 if (_PyStatus_EXCEPTION(status)) {
694 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800695 }
Nick Coghland6009512014-11-20 21:39:37 +1000696
Victor Stinner30a89332020-06-23 15:55:45 +0200697 /* Initialize _warnings. */
698 status = _PyWarnings_InitState(tstate);
699 if (_PyStatus_EXCEPTION(status)) {
700 return status;
701 }
Nick Coghland6009512014-11-20 21:39:37 +1000702
Victor Stinner30a89332020-06-23 15:55:45 +0200703 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
704 if (config->_install_importlib) {
705 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner2ec1a1b2019-11-22 21:54:33 +0100706 status = _PyConfig_WritePathConfig(config);
707 if (_PyStatus_EXCEPTION(status)) {
708 return status;
709 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200710 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200711
Victor Stinner30a89332020-06-23 15:55:45 +0200712 /* This call sets up builtin and frozen import support */
Victor Stinnerb45d2592019-06-20 00:05:23 +0200713 status = init_importlib(tstate, sysmod);
Victor Stinner331a6a52019-05-27 16:39:22 +0200714 if (_PyStatus_EXCEPTION(status)) {
715 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800716 }
Eric Snow1abcf672017-05-23 21:46:51 -0700717 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100718
719 assert(!_PyErr_Occurred(tstate));
720
Victor Stinner331a6a52019-05-27 16:39:22 +0200721 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100722}
723
724
Victor Stinner331a6a52019-05-27 16:39:22 +0200725static PyStatus
Victor Stinnerd863ade2019-12-06 03:37:07 +0100726pycore_interp_init(PyThreadState *tstate)
727{
728 PyStatus status;
Victor Stinner080ee5a2019-12-08 21:55:58 +0100729 PyObject *sysmod = NULL;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100730
731 status = pycore_init_types(tstate);
732 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100733 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100734 }
735
Victor Stinnerd863ade2019-12-06 03:37:07 +0100736 status = _PySys_Create(tstate, &sysmod);
737 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100738 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100739 }
740
741 status = pycore_init_builtins(tstate);
742 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100743 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100744 }
745
Victor Stinner080ee5a2019-12-08 21:55:58 +0100746 status = pycore_init_import_warnings(tstate, sysmod);
747
748done:
749 /* sys.modules['sys'] contains a strong reference to the module */
750 Py_XDECREF(sysmod);
751 return status;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100752}
753
754
755static PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +0200756pyinit_config(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200757 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200758 const PyConfig *config)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100759{
Victor Stinner331a6a52019-05-27 16:39:22 +0200760 PyStatus status = pycore_init_runtime(runtime, config);
761 if (_PyStatus_EXCEPTION(status)) {
762 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100763 }
764
Victor Stinnerb45d2592019-06-20 00:05:23 +0200765 PyThreadState *tstate;
766 status = pycore_create_interpreter(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200767 if (_PyStatus_EXCEPTION(status)) {
768 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100769 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200770 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100771
Victor Stinnerd863ade2019-12-06 03:37:07 +0100772 status = pycore_interp_init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200773 if (_PyStatus_EXCEPTION(status)) {
774 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100775 }
Eric Snow1abcf672017-05-23 21:46:51 -0700776
777 /* Only when we get here is the runtime core fully initialized */
Victor Stinner43125222019-04-24 18:23:53 +0200778 runtime->core_initialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200779 return _PyStatus_OK();
Eric Snow1abcf672017-05-23 21:46:51 -0700780}
781
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100782
Victor Stinner331a6a52019-05-27 16:39:22 +0200783PyStatus
784_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100785{
Victor Stinner331a6a52019-05-27 16:39:22 +0200786 PyStatus status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100787
Victor Stinner6d1c4672019-05-20 11:02:00 +0200788 if (src_config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200789 return _PyStatus_ERR("preinitialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +0200790 }
791
Victor Stinner331a6a52019-05-27 16:39:22 +0200792 status = _PyRuntime_Initialize();
793 if (_PyStatus_EXCEPTION(status)) {
794 return status;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100795 }
Victor Stinner43125222019-04-24 18:23:53 +0200796 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100797
Victor Stinnerd3b90412019-09-17 23:59:51 +0200798 if (runtime->preinitialized) {
Victor Stinnerf72346c2019-03-25 17:54:58 +0100799 /* If it's already configured: ignored the new configuration */
Victor Stinner331a6a52019-05-27 16:39:22 +0200800 return _PyStatus_OK();
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100801 }
802
Victor Stinnerd3b90412019-09-17 23:59:51 +0200803 /* Note: preinitialized remains 1 on error, it is only set to 0
804 at exit on success. */
805 runtime->preinitializing = 1;
806
Victor Stinner331a6a52019-05-27 16:39:22 +0200807 PyPreConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200808
809 status = _PyPreConfig_InitFromPreConfig(&config, src_config);
810 if (_PyStatus_EXCEPTION(status)) {
811 return status;
812 }
Victor Stinnerf72346c2019-03-25 17:54:58 +0100813
Victor Stinner331a6a52019-05-27 16:39:22 +0200814 status = _PyPreConfig_Read(&config, args);
815 if (_PyStatus_EXCEPTION(status)) {
816 return status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100817 }
818
Victor Stinner331a6a52019-05-27 16:39:22 +0200819 status = _PyPreConfig_Write(&config);
820 if (_PyStatus_EXCEPTION(status)) {
821 return status;
Victor Stinnerf72346c2019-03-25 17:54:58 +0100822 }
823
Victor Stinnerd3b90412019-09-17 23:59:51 +0200824 runtime->preinitializing = 0;
825 runtime->preinitialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200826 return _PyStatus_OK();
Victor Stinnerf72346c2019-03-25 17:54:58 +0100827}
828
Victor Stinner70005ac2019-05-02 15:25:34 -0400829
Victor Stinner331a6a52019-05-27 16:39:22 +0200830PyStatus
831Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
Victor Stinnerf72346c2019-03-25 17:54:58 +0100832{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100833 _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400834 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinnerf29084d2019-03-20 02:20:13 +0100835}
836
837
Victor Stinner331a6a52019-05-27 16:39:22 +0200838PyStatus
839Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
Victor Stinner20004952019-03-26 02:31:11 +0100840{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100841 _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400842 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinner20004952019-03-26 02:31:11 +0100843}
844
845
Victor Stinner331a6a52019-05-27 16:39:22 +0200846PyStatus
847Py_PreInitialize(const PyPreConfig *src_config)
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100848{
Victor Stinner70005ac2019-05-02 15:25:34 -0400849 return _Py_PreInitializeFromPyArgv(src_config, NULL);
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100850}
851
852
Victor Stinner331a6a52019-05-27 16:39:22 +0200853PyStatus
854_Py_PreInitializeFromConfig(const PyConfig *config,
855 const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100856{
Victor Stinner331a6a52019-05-27 16:39:22 +0200857 assert(config != NULL);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200858
Victor Stinner331a6a52019-05-27 16:39:22 +0200859 PyStatus status = _PyRuntime_Initialize();
860 if (_PyStatus_EXCEPTION(status)) {
861 return status;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200862 }
863 _PyRuntimeState *runtime = &_PyRuntime;
864
Victor Stinnerd3b90412019-09-17 23:59:51 +0200865 if (runtime->preinitialized) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200866 /* Already initialized: do nothing */
Victor Stinner331a6a52019-05-27 16:39:22 +0200867 return _PyStatus_OK();
Victor Stinner70005ac2019-05-02 15:25:34 -0400868 }
Victor Stinnercab5d072019-05-17 19:01:14 +0200869
Victor Stinner331a6a52019-05-27 16:39:22 +0200870 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200871
Victor Stinner3c30a762019-10-01 10:56:37 +0200872 _PyPreConfig_InitFromConfig(&preconfig, config);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200873
Victor Stinner331a6a52019-05-27 16:39:22 +0200874 if (!config->parse_argv) {
875 return Py_PreInitialize(&preconfig);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200876 }
877 else if (args == NULL) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200878 _PyArgv config_args = {
879 .use_bytes_argv = 0,
Victor Stinner331a6a52019-05-27 16:39:22 +0200880 .argc = config->argv.length,
881 .wchar_argv = config->argv.items};
Victor Stinner6d1c4672019-05-20 11:02:00 +0200882 return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200883 }
884 else {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200885 return _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200886 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100887}
888
889
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100890/* Begin interpreter initialization
891 *
892 * On return, the first thread and interpreter state have been created,
893 * but the compiler, signal handling, multithreading and
894 * multiple interpreter support, and codec infrastructure are not yet
895 * available.
896 *
897 * The import system will support builtin and frozen modules only.
898 * The only supported io is writing to sys.stderr
899 *
900 * If any operation invoked by this function fails, a fatal error is
901 * issued and the function does not return.
902 *
903 * Any code invoked from this function should *not* assume it has access
904 * to the Python C API (unless the API is explicitly listed as being
905 * safe to call without calling Py_Initialize first)
906 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200907static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200908pyinit_core(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 const PyConfig *src_config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200910 PyThreadState **tstate_p)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200911{
Victor Stinner331a6a52019-05-27 16:39:22 +0200912 PyStatus status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200913
Victor Stinner331a6a52019-05-27 16:39:22 +0200914 status = _Py_PreInitializeFromConfig(src_config, NULL);
915 if (_PyStatus_EXCEPTION(status)) {
916 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200917 }
918
Victor Stinner331a6a52019-05-27 16:39:22 +0200919 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +0200920 _PyConfig_InitCompatConfig(&config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200921
Victor Stinner331a6a52019-05-27 16:39:22 +0200922 status = _PyConfig_Copy(&config, src_config);
923 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200924 goto done;
925 }
926
Victor Stinner331a6a52019-05-27 16:39:22 +0200927 status = PyConfig_Read(&config);
928 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200929 goto done;
930 }
931
932 if (!runtime->core_initialized) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200933 status = pyinit_config(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200934 }
935 else {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200936 status = pyinit_core_reconfigure(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200937 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200938 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200939 goto done;
940 }
941
942done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200943 PyConfig_Clear(&config);
944 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200945}
946
Victor Stinner5ac27a52019-03-27 13:40:14 +0100947
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200948/* Py_Initialize() has already been called: update the main interpreter
949 configuration. Example of bpo-34008: Py_Main() called after
950 Py_Initialize(). */
Victor Stinner331a6a52019-05-27 16:39:22 +0200951static PyStatus
Victor Stinneraf1d64d2020-11-04 17:34:34 +0100952pyinit_main_reconfigure(PyThreadState *tstate)
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200953{
Victor Stinneraf1d64d2020-11-04 17:34:34 +0100954 if (_PySys_UpdateConfig(tstate) < 0) {
955 return _PyStatus_ERR("fail to update sys for the new conf");
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200956 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200957 return _PyStatus_OK();
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200958}
959
Victor Stinnerb0051362019-11-22 17:52:42 +0100960
961static PyStatus
962init_interp_main(PyThreadState *tstate)
963{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100964 assert(!_PyErr_Occurred(tstate));
965
Victor Stinnerb0051362019-11-22 17:52:42 +0100966 PyStatus status;
967 int is_main_interp = _Py_IsMainInterpreter(tstate);
968 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200969 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
Victor Stinnerb0051362019-11-22 17:52:42 +0100970
971 if (!config->_install_importlib) {
972 /* Special mode for freeze_importlib: run with no import system
973 *
974 * This means anything which needs support from extension modules
975 * or pure Python code in the standard library won't work.
976 */
977 if (is_main_interp) {
978 interp->runtime->initialized = 1;
979 }
980 return _PyStatus_OK();
981 }
982
983 if (is_main_interp) {
984 if (_PyTime_Init() < 0) {
985 return _PyStatus_ERR("can't initialize time");
986 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100987 }
Victor Stinnerb0051362019-11-22 17:52:42 +0100988
Victor Stinneraf1d64d2020-11-04 17:34:34 +0100989 if (_PySys_UpdateConfig(tstate) < 0) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100990 return _PyStatus_ERR("can't finish initializing sys");
Victor Stinnerb0051362019-11-22 17:52:42 +0100991 }
992
993 status = init_importlib_external(tstate);
994 if (_PyStatus_EXCEPTION(status)) {
995 return status;
996 }
997
998 if (is_main_interp) {
999 /* initialize the faulthandler module */
1000 status = _PyFaulthandler_Init(config->faulthandler);
1001 if (_PyStatus_EXCEPTION(status)) {
1002 return status;
1003 }
1004 }
1005
1006 status = _PyUnicode_InitEncodings(tstate);
1007 if (_PyStatus_EXCEPTION(status)) {
1008 return status;
1009 }
1010
1011 if (is_main_interp) {
1012 if (config->install_signal_handlers) {
1013 status = init_signals(tstate);
1014 if (_PyStatus_EXCEPTION(status)) {
1015 return status;
1016 }
1017 }
1018
1019 if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1020 return _PyStatus_ERR("can't initialize tracemalloc");
1021 }
1022 }
1023
1024 status = init_sys_streams(tstate);
1025 if (_PyStatus_EXCEPTION(status)) {
1026 return status;
1027 }
1028
Andy Lester75cd5bf2020-03-12 02:49:05 -05001029 status = init_set_builtins_open();
Victor Stinnerb0051362019-11-22 17:52:42 +01001030 if (_PyStatus_EXCEPTION(status)) {
1031 return status;
1032 }
1033
1034 status = add_main_module(interp);
1035 if (_PyStatus_EXCEPTION(status)) {
1036 return status;
1037 }
1038
1039 if (is_main_interp) {
1040 /* Initialize warnings. */
1041 PyObject *warnoptions = PySys_GetObject("warnoptions");
1042 if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1043 {
1044 PyObject *warnings_module = PyImport_ImportModule("warnings");
1045 if (warnings_module == NULL) {
1046 fprintf(stderr, "'import warnings' failed; traceback:\n");
1047 _PyErr_Print(tstate);
1048 }
1049 Py_XDECREF(warnings_module);
1050 }
1051
1052 interp->runtime->initialized = 1;
1053 }
1054
1055 if (config->site_import) {
1056 status = init_import_site();
1057 if (_PyStatus_EXCEPTION(status)) {
1058 return status;
1059 }
1060 }
1061
1062 if (is_main_interp) {
1063#ifndef MS_WINDOWS
1064 emit_stderr_warning_for_legacy_locale(interp->runtime);
1065#endif
1066 }
1067
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001068 assert(!_PyErr_Occurred(tstate));
1069
Victor Stinnerb0051362019-11-22 17:52:42 +01001070 return _PyStatus_OK();
1071}
1072
1073
Eric Snowc7ec9982017-05-23 23:00:52 -07001074/* Update interpreter state based on supplied configuration settings
1075 *
1076 * After calling this function, most of the restrictions on the interpreter
1077 * are lifted. The only remaining incomplete settings are those related
1078 * to the main module (sys.argv[0], __main__ metadata)
1079 *
1080 * Calling this when the interpreter is not initializing, is already
1081 * initialized or without a valid current thread state is a fatal error.
1082 * Other errors should be reported as normal Python exceptions with a
1083 * non-zero return code.
1084 */
Victor Stinner331a6a52019-05-27 16:39:22 +02001085static PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +01001086pyinit_main(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -07001087{
Victor Stinnerb0051362019-11-22 17:52:42 +01001088 PyInterpreterState *interp = tstate->interp;
1089 if (!interp->runtime->core_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001090 return _PyStatus_ERR("runtime core not initialized");
Eric Snowc7ec9982017-05-23 23:00:52 -07001091 }
Eric Snowc7ec9982017-05-23 23:00:52 -07001092
Victor Stinnerb0051362019-11-22 17:52:42 +01001093 if (interp->runtime->initialized) {
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001094 return pyinit_main_reconfigure(tstate);
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001095 }
1096
Victor Stinnerb0051362019-11-22 17:52:42 +01001097 PyStatus status = init_interp_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001098 if (_PyStatus_EXCEPTION(status)) {
1099 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001100 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001101 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001102}
1103
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001104
Victor Stinner331a6a52019-05-27 16:39:22 +02001105PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +02001106Py_InitializeFromConfig(const PyConfig *config)
Eric Snow1abcf672017-05-23 21:46:51 -07001107{
Victor Stinner6d1c4672019-05-20 11:02:00 +02001108 if (config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001109 return _PyStatus_ERR("initialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +02001110 }
1111
Victor Stinner331a6a52019-05-27 16:39:22 +02001112 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001113
Victor Stinner331a6a52019-05-27 16:39:22 +02001114 status = _PyRuntime_Initialize();
1115 if (_PyStatus_EXCEPTION(status)) {
1116 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001117 }
1118 _PyRuntimeState *runtime = &_PyRuntime;
1119
Victor Stinnerb45d2592019-06-20 00:05:23 +02001120 PyThreadState *tstate = NULL;
1121 status = pyinit_core(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001122 if (_PyStatus_EXCEPTION(status)) {
1123 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001124 }
Victor Stinnerda7933e2020-04-13 03:04:28 +02001125 config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerbc8ac6b2017-11-30 18:03:55 +01001126
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001127 if (config->_init_main) {
Victor Stinner01b1cc12019-11-20 02:27:56 +01001128 status = pyinit_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001129 if (_PyStatus_EXCEPTION(status)) {
1130 return status;
Victor Stinner484f20d2019-03-27 02:04:16 +01001131 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001132 }
Victor Stinner484f20d2019-03-27 02:04:16 +01001133
Victor Stinner331a6a52019-05-27 16:39:22 +02001134 return _PyStatus_OK();
Victor Stinner5ac27a52019-03-27 13:40:14 +01001135}
1136
1137
Eric Snow1abcf672017-05-23 21:46:51 -07001138void
Nick Coghland6009512014-11-20 21:39:37 +10001139Py_InitializeEx(int install_sigs)
1140{
Victor Stinner331a6a52019-05-27 16:39:22 +02001141 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001142
Victor Stinner331a6a52019-05-27 16:39:22 +02001143 status = _PyRuntime_Initialize();
1144 if (_PyStatus_EXCEPTION(status)) {
1145 Py_ExitStatusException(status);
Victor Stinner43125222019-04-24 18:23:53 +02001146 }
1147 _PyRuntimeState *runtime = &_PyRuntime;
1148
1149 if (runtime->initialized) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001150 /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1151 return;
1152 }
1153
Victor Stinner331a6a52019-05-27 16:39:22 +02001154 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001155 _PyConfig_InitCompatConfig(&config);
Victor Stinner441b10c2019-09-28 04:28:35 +02001156
Victor Stinner1dc6e392018-07-25 02:49:17 +02001157 config.install_signal_handlers = install_sigs;
1158
Victor Stinner331a6a52019-05-27 16:39:22 +02001159 status = Py_InitializeFromConfig(&config);
1160 if (_PyStatus_EXCEPTION(status)) {
1161 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001162 }
Nick Coghland6009512014-11-20 21:39:37 +10001163}
1164
1165void
1166Py_Initialize(void)
1167{
1168 Py_InitializeEx(1);
1169}
1170
1171
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001172PyStatus
1173_Py_InitializeMain(void)
1174{
1175 PyStatus status = _PyRuntime_Initialize();
1176 if (_PyStatus_EXCEPTION(status)) {
1177 return status;
1178 }
1179 _PyRuntimeState *runtime = &_PyRuntime;
1180 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1181 return pyinit_main(tstate);
1182}
1183
1184
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001185static void
1186finalize_modules_delete_special(PyThreadState *tstate, int verbose)
1187{
1188 // List of names to clear in sys
1189 static const char * const sys_deletes[] = {
1190 "path", "argv", "ps1", "ps2",
1191 "last_type", "last_value", "last_traceback",
1192 "path_hooks", "path_importer_cache", "meta_path",
1193 "__interactivehook__",
1194 NULL
1195 };
1196
1197 static const char * const sys_files[] = {
1198 "stdin", "__stdin__",
1199 "stdout", "__stdout__",
1200 "stderr", "__stderr__",
1201 NULL
1202 };
1203
1204 PyInterpreterState *interp = tstate->interp;
1205 if (verbose) {
1206 PySys_WriteStderr("# clear builtins._\n");
1207 }
1208 if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
1209 PyErr_WriteUnraisable(NULL);
1210 }
1211
1212 const char * const *p;
1213 for (p = sys_deletes; *p != NULL; p++) {
1214 if (verbose) {
1215 PySys_WriteStderr("# clear sys.%s\n", *p);
1216 }
1217 if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
1218 PyErr_WriteUnraisable(NULL);
1219 }
1220 }
1221 for (p = sys_files; *p != NULL; p+=2) {
1222 const char *name = p[0];
1223 const char *orig_name = p[1];
1224 if (verbose) {
1225 PySys_WriteStderr("# restore sys.%s\n", name);
1226 }
1227 PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
1228 orig_name);
1229 if (value == NULL) {
1230 if (_PyErr_Occurred(tstate)) {
1231 PyErr_WriteUnraisable(NULL);
1232 }
1233 value = Py_None;
1234 }
1235 if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
1236 PyErr_WriteUnraisable(NULL);
1237 }
1238 }
1239}
1240
1241
1242static PyObject*
1243finalize_remove_modules(PyObject *modules, int verbose)
1244{
1245 PyObject *weaklist = PyList_New(0);
1246 if (weaklist == NULL) {
1247 PyErr_WriteUnraisable(NULL);
1248 }
1249
1250#define STORE_MODULE_WEAKREF(name, mod) \
1251 if (weaklist != NULL) { \
1252 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1253 if (wr) { \
1254 PyObject *tup = PyTuple_Pack(2, name, wr); \
1255 if (!tup || PyList_Append(weaklist, tup) < 0) { \
1256 PyErr_WriteUnraisable(NULL); \
1257 } \
1258 Py_XDECREF(tup); \
1259 Py_DECREF(wr); \
1260 } \
1261 else { \
1262 PyErr_WriteUnraisable(NULL); \
1263 } \
1264 }
1265
1266#define CLEAR_MODULE(name, mod) \
1267 if (PyModule_Check(mod)) { \
1268 if (verbose && PyUnicode_Check(name)) { \
1269 PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
1270 } \
1271 STORE_MODULE_WEAKREF(name, mod); \
1272 if (PyObject_SetItem(modules, name, Py_None) < 0) { \
1273 PyErr_WriteUnraisable(NULL); \
1274 } \
1275 }
1276
1277 if (PyDict_CheckExact(modules)) {
1278 Py_ssize_t pos = 0;
1279 PyObject *key, *value;
1280 while (PyDict_Next(modules, &pos, &key, &value)) {
1281 CLEAR_MODULE(key, value);
1282 }
1283 }
1284 else {
1285 PyObject *iterator = PyObject_GetIter(modules);
1286 if (iterator == NULL) {
1287 PyErr_WriteUnraisable(NULL);
1288 }
1289 else {
1290 PyObject *key;
1291 while ((key = PyIter_Next(iterator))) {
1292 PyObject *value = PyObject_GetItem(modules, key);
1293 if (value == NULL) {
1294 PyErr_WriteUnraisable(NULL);
1295 continue;
1296 }
1297 CLEAR_MODULE(key, value);
1298 Py_DECREF(value);
1299 Py_DECREF(key);
1300 }
1301 if (PyErr_Occurred()) {
1302 PyErr_WriteUnraisable(NULL);
1303 }
1304 Py_DECREF(iterator);
1305 }
1306 }
1307#undef CLEAR_MODULE
1308#undef STORE_MODULE_WEAKREF
1309
1310 return weaklist;
1311}
1312
1313
1314static void
1315finalize_clear_modules_dict(PyObject *modules)
1316{
1317 if (PyDict_CheckExact(modules)) {
1318 PyDict_Clear(modules);
1319 }
1320 else {
1321 _Py_IDENTIFIER(clear);
1322 if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) {
1323 PyErr_WriteUnraisable(NULL);
1324 }
1325 }
1326}
1327
1328
1329static void
1330finalize_restore_builtins(PyThreadState *tstate)
1331{
1332 PyInterpreterState *interp = tstate->interp;
1333 PyObject *dict = PyDict_Copy(interp->builtins);
1334 if (dict == NULL) {
1335 PyErr_WriteUnraisable(NULL);
1336 }
1337 PyDict_Clear(interp->builtins);
1338 if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
1339 _PyErr_Clear(tstate);
1340 }
1341 Py_XDECREF(dict);
1342}
1343
1344
1345static void
1346finalize_modules_clear_weaklist(PyInterpreterState *interp,
1347 PyObject *weaklist, int verbose)
1348{
1349 // First clear modules imported later
1350 for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
1351 PyObject *tup = PyList_GET_ITEM(weaklist, i);
1352 PyObject *name = PyTuple_GET_ITEM(tup, 0);
1353 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
1354 if (mod == Py_None) {
1355 continue;
1356 }
1357 assert(PyModule_Check(mod));
1358 PyObject *dict = PyModule_GetDict(mod);
1359 if (dict == interp->builtins || dict == interp->sysdict) {
1360 continue;
1361 }
1362 Py_INCREF(mod);
1363 if (verbose && PyUnicode_Check(name)) {
1364 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
1365 }
1366 _PyModule_Clear(mod);
1367 Py_DECREF(mod);
1368 }
1369}
1370
1371
1372static void
1373finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
1374{
1375 // Clear sys dict
1376 if (verbose) {
1377 PySys_FormatStderr("# cleanup[3] wiping sys\n");
1378 }
1379 _PyModule_ClearDict(interp->sysdict);
1380
1381 // Clear builtins dict
1382 if (verbose) {
1383 PySys_FormatStderr("# cleanup[3] wiping builtins\n");
1384 }
1385 _PyModule_ClearDict(interp->builtins);
1386}
1387
1388
1389/* Clear modules, as good as we can */
1390static void
1391finalize_modules(PyThreadState *tstate)
1392{
1393 PyInterpreterState *interp = tstate->interp;
1394 PyObject *modules = interp->modules;
1395 if (modules == NULL) {
1396 // Already done
1397 return;
1398 }
1399 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
1400
1401 // Delete some special builtins._ and sys attributes first. These are
1402 // common places where user values hide and people complain when their
1403 // destructors fail. Since the modules containing them are
1404 // deleted *last* of all, they would come too late in the normal
1405 // destruction order. Sigh.
1406 //
1407 // XXX Perhaps these precautions are obsolete. Who knows?
1408 finalize_modules_delete_special(tstate, verbose);
1409
1410 // Remove all modules from sys.modules, hoping that garbage collection
1411 // can reclaim most of them: set all sys.modules values to None.
1412 //
1413 // We prepare a list which will receive (name, weakref) tuples of
1414 // modules when they are removed from sys.modules. The name is used
1415 // for diagnosis messages (in verbose mode), while the weakref helps
1416 // detect those modules which have been held alive.
1417 PyObject *weaklist = finalize_remove_modules(modules, verbose);
1418
1419 // Clear the modules dict
1420 finalize_clear_modules_dict(modules);
1421
1422 // Restore the original builtins dict, to ensure that any
1423 // user data gets cleared.
1424 finalize_restore_builtins(tstate);
1425
1426 // Collect garbage
1427 _PyGC_CollectNoFail(tstate);
1428
1429 // Dump GC stats before it's too late, since it uses the warnings
1430 // machinery.
1431 _PyGC_DumpShutdownStats(tstate);
1432
1433 if (weaklist != NULL) {
1434 // Now, if there are any modules left alive, clear their globals to
1435 // minimize potential leaks. All C extension modules actually end
1436 // up here, since they are kept alive in the interpreter state.
1437 //
1438 // The special treatment of "builtins" here is because even
1439 // when it's not referenced as a module, its dictionary is
1440 // referenced by almost every module's __builtins__. Since
1441 // deleting a module clears its dictionary (even if there are
1442 // references left to it), we need to delete the "builtins"
1443 // module last. Likewise, we don't delete sys until the very
1444 // end because it is implicitly referenced (e.g. by print).
1445 //
1446 // Since dict is ordered in CPython 3.6+, modules are saved in
1447 // importing order. First clear modules imported later.
1448 finalize_modules_clear_weaklist(interp, weaklist, verbose);
1449 Py_DECREF(weaklist);
1450 }
1451
1452 // Clear sys and builtins modules dict
1453 finalize_clear_sys_builtins_dict(interp, verbose);
1454
1455 // Clear module dict copies stored in the interpreter state:
1456 // clear PyInterpreterState.modules_by_index and
1457 // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
1458 // initialization API)
1459 _PyInterpreterState_ClearModules(interp);
1460
1461 // Clear and delete the modules directory. Actual modules will
1462 // still be there only if imported during the execution of some
1463 // destructor.
1464 Py_SETREF(interp->modules, NULL);
1465
1466 // Collect garbage once more
1467 _PyGC_CollectNoFail(tstate);
1468}
1469
1470
Nick Coghland6009512014-11-20 21:39:37 +10001471/* Flush stdout and stderr */
1472
1473static int
1474file_is_closed(PyObject *fobj)
1475{
1476 int r;
1477 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1478 if (tmp == NULL) {
1479 PyErr_Clear();
1480 return 0;
1481 }
1482 r = PyObject_IsTrue(tmp);
1483 Py_DECREF(tmp);
1484 if (r < 0)
1485 PyErr_Clear();
1486 return r > 0;
1487}
1488
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001489
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001490static int
Nick Coghland6009512014-11-20 21:39:37 +10001491flush_std_files(void)
1492{
1493 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1494 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1495 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001496 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001497
1498 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001499 tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001500 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001501 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001502 status = -1;
1503 }
Nick Coghland6009512014-11-20 21:39:37 +10001504 else
1505 Py_DECREF(tmp);
1506 }
1507
1508 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001509 tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001510 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001511 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001512 status = -1;
1513 }
Nick Coghland6009512014-11-20 21:39:37 +10001514 else
1515 Py_DECREF(tmp);
1516 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001517
1518 return status;
Nick Coghland6009512014-11-20 21:39:37 +10001519}
1520
1521/* Undo the effect of Py_Initialize().
1522
1523 Beware: if multiple interpreter and/or thread states exist, these
1524 are not wiped out; only the current thread and interpreter state
1525 are deleted. But since everything else is deleted, those other
1526 interpreter and thread states should no longer be used.
1527
1528 (XXX We should do better, e.g. wipe out all interpreters and
1529 threads.)
1530
1531 Locking: as above.
1532
1533*/
1534
Victor Stinner7eee5be2019-11-20 10:38:34 +01001535
1536static void
Victor Stinner90db4652020-07-01 23:21:36 +02001537finalize_interp_types(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001538{
Victor Stinner281cce12020-06-23 22:55:46 +02001539 _PyExc_Fini(tstate);
Victor Stinner3744ed22020-06-05 01:39:24 +02001540 _PyFrame_Fini(tstate);
Victor Stinner78a02c22020-06-05 02:34:14 +02001541 _PyAsyncGen_Fini(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001542 _PyContext_Fini(tstate);
Victor Stinner666ecfb2020-07-02 01:19:57 +02001543 _PyUnicode_ClearInterned(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001544
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001545 _PyDict_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001546 _PyList_Fini(tstate);
1547 _PyTuple_Fini(tstate);
1548
1549 _PySlice_Fini(tstate);
Victor Stinner3d483342019-11-22 12:27:50 +01001550
Victor Stinnerc41eed12020-06-23 15:54:35 +02001551 _PyBytes_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001552 _PyUnicode_Fini(tstate);
1553 _PyFloat_Fini(tstate);
1554 _PyLong_Fini(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001555}
1556
1557
1558static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001559finalize_interp_clear(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001560{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001561 int is_main_interp = _Py_IsMainInterpreter(tstate);
1562
Victor Stinner7eee5be2019-11-20 10:38:34 +01001563 /* Clear interpreter state and all thread states */
Victor Stinnereba5bf22020-10-30 22:51:02 +01001564 _PyInterpreterState_Clear(tstate);
Pablo Galindoac0e1c22019-12-04 11:51:03 +00001565
Kongedaa0fe02020-07-04 05:06:46 +08001566 /* Clear all loghooks */
1567 /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1568 Call _PySys_ClearAuditHooks when PyObject available. */
1569 if (is_main_interp) {
1570 _PySys_ClearAuditHooks(tstate);
1571 }
1572
Victor Stinner7907f8c2020-06-08 01:22:36 +02001573 if (is_main_interp) {
1574 _Py_HashRandomization_Fini();
1575 _PyArg_Fini();
1576 _Py_ClearFileSystemEncoding();
1577 }
1578
Victor Stinner90db4652020-07-01 23:21:36 +02001579 finalize_interp_types(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001580}
1581
1582
1583static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001584finalize_interp_delete(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001585{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001586 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001587 /* Cleanup auto-thread-state */
1588 _PyGILState_Fini(tstate);
1589 }
1590
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001591 /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1592 fail when it is being awaited by another running daemon thread (see
1593 bpo-9901). Instead pycore_create_interpreter() destroys the previously
1594 created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1595 called multiple times. */
1596
Victor Stinner7eee5be2019-11-20 10:38:34 +01001597 PyInterpreterState_Delete(tstate->interp);
1598}
1599
1600
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001601int
1602Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +10001603{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001604 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001605
Victor Stinner8e91c242019-04-24 17:24:01 +02001606 _PyRuntimeState *runtime = &_PyRuntime;
1607 if (!runtime->initialized) {
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001608 return status;
Victor Stinner8e91c242019-04-24 17:24:01 +02001609 }
Nick Coghland6009512014-11-20 21:39:37 +10001610
Victor Stinnere225beb2019-06-03 18:14:24 +02001611 /* Get current thread state and interpreter pointer */
1612 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1613 PyInterpreterState *interp = tstate->interp;
Victor Stinner8e91c242019-04-24 17:24:01 +02001614
Victor Stinnerb45d2592019-06-20 00:05:23 +02001615 // Wrap up existing "threading"-module-created, non-daemon threads.
1616 wait_for_thread_shutdown(tstate);
1617
1618 // Make any remaining pending calls.
Victor Stinner2b1df452020-01-13 18:46:59 +01001619 _Py_FinishPendingCalls(tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001620
Nick Coghland6009512014-11-20 21:39:37 +10001621 /* The interpreter is still entirely intact at this point, and the
1622 * exit funcs may be relying on that. In particular, if some thread
1623 * or exit func is still waiting to do an import, the import machinery
1624 * expects Py_IsInitialized() to return true. So don't say the
Eric Snow842a2f02019-03-15 15:47:51 -06001625 * runtime is uninitialized until after the exit funcs have run.
Nick Coghland6009512014-11-20 21:39:37 +10001626 * Note that Threading.py uses an exit func to do a join on all the
1627 * threads created thru it, so this also protects pending imports in
1628 * the threads created via Threading.
1629 */
Nick Coghland6009512014-11-20 21:39:37 +10001630
Victor Stinnerb45d2592019-06-20 00:05:23 +02001631 call_py_exitfuncs(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001632
Victor Stinnerda273412017-12-15 01:46:02 +01001633 /* Copy the core config, PyInterpreterState_Delete() free
1634 the core config memory */
Victor Stinner5d862462017-12-19 11:35:58 +01001635#ifdef Py_REF_DEBUG
Victor Stinner331a6a52019-05-27 16:39:22 +02001636 int show_ref_count = interp->config.show_ref_count;
Victor Stinner5d862462017-12-19 11:35:58 +01001637#endif
1638#ifdef Py_TRACE_REFS
Victor Stinner331a6a52019-05-27 16:39:22 +02001639 int dump_refs = interp->config.dump_refs;
Victor Stinner5d862462017-12-19 11:35:58 +01001640#endif
1641#ifdef WITH_PYMALLOC
Victor Stinner331a6a52019-05-27 16:39:22 +02001642 int malloc_stats = interp->config.malloc_stats;
Victor Stinner5d862462017-12-19 11:35:58 +01001643#endif
Victor Stinner6bf992a2017-12-06 17:26:10 +01001644
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001645 /* Remaining daemon threads will automatically exit
1646 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
Victor Stinner7b3c2522020-03-07 00:24:23 +01001647 _PyRuntimeState_SetFinalizing(runtime, tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +02001648 runtime->initialized = 0;
1649 runtime->core_initialized = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001650
Victor Stinner9ad58ac2020-03-09 23:37:49 +01001651 /* Destroy the state of all threads of the interpreter, except of the
1652 current thread. In practice, only daemon threads should still be alive,
1653 except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1654 Clear frames of other threads to call objects destructors. Destructors
1655 will be called in the current Python thread. Since
1656 _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1657 can take the GIL at this point: if they try, they will exit
1658 immediately. */
1659 _PyThreadState_DeleteExcept(runtime, tstate);
1660
Victor Stinnere0deff32015-03-24 13:46:18 +01001661 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001662 if (flush_std_files() < 0) {
1663 status = -1;
1664 }
Nick Coghland6009512014-11-20 21:39:37 +10001665
1666 /* Disable signal handling */
1667 PyOS_FiniInterrupts();
1668
1669 /* Collect garbage. This may call finalizers; it's nice to call these
1670 * before all modules are destroyed.
1671 * XXX If a __del__ or weakref callback is triggered here, and tries to
1672 * XXX import a module, bad things can happen, because Python no
1673 * XXX longer believes it's initialized.
1674 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
1675 * XXX is easy to provoke that way. I've also seen, e.g.,
1676 * XXX Exception exceptions.ImportError: 'No module named sha'
1677 * XXX in <function callback at 0x008F5718> ignored
1678 * XXX but I'm unclear on exactly how that one happens. In any case,
1679 * XXX I haven't seen a real-life report of either of these.
1680 */
Victor Stinner8b341482020-10-30 17:00:00 +01001681 PyGC_Collect();
Eric Snowdae02762017-09-14 00:35:58 -07001682
Nick Coghland6009512014-11-20 21:39:37 +10001683 /* Destroy all modules */
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001684 finalize_modules(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001685
Inada Naoki91234a12019-06-03 21:30:58 +09001686 /* Print debug stats if any */
1687 _PyEval_Fini();
1688
Victor Stinnere0deff32015-03-24 13:46:18 +01001689 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001690 if (flush_std_files() < 0) {
1691 status = -1;
1692 }
Nick Coghland6009512014-11-20 21:39:37 +10001693
1694 /* Collect final garbage. This disposes of cycles created by
1695 * class definitions, for example.
1696 * XXX This is disabled because it caused too many problems. If
1697 * XXX a __del__ or weakref callback triggers here, Python code has
1698 * XXX a hard time running, because even the sys module has been
1699 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1700 * XXX One symptom is a sequence of information-free messages
1701 * XXX coming from threads (if a __del__ or callback is invoked,
1702 * XXX other threads can execute too, and any exception they encounter
1703 * XXX triggers a comedy of errors as subsystem after subsystem
1704 * XXX fails to find what it *expects* to find in sys to help report
1705 * XXX the exception and consequent unexpected failures). I've also
1706 * XXX seen segfaults then, after adding print statements to the
1707 * XXX Python code getting called.
1708 */
1709#if 0
Łukasz Langafef7e942016-09-09 21:47:46 -07001710 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +10001711#endif
1712
1713 /* Disable tracemalloc after all Python objects have been destroyed,
1714 so it is possible to use tracemalloc in objects destructor. */
1715 _PyTraceMalloc_Fini();
1716
1717 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1718 _PyImport_Fini();
1719
1720 /* Cleanup typeobject.c's internal caches. */
1721 _PyType_Fini();
1722
1723 /* unload faulthandler module */
1724 _PyFaulthandler_Fini();
1725
Nick Coghland6009512014-11-20 21:39:37 +10001726 /* dump hash stats */
1727 _PyHash_Fini();
1728
Eric Snowdae02762017-09-14 00:35:58 -07001729#ifdef Py_REF_DEBUG
Victor Stinnerda273412017-12-15 01:46:02 +01001730 if (show_ref_count) {
Victor Stinner25420fe2017-11-20 18:12:22 -08001731 _PyDebug_PrintTotalRefs();
1732 }
Eric Snowdae02762017-09-14 00:35:58 -07001733#endif
Nick Coghland6009512014-11-20 21:39:37 +10001734
1735#ifdef Py_TRACE_REFS
1736 /* Display all objects still alive -- this can invoke arbitrary
1737 * __repr__ overrides, so requires a mostly-intact interpreter.
1738 * Alas, a lot of stuff may still be alive now that will be cleaned
1739 * up later.
1740 */
Victor Stinnerda273412017-12-15 01:46:02 +01001741 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001742 _Py_PrintReferences(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001743 }
Nick Coghland6009512014-11-20 21:39:37 +10001744#endif /* Py_TRACE_REFS */
1745
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001746 finalize_interp_clear(tstate);
1747 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001748
1749#ifdef Py_TRACE_REFS
1750 /* Display addresses (& refcnts) of all objects still alive.
1751 * An address can be used to find the repr of the object, printed
1752 * above by _Py_PrintReferences.
1753 */
Victor Stinnerda273412017-12-15 01:46:02 +01001754 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001755 _Py_PrintReferenceAddresses(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001756 }
Nick Coghland6009512014-11-20 21:39:37 +10001757#endif /* Py_TRACE_REFS */
Victor Stinner34be807c2016-03-14 12:04:26 +01001758#ifdef WITH_PYMALLOC
Victor Stinnerda273412017-12-15 01:46:02 +01001759 if (malloc_stats) {
Victor Stinner6bf992a2017-12-06 17:26:10 +01001760 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be807c2016-03-14 12:04:26 +01001761 }
Nick Coghland6009512014-11-20 21:39:37 +10001762#endif
1763
Victor Stinner8e91c242019-04-24 17:24:01 +02001764 call_ll_exitfuncs(runtime);
Victor Stinner9316ee42017-11-25 03:17:57 +01001765
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001766 _PyRuntime_Finalize();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001767 return status;
1768}
1769
1770void
1771Py_Finalize(void)
1772{
1773 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +10001774}
1775
Victor Stinnerb0051362019-11-22 17:52:42 +01001776
Nick Coghland6009512014-11-20 21:39:37 +10001777/* Create and initialize a new interpreter and thread, and return the
1778 new thread. This requires that Py_Initialize() has been called
1779 first.
1780
1781 Unsuccessful initialization yields a NULL pointer. Note that *no*
1782 exception information is available even in this case -- the
1783 exception information is held in the thread, and there is no
1784 thread.
1785
1786 Locking: as above.
1787
1788*/
1789
Victor Stinner331a6a52019-05-27 16:39:22 +02001790static PyStatus
Victor Stinner252346a2020-05-01 11:33:44 +02001791new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
Nick Coghland6009512014-11-20 21:39:37 +10001792{
Victor Stinner331a6a52019-05-27 16:39:22 +02001793 PyStatus status;
Nick Coghland6009512014-11-20 21:39:37 +10001794
Victor Stinner331a6a52019-05-27 16:39:22 +02001795 status = _PyRuntime_Initialize();
1796 if (_PyStatus_EXCEPTION(status)) {
1797 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001798 }
1799 _PyRuntimeState *runtime = &_PyRuntime;
1800
1801 if (!runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001802 return _PyStatus_ERR("Py_Initialize must be called first");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001803 }
Nick Coghland6009512014-11-20 21:39:37 +10001804
Victor Stinner8a1be612016-03-14 22:07:55 +01001805 /* Issue #10915, #15751: The GIL API doesn't work with multiple
1806 interpreters: disable PyGILState_Check(). */
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001807 runtime->gilstate.check_enabled = 0;
Victor Stinner8a1be612016-03-14 22:07:55 +01001808
Victor Stinner43125222019-04-24 18:23:53 +02001809 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001810 if (interp == NULL) {
1811 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001812 return _PyStatus_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001813 }
Nick Coghland6009512014-11-20 21:39:37 +10001814
Victor Stinner43125222019-04-24 18:23:53 +02001815 PyThreadState *tstate = PyThreadState_New(interp);
Nick Coghland6009512014-11-20 21:39:37 +10001816 if (tstate == NULL) {
1817 PyInterpreterState_Delete(interp);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001818 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001819 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001820 }
1821
Victor Stinner43125222019-04-24 18:23:53 +02001822 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001823
Eric Snow1abcf672017-05-23 21:46:51 -07001824 /* Copy the current interpreter config into the new interpreter */
Victor Stinnerda7933e2020-04-13 03:04:28 +02001825 const PyConfig *config;
Victor Stinner7be4e352020-05-05 20:27:47 +02001826#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Eric Snow1abcf672017-05-23 21:46:51 -07001827 if (save_tstate != NULL) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001828 config = _PyInterpreterState_GetConfig(save_tstate->interp);
Victor Stinner7be4e352020-05-05 20:27:47 +02001829 }
1830 else
1831#endif
1832 {
Eric Snow1abcf672017-05-23 21:46:51 -07001833 /* No current thread state, copy from the main interpreter */
1834 PyInterpreterState *main_interp = PyInterpreterState_Main();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001835 config = _PyInterpreterState_GetConfig(main_interp);
Victor Stinnerda273412017-12-15 01:46:02 +01001836 }
1837
Victor Stinnerda7933e2020-04-13 03:04:28 +02001838 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001839 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001840 goto error;
Victor Stinnerda273412017-12-15 01:46:02 +01001841 }
Victor Stinner252346a2020-05-01 11:33:44 +02001842 interp->config._isolated_interpreter = isolated_subinterpreter;
Eric Snow1abcf672017-05-23 21:46:51 -07001843
Victor Stinner0dd5e7a2020-05-05 20:16:37 +02001844 status = init_interp_create_gil(tstate);
1845 if (_PyStatus_EXCEPTION(status)) {
1846 goto error;
1847 }
1848
Victor Stinnerd863ade2019-12-06 03:37:07 +01001849 status = pycore_interp_init(tstate);
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001850 if (_PyStatus_EXCEPTION(status)) {
1851 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001852 }
1853
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001854 status = init_interp_main(tstate);
1855 if (_PyStatus_EXCEPTION(status)) {
1856 goto error;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001857 }
Nick Coghland6009512014-11-20 21:39:37 +10001858
Victor Stinnera7368ac2017-11-15 18:11:45 -08001859 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +02001860 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001861
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001862error:
Victor Stinnerb0051362019-11-22 17:52:42 +01001863 *tstate_p = NULL;
1864
1865 /* Oops, it didn't work. Undo it all. */
Nick Coghland6009512014-11-20 21:39:37 +10001866 PyErr_PrintEx(0);
1867 PyThreadState_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001868 PyThreadState_Delete(tstate);
1869 PyInterpreterState_Delete(interp);
Victor Stinner9da74302019-11-20 11:17:17 +01001870 PyThreadState_Swap(save_tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001871
Victor Stinnerb0051362019-11-22 17:52:42 +01001872 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001873}
1874
1875PyThreadState *
Victor Stinner252346a2020-05-01 11:33:44 +02001876_Py_NewInterpreter(int isolated_subinterpreter)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001877{
Stéphane Wirtel9e06d2b2019-03-18 17:10:29 +01001878 PyThreadState *tstate = NULL;
Victor Stinner252346a2020-05-01 11:33:44 +02001879 PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
Victor Stinner331a6a52019-05-27 16:39:22 +02001880 if (_PyStatus_EXCEPTION(status)) {
1881 Py_ExitStatusException(status);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001882 }
1883 return tstate;
1884
Nick Coghland6009512014-11-20 21:39:37 +10001885}
1886
Victor Stinner252346a2020-05-01 11:33:44 +02001887PyThreadState *
1888Py_NewInterpreter(void)
1889{
1890 return _Py_NewInterpreter(0);
1891}
1892
Nick Coghland6009512014-11-20 21:39:37 +10001893/* Delete an interpreter and its last thread. This requires that the
1894 given thread state is current, that the thread has no remaining
1895 frames, and that it is its interpreter's only remaining thread.
1896 It is a fatal error to violate these constraints.
1897
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001898 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +10001899 everything, regardless.)
1900
1901 Locking: as above.
1902
1903*/
1904
1905void
1906Py_EndInterpreter(PyThreadState *tstate)
1907{
1908 PyInterpreterState *interp = tstate->interp;
1909
Victor Stinnerb45d2592019-06-20 00:05:23 +02001910 if (tstate != _PyThreadState_GET()) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001911 Py_FatalError("thread is not current");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001912 }
1913 if (tstate->frame != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001914 Py_FatalError("thread still has a frame");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001915 }
Eric Snow5be45a62019-03-08 22:47:07 -07001916 interp->finalizing = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001917
Eric Snow842a2f02019-03-15 15:47:51 -06001918 // Wrap up existing "threading"-module-created, non-daemon threads.
Victor Stinnerb45d2592019-06-20 00:05:23 +02001919 wait_for_thread_shutdown(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001920
Victor Stinnerb45d2592019-06-20 00:05:23 +02001921 call_py_exitfuncs(tstate);
Marcel Plch776407f2017-12-20 11:17:58 +01001922
Victor Stinnerb45d2592019-06-20 00:05:23 +02001923 if (tstate != interp->tstate_head || tstate->next != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001924 Py_FatalError("not the last thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001925 }
Nick Coghland6009512014-11-20 21:39:37 +10001926
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001927 finalize_modules(tstate);
1928
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001929 finalize_interp_clear(tstate);
1930 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001931}
1932
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001933/* Add the __main__ module */
Nick Coghland6009512014-11-20 21:39:37 +10001934
Victor Stinner331a6a52019-05-27 16:39:22 +02001935static PyStatus
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001936add_main_module(PyInterpreterState *interp)
Nick Coghland6009512014-11-20 21:39:37 +10001937{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001938 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +10001939 m = PyImport_AddModule("__main__");
1940 if (m == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +02001941 return _PyStatus_ERR("can't create __main__ module");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001942
Nick Coghland6009512014-11-20 21:39:37 +10001943 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001944 ann_dict = PyDict_New();
1945 if ((ann_dict == NULL) ||
1946 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001947 return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001948 }
1949 Py_DECREF(ann_dict);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001950
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001951 if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
1952 if (PyErr_Occurred()) {
1953 return _PyStatus_ERR("Failed to test __main__.__builtins__");
1954 }
Nick Coghland6009512014-11-20 21:39:37 +10001955 PyObject *bimod = PyImport_ImportModule("builtins");
1956 if (bimod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001957 return _PyStatus_ERR("Failed to retrieve builtins module");
Nick Coghland6009512014-11-20 21:39:37 +10001958 }
1959 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001960 return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
Nick Coghland6009512014-11-20 21:39:37 +10001961 }
1962 Py_DECREF(bimod);
1963 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001964
Nick Coghland6009512014-11-20 21:39:37 +10001965 /* Main is a little special - imp.is_builtin("__main__") will return
1966 * False, but BuiltinImporter is still the most appropriate initial
1967 * setting for its __loader__ attribute. A more suitable value will
1968 * be set if __main__ gets further initialized later in the startup
1969 * process.
1970 */
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001971 loader = _PyDict_GetItemStringWithError(d, "__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10001972 if (loader == NULL || loader == Py_None) {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001973 if (PyErr_Occurred()) {
1974 return _PyStatus_ERR("Failed to test __main__.__loader__");
1975 }
Nick Coghland6009512014-11-20 21:39:37 +10001976 PyObject *loader = PyObject_GetAttrString(interp->importlib,
1977 "BuiltinImporter");
1978 if (loader == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001979 return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
Nick Coghland6009512014-11-20 21:39:37 +10001980 }
1981 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001982 return _PyStatus_ERR("Failed to initialize __main__.__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10001983 }
1984 Py_DECREF(loader);
1985 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001986 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001987}
1988
Nick Coghland6009512014-11-20 21:39:37 +10001989/* Import the site module (not into __main__ though) */
1990
Victor Stinner331a6a52019-05-27 16:39:22 +02001991static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02001992init_import_site(void)
Nick Coghland6009512014-11-20 21:39:37 +10001993{
1994 PyObject *m;
1995 m = PyImport_ImportModule("site");
1996 if (m == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001997 return _PyStatus_ERR("Failed to import the site module");
Nick Coghland6009512014-11-20 21:39:37 +10001998 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001999 Py_DECREF(m);
Victor Stinner331a6a52019-05-27 16:39:22 +02002000 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002001}
2002
Victor Stinner874dbe82015-09-04 17:29:57 +02002003/* Check if a file descriptor is valid or not.
2004 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
2005static int
2006is_valid_fd(int fd)
2007{
Victor Stinner3092d6b2019-04-17 18:09:12 +02002008/* dup() is faster than fstat(): fstat() can require input/output operations,
2009 whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
2010 startup. Problem: dup() doesn't check if the file descriptor is valid on
2011 some platforms.
2012
2013 bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
2014 side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
2015 EBADF. FreeBSD has similar issue (bpo-32849).
2016
2017 Only use dup() on platforms where dup() is enough to detect invalid FD in
2018 corner cases: on Linux and Windows (bpo-32849). */
2019#if defined(__linux__) || defined(MS_WINDOWS)
2020 if (fd < 0) {
2021 return 0;
2022 }
2023 int fd2;
2024
2025 _Py_BEGIN_SUPPRESS_IPH
2026 fd2 = dup(fd);
2027 if (fd2 >= 0) {
2028 close(fd2);
2029 }
2030 _Py_END_SUPPRESS_IPH
2031
2032 return (fd2 >= 0);
2033#else
Victor Stinner1c4670e2017-05-04 00:45:56 +02002034 struct stat st;
2035 return (fstat(fd, &st) == 0);
Victor Stinner1c4670e2017-05-04 00:45:56 +02002036#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02002037}
2038
2039/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10002040static PyObject*
Victor Stinner331a6a52019-05-27 16:39:22 +02002041create_stdio(const PyConfig *config, PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002042 int fd, int write_mode, const char* name,
Victor Stinner709d23d2019-05-02 14:56:30 -04002043 const wchar_t* encoding, const wchar_t* errors)
Nick Coghland6009512014-11-20 21:39:37 +10002044{
2045 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
2046 const char* mode;
2047 const char* newline;
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002048 PyObject *line_buffering, *write_through;
Nick Coghland6009512014-11-20 21:39:37 +10002049 int buffering, isatty;
2050 _Py_IDENTIFIER(open);
2051 _Py_IDENTIFIER(isatty);
2052 _Py_IDENTIFIER(TextIOWrapper);
2053 _Py_IDENTIFIER(mode);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002054 const int buffered_stdio = config->buffered_stdio;
Nick Coghland6009512014-11-20 21:39:37 +10002055
Victor Stinner874dbe82015-09-04 17:29:57 +02002056 if (!is_valid_fd(fd))
2057 Py_RETURN_NONE;
2058
Nick Coghland6009512014-11-20 21:39:37 +10002059 /* stdin is always opened in buffered mode, first because it shouldn't
2060 make a difference in common use cases, second because TextIOWrapper
2061 depends on the presence of a read1() method which only exists on
2062 buffered streams.
2063 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002064 if (!buffered_stdio && write_mode)
Nick Coghland6009512014-11-20 21:39:37 +10002065 buffering = 0;
2066 else
2067 buffering = -1;
2068 if (write_mode)
2069 mode = "wb";
2070 else
2071 mode = "rb";
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002072 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
Nick Coghland6009512014-11-20 21:39:37 +10002073 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002074 Py_None, Py_None, /* encoding, errors */
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002075 Py_None, Py_False); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10002076 if (buf == NULL)
2077 goto error;
2078
2079 if (buffering) {
2080 _Py_IDENTIFIER(raw);
2081 raw = _PyObject_GetAttrId(buf, &PyId_raw);
2082 if (raw == NULL)
2083 goto error;
2084 }
2085 else {
2086 raw = buf;
2087 Py_INCREF(raw);
2088 }
2089
Steve Dower39294992016-08-30 21:22:36 -07002090#ifdef MS_WINDOWS
2091 /* Windows console IO is always UTF-8 encoded */
2092 if (PyWindowsConsoleIO_Check(raw))
Victor Stinner709d23d2019-05-02 14:56:30 -04002093 encoding = L"utf-8";
Steve Dower39294992016-08-30 21:22:36 -07002094#endif
2095
Nick Coghland6009512014-11-20 21:39:37 +10002096 text = PyUnicode_FromString(name);
2097 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
2098 goto error;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002099 res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
Nick Coghland6009512014-11-20 21:39:37 +10002100 if (res == NULL)
2101 goto error;
2102 isatty = PyObject_IsTrue(res);
2103 Py_DECREF(res);
2104 if (isatty == -1)
2105 goto error;
Victor Stinnerfbca9082018-08-30 00:50:45 +02002106 if (!buffered_stdio)
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002107 write_through = Py_True;
2108 else
2109 write_through = Py_False;
Jendrik Seipp5b907712020-01-01 23:21:43 +01002110 if (buffered_stdio && (isatty || fd == fileno(stderr)))
Nick Coghland6009512014-11-20 21:39:37 +10002111 line_buffering = Py_True;
2112 else
2113 line_buffering = Py_False;
2114
2115 Py_CLEAR(raw);
2116 Py_CLEAR(text);
2117
2118#ifdef MS_WINDOWS
2119 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
2120 newlines to "\n".
2121 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
2122 newline = NULL;
2123#else
2124 /* sys.stdin: split lines at "\n".
2125 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
2126 newline = "\n";
2127#endif
2128
Victor Stinner709d23d2019-05-02 14:56:30 -04002129 PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
2130 if (encoding_str == NULL) {
2131 Py_CLEAR(buf);
2132 goto error;
2133 }
2134
2135 PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
2136 if (errors_str == NULL) {
2137 Py_CLEAR(buf);
2138 Py_CLEAR(encoding_str);
2139 goto error;
2140 }
2141
2142 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
2143 buf, encoding_str, errors_str,
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002144 newline, line_buffering, write_through);
Nick Coghland6009512014-11-20 21:39:37 +10002145 Py_CLEAR(buf);
Victor Stinner709d23d2019-05-02 14:56:30 -04002146 Py_CLEAR(encoding_str);
2147 Py_CLEAR(errors_str);
Nick Coghland6009512014-11-20 21:39:37 +10002148 if (stream == NULL)
2149 goto error;
2150
2151 if (write_mode)
2152 mode = "w";
2153 else
2154 mode = "r";
2155 text = PyUnicode_FromString(mode);
2156 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
2157 goto error;
2158 Py_CLEAR(text);
2159 return stream;
2160
2161error:
2162 Py_XDECREF(buf);
2163 Py_XDECREF(stream);
2164 Py_XDECREF(text);
2165 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10002166
Victor Stinner874dbe82015-09-04 17:29:57 +02002167 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
2168 /* Issue #24891: the file descriptor was closed after the first
2169 is_valid_fd() check was called. Ignore the OSError and set the
2170 stream to None. */
2171 PyErr_Clear();
2172 Py_RETURN_NONE;
2173 }
2174 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002175}
2176
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002177/* Set builtins.open to io.OpenWrapper */
2178static PyStatus
Andy Lester75cd5bf2020-03-12 02:49:05 -05002179init_set_builtins_open(void)
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002180{
2181 PyObject *iomod = NULL, *wrapper;
2182 PyObject *bimod = NULL;
2183 PyStatus res = _PyStatus_OK();
2184
2185 if (!(iomod = PyImport_ImportModule("io"))) {
2186 goto error;
2187 }
2188
2189 if (!(bimod = PyImport_ImportModule("builtins"))) {
2190 goto error;
2191 }
2192
2193 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
2194 goto error;
2195 }
2196
2197 /* Set builtins.open */
2198 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
2199 Py_DECREF(wrapper);
2200 goto error;
2201 }
2202 Py_DECREF(wrapper);
2203 goto done;
2204
2205error:
2206 res = _PyStatus_ERR("can't initialize io.open");
2207
2208done:
2209 Py_XDECREF(bimod);
2210 Py_XDECREF(iomod);
2211 return res;
2212}
2213
2214
Nick Coghland6009512014-11-20 21:39:37 +10002215/* Initialize sys.stdin, stdout, stderr and builtins.open */
Victor Stinner331a6a52019-05-27 16:39:22 +02002216static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002217init_sys_streams(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002218{
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002219 PyObject *iomod = NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002220 PyObject *std = NULL;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002221 int fd;
Nick Coghland6009512014-11-20 21:39:37 +10002222 PyObject * encoding_attr;
Victor Stinner331a6a52019-05-27 16:39:22 +02002223 PyStatus res = _PyStatus_OK();
Victor Stinnerda7933e2020-04-13 03:04:28 +02002224 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002225
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002226 /* Check that stdin is not a directory
2227 Using shell redirection, you can redirect stdin to a directory,
2228 crashing the Python interpreter. Catch this common mistake here
2229 and output a useful error message. Note that under MS Windows,
2230 the shell already prevents that. */
2231#ifndef MS_WINDOWS
2232 struct _Py_stat_struct sb;
2233 if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
2234 S_ISDIR(sb.st_mode)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002235 return _PyStatus_ERR("<stdin> is a directory, cannot continue");
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002236 }
2237#endif
2238
Nick Coghland6009512014-11-20 21:39:37 +10002239 if (!(iomod = PyImport_ImportModule("io"))) {
2240 goto error;
2241 }
Nick Coghland6009512014-11-20 21:39:37 +10002242
Nick Coghland6009512014-11-20 21:39:37 +10002243 /* Set sys.stdin */
2244 fd = fileno(stdin);
2245 /* Under some conditions stdin, stdout and stderr may not be connected
2246 * and fileno() may point to an invalid file descriptor. For example
2247 * GUI apps don't have valid standard streams by default.
2248 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002249 std = create_stdio(config, iomod, fd, 0, "<stdin>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002250 config->stdio_encoding,
2251 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002252 if (std == NULL)
2253 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002254 PySys_SetObject("__stdin__", std);
2255 _PySys_SetObjectId(&PyId_stdin, std);
2256 Py_DECREF(std);
2257
2258 /* Set sys.stdout */
2259 fd = fileno(stdout);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002260 std = create_stdio(config, iomod, fd, 1, "<stdout>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002261 config->stdio_encoding,
2262 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002263 if (std == NULL)
2264 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002265 PySys_SetObject("__stdout__", std);
2266 _PySys_SetObjectId(&PyId_stdout, std);
2267 Py_DECREF(std);
2268
2269#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2270 /* Set sys.stderr, replaces the preliminary stderr */
2271 fd = fileno(stderr);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002272 std = create_stdio(config, iomod, fd, 1, "<stderr>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002273 config->stdio_encoding,
Victor Stinner709d23d2019-05-02 14:56:30 -04002274 L"backslashreplace");
Victor Stinner874dbe82015-09-04 17:29:57 +02002275 if (std == NULL)
2276 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002277
2278 /* Same as hack above, pre-import stderr's codec to avoid recursion
2279 when import.c tries to write to stderr in verbose mode. */
2280 encoding_attr = PyObject_GetAttrString(std, "encoding");
2281 if (encoding_attr != NULL) {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002282 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10002283 if (std_encoding != NULL) {
2284 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2285 Py_XDECREF(codec_info);
2286 }
2287 Py_DECREF(encoding_attr);
2288 }
Victor Stinnerb45d2592019-06-20 00:05:23 +02002289 _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
Nick Coghland6009512014-11-20 21:39:37 +10002290
2291 if (PySys_SetObject("__stderr__", std) < 0) {
2292 Py_DECREF(std);
2293 goto error;
2294 }
2295 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2296 Py_DECREF(std);
2297 goto error;
2298 }
2299 Py_DECREF(std);
2300#endif
2301
Victor Stinnera7368ac2017-11-15 18:11:45 -08002302 goto done;
Nick Coghland6009512014-11-20 21:39:37 +10002303
Victor Stinnera7368ac2017-11-15 18:11:45 -08002304error:
Victor Stinner331a6a52019-05-27 16:39:22 +02002305 res = _PyStatus_ERR("can't initialize sys standard streams");
Victor Stinnera7368ac2017-11-15 18:11:45 -08002306
2307done:
Victor Stinner124b9eb2018-08-29 01:29:06 +02002308 _Py_ClearStandardStreamEncoding();
Nick Coghland6009512014-11-20 21:39:37 +10002309 Py_XDECREF(iomod);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002310 return res;
Nick Coghland6009512014-11-20 21:39:37 +10002311}
2312
2313
Victor Stinner10dc4842015-03-24 12:01:30 +01002314static void
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002315_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2316 PyThreadState *tstate)
Victor Stinner10dc4842015-03-24 12:01:30 +01002317{
Victor Stinner10dc4842015-03-24 12:01:30 +01002318 fputc('\n', stderr);
2319 fflush(stderr);
2320
2321 /* display the current Python stack */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002322 _Py_DumpTracebackThreads(fd, interp, tstate);
Victor Stinner10dc4842015-03-24 12:01:30 +01002323}
Victor Stinner791da1c2016-03-14 16:53:12 +01002324
2325/* Print the current exception (if an exception is set) with its traceback,
2326 or display the current Python stack.
2327
2328 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2329 called on catastrophic cases.
2330
2331 Return 1 if the traceback was displayed, 0 otherwise. */
2332
2333static int
Andy Lester75cd5bf2020-03-12 02:49:05 -05002334_Py_FatalError_PrintExc(PyThreadState *tstate)
Victor Stinner791da1c2016-03-14 16:53:12 +01002335{
2336 PyObject *ferr, *res;
2337 PyObject *exception, *v, *tb;
2338 int has_tb;
2339
Victor Stinnerb45d2592019-06-20 00:05:23 +02002340 _PyErr_Fetch(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002341 if (exception == NULL) {
2342 /* No current exception */
2343 return 0;
2344 }
2345
2346 ferr = _PySys_GetObjectId(&PyId_stderr);
2347 if (ferr == NULL || ferr == Py_None) {
2348 /* sys.stderr is not set yet or set to None,
2349 no need to try to display the exception */
2350 return 0;
2351 }
2352
Victor Stinnerb45d2592019-06-20 00:05:23 +02002353 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002354 if (tb == NULL) {
2355 tb = Py_None;
2356 Py_INCREF(tb);
2357 }
2358 PyException_SetTraceback(v, tb);
2359 if (exception == NULL) {
2360 /* PyErr_NormalizeException() failed */
2361 return 0;
2362 }
2363
2364 has_tb = (tb != Py_None);
2365 PyErr_Display(exception, v, tb);
2366 Py_XDECREF(exception);
2367 Py_XDECREF(v);
2368 Py_XDECREF(tb);
2369
2370 /* sys.stderr may be buffered: call sys.stderr.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002371 res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002372 if (res == NULL) {
2373 _PyErr_Clear(tstate);
2374 }
2375 else {
Victor Stinner791da1c2016-03-14 16:53:12 +01002376 Py_DECREF(res);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002377 }
Victor Stinner791da1c2016-03-14 16:53:12 +01002378
2379 return has_tb;
2380}
2381
Nick Coghland6009512014-11-20 21:39:37 +10002382/* Print fatal error message and abort */
2383
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002384#ifdef MS_WINDOWS
2385static void
2386fatal_output_debug(const char *msg)
2387{
2388 /* buffer of 256 bytes allocated on the stack */
2389 WCHAR buffer[256 / sizeof(WCHAR)];
2390 size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2391 size_t msglen;
2392
2393 OutputDebugStringW(L"Fatal Python error: ");
2394
2395 msglen = strlen(msg);
2396 while (msglen) {
2397 size_t i;
2398
2399 if (buflen > msglen) {
2400 buflen = msglen;
2401 }
2402
2403 /* Convert the message to wchar_t. This uses a simple one-to-one
2404 conversion, assuming that the this error message actually uses
2405 ASCII only. If this ceases to be true, we will have to convert. */
2406 for (i=0; i < buflen; ++i) {
2407 buffer[i] = msg[i];
2408 }
2409 buffer[i] = L'\0';
2410 OutputDebugStringW(buffer);
2411
2412 msg += buflen;
2413 msglen -= buflen;
2414 }
2415 OutputDebugStringW(L"\n");
2416}
2417#endif
2418
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002419
2420static void
2421fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
2422{
2423 fprintf(stream, "Python runtime state: ");
Victor Stinner7b3c2522020-03-07 00:24:23 +01002424 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2425 if (finalizing) {
2426 fprintf(stream, "finalizing (tstate=%p)", finalizing);
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002427 }
2428 else if (runtime->initialized) {
2429 fprintf(stream, "initialized");
2430 }
2431 else if (runtime->core_initialized) {
2432 fprintf(stream, "core initialized");
2433 }
2434 else if (runtime->preinitialized) {
2435 fprintf(stream, "preinitialized");
2436 }
2437 else if (runtime->preinitializing) {
2438 fprintf(stream, "preinitializing");
2439 }
2440 else {
2441 fprintf(stream, "unknown");
2442 }
2443 fprintf(stream, "\n");
2444 fflush(stream);
2445}
2446
2447
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002448static inline void _Py_NO_RETURN
2449fatal_error_exit(int status)
Nick Coghland6009512014-11-20 21:39:37 +10002450{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002451 if (status < 0) {
2452#if defined(MS_WINDOWS) && defined(_DEBUG)
2453 DebugBreak();
2454#endif
2455 abort();
2456 }
2457 else {
2458 exit(status);
2459 }
2460}
2461
2462
2463static void _Py_NO_RETURN
2464fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2465 int status)
2466{
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002467 const int fd = fileno(stream);
Victor Stinner53345a42015-03-25 01:55:14 +01002468 static int reentrant = 0;
Victor Stinner53345a42015-03-25 01:55:14 +01002469
2470 if (reentrant) {
2471 /* Py_FatalError() caused a second fatal error.
2472 Example: flush_std_files() raises a recursion error. */
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002473 fatal_error_exit(status);
Victor Stinner53345a42015-03-25 01:55:14 +01002474 }
2475 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10002476
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002477 if (header) {
2478 fprintf(stream, "Fatal Python error: ");
2479 if (prefix) {
2480 fputs(prefix, stream);
2481 fputs(": ", stream);
2482 }
2483 if (msg) {
2484 fputs(msg, stream);
2485 }
2486 else {
2487 fprintf(stream, "<message not set>");
2488 }
2489 fputs("\n", stream);
2490 fflush(stream);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002491 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002492
2493 _PyRuntimeState *runtime = &_PyRuntime;
2494 fatal_error_dump_runtime(stream, runtime);
2495
2496 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2497 PyInterpreterState *interp = NULL;
2498 if (tstate != NULL) {
2499 interp = tstate->interp;
2500 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002501
Victor Stinner3a228ab2018-11-01 00:26:41 +01002502 /* Check if the current thread has a Python thread state
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002503 and holds the GIL.
Victor Stinner3a228ab2018-11-01 00:26:41 +01002504
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002505 tss_tstate is NULL if Py_FatalError() is called from a C thread which
2506 has no Python thread state.
2507
2508 tss_tstate != tstate if the current Python thread does not hold the GIL.
2509 */
2510 PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
2511 int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002512 if (has_tstate_and_gil) {
2513 /* If an exception is set, print the exception with its traceback */
Andy Lester75cd5bf2020-03-12 02:49:05 -05002514 if (!_Py_FatalError_PrintExc(tss_tstate)) {
Victor Stinner3a228ab2018-11-01 00:26:41 +01002515 /* No exception is set, or an exception is set without traceback */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002516 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002517 }
2518 }
2519 else {
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002520 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002521 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002522
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002523 /* The main purpose of faulthandler is to display the traceback.
2524 This function already did its best to display a traceback.
2525 Disable faulthandler to prevent writing a second traceback
2526 on abort(). */
Victor Stinner2025d782016-03-16 23:19:15 +01002527 _PyFaulthandler_Fini();
2528
Victor Stinner791da1c2016-03-14 16:53:12 +01002529 /* Check if the current Python thread hold the GIL */
Victor Stinner3a228ab2018-11-01 00:26:41 +01002530 if (has_tstate_and_gil) {
Victor Stinner791da1c2016-03-14 16:53:12 +01002531 /* Flush sys.stdout and sys.stderr */
2532 flush_std_files();
2533 }
Victor Stinnere0deff32015-03-24 13:46:18 +01002534
Nick Coghland6009512014-11-20 21:39:37 +10002535#ifdef MS_WINDOWS
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002536 fatal_output_debug(msg);
Victor Stinner53345a42015-03-25 01:55:14 +01002537#endif /* MS_WINDOWS */
2538
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002539 fatal_error_exit(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002540}
2541
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002542
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002543#undef Py_FatalError
2544
Victor Stinner19760862017-12-20 01:41:59 +01002545void _Py_NO_RETURN
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002546Py_FatalError(const char *msg)
2547{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002548 fatal_error(stderr, 1, NULL, msg, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002549}
2550
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002551
Victor Stinner19760862017-12-20 01:41:59 +01002552void _Py_NO_RETURN
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002553_Py_FatalErrorFunc(const char *func, const char *msg)
2554{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002555 fatal_error(stderr, 1, func, msg, -1);
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002556}
2557
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002558
2559void _Py_NO_RETURN
2560_Py_FatalErrorFormat(const char *func, const char *format, ...)
2561{
2562 static int reentrant = 0;
2563 if (reentrant) {
2564 /* _Py_FatalErrorFormat() caused a second fatal error */
2565 fatal_error_exit(-1);
2566 }
2567 reentrant = 1;
2568
2569 FILE *stream = stderr;
2570 fprintf(stream, "Fatal Python error: ");
2571 if (func) {
2572 fputs(func, stream);
2573 fputs(": ", stream);
2574 }
2575 fflush(stream);
2576
2577 va_list vargs;
2578#ifdef HAVE_STDARG_PROTOTYPES
2579 va_start(vargs, format);
2580#else
2581 va_start(vargs);
2582#endif
2583 vfprintf(stream, format, vargs);
2584 va_end(vargs);
2585
2586 fputs("\n", stream);
2587 fflush(stream);
2588
2589 fatal_error(stream, 0, NULL, NULL, -1);
2590}
2591
2592
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002593void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +02002594Py_ExitStatusException(PyStatus status)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002595{
Victor Stinner331a6a52019-05-27 16:39:22 +02002596 if (_PyStatus_IS_EXIT(status)) {
2597 exit(status.exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +02002598 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002599 else if (_PyStatus_IS_ERROR(status)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002600 fatal_error(stderr, 1, status.func, status.err_msg, 1);
Victor Stinnerdfe88472019-03-01 12:14:41 +01002601 }
2602 else {
Victor Stinner331a6a52019-05-27 16:39:22 +02002603 Py_FatalError("Py_ExitStatusException() must not be called on success");
Victor Stinnerdfe88472019-03-01 12:14:41 +01002604 }
Nick Coghland6009512014-11-20 21:39:37 +10002605}
2606
2607/* Clean up and exit */
2608
Nick Coghland6009512014-11-20 21:39:37 +10002609/* For the atexit module. */
Marcel Plch776407f2017-12-20 11:17:58 +01002610void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
Nick Coghland6009512014-11-20 21:39:37 +10002611{
Victor Stinner81a7be32020-04-14 15:14:01 +02002612 PyInterpreterState *is = _PyInterpreterState_GET();
Marcel Plch776407f2017-12-20 11:17:58 +01002613
Antoine Pitroufc5db952017-12-13 02:29:07 +01002614 /* Guard against API misuse (see bpo-17852) */
Marcel Plch776407f2017-12-20 11:17:58 +01002615 assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
2616
2617 is->pyexitfunc = func;
2618 is->pyexitmodule = module;
Nick Coghland6009512014-11-20 21:39:37 +10002619}
2620
2621static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002622call_py_exitfuncs(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002623{
Victor Stinnerb45d2592019-06-20 00:05:23 +02002624 PyInterpreterState *interp = tstate->interp;
2625 if (interp->pyexitfunc == NULL)
Nick Coghland6009512014-11-20 21:39:37 +10002626 return;
2627
Victor Stinnerb45d2592019-06-20 00:05:23 +02002628 (*interp->pyexitfunc)(interp->pyexitmodule);
2629 _PyErr_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10002630}
2631
2632/* Wait until threading._shutdown completes, provided
2633 the threading module was imported in the first place.
2634 The shutdown routine will wait until all non-daemon
2635 "threading" threads have completed. */
2636static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002637wait_for_thread_shutdown(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002638{
Nick Coghland6009512014-11-20 21:39:37 +10002639 _Py_IDENTIFIER(_shutdown);
2640 PyObject *result;
Eric Snow3f9eee62017-09-15 16:35:20 -06002641 PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
Nick Coghland6009512014-11-20 21:39:37 +10002642 if (threading == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +02002643 if (_PyErr_Occurred(tstate)) {
Stefan Krah027b09c2019-03-25 21:50:58 +01002644 PyErr_WriteUnraisable(NULL);
2645 }
2646 /* else: threading not imported */
Nick Coghland6009512014-11-20 21:39:37 +10002647 return;
2648 }
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002649 result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
Nick Coghland6009512014-11-20 21:39:37 +10002650 if (result == NULL) {
2651 PyErr_WriteUnraisable(threading);
2652 }
2653 else {
2654 Py_DECREF(result);
2655 }
2656 Py_DECREF(threading);
Nick Coghland6009512014-11-20 21:39:37 +10002657}
2658
2659#define NEXITFUNCS 32
Nick Coghland6009512014-11-20 21:39:37 +10002660int Py_AtExit(void (*func)(void))
2661{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002662 if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
Nick Coghland6009512014-11-20 21:39:37 +10002663 return -1;
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002664 _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
Nick Coghland6009512014-11-20 21:39:37 +10002665 return 0;
2666}
2667
2668static void
Victor Stinner8e91c242019-04-24 17:24:01 +02002669call_ll_exitfuncs(_PyRuntimeState *runtime)
Nick Coghland6009512014-11-20 21:39:37 +10002670{
Victor Stinner8e91c242019-04-24 17:24:01 +02002671 while (runtime->nexitfuncs > 0) {
Victor Stinner87d23a02019-04-26 05:49:26 +02002672 /* pop last function from the list */
2673 runtime->nexitfuncs--;
2674 void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2675 runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2676
2677 exitfunc();
Victor Stinner8e91c242019-04-24 17:24:01 +02002678 }
Nick Coghland6009512014-11-20 21:39:37 +10002679
2680 fflush(stdout);
2681 fflush(stderr);
2682}
2683
Victor Stinnercfc88312018-08-01 16:41:25 +02002684void _Py_NO_RETURN
Nick Coghland6009512014-11-20 21:39:37 +10002685Py_Exit(int sts)
2686{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002687 if (Py_FinalizeEx() < 0) {
2688 sts = 120;
2689 }
Nick Coghland6009512014-11-20 21:39:37 +10002690
2691 exit(sts);
2692}
2693
Victor Stinner331a6a52019-05-27 16:39:22 +02002694static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002695init_signals(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002696{
2697#ifdef SIGPIPE
2698 PyOS_setsig(SIGPIPE, SIG_IGN);
2699#endif
2700#ifdef SIGXFZ
2701 PyOS_setsig(SIGXFZ, SIG_IGN);
2702#endif
2703#ifdef SIGXFSZ
2704 PyOS_setsig(SIGXFSZ, SIG_IGN);
2705#endif
Victor Stinner400e1db2020-03-31 19:13:10 +02002706 PyOS_InitInterrupts(); /* May imply init_signals() */
Victor Stinnerb45d2592019-06-20 00:05:23 +02002707 if (_PyErr_Occurred(tstate)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002708 return _PyStatus_ERR("can't import signal");
Nick Coghland6009512014-11-20 21:39:37 +10002709 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002710 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002711}
2712
2713
2714/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2715 *
2716 * All of the code in this function must only use async-signal-safe functions,
2717 * listed at `man 7 signal` or
2718 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
Victor Stinnerefc28bb2020-03-05 18:13:56 +01002719 *
2720 * If this function is updated, update also _posix_spawn() of subprocess.py.
Nick Coghland6009512014-11-20 21:39:37 +10002721 */
2722void
2723_Py_RestoreSignals(void)
2724{
2725#ifdef SIGPIPE
2726 PyOS_setsig(SIGPIPE, SIG_DFL);
2727#endif
2728#ifdef SIGXFZ
2729 PyOS_setsig(SIGXFZ, SIG_DFL);
2730#endif
2731#ifdef SIGXFSZ
2732 PyOS_setsig(SIGXFSZ, SIG_DFL);
2733#endif
2734}
2735
2736
2737/*
2738 * The file descriptor fd is considered ``interactive'' if either
2739 * a) isatty(fd) is TRUE, or
2740 * b) the -i flag was given, and the filename associated with
2741 * the descriptor is NULL or "<stdin>" or "???".
2742 */
2743int
2744Py_FdIsInteractive(FILE *fp, const char *filename)
2745{
2746 if (isatty((int)fileno(fp)))
2747 return 1;
2748 if (!Py_InteractiveFlag)
2749 return 0;
2750 return (filename == NULL) ||
2751 (strcmp(filename, "<stdin>") == 0) ||
2752 (strcmp(filename, "???") == 0);
2753}
2754
2755
Nick Coghland6009512014-11-20 21:39:37 +10002756/* Wrappers around sigaction() or signal(). */
2757
2758PyOS_sighandler_t
2759PyOS_getsig(int sig)
2760{
2761#ifdef HAVE_SIGACTION
2762 struct sigaction context;
2763 if (sigaction(sig, NULL, &context) == -1)
2764 return SIG_ERR;
2765 return context.sa_handler;
2766#else
2767 PyOS_sighandler_t handler;
2768/* Special signal handling for the secure CRT in Visual Studio 2005 */
2769#if defined(_MSC_VER) && _MSC_VER >= 1400
2770 switch (sig) {
2771 /* Only these signals are valid */
2772 case SIGINT:
2773 case SIGILL:
2774 case SIGFPE:
2775 case SIGSEGV:
2776 case SIGTERM:
2777 case SIGBREAK:
2778 case SIGABRT:
2779 break;
2780 /* Don't call signal() with other values or it will assert */
2781 default:
2782 return SIG_ERR;
2783 }
2784#endif /* _MSC_VER && _MSC_VER >= 1400 */
2785 handler = signal(sig, SIG_IGN);
2786 if (handler != SIG_ERR)
2787 signal(sig, handler);
2788 return handler;
2789#endif
2790}
2791
2792/*
2793 * All of the code in this function must only use async-signal-safe functions,
2794 * listed at `man 7 signal` or
2795 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2796 */
2797PyOS_sighandler_t
2798PyOS_setsig(int sig, PyOS_sighandler_t handler)
2799{
2800#ifdef HAVE_SIGACTION
2801 /* Some code in Modules/signalmodule.c depends on sigaction() being
2802 * used here if HAVE_SIGACTION is defined. Fix that if this code
2803 * changes to invalidate that assumption.
2804 */
2805 struct sigaction context, ocontext;
2806 context.sa_handler = handler;
2807 sigemptyset(&context.sa_mask);
2808 context.sa_flags = 0;
2809 if (sigaction(sig, &context, &ocontext) == -1)
2810 return SIG_ERR;
2811 return ocontext.sa_handler;
2812#else
2813 PyOS_sighandler_t oldhandler;
2814 oldhandler = signal(sig, handler);
2815#ifdef HAVE_SIGINTERRUPT
2816 siginterrupt(sig, 1);
2817#endif
2818 return oldhandler;
2819#endif
2820}
2821
2822#ifdef __cplusplus
2823}
2824#endif