blob: 2d43e016efd50815beccba4efc393a6de2e5e56c [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*/
Victor Stinneref75a622020-11-12 15:14:13 +0100138static int
Victor Stinnerb45d2592019-06-20 00:05:23 +0200139init_importlib(PyThreadState *tstate, PyObject *sysmod)
Nick Coghland6009512014-11-20 21:39:37 +1000140{
Victor Stinneref75a622020-11-12 15:14:13 +0100141 assert(!_PyErr_Occurred(tstate));
142
Victor Stinnerb45d2592019-06-20 00:05:23 +0200143 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200144 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
Nick Coghland6009512014-11-20 21:39:37 +1000145
Victor Stinneref75a622020-11-12 15:14:13 +0100146 // Import _importlib through its frozen version, _frozen_importlib.
147 if (verbose) {
Nick Coghland6009512014-11-20 21:39:37 +1000148 PySys_FormatStderr("import _frozen_importlib # frozen\n");
149 }
Victor Stinneref75a622020-11-12 15:14:13 +0100150 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
151 return -1;
152 }
153 PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed
Nick Coghland6009512014-11-20 21:39:37 +1000154 if (importlib == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100155 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000156 }
Victor Stinneref75a622020-11-12 15:14:13 +0100157 interp->importlib = Py_NewRef(importlib);
Nick Coghland6009512014-11-20 21:39:37 +1000158
Victor Stinneref75a622020-11-12 15:14:13 +0100159 PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
160 "__import__");
161 if (import_func == NULL) {
162 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000163 }
Victor Stinneref75a622020-11-12 15:14:13 +0100164 interp->import_func = Py_NewRef(import_func);
165
166 // Import the _imp module
167 if (verbose) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200168 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000169 }
Victor Stinneref75a622020-11-12 15:14:13 +0100170 PyObject *imp_mod = PyInit__imp();
171 if (imp_mod == NULL) {
172 return -1;
173 }
174 if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
175 Py_DECREF(imp_mod);
176 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000177 }
178
Victor Stinneref75a622020-11-12 15:14:13 +0100179 // Install importlib as the implementation of import
180 PyObject *value = PyObject_CallMethod(importlib, "_install",
181 "OO", sysmod, imp_mod);
182 Py_DECREF(imp_mod);
Nick Coghland6009512014-11-20 21:39:37 +1000183 if (value == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100184 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000185 }
186 Py_DECREF(value);
Nick Coghland6009512014-11-20 21:39:37 +1000187
Victor Stinneref75a622020-11-12 15:14:13 +0100188 assert(!_PyErr_Occurred(tstate));
189 return 0;
Nick Coghland6009512014-11-20 21:39:37 +1000190}
191
Victor Stinneref75a622020-11-12 15:14:13 +0100192
Victor Stinner331a6a52019-05-27 16:39:22 +0200193static PyStatus
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200194init_importlib_external(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -0700195{
196 PyObject *value;
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200197 value = PyObject_CallMethod(tstate->interp->importlib,
Eric Snow1abcf672017-05-23 21:46:51 -0700198 "_install_external_importers", "");
199 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200200 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200201 return _PyStatus_ERR("external importer setup failed");
Eric Snow1abcf672017-05-23 21:46:51 -0700202 }
Stéphane Wirtelab1cb802017-06-08 13:13:20 +0200203 Py_DECREF(value);
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200204 return _PyImportZip_Init(tstate);
Eric Snow1abcf672017-05-23 21:46:51 -0700205}
Nick Coghland6009512014-11-20 21:39:37 +1000206
Nick Coghlan6ea41862017-06-11 13:16:15 +1000207/* Helper functions to better handle the legacy C locale
208 *
209 * The legacy C locale assumes ASCII as the default text encoding, which
210 * causes problems not only for the CPython runtime, but also other
211 * components like GNU readline.
212 *
213 * Accordingly, when the CLI detects it, it attempts to coerce it to a
214 * more capable UTF-8 based alternative as follows:
215 *
216 * if (_Py_LegacyLocaleDetected()) {
217 * _Py_CoerceLegacyLocale();
218 * }
219 *
220 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
221 *
222 * Locale coercion also impacts the default error handler for the standard
223 * streams: while the usual default is "strict", the default for the legacy
224 * C locale and for any of the coercion target locales is "surrogateescape".
225 */
226
227int
Victor Stinner0f721472019-05-20 17:16:38 +0200228_Py_LegacyLocaleDetected(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000229{
230#ifndef MS_WINDOWS
Victor Stinner0f721472019-05-20 17:16:38 +0200231 if (!warn) {
232 const char *locale_override = getenv("LC_ALL");
233 if (locale_override != NULL && *locale_override != '\0') {
234 /* Don't coerce C locale if the LC_ALL environment variable
235 is set */
236 return 0;
237 }
238 }
239
Nick Coghlan6ea41862017-06-11 13:16:15 +1000240 /* On non-Windows systems, the C locale is considered a legacy locale */
Nick Coghlaneb817952017-06-18 12:29:42 +1000241 /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
242 * the POSIX locale as a simple alias for the C locale, so
243 * we may also want to check for that explicitly.
244 */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000245 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
246 return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
247#else
248 /* Windows uses code pages instead of locales, so no locale is legacy */
249 return 0;
250#endif
251}
252
Victor Stinnerb0051362019-11-22 17:52:42 +0100253#ifndef MS_WINDOWS
Nick Coghlaneb817952017-06-18 12:29:42 +1000254static const char *_C_LOCALE_WARNING =
255 "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
256 "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
257 "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
258 "locales is recommended.\n";
259
Nick Coghlaneb817952017-06-18 12:29:42 +1000260static void
Victor Stinner43125222019-04-24 18:23:53 +0200261emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
Nick Coghlaneb817952017-06-18 12:29:42 +1000262{
Victor Stinner331a6a52019-05-27 16:39:22 +0200263 const PyPreConfig *preconfig = &runtime->preconfig;
Victor Stinner0f721472019-05-20 17:16:38 +0200264 if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
Victor Stinnercf215042018-08-29 22:56:06 +0200265 PySys_FormatStderr("%s", _C_LOCALE_WARNING);
Nick Coghlaneb817952017-06-18 12:29:42 +1000266 }
267}
Victor Stinnerb0051362019-11-22 17:52:42 +0100268#endif /* !defined(MS_WINDOWS) */
Nick Coghlaneb817952017-06-18 12:29:42 +1000269
Nick Coghlan6ea41862017-06-11 13:16:15 +1000270typedef struct _CandidateLocale {
271 const char *locale_name; /* The locale to try as a coercion target */
272} _LocaleCoercionTarget;
273
274static _LocaleCoercionTarget _TARGET_LOCALES[] = {
275 {"C.UTF-8"},
276 {"C.utf8"},
Nick Coghlan18974c32017-06-30 00:48:14 +1000277 {"UTF-8"},
Nick Coghlan6ea41862017-06-11 13:16:15 +1000278 {NULL}
279};
280
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200281
282int
283_Py_IsLocaleCoercionTarget(const char *ctype_loc)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000284{
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200285 const _LocaleCoercionTarget *target = NULL;
286 for (target = _TARGET_LOCALES; target->locale_name; target++) {
287 if (strcmp(ctype_loc, target->locale_name) == 0) {
288 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000289 }
Victor Stinner124b9eb2018-08-29 01:29:06 +0200290 }
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200291 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000292}
293
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200294
Nick Coghlan6ea41862017-06-11 13:16:15 +1000295#ifdef PY_COERCE_C_LOCALE
Victor Stinner94540602017-12-16 04:54:22 +0100296static const char C_LOCALE_COERCION_WARNING[] =
Nick Coghlan6ea41862017-06-11 13:16:15 +1000297 "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
298 "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
299
Victor Stinner0f721472019-05-20 17:16:38 +0200300static int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200301_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000302{
303 const char *newloc = target->locale_name;
304
305 /* Reset locale back to currently configured defaults */
xdegaye1588be62017-11-12 12:45:59 +0100306 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000307
308 /* Set the relevant locale environment variable */
309 if (setenv("LC_CTYPE", newloc, 1)) {
310 fprintf(stderr,
311 "Error setting LC_CTYPE, skipping C locale coercion\n");
Victor Stinner0f721472019-05-20 17:16:38 +0200312 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000313 }
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200314 if (warn) {
Victor Stinner94540602017-12-16 04:54:22 +0100315 fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
Nick Coghlaneb817952017-06-18 12:29:42 +1000316 }
Nick Coghlan6ea41862017-06-11 13:16:15 +1000317
318 /* Reconfigure with the overridden environment variables */
xdegaye1588be62017-11-12 12:45:59 +0100319 _Py_SetLocaleFromEnv(LC_ALL);
Victor Stinner0f721472019-05-20 17:16:38 +0200320 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000321}
322#endif
323
Victor Stinner0f721472019-05-20 17:16:38 +0200324int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200325_Py_CoerceLegacyLocale(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000326{
Victor Stinner0f721472019-05-20 17:16:38 +0200327 int coerced = 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000328#ifdef PY_COERCE_C_LOCALE
Victor Stinner8ea09112018-09-03 17:05:18 +0200329 char *oldloc = NULL;
330
331 oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
332 if (oldloc == NULL) {
Victor Stinner0f721472019-05-20 17:16:38 +0200333 return coerced;
Victor Stinner8ea09112018-09-03 17:05:18 +0200334 }
335
Victor Stinner94540602017-12-16 04:54:22 +0100336 const char *locale_override = getenv("LC_ALL");
337 if (locale_override == NULL || *locale_override == '\0') {
338 /* LC_ALL is also not set (or is set to an empty string) */
339 const _LocaleCoercionTarget *target = NULL;
340 for (target = _TARGET_LOCALES; target->locale_name; target++) {
341 const char *new_locale = setlocale(LC_CTYPE,
342 target->locale_name);
343 if (new_locale != NULL) {
Victor Stinnere2510952019-05-02 11:28:57 -0400344#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94540602017-12-16 04:54:22 +0100345 /* Also ensure that nl_langinfo works in this locale */
346 char *codeset = nl_langinfo(CODESET);
347 if (!codeset || *codeset == '\0') {
348 /* CODESET is not set or empty, so skip coercion */
349 new_locale = NULL;
350 _Py_SetLocaleFromEnv(LC_CTYPE);
351 continue;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000352 }
Victor Stinner94540602017-12-16 04:54:22 +0100353#endif
354 /* Successfully configured locale, so make it the default */
Victor Stinner0f721472019-05-20 17:16:38 +0200355 coerced = _coerce_default_locale_settings(warn, target);
Victor Stinner8ea09112018-09-03 17:05:18 +0200356 goto done;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000357 }
358 }
359 }
360 /* No C locale warning here, as Py_Initialize will emit one later */
Victor Stinner8ea09112018-09-03 17:05:18 +0200361
362 setlocale(LC_CTYPE, oldloc);
363
364done:
365 PyMem_RawFree(oldloc);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000366#endif
Victor Stinner0f721472019-05-20 17:16:38 +0200367 return coerced;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000368}
369
xdegaye1588be62017-11-12 12:45:59 +0100370/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
371 * isolate the idiosyncrasies of different libc implementations. It reads the
372 * appropriate environment variable and uses its value to select the locale for
373 * 'category'. */
374char *
375_Py_SetLocaleFromEnv(int category)
376{
Victor Stinner353933e2018-11-23 13:08:26 +0100377 char *res;
xdegaye1588be62017-11-12 12:45:59 +0100378#ifdef __ANDROID__
379 const char *locale;
380 const char **pvar;
381#ifdef PY_COERCE_C_LOCALE
382 const char *coerce_c_locale;
383#endif
384 const char *utf8_locale = "C.UTF-8";
385 const char *env_var_set[] = {
386 "LC_ALL",
387 "LC_CTYPE",
388 "LANG",
389 NULL,
390 };
391
392 /* Android setlocale(category, "") doesn't check the environment variables
393 * and incorrectly sets the "C" locale at API 24 and older APIs. We only
394 * check the environment variables listed in env_var_set. */
395 for (pvar=env_var_set; *pvar; pvar++) {
396 locale = getenv(*pvar);
397 if (locale != NULL && *locale != '\0') {
398 if (strcmp(locale, utf8_locale) == 0 ||
399 strcmp(locale, "en_US.UTF-8") == 0) {
400 return setlocale(category, utf8_locale);
401 }
402 return setlocale(category, "C");
403 }
404 }
405
406 /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
407 * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
408 * Quote from POSIX section "8.2 Internationalization Variables":
409 * "4. If the LANG environment variable is not set or is set to the empty
410 * string, the implementation-defined default locale shall be used." */
411
412#ifdef PY_COERCE_C_LOCALE
413 coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
414 if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
415 /* Some other ported code may check the environment variables (e.g. in
416 * extension modules), so we make sure that they match the locale
417 * configuration */
418 if (setenv("LC_CTYPE", utf8_locale, 1)) {
419 fprintf(stderr, "Warning: failed setting the LC_CTYPE "
420 "environment variable to %s\n", utf8_locale);
421 }
422 }
423#endif
Victor Stinner353933e2018-11-23 13:08:26 +0100424 res = setlocale(category, utf8_locale);
425#else /* !defined(__ANDROID__) */
426 res = setlocale(category, "");
427#endif
428 _Py_ResetForceASCII();
429 return res;
xdegaye1588be62017-11-12 12:45:59 +0100430}
431
Nick Coghlan6ea41862017-06-11 13:16:15 +1000432
Victor Stinner048a3562020-11-05 00:45:56 +0100433static int
Victor Stinner9e1b8282020-11-10 13:21:52 +0100434interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
Victor Stinner048a3562020-11-05 00:45:56 +0100435{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100436 const PyConfig *config = &tstate->interp->config;
Victor Stinner048a3562020-11-05 00:45:56 +0100437
Victor Stinner9e1b8282020-11-10 13:21:52 +0100438 if (!only_update_path_config) {
439 PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
440 if (_PyStatus_EXCEPTION(status)) {
441 _PyErr_SetFromPyStatus(status);
442 return -1;
443 }
Victor Stinner048a3562020-11-05 00:45:56 +0100444 }
445
Victor Stinner9e1b8282020-11-10 13:21:52 +0100446 if (_Py_IsMainInterpreter(tstate)) {
447 PyStatus status = _PyConfig_WritePathConfig(config);
Victor Stinner048a3562020-11-05 00:45:56 +0100448 if (_PyStatus_EXCEPTION(status)) {
449 _PyErr_SetFromPyStatus(status);
450 return -1;
451 }
452 }
453
454 // Update the sys module for the new configuration
455 if (_PySys_UpdateConfig(tstate) < 0) {
456 return -1;
457 }
458 return 0;
459}
460
461
462int
463_PyInterpreterState_SetConfig(const PyConfig *src_config)
464{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100465 PyThreadState *tstate = PyThreadState_Get();
Victor Stinner048a3562020-11-05 00:45:56 +0100466 int res = -1;
467
468 PyConfig config;
469 PyConfig_InitPythonConfig(&config);
470 PyStatus status = _PyConfig_Copy(&config, src_config);
471 if (_PyStatus_EXCEPTION(status)) {
472 _PyErr_SetFromPyStatus(status);
473 goto done;
474 }
475
476 status = PyConfig_Read(&config);
477 if (_PyStatus_EXCEPTION(status)) {
478 _PyErr_SetFromPyStatus(status);
479 goto done;
480 }
481
Victor Stinner9e1b8282020-11-10 13:21:52 +0100482 status = _PyConfig_Copy(&tstate->interp->config, &config);
483 if (_PyStatus_EXCEPTION(status)) {
484 _PyErr_SetFromPyStatus(status);
485 goto done;
486 }
487
488 res = interpreter_update_config(tstate, 0);
Victor Stinner048a3562020-11-05 00:45:56 +0100489
490done:
491 PyConfig_Clear(&config);
492 return res;
493}
494
495
Eric Snow1abcf672017-05-23 21:46:51 -0700496/* Global initializations. Can be undone by Py_Finalize(). Don't
497 call this twice without an intervening Py_Finalize() call.
498
Victor Stinner331a6a52019-05-27 16:39:22 +0200499 Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
Eric Snow1abcf672017-05-23 21:46:51 -0700500 must have a corresponding call to Py_Finalize.
501
502 Locking: you must hold the interpreter lock while calling these APIs.
503 (If the lock has not yet been initialized, that's equivalent to
504 having the lock, but you cannot use multiple threads.)
505
506*/
507
Victor Stinner331a6a52019-05-27 16:39:22 +0200508static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200509pyinit_core_reconfigure(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200510 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200511 const PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200512{
Victor Stinner331a6a52019-05-27 16:39:22 +0200513 PyStatus status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100514 PyThreadState *tstate = _PyThreadState_GET();
515 if (!tstate) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200516 return _PyStatus_ERR("failed to read thread state");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100517 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200518 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100519
520 PyInterpreterState *interp = tstate->interp;
521 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200522 return _PyStatus_ERR("can't make main interpreter");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100523 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100524
Victor Stinnere81f6e62020-06-08 18:12:59 +0200525 status = _PyConfig_Write(config, runtime);
526 if (_PyStatus_EXCEPTION(status)) {
527 return status;
528 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200529
Victor Stinner048a3562020-11-05 00:45:56 +0100530 status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200531 if (_PyStatus_EXCEPTION(status)) {
532 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200533 }
Victor Stinnerda7933e2020-04-13 03:04:28 +0200534 config = _PyInterpreterState_GetConfig(interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +0200535
Victor Stinner331a6a52019-05-27 16:39:22 +0200536 if (config->_install_importlib) {
Victor Stinner12f2f172019-09-26 15:51:50 +0200537 status = _PyConfig_WritePathConfig(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200538 if (_PyStatus_EXCEPTION(status)) {
539 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200540 }
541 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200542 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200543}
544
545
Victor Stinner331a6a52019-05-27 16:39:22 +0200546static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200547pycore_init_runtime(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200548 const PyConfig *config)
Nick Coghland6009512014-11-20 21:39:37 +1000549{
Victor Stinner43125222019-04-24 18:23:53 +0200550 if (runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200551 return _PyStatus_ERR("main interpreter already initialized");
Victor Stinner1dc6e392018-07-25 02:49:17 +0200552 }
Victor Stinnerda273412017-12-15 01:46:02 +0100553
Victor Stinnere81f6e62020-06-08 18:12:59 +0200554 PyStatus status = _PyConfig_Write(config, runtime);
555 if (_PyStatus_EXCEPTION(status)) {
556 return status;
557 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600558
Eric Snow1abcf672017-05-23 21:46:51 -0700559 /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
560 * threads behave a little more gracefully at interpreter shutdown.
561 * We clobber it here so the new interpreter can start with a clean
562 * slate.
563 *
564 * However, this may still lead to misbehaviour if there are daemon
565 * threads still hanging around from a previous Py_Initialize/Finalize
566 * pair :(
567 */
Victor Stinner7b3c2522020-03-07 00:24:23 +0100568 _PyRuntimeState_SetFinalizing(runtime, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600569
Victor Stinnere81f6e62020-06-08 18:12:59 +0200570 status = _Py_HashRandomization_Init(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200571 if (_PyStatus_EXCEPTION(status)) {
572 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800573 }
574
Victor Stinner331a6a52019-05-27 16:39:22 +0200575 status = _PyInterpreterState_Enable(runtime);
576 if (_PyStatus_EXCEPTION(status)) {
577 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800578 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200579 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100580}
Victor Stinnera7368ac2017-11-15 18:11:45 -0800581
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100582
Victor Stinner331a6a52019-05-27 16:39:22 +0200583static PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200584init_interp_create_gil(PyThreadState *tstate)
585{
586 PyStatus status;
587
588 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
589 only called here. */
590 _PyEval_FiniGIL(tstate);
591
592 /* Auto-thread-state API */
593 status = _PyGILState_Init(tstate);
594 if (_PyStatus_EXCEPTION(status)) {
595 return status;
596 }
597
598 /* Create the GIL and take it */
599 status = _PyEval_InitGIL(tstate);
600 if (_PyStatus_EXCEPTION(status)) {
601 return status;
602 }
603
604 return _PyStatus_OK();
605}
606
607
608static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200609pycore_create_interpreter(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200610 const PyConfig *config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200611 PyThreadState **tstate_p)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100612{
613 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnerda273412017-12-15 01:46:02 +0100614 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200615 return _PyStatus_ERR("can't make main interpreter");
Victor Stinnerda273412017-12-15 01:46:02 +0100616 }
617
Victor Stinner048a3562020-11-05 00:45:56 +0100618 PyStatus status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200619 if (_PyStatus_EXCEPTION(status)) {
620 return status;
Victor Stinnerda273412017-12-15 01:46:02 +0100621 }
Nick Coghland6009512014-11-20 21:39:37 +1000622
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200623 PyThreadState *tstate = PyThreadState_New(interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +0200624 if (tstate == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200625 return _PyStatus_ERR("can't make first thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +0200626 }
Nick Coghland6009512014-11-20 21:39:37 +1000627 (void) PyThreadState_Swap(tstate);
628
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200629 status = init_interp_create_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100630 if (_PyStatus_EXCEPTION(status)) {
631 return status;
632 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100633
Victor Stinnerb45d2592019-06-20 00:05:23 +0200634 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +0200635 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100636}
Nick Coghland6009512014-11-20 21:39:37 +1000637
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100638
Victor Stinner331a6a52019-05-27 16:39:22 +0200639static PyStatus
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100640pycore_init_types(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100641{
Victor Stinner444b39b2019-11-20 01:18:11 +0100642 PyStatus status;
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100643 int is_main_interp = _Py_IsMainInterpreter(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100644
Victor Stinner01b1cc12019-11-20 02:27:56 +0100645 status = _PyGC_Init(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100646 if (_PyStatus_EXCEPTION(status)) {
647 return status;
648 }
649
Victor Stinner0430dfa2020-06-24 15:21:54 +0200650 // Create the empty tuple singleton. It must be created before the first
651 // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
652 // for example.
653 status = _PyTuple_Init(tstate);
654 if (_PyStatus_EXCEPTION(status)) {
655 return status;
656 }
657
Victor Stinnere7e699e2019-11-20 12:08:13 +0100658 if (is_main_interp) {
659 status = _PyTypes_Init();
660 if (_PyStatus_EXCEPTION(status)) {
661 return status;
662 }
Victor Stinner630c8df2019-12-17 13:02:18 +0100663 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100664
Victor Stinner630c8df2019-12-17 13:02:18 +0100665 if (!_PyLong_Init(tstate)) {
666 return _PyStatus_ERR("can't init longs");
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100667 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100668
Victor Stinnerf363d0a2020-06-24 00:10:40 +0200669 status = _PyUnicode_Init(tstate);
670 if (_PyStatus_EXCEPTION(status)) {
671 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100672 }
673
Victor Stinner91698d82020-06-25 14:07:40 +0200674 status = _PyBytes_Init(tstate);
675 if (_PyStatus_EXCEPTION(status)) {
676 return status;
677 }
678
Victor Stinner281cce12020-06-23 22:55:46 +0200679 status = _PyExc_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200680 if (_PyStatus_EXCEPTION(status)) {
681 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100682 }
683
Victor Stinnere7e699e2019-11-20 12:08:13 +0100684 if (is_main_interp) {
685 if (!_PyFloat_Init()) {
686 return _PyStatus_ERR("can't init float");
687 }
Nick Coghland6009512014-11-20 21:39:37 +1000688
Victor Stinnere7e699e2019-11-20 12:08:13 +0100689 if (_PyStructSequence_Init() < 0) {
690 return _PyStatus_ERR("can't initialize structseq");
691 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100692 }
Victor Stinneref9d9b62019-05-22 11:28:22 +0200693
Victor Stinner331a6a52019-05-27 16:39:22 +0200694 status = _PyErr_Init();
695 if (_PyStatus_EXCEPTION(status)) {
696 return status;
Victor Stinneref9d9b62019-05-22 11:28:22 +0200697 }
698
Victor Stinnere7e699e2019-11-20 12:08:13 +0100699 if (is_main_interp) {
700 if (!_PyContext_Init()) {
701 return _PyStatus_ERR("can't init context");
702 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100703 }
704
Victor Stinneref75a622020-11-12 15:14:13 +0100705 if (_PyWarnings_InitState(tstate) < 0) {
706 return _PyStatus_ERR("can't initialize warnings");
707 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200708 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100709}
710
711
Victor Stinner331a6a52019-05-27 16:39:22 +0200712static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200713pycore_init_builtins(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100714{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100715 assert(!_PyErr_Occurred(tstate));
716
Victor Stinnerb45d2592019-06-20 00:05:23 +0200717 PyObject *bimod = _PyBuiltin_Init(tstate);
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100718 if (bimod == NULL) {
Victor Stinner2582d462019-11-22 19:24:49 +0100719 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100720 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100721
Victor Stinner2582d462019-11-22 19:24:49 +0100722 PyInterpreterState *interp = tstate->interp;
723 if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
724 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100725 }
Victor Stinner2582d462019-11-22 19:24:49 +0100726
727 PyObject *builtins_dict = PyModule_GetDict(bimod);
728 if (builtins_dict == NULL) {
729 goto error;
730 }
731 Py_INCREF(builtins_dict);
732 interp->builtins = builtins_dict;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100733
Victor Stinner331a6a52019-05-27 16:39:22 +0200734 PyStatus status = _PyBuiltins_AddExceptions(bimod);
735 if (_PyStatus_EXCEPTION(status)) {
736 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100737 }
Victor Stinner2582d462019-11-22 19:24:49 +0100738
739 interp->builtins_copy = PyDict_Copy(interp->builtins);
740 if (interp->builtins_copy == NULL) {
741 goto error;
742 }
Pablo Galindob96c6b02019-12-04 11:19:59 +0000743 Py_DECREF(bimod);
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100744
745 assert(!_PyErr_Occurred(tstate));
746
Victor Stinner331a6a52019-05-27 16:39:22 +0200747 return _PyStatus_OK();
Victor Stinner2582d462019-11-22 19:24:49 +0100748
749error:
Pablo Galindob96c6b02019-12-04 11:19:59 +0000750 Py_XDECREF(bimod);
Victor Stinner2582d462019-11-22 19:24:49 +0100751 return _PyStatus_ERR("can't initialize builtins module");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100752}
753
754
Victor Stinner331a6a52019-05-27 16:39:22 +0200755static PyStatus
Victor Stinnerd863ade2019-12-06 03:37:07 +0100756pycore_interp_init(PyThreadState *tstate)
757{
758 PyStatus status;
Victor Stinner080ee5a2019-12-08 21:55:58 +0100759 PyObject *sysmod = NULL;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100760
761 status = pycore_init_types(tstate);
762 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100763 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100764 }
765
Victor Stinneref75a622020-11-12 15:14:13 +0100766 if (_Py_IsMainInterpreter(tstate)) {
767 if (_PyTime_Init() < 0) {
768 return _PyStatus_ERR("can't initialize time");
769 }
770 }
771
Victor Stinnerd863ade2019-12-06 03:37:07 +0100772 status = _PySys_Create(tstate, &sysmod);
773 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100774 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100775 }
776
777 status = pycore_init_builtins(tstate);
778 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100779 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100780 }
781
Victor Stinneref75a622020-11-12 15:14:13 +0100782 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
783 if (config->_install_importlib) {
784 /* This call sets up builtin and frozen import support */
785 if (init_importlib(tstate, sysmod) < 0) {
786 return _PyStatus_ERR("failed to initialize importlib");
787 }
788 }
Victor Stinner080ee5a2019-12-08 21:55:58 +0100789
790done:
791 /* sys.modules['sys'] contains a strong reference to the module */
792 Py_XDECREF(sysmod);
793 return status;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100794}
795
796
797static PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +0200798pyinit_config(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200799 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200800 const PyConfig *config)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100801{
Victor Stinner331a6a52019-05-27 16:39:22 +0200802 PyStatus status = pycore_init_runtime(runtime, config);
803 if (_PyStatus_EXCEPTION(status)) {
804 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100805 }
806
Victor Stinnerb45d2592019-06-20 00:05:23 +0200807 PyThreadState *tstate;
808 status = pycore_create_interpreter(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200809 if (_PyStatus_EXCEPTION(status)) {
810 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100811 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200812 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100813
Victor Stinnerd863ade2019-12-06 03:37:07 +0100814 status = pycore_interp_init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200815 if (_PyStatus_EXCEPTION(status)) {
816 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100817 }
Eric Snow1abcf672017-05-23 21:46:51 -0700818
819 /* Only when we get here is the runtime core fully initialized */
Victor Stinner43125222019-04-24 18:23:53 +0200820 runtime->core_initialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200821 return _PyStatus_OK();
Eric Snow1abcf672017-05-23 21:46:51 -0700822}
823
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100824
Victor Stinner331a6a52019-05-27 16:39:22 +0200825PyStatus
826_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100827{
Victor Stinner331a6a52019-05-27 16:39:22 +0200828 PyStatus status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100829
Victor Stinner6d1c4672019-05-20 11:02:00 +0200830 if (src_config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200831 return _PyStatus_ERR("preinitialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +0200832 }
833
Victor Stinner331a6a52019-05-27 16:39:22 +0200834 status = _PyRuntime_Initialize();
835 if (_PyStatus_EXCEPTION(status)) {
836 return status;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100837 }
Victor Stinner43125222019-04-24 18:23:53 +0200838 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100839
Victor Stinnerd3b90412019-09-17 23:59:51 +0200840 if (runtime->preinitialized) {
Victor Stinnerf72346c2019-03-25 17:54:58 +0100841 /* If it's already configured: ignored the new configuration */
Victor Stinner331a6a52019-05-27 16:39:22 +0200842 return _PyStatus_OK();
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100843 }
844
Victor Stinnerd3b90412019-09-17 23:59:51 +0200845 /* Note: preinitialized remains 1 on error, it is only set to 0
846 at exit on success. */
847 runtime->preinitializing = 1;
848
Victor Stinner331a6a52019-05-27 16:39:22 +0200849 PyPreConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200850
851 status = _PyPreConfig_InitFromPreConfig(&config, src_config);
852 if (_PyStatus_EXCEPTION(status)) {
853 return status;
854 }
Victor Stinnerf72346c2019-03-25 17:54:58 +0100855
Victor Stinner331a6a52019-05-27 16:39:22 +0200856 status = _PyPreConfig_Read(&config, args);
857 if (_PyStatus_EXCEPTION(status)) {
858 return status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100859 }
860
Victor Stinner331a6a52019-05-27 16:39:22 +0200861 status = _PyPreConfig_Write(&config);
862 if (_PyStatus_EXCEPTION(status)) {
863 return status;
Victor Stinnerf72346c2019-03-25 17:54:58 +0100864 }
865
Victor Stinnerd3b90412019-09-17 23:59:51 +0200866 runtime->preinitializing = 0;
867 runtime->preinitialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200868 return _PyStatus_OK();
Victor Stinnerf72346c2019-03-25 17:54:58 +0100869}
870
Victor Stinner70005ac2019-05-02 15:25:34 -0400871
Victor Stinner331a6a52019-05-27 16:39:22 +0200872PyStatus
873Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
Victor Stinnerf72346c2019-03-25 17:54:58 +0100874{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100875 _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400876 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinnerf29084d2019-03-20 02:20:13 +0100877}
878
879
Victor Stinner331a6a52019-05-27 16:39:22 +0200880PyStatus
881Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
Victor Stinner20004952019-03-26 02:31:11 +0100882{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100883 _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400884 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinner20004952019-03-26 02:31:11 +0100885}
886
887
Victor Stinner331a6a52019-05-27 16:39:22 +0200888PyStatus
889Py_PreInitialize(const PyPreConfig *src_config)
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100890{
Victor Stinner70005ac2019-05-02 15:25:34 -0400891 return _Py_PreInitializeFromPyArgv(src_config, NULL);
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100892}
893
894
Victor Stinner331a6a52019-05-27 16:39:22 +0200895PyStatus
896_Py_PreInitializeFromConfig(const PyConfig *config,
897 const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100898{
Victor Stinner331a6a52019-05-27 16:39:22 +0200899 assert(config != NULL);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200900
Victor Stinner331a6a52019-05-27 16:39:22 +0200901 PyStatus status = _PyRuntime_Initialize();
902 if (_PyStatus_EXCEPTION(status)) {
903 return status;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200904 }
905 _PyRuntimeState *runtime = &_PyRuntime;
906
Victor Stinnerd3b90412019-09-17 23:59:51 +0200907 if (runtime->preinitialized) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200908 /* Already initialized: do nothing */
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 return _PyStatus_OK();
Victor Stinner70005ac2019-05-02 15:25:34 -0400910 }
Victor Stinnercab5d072019-05-17 19:01:14 +0200911
Victor Stinner331a6a52019-05-27 16:39:22 +0200912 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200913
Victor Stinner3c30a762019-10-01 10:56:37 +0200914 _PyPreConfig_InitFromConfig(&preconfig, config);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200915
Victor Stinner331a6a52019-05-27 16:39:22 +0200916 if (!config->parse_argv) {
917 return Py_PreInitialize(&preconfig);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200918 }
919 else if (args == NULL) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200920 _PyArgv config_args = {
921 .use_bytes_argv = 0,
Victor Stinner331a6a52019-05-27 16:39:22 +0200922 .argc = config->argv.length,
923 .wchar_argv = config->argv.items};
Victor Stinner6d1c4672019-05-20 11:02:00 +0200924 return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200925 }
926 else {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200927 return _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200928 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100929}
930
931
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100932/* Begin interpreter initialization
933 *
934 * On return, the first thread and interpreter state have been created,
935 * but the compiler, signal handling, multithreading and
936 * multiple interpreter support, and codec infrastructure are not yet
937 * available.
938 *
939 * The import system will support builtin and frozen modules only.
940 * The only supported io is writing to sys.stderr
941 *
942 * If any operation invoked by this function fails, a fatal error is
943 * issued and the function does not return.
944 *
945 * Any code invoked from this function should *not* assume it has access
946 * to the Python C API (unless the API is explicitly listed as being
947 * safe to call without calling Py_Initialize first)
948 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200949static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200950pyinit_core(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200951 const PyConfig *src_config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200952 PyThreadState **tstate_p)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200953{
Victor Stinner331a6a52019-05-27 16:39:22 +0200954 PyStatus status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200955
Victor Stinner331a6a52019-05-27 16:39:22 +0200956 status = _Py_PreInitializeFromConfig(src_config, NULL);
957 if (_PyStatus_EXCEPTION(status)) {
958 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200959 }
960
Victor Stinner331a6a52019-05-27 16:39:22 +0200961 PyConfig config;
Victor Stinner048a3562020-11-05 00:45:56 +0100962 PyConfig_InitPythonConfig(&config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200963
Victor Stinner331a6a52019-05-27 16:39:22 +0200964 status = _PyConfig_Copy(&config, src_config);
965 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200966 goto done;
967 }
968
Victor Stinner9e1b8282020-11-10 13:21:52 +0100969 // Read the configuration, but don't compute the path configuration
970 // (it is computed in the main init).
971 status = _PyConfig_Read(&config, 0);
Victor Stinner331a6a52019-05-27 16:39:22 +0200972 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200973 goto done;
974 }
975
976 if (!runtime->core_initialized) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200977 status = pyinit_config(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200978 }
979 else {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200980 status = pyinit_core_reconfigure(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200981 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200982 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200983 goto done;
984 }
985
986done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200987 PyConfig_Clear(&config);
988 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200989}
990
Victor Stinner5ac27a52019-03-27 13:40:14 +0100991
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200992/* Py_Initialize() has already been called: update the main interpreter
993 configuration. Example of bpo-34008: Py_Main() called after
994 Py_Initialize(). */
Victor Stinner331a6a52019-05-27 16:39:22 +0200995static PyStatus
Victor Stinneraf1d64d2020-11-04 17:34:34 +0100996pyinit_main_reconfigure(PyThreadState *tstate)
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200997{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100998 if (interpreter_update_config(tstate, 0) < 0) {
999 return _PyStatus_ERR("fail to reconfigure Python");
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001000 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001001 return _PyStatus_OK();
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001002}
1003
Victor Stinnerb0051362019-11-22 17:52:42 +01001004
1005static PyStatus
1006init_interp_main(PyThreadState *tstate)
1007{
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001008 assert(!_PyErr_Occurred(tstate));
1009
Victor Stinnerb0051362019-11-22 17:52:42 +01001010 PyStatus status;
1011 int is_main_interp = _Py_IsMainInterpreter(tstate);
1012 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +02001013 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
Victor Stinnerb0051362019-11-22 17:52:42 +01001014
1015 if (!config->_install_importlib) {
1016 /* Special mode for freeze_importlib: run with no import system
1017 *
1018 * This means anything which needs support from extension modules
1019 * or pure Python code in the standard library won't work.
1020 */
1021 if (is_main_interp) {
1022 interp->runtime->initialized = 1;
1023 }
1024 return _PyStatus_OK();
1025 }
1026
Victor Stinner9e1b8282020-11-10 13:21:52 +01001027 // Compute the path configuration
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001028 status = _PyConfig_InitPathConfig(&interp->config, 1);
Victor Stinner9e1b8282020-11-10 13:21:52 +01001029 if (_PyStatus_EXCEPTION(status)) {
1030 return status;
1031 }
1032
Victor Stinner9e1b8282020-11-10 13:21:52 +01001033 if (interpreter_update_config(tstate, 1) < 0) {
1034 return _PyStatus_ERR("failed to update the Python config");
Victor Stinnerb0051362019-11-22 17:52:42 +01001035 }
1036
1037 status = init_importlib_external(tstate);
1038 if (_PyStatus_EXCEPTION(status)) {
1039 return status;
1040 }
1041
1042 if (is_main_interp) {
1043 /* initialize the faulthandler module */
1044 status = _PyFaulthandler_Init(config->faulthandler);
1045 if (_PyStatus_EXCEPTION(status)) {
1046 return status;
1047 }
1048 }
1049
1050 status = _PyUnicode_InitEncodings(tstate);
1051 if (_PyStatus_EXCEPTION(status)) {
1052 return status;
1053 }
1054
1055 if (is_main_interp) {
1056 if (config->install_signal_handlers) {
1057 status = init_signals(tstate);
1058 if (_PyStatus_EXCEPTION(status)) {
1059 return status;
1060 }
1061 }
1062
1063 if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1064 return _PyStatus_ERR("can't initialize tracemalloc");
1065 }
1066 }
1067
1068 status = init_sys_streams(tstate);
1069 if (_PyStatus_EXCEPTION(status)) {
1070 return status;
1071 }
1072
Andy Lester75cd5bf2020-03-12 02:49:05 -05001073 status = init_set_builtins_open();
Victor Stinnerb0051362019-11-22 17:52:42 +01001074 if (_PyStatus_EXCEPTION(status)) {
1075 return status;
1076 }
1077
1078 status = add_main_module(interp);
1079 if (_PyStatus_EXCEPTION(status)) {
1080 return status;
1081 }
1082
1083 if (is_main_interp) {
1084 /* Initialize warnings. */
1085 PyObject *warnoptions = PySys_GetObject("warnoptions");
1086 if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1087 {
1088 PyObject *warnings_module = PyImport_ImportModule("warnings");
1089 if (warnings_module == NULL) {
1090 fprintf(stderr, "'import warnings' failed; traceback:\n");
1091 _PyErr_Print(tstate);
1092 }
1093 Py_XDECREF(warnings_module);
1094 }
1095
1096 interp->runtime->initialized = 1;
1097 }
1098
1099 if (config->site_import) {
1100 status = init_import_site();
1101 if (_PyStatus_EXCEPTION(status)) {
1102 return status;
1103 }
1104 }
1105
1106 if (is_main_interp) {
1107#ifndef MS_WINDOWS
1108 emit_stderr_warning_for_legacy_locale(interp->runtime);
1109#endif
1110 }
1111
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001112 assert(!_PyErr_Occurred(tstate));
1113
Victor Stinnerb0051362019-11-22 17:52:42 +01001114 return _PyStatus_OK();
1115}
1116
1117
Eric Snowc7ec9982017-05-23 23:00:52 -07001118/* Update interpreter state based on supplied configuration settings
1119 *
1120 * After calling this function, most of the restrictions on the interpreter
1121 * are lifted. The only remaining incomplete settings are those related
1122 * to the main module (sys.argv[0], __main__ metadata)
1123 *
1124 * Calling this when the interpreter is not initializing, is already
1125 * initialized or without a valid current thread state is a fatal error.
1126 * Other errors should be reported as normal Python exceptions with a
1127 * non-zero return code.
1128 */
Victor Stinner331a6a52019-05-27 16:39:22 +02001129static PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +01001130pyinit_main(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -07001131{
Victor Stinnerb0051362019-11-22 17:52:42 +01001132 PyInterpreterState *interp = tstate->interp;
1133 if (!interp->runtime->core_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001134 return _PyStatus_ERR("runtime core not initialized");
Eric Snowc7ec9982017-05-23 23:00:52 -07001135 }
Eric Snowc7ec9982017-05-23 23:00:52 -07001136
Victor Stinnerb0051362019-11-22 17:52:42 +01001137 if (interp->runtime->initialized) {
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001138 return pyinit_main_reconfigure(tstate);
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001139 }
1140
Victor Stinnerb0051362019-11-22 17:52:42 +01001141 PyStatus status = init_interp_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001142 if (_PyStatus_EXCEPTION(status)) {
1143 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001144 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001145 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001146}
1147
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001148
Victor Stinner331a6a52019-05-27 16:39:22 +02001149PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +02001150Py_InitializeFromConfig(const PyConfig *config)
Eric Snow1abcf672017-05-23 21:46:51 -07001151{
Victor Stinner6d1c4672019-05-20 11:02:00 +02001152 if (config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001153 return _PyStatus_ERR("initialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +02001154 }
1155
Victor Stinner331a6a52019-05-27 16:39:22 +02001156 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001157
Victor Stinner331a6a52019-05-27 16:39:22 +02001158 status = _PyRuntime_Initialize();
1159 if (_PyStatus_EXCEPTION(status)) {
1160 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001161 }
1162 _PyRuntimeState *runtime = &_PyRuntime;
1163
Victor Stinnerb45d2592019-06-20 00:05:23 +02001164 PyThreadState *tstate = NULL;
1165 status = pyinit_core(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001166 if (_PyStatus_EXCEPTION(status)) {
1167 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001168 }
Victor Stinnerda7933e2020-04-13 03:04:28 +02001169 config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerbc8ac6b2017-11-30 18:03:55 +01001170
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001171 if (config->_init_main) {
Victor Stinner01b1cc12019-11-20 02:27:56 +01001172 status = pyinit_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001173 if (_PyStatus_EXCEPTION(status)) {
1174 return status;
Victor Stinner484f20d2019-03-27 02:04:16 +01001175 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001176 }
Victor Stinner484f20d2019-03-27 02:04:16 +01001177
Victor Stinner331a6a52019-05-27 16:39:22 +02001178 return _PyStatus_OK();
Victor Stinner5ac27a52019-03-27 13:40:14 +01001179}
1180
1181
Eric Snow1abcf672017-05-23 21:46:51 -07001182void
Nick Coghland6009512014-11-20 21:39:37 +10001183Py_InitializeEx(int install_sigs)
1184{
Victor Stinner331a6a52019-05-27 16:39:22 +02001185 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001186
Victor Stinner331a6a52019-05-27 16:39:22 +02001187 status = _PyRuntime_Initialize();
1188 if (_PyStatus_EXCEPTION(status)) {
1189 Py_ExitStatusException(status);
Victor Stinner43125222019-04-24 18:23:53 +02001190 }
1191 _PyRuntimeState *runtime = &_PyRuntime;
1192
1193 if (runtime->initialized) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001194 /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1195 return;
1196 }
1197
Victor Stinner331a6a52019-05-27 16:39:22 +02001198 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001199 _PyConfig_InitCompatConfig(&config);
Victor Stinner441b10c2019-09-28 04:28:35 +02001200
Victor Stinner1dc6e392018-07-25 02:49:17 +02001201 config.install_signal_handlers = install_sigs;
1202
Victor Stinner331a6a52019-05-27 16:39:22 +02001203 status = Py_InitializeFromConfig(&config);
1204 if (_PyStatus_EXCEPTION(status)) {
1205 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001206 }
Nick Coghland6009512014-11-20 21:39:37 +10001207}
1208
1209void
1210Py_Initialize(void)
1211{
1212 Py_InitializeEx(1);
1213}
1214
1215
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001216PyStatus
1217_Py_InitializeMain(void)
1218{
1219 PyStatus status = _PyRuntime_Initialize();
1220 if (_PyStatus_EXCEPTION(status)) {
1221 return status;
1222 }
1223 _PyRuntimeState *runtime = &_PyRuntime;
1224 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1225 return pyinit_main(tstate);
1226}
1227
1228
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001229static void
1230finalize_modules_delete_special(PyThreadState *tstate, int verbose)
1231{
1232 // List of names to clear in sys
1233 static const char * const sys_deletes[] = {
1234 "path", "argv", "ps1", "ps2",
1235 "last_type", "last_value", "last_traceback",
1236 "path_hooks", "path_importer_cache", "meta_path",
1237 "__interactivehook__",
1238 NULL
1239 };
1240
1241 static const char * const sys_files[] = {
1242 "stdin", "__stdin__",
1243 "stdout", "__stdout__",
1244 "stderr", "__stderr__",
1245 NULL
1246 };
1247
1248 PyInterpreterState *interp = tstate->interp;
1249 if (verbose) {
1250 PySys_WriteStderr("# clear builtins._\n");
1251 }
1252 if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
1253 PyErr_WriteUnraisable(NULL);
1254 }
1255
1256 const char * const *p;
1257 for (p = sys_deletes; *p != NULL; p++) {
1258 if (verbose) {
1259 PySys_WriteStderr("# clear sys.%s\n", *p);
1260 }
1261 if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
1262 PyErr_WriteUnraisable(NULL);
1263 }
1264 }
1265 for (p = sys_files; *p != NULL; p+=2) {
1266 const char *name = p[0];
1267 const char *orig_name = p[1];
1268 if (verbose) {
1269 PySys_WriteStderr("# restore sys.%s\n", name);
1270 }
1271 PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
1272 orig_name);
1273 if (value == NULL) {
1274 if (_PyErr_Occurred(tstate)) {
1275 PyErr_WriteUnraisable(NULL);
1276 }
1277 value = Py_None;
1278 }
1279 if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
1280 PyErr_WriteUnraisable(NULL);
1281 }
1282 }
1283}
1284
1285
1286static PyObject*
1287finalize_remove_modules(PyObject *modules, int verbose)
1288{
1289 PyObject *weaklist = PyList_New(0);
1290 if (weaklist == NULL) {
1291 PyErr_WriteUnraisable(NULL);
1292 }
1293
1294#define STORE_MODULE_WEAKREF(name, mod) \
1295 if (weaklist != NULL) { \
1296 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1297 if (wr) { \
1298 PyObject *tup = PyTuple_Pack(2, name, wr); \
1299 if (!tup || PyList_Append(weaklist, tup) < 0) { \
1300 PyErr_WriteUnraisable(NULL); \
1301 } \
1302 Py_XDECREF(tup); \
1303 Py_DECREF(wr); \
1304 } \
1305 else { \
1306 PyErr_WriteUnraisable(NULL); \
1307 } \
1308 }
1309
1310#define CLEAR_MODULE(name, mod) \
1311 if (PyModule_Check(mod)) { \
1312 if (verbose && PyUnicode_Check(name)) { \
1313 PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
1314 } \
1315 STORE_MODULE_WEAKREF(name, mod); \
1316 if (PyObject_SetItem(modules, name, Py_None) < 0) { \
1317 PyErr_WriteUnraisable(NULL); \
1318 } \
1319 }
1320
1321 if (PyDict_CheckExact(modules)) {
1322 Py_ssize_t pos = 0;
1323 PyObject *key, *value;
1324 while (PyDict_Next(modules, &pos, &key, &value)) {
1325 CLEAR_MODULE(key, value);
1326 }
1327 }
1328 else {
1329 PyObject *iterator = PyObject_GetIter(modules);
1330 if (iterator == NULL) {
1331 PyErr_WriteUnraisable(NULL);
1332 }
1333 else {
1334 PyObject *key;
1335 while ((key = PyIter_Next(iterator))) {
1336 PyObject *value = PyObject_GetItem(modules, key);
1337 if (value == NULL) {
1338 PyErr_WriteUnraisable(NULL);
1339 continue;
1340 }
1341 CLEAR_MODULE(key, value);
1342 Py_DECREF(value);
1343 Py_DECREF(key);
1344 }
1345 if (PyErr_Occurred()) {
1346 PyErr_WriteUnraisable(NULL);
1347 }
1348 Py_DECREF(iterator);
1349 }
1350 }
1351#undef CLEAR_MODULE
1352#undef STORE_MODULE_WEAKREF
1353
1354 return weaklist;
1355}
1356
1357
1358static void
1359finalize_clear_modules_dict(PyObject *modules)
1360{
1361 if (PyDict_CheckExact(modules)) {
1362 PyDict_Clear(modules);
1363 }
1364 else {
1365 _Py_IDENTIFIER(clear);
1366 if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) {
1367 PyErr_WriteUnraisable(NULL);
1368 }
1369 }
1370}
1371
1372
1373static void
1374finalize_restore_builtins(PyThreadState *tstate)
1375{
1376 PyInterpreterState *interp = tstate->interp;
1377 PyObject *dict = PyDict_Copy(interp->builtins);
1378 if (dict == NULL) {
1379 PyErr_WriteUnraisable(NULL);
1380 }
1381 PyDict_Clear(interp->builtins);
1382 if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
1383 _PyErr_Clear(tstate);
1384 }
1385 Py_XDECREF(dict);
1386}
1387
1388
1389static void
1390finalize_modules_clear_weaklist(PyInterpreterState *interp,
1391 PyObject *weaklist, int verbose)
1392{
1393 // First clear modules imported later
1394 for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
1395 PyObject *tup = PyList_GET_ITEM(weaklist, i);
1396 PyObject *name = PyTuple_GET_ITEM(tup, 0);
1397 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
1398 if (mod == Py_None) {
1399 continue;
1400 }
1401 assert(PyModule_Check(mod));
1402 PyObject *dict = PyModule_GetDict(mod);
1403 if (dict == interp->builtins || dict == interp->sysdict) {
1404 continue;
1405 }
1406 Py_INCREF(mod);
1407 if (verbose && PyUnicode_Check(name)) {
1408 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
1409 }
1410 _PyModule_Clear(mod);
1411 Py_DECREF(mod);
1412 }
1413}
1414
1415
1416static void
1417finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
1418{
1419 // Clear sys dict
1420 if (verbose) {
1421 PySys_FormatStderr("# cleanup[3] wiping sys\n");
1422 }
1423 _PyModule_ClearDict(interp->sysdict);
1424
1425 // Clear builtins dict
1426 if (verbose) {
1427 PySys_FormatStderr("# cleanup[3] wiping builtins\n");
1428 }
1429 _PyModule_ClearDict(interp->builtins);
1430}
1431
1432
1433/* Clear modules, as good as we can */
1434static void
1435finalize_modules(PyThreadState *tstate)
1436{
1437 PyInterpreterState *interp = tstate->interp;
1438 PyObject *modules = interp->modules;
1439 if (modules == NULL) {
1440 // Already done
1441 return;
1442 }
1443 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
1444
1445 // Delete some special builtins._ and sys attributes first. These are
1446 // common places where user values hide and people complain when their
1447 // destructors fail. Since the modules containing them are
1448 // deleted *last* of all, they would come too late in the normal
1449 // destruction order. Sigh.
1450 //
1451 // XXX Perhaps these precautions are obsolete. Who knows?
1452 finalize_modules_delete_special(tstate, verbose);
1453
1454 // Remove all modules from sys.modules, hoping that garbage collection
1455 // can reclaim most of them: set all sys.modules values to None.
1456 //
1457 // We prepare a list which will receive (name, weakref) tuples of
1458 // modules when they are removed from sys.modules. The name is used
1459 // for diagnosis messages (in verbose mode), while the weakref helps
1460 // detect those modules which have been held alive.
1461 PyObject *weaklist = finalize_remove_modules(modules, verbose);
1462
1463 // Clear the modules dict
1464 finalize_clear_modules_dict(modules);
1465
1466 // Restore the original builtins dict, to ensure that any
1467 // user data gets cleared.
1468 finalize_restore_builtins(tstate);
1469
1470 // Collect garbage
1471 _PyGC_CollectNoFail(tstate);
1472
1473 // Dump GC stats before it's too late, since it uses the warnings
1474 // machinery.
1475 _PyGC_DumpShutdownStats(tstate);
1476
1477 if (weaklist != NULL) {
1478 // Now, if there are any modules left alive, clear their globals to
1479 // minimize potential leaks. All C extension modules actually end
1480 // up here, since they are kept alive in the interpreter state.
1481 //
1482 // The special treatment of "builtins" here is because even
1483 // when it's not referenced as a module, its dictionary is
1484 // referenced by almost every module's __builtins__. Since
1485 // deleting a module clears its dictionary (even if there are
1486 // references left to it), we need to delete the "builtins"
1487 // module last. Likewise, we don't delete sys until the very
1488 // end because it is implicitly referenced (e.g. by print).
1489 //
1490 // Since dict is ordered in CPython 3.6+, modules are saved in
1491 // importing order. First clear modules imported later.
1492 finalize_modules_clear_weaklist(interp, weaklist, verbose);
1493 Py_DECREF(weaklist);
1494 }
1495
1496 // Clear sys and builtins modules dict
1497 finalize_clear_sys_builtins_dict(interp, verbose);
1498
1499 // Clear module dict copies stored in the interpreter state:
1500 // clear PyInterpreterState.modules_by_index and
1501 // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
1502 // initialization API)
1503 _PyInterpreterState_ClearModules(interp);
1504
1505 // Clear and delete the modules directory. Actual modules will
1506 // still be there only if imported during the execution of some
1507 // destructor.
1508 Py_SETREF(interp->modules, NULL);
1509
1510 // Collect garbage once more
1511 _PyGC_CollectNoFail(tstate);
1512}
1513
1514
Nick Coghland6009512014-11-20 21:39:37 +10001515/* Flush stdout and stderr */
1516
1517static int
1518file_is_closed(PyObject *fobj)
1519{
1520 int r;
1521 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1522 if (tmp == NULL) {
1523 PyErr_Clear();
1524 return 0;
1525 }
1526 r = PyObject_IsTrue(tmp);
1527 Py_DECREF(tmp);
1528 if (r < 0)
1529 PyErr_Clear();
1530 return r > 0;
1531}
1532
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001533
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001534static int
Nick Coghland6009512014-11-20 21:39:37 +10001535flush_std_files(void)
1536{
1537 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1538 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1539 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001540 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001541
1542 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001543 tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001544 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001545 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001546 status = -1;
1547 }
Nick Coghland6009512014-11-20 21:39:37 +10001548 else
1549 Py_DECREF(tmp);
1550 }
1551
1552 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001553 tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001554 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001555 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001556 status = -1;
1557 }
Nick Coghland6009512014-11-20 21:39:37 +10001558 else
1559 Py_DECREF(tmp);
1560 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001561
1562 return status;
Nick Coghland6009512014-11-20 21:39:37 +10001563}
1564
1565/* Undo the effect of Py_Initialize().
1566
1567 Beware: if multiple interpreter and/or thread states exist, these
1568 are not wiped out; only the current thread and interpreter state
1569 are deleted. But since everything else is deleted, those other
1570 interpreter and thread states should no longer be used.
1571
1572 (XXX We should do better, e.g. wipe out all interpreters and
1573 threads.)
1574
1575 Locking: as above.
1576
1577*/
1578
Victor Stinner7eee5be2019-11-20 10:38:34 +01001579
1580static void
Victor Stinner90db4652020-07-01 23:21:36 +02001581finalize_interp_types(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001582{
Victor Stinner281cce12020-06-23 22:55:46 +02001583 _PyExc_Fini(tstate);
Victor Stinner3744ed22020-06-05 01:39:24 +02001584 _PyFrame_Fini(tstate);
Victor Stinner78a02c22020-06-05 02:34:14 +02001585 _PyAsyncGen_Fini(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001586 _PyContext_Fini(tstate);
Victor Stinner666ecfb2020-07-02 01:19:57 +02001587 _PyUnicode_ClearInterned(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001588
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001589 _PyDict_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001590 _PyList_Fini(tstate);
1591 _PyTuple_Fini(tstate);
1592
1593 _PySlice_Fini(tstate);
Victor Stinner3d483342019-11-22 12:27:50 +01001594
Victor Stinnerc41eed12020-06-23 15:54:35 +02001595 _PyBytes_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001596 _PyUnicode_Fini(tstate);
1597 _PyFloat_Fini(tstate);
1598 _PyLong_Fini(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001599}
1600
1601
1602static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001603finalize_interp_clear(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001604{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001605 int is_main_interp = _Py_IsMainInterpreter(tstate);
1606
Victor Stinner7eee5be2019-11-20 10:38:34 +01001607 /* Clear interpreter state and all thread states */
Victor Stinnereba5bf22020-10-30 22:51:02 +01001608 _PyInterpreterState_Clear(tstate);
Pablo Galindoac0e1c22019-12-04 11:51:03 +00001609
Kongedaa0fe02020-07-04 05:06:46 +08001610 /* Clear all loghooks */
1611 /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1612 Call _PySys_ClearAuditHooks when PyObject available. */
1613 if (is_main_interp) {
1614 _PySys_ClearAuditHooks(tstate);
1615 }
1616
Victor Stinner7907f8c2020-06-08 01:22:36 +02001617 if (is_main_interp) {
1618 _Py_HashRandomization_Fini();
1619 _PyArg_Fini();
1620 _Py_ClearFileSystemEncoding();
1621 }
1622
Victor Stinner90db4652020-07-01 23:21:36 +02001623 finalize_interp_types(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001624}
1625
1626
1627static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001628finalize_interp_delete(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001629{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001630 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001631 /* Cleanup auto-thread-state */
1632 _PyGILState_Fini(tstate);
1633 }
1634
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001635 /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1636 fail when it is being awaited by another running daemon thread (see
1637 bpo-9901). Instead pycore_create_interpreter() destroys the previously
1638 created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1639 called multiple times. */
1640
Victor Stinner7eee5be2019-11-20 10:38:34 +01001641 PyInterpreterState_Delete(tstate->interp);
1642}
1643
1644
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001645int
1646Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +10001647{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001648 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001649
Victor Stinner8e91c242019-04-24 17:24:01 +02001650 _PyRuntimeState *runtime = &_PyRuntime;
1651 if (!runtime->initialized) {
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001652 return status;
Victor Stinner8e91c242019-04-24 17:24:01 +02001653 }
Nick Coghland6009512014-11-20 21:39:37 +10001654
Victor Stinnere225beb2019-06-03 18:14:24 +02001655 /* Get current thread state and interpreter pointer */
1656 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1657 PyInterpreterState *interp = tstate->interp;
Victor Stinner8e91c242019-04-24 17:24:01 +02001658
Victor Stinnerb45d2592019-06-20 00:05:23 +02001659 // Wrap up existing "threading"-module-created, non-daemon threads.
1660 wait_for_thread_shutdown(tstate);
1661
1662 // Make any remaining pending calls.
Victor Stinner2b1df452020-01-13 18:46:59 +01001663 _Py_FinishPendingCalls(tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001664
Nick Coghland6009512014-11-20 21:39:37 +10001665 /* The interpreter is still entirely intact at this point, and the
1666 * exit funcs may be relying on that. In particular, if some thread
1667 * or exit func is still waiting to do an import, the import machinery
1668 * expects Py_IsInitialized() to return true. So don't say the
Eric Snow842a2f02019-03-15 15:47:51 -06001669 * runtime is uninitialized until after the exit funcs have run.
Nick Coghland6009512014-11-20 21:39:37 +10001670 * Note that Threading.py uses an exit func to do a join on all the
1671 * threads created thru it, so this also protects pending imports in
1672 * the threads created via Threading.
1673 */
Nick Coghland6009512014-11-20 21:39:37 +10001674
Victor Stinnerb45d2592019-06-20 00:05:23 +02001675 call_py_exitfuncs(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001676
Victor Stinnerda273412017-12-15 01:46:02 +01001677 /* Copy the core config, PyInterpreterState_Delete() free
1678 the core config memory */
Victor Stinner5d862462017-12-19 11:35:58 +01001679#ifdef Py_REF_DEBUG
Victor Stinner331a6a52019-05-27 16:39:22 +02001680 int show_ref_count = interp->config.show_ref_count;
Victor Stinner5d862462017-12-19 11:35:58 +01001681#endif
1682#ifdef Py_TRACE_REFS
Victor Stinner331a6a52019-05-27 16:39:22 +02001683 int dump_refs = interp->config.dump_refs;
Victor Stinner5d862462017-12-19 11:35:58 +01001684#endif
1685#ifdef WITH_PYMALLOC
Victor Stinner331a6a52019-05-27 16:39:22 +02001686 int malloc_stats = interp->config.malloc_stats;
Victor Stinner5d862462017-12-19 11:35:58 +01001687#endif
Victor Stinner6bf992a2017-12-06 17:26:10 +01001688
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001689 /* Remaining daemon threads will automatically exit
1690 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
Victor Stinner7b3c2522020-03-07 00:24:23 +01001691 _PyRuntimeState_SetFinalizing(runtime, tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +02001692 runtime->initialized = 0;
1693 runtime->core_initialized = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001694
Victor Stinner9ad58ac2020-03-09 23:37:49 +01001695 /* Destroy the state of all threads of the interpreter, except of the
1696 current thread. In practice, only daemon threads should still be alive,
1697 except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1698 Clear frames of other threads to call objects destructors. Destructors
1699 will be called in the current Python thread. Since
1700 _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1701 can take the GIL at this point: if they try, they will exit
1702 immediately. */
1703 _PyThreadState_DeleteExcept(runtime, tstate);
1704
Victor Stinnere0deff32015-03-24 13:46:18 +01001705 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001706 if (flush_std_files() < 0) {
1707 status = -1;
1708 }
Nick Coghland6009512014-11-20 21:39:37 +10001709
1710 /* Disable signal handling */
1711 PyOS_FiniInterrupts();
1712
1713 /* Collect garbage. This may call finalizers; it's nice to call these
1714 * before all modules are destroyed.
1715 * XXX If a __del__ or weakref callback is triggered here, and tries to
1716 * XXX import a module, bad things can happen, because Python no
1717 * XXX longer believes it's initialized.
1718 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
1719 * XXX is easy to provoke that way. I've also seen, e.g.,
1720 * XXX Exception exceptions.ImportError: 'No module named sha'
1721 * XXX in <function callback at 0x008F5718> ignored
1722 * XXX but I'm unclear on exactly how that one happens. In any case,
1723 * XXX I haven't seen a real-life report of either of these.
1724 */
Victor Stinner8b341482020-10-30 17:00:00 +01001725 PyGC_Collect();
Eric Snowdae02762017-09-14 00:35:58 -07001726
Nick Coghland6009512014-11-20 21:39:37 +10001727 /* Destroy all modules */
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001728 finalize_modules(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001729
Inada Naoki91234a12019-06-03 21:30:58 +09001730 /* Print debug stats if any */
1731 _PyEval_Fini();
1732
Victor Stinnere0deff32015-03-24 13:46:18 +01001733 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001734 if (flush_std_files() < 0) {
1735 status = -1;
1736 }
Nick Coghland6009512014-11-20 21:39:37 +10001737
1738 /* Collect final garbage. This disposes of cycles created by
1739 * class definitions, for example.
1740 * XXX This is disabled because it caused too many problems. If
1741 * XXX a __del__ or weakref callback triggers here, Python code has
1742 * XXX a hard time running, because even the sys module has been
1743 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1744 * XXX One symptom is a sequence of information-free messages
1745 * XXX coming from threads (if a __del__ or callback is invoked,
1746 * XXX other threads can execute too, and any exception they encounter
1747 * XXX triggers a comedy of errors as subsystem after subsystem
1748 * XXX fails to find what it *expects* to find in sys to help report
1749 * XXX the exception and consequent unexpected failures). I've also
1750 * XXX seen segfaults then, after adding print statements to the
1751 * XXX Python code getting called.
1752 */
1753#if 0
Łukasz Langafef7e942016-09-09 21:47:46 -07001754 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +10001755#endif
1756
1757 /* Disable tracemalloc after all Python objects have been destroyed,
1758 so it is possible to use tracemalloc in objects destructor. */
1759 _PyTraceMalloc_Fini();
1760
1761 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1762 _PyImport_Fini();
1763
1764 /* Cleanup typeobject.c's internal caches. */
1765 _PyType_Fini();
1766
1767 /* unload faulthandler module */
1768 _PyFaulthandler_Fini();
1769
Nick Coghland6009512014-11-20 21:39:37 +10001770 /* dump hash stats */
1771 _PyHash_Fini();
1772
Eric Snowdae02762017-09-14 00:35:58 -07001773#ifdef Py_REF_DEBUG
Victor Stinnerda273412017-12-15 01:46:02 +01001774 if (show_ref_count) {
Victor Stinner25420fe2017-11-20 18:12:22 -08001775 _PyDebug_PrintTotalRefs();
1776 }
Eric Snowdae02762017-09-14 00:35:58 -07001777#endif
Nick Coghland6009512014-11-20 21:39:37 +10001778
1779#ifdef Py_TRACE_REFS
1780 /* Display all objects still alive -- this can invoke arbitrary
1781 * __repr__ overrides, so requires a mostly-intact interpreter.
1782 * Alas, a lot of stuff may still be alive now that will be cleaned
1783 * up later.
1784 */
Victor Stinnerda273412017-12-15 01:46:02 +01001785 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001786 _Py_PrintReferences(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001787 }
Nick Coghland6009512014-11-20 21:39:37 +10001788#endif /* Py_TRACE_REFS */
1789
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001790 finalize_interp_clear(tstate);
1791 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001792
1793#ifdef Py_TRACE_REFS
1794 /* Display addresses (& refcnts) of all objects still alive.
1795 * An address can be used to find the repr of the object, printed
1796 * above by _Py_PrintReferences.
1797 */
Victor Stinnerda273412017-12-15 01:46:02 +01001798 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001799 _Py_PrintReferenceAddresses(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001800 }
Nick Coghland6009512014-11-20 21:39:37 +10001801#endif /* Py_TRACE_REFS */
Victor Stinner34be8072016-03-14 12:04:26 +01001802#ifdef WITH_PYMALLOC
Victor Stinnerda273412017-12-15 01:46:02 +01001803 if (malloc_stats) {
Victor Stinner6bf992a2017-12-06 17:26:10 +01001804 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be8072016-03-14 12:04:26 +01001805 }
Nick Coghland6009512014-11-20 21:39:37 +10001806#endif
1807
Victor Stinner8e91c242019-04-24 17:24:01 +02001808 call_ll_exitfuncs(runtime);
Victor Stinner9316ee42017-11-25 03:17:57 +01001809
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001810 _PyRuntime_Finalize();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001811 return status;
1812}
1813
1814void
1815Py_Finalize(void)
1816{
1817 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +10001818}
1819
Victor Stinnerb0051362019-11-22 17:52:42 +01001820
Nick Coghland6009512014-11-20 21:39:37 +10001821/* Create and initialize a new interpreter and thread, and return the
1822 new thread. This requires that Py_Initialize() has been called
1823 first.
1824
1825 Unsuccessful initialization yields a NULL pointer. Note that *no*
1826 exception information is available even in this case -- the
1827 exception information is held in the thread, and there is no
1828 thread.
1829
1830 Locking: as above.
1831
1832*/
1833
Victor Stinner331a6a52019-05-27 16:39:22 +02001834static PyStatus
Victor Stinner252346a2020-05-01 11:33:44 +02001835new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
Nick Coghland6009512014-11-20 21:39:37 +10001836{
Victor Stinner331a6a52019-05-27 16:39:22 +02001837 PyStatus status;
Nick Coghland6009512014-11-20 21:39:37 +10001838
Victor Stinner331a6a52019-05-27 16:39:22 +02001839 status = _PyRuntime_Initialize();
1840 if (_PyStatus_EXCEPTION(status)) {
1841 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001842 }
1843 _PyRuntimeState *runtime = &_PyRuntime;
1844
1845 if (!runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001846 return _PyStatus_ERR("Py_Initialize must be called first");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001847 }
Nick Coghland6009512014-11-20 21:39:37 +10001848
Victor Stinner8a1be612016-03-14 22:07:55 +01001849 /* Issue #10915, #15751: The GIL API doesn't work with multiple
1850 interpreters: disable PyGILState_Check(). */
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001851 runtime->gilstate.check_enabled = 0;
Victor Stinner8a1be612016-03-14 22:07:55 +01001852
Victor Stinner43125222019-04-24 18:23:53 +02001853 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001854 if (interp == NULL) {
1855 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001856 return _PyStatus_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001857 }
Nick Coghland6009512014-11-20 21:39:37 +10001858
Victor Stinner43125222019-04-24 18:23:53 +02001859 PyThreadState *tstate = PyThreadState_New(interp);
Nick Coghland6009512014-11-20 21:39:37 +10001860 if (tstate == NULL) {
1861 PyInterpreterState_Delete(interp);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001862 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001863 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001864 }
1865
Victor Stinner43125222019-04-24 18:23:53 +02001866 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001867
Eric Snow1abcf672017-05-23 21:46:51 -07001868 /* Copy the current interpreter config into the new interpreter */
Victor Stinnerda7933e2020-04-13 03:04:28 +02001869 const PyConfig *config;
Victor Stinner7be4e352020-05-05 20:27:47 +02001870#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Eric Snow1abcf672017-05-23 21:46:51 -07001871 if (save_tstate != NULL) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001872 config = _PyInterpreterState_GetConfig(save_tstate->interp);
Victor Stinner7be4e352020-05-05 20:27:47 +02001873 }
1874 else
1875#endif
1876 {
Eric Snow1abcf672017-05-23 21:46:51 -07001877 /* No current thread state, copy from the main interpreter */
1878 PyInterpreterState *main_interp = PyInterpreterState_Main();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001879 config = _PyInterpreterState_GetConfig(main_interp);
Victor Stinnerda273412017-12-15 01:46:02 +01001880 }
1881
Victor Stinner048a3562020-11-05 00:45:56 +01001882
1883 status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001884 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001885 goto error;
Victor Stinnerda273412017-12-15 01:46:02 +01001886 }
Victor Stinner252346a2020-05-01 11:33:44 +02001887 interp->config._isolated_interpreter = isolated_subinterpreter;
Eric Snow1abcf672017-05-23 21:46:51 -07001888
Victor Stinner0dd5e7a2020-05-05 20:16:37 +02001889 status = init_interp_create_gil(tstate);
1890 if (_PyStatus_EXCEPTION(status)) {
1891 goto error;
1892 }
1893
Victor Stinnerd863ade2019-12-06 03:37:07 +01001894 status = pycore_interp_init(tstate);
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001895 if (_PyStatus_EXCEPTION(status)) {
1896 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001897 }
1898
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001899 status = init_interp_main(tstate);
1900 if (_PyStatus_EXCEPTION(status)) {
1901 goto error;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001902 }
Nick Coghland6009512014-11-20 21:39:37 +10001903
Victor Stinnera7368ac2017-11-15 18:11:45 -08001904 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +02001905 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001906
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001907error:
Victor Stinnerb0051362019-11-22 17:52:42 +01001908 *tstate_p = NULL;
1909
1910 /* Oops, it didn't work. Undo it all. */
Nick Coghland6009512014-11-20 21:39:37 +10001911 PyErr_PrintEx(0);
1912 PyThreadState_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001913 PyThreadState_Delete(tstate);
1914 PyInterpreterState_Delete(interp);
Victor Stinner9da74302019-11-20 11:17:17 +01001915 PyThreadState_Swap(save_tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001916
Victor Stinnerb0051362019-11-22 17:52:42 +01001917 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001918}
1919
1920PyThreadState *
Victor Stinner252346a2020-05-01 11:33:44 +02001921_Py_NewInterpreter(int isolated_subinterpreter)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001922{
Stéphane Wirtel9e06d2b2019-03-18 17:10:29 +01001923 PyThreadState *tstate = NULL;
Victor Stinner252346a2020-05-01 11:33:44 +02001924 PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
Victor Stinner331a6a52019-05-27 16:39:22 +02001925 if (_PyStatus_EXCEPTION(status)) {
1926 Py_ExitStatusException(status);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001927 }
1928 return tstate;
1929
Nick Coghland6009512014-11-20 21:39:37 +10001930}
1931
Victor Stinner252346a2020-05-01 11:33:44 +02001932PyThreadState *
1933Py_NewInterpreter(void)
1934{
1935 return _Py_NewInterpreter(0);
1936}
1937
Nick Coghland6009512014-11-20 21:39:37 +10001938/* Delete an interpreter and its last thread. This requires that the
1939 given thread state is current, that the thread has no remaining
1940 frames, and that it is its interpreter's only remaining thread.
1941 It is a fatal error to violate these constraints.
1942
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001943 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +10001944 everything, regardless.)
1945
1946 Locking: as above.
1947
1948*/
1949
1950void
1951Py_EndInterpreter(PyThreadState *tstate)
1952{
1953 PyInterpreterState *interp = tstate->interp;
1954
Victor Stinnerb45d2592019-06-20 00:05:23 +02001955 if (tstate != _PyThreadState_GET()) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001956 Py_FatalError("thread is not current");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001957 }
1958 if (tstate->frame != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001959 Py_FatalError("thread still has a frame");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001960 }
Eric Snow5be45a62019-03-08 22:47:07 -07001961 interp->finalizing = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001962
Eric Snow842a2f02019-03-15 15:47:51 -06001963 // Wrap up existing "threading"-module-created, non-daemon threads.
Victor Stinnerb45d2592019-06-20 00:05:23 +02001964 wait_for_thread_shutdown(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001965
Victor Stinnerb45d2592019-06-20 00:05:23 +02001966 call_py_exitfuncs(tstate);
Marcel Plch776407f2017-12-20 11:17:58 +01001967
Victor Stinnerb45d2592019-06-20 00:05:23 +02001968 if (tstate != interp->tstate_head || tstate->next != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001969 Py_FatalError("not the last thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001970 }
Nick Coghland6009512014-11-20 21:39:37 +10001971
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001972 finalize_modules(tstate);
1973
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001974 finalize_interp_clear(tstate);
1975 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001976}
1977
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001978/* Add the __main__ module */
Nick Coghland6009512014-11-20 21:39:37 +10001979
Victor Stinner331a6a52019-05-27 16:39:22 +02001980static PyStatus
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001981add_main_module(PyInterpreterState *interp)
Nick Coghland6009512014-11-20 21:39:37 +10001982{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001983 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +10001984 m = PyImport_AddModule("__main__");
1985 if (m == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +02001986 return _PyStatus_ERR("can't create __main__ module");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001987
Nick Coghland6009512014-11-20 21:39:37 +10001988 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001989 ann_dict = PyDict_New();
1990 if ((ann_dict == NULL) ||
1991 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001992 return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001993 }
1994 Py_DECREF(ann_dict);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001995
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001996 if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
1997 if (PyErr_Occurred()) {
1998 return _PyStatus_ERR("Failed to test __main__.__builtins__");
1999 }
Nick Coghland6009512014-11-20 21:39:37 +10002000 PyObject *bimod = PyImport_ImportModule("builtins");
2001 if (bimod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002002 return _PyStatus_ERR("Failed to retrieve builtins module");
Nick Coghland6009512014-11-20 21:39:37 +10002003 }
2004 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002005 return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
Nick Coghland6009512014-11-20 21:39:37 +10002006 }
2007 Py_DECREF(bimod);
2008 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002009
Nick Coghland6009512014-11-20 21:39:37 +10002010 /* Main is a little special - imp.is_builtin("__main__") will return
2011 * False, but BuiltinImporter is still the most appropriate initial
2012 * setting for its __loader__ attribute. A more suitable value will
2013 * be set if __main__ gets further initialized later in the startup
2014 * process.
2015 */
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002016 loader = _PyDict_GetItemStringWithError(d, "__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10002017 if (loader == NULL || loader == Py_None) {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002018 if (PyErr_Occurred()) {
2019 return _PyStatus_ERR("Failed to test __main__.__loader__");
2020 }
Nick Coghland6009512014-11-20 21:39:37 +10002021 PyObject *loader = PyObject_GetAttrString(interp->importlib,
2022 "BuiltinImporter");
2023 if (loader == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002024 return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
Nick Coghland6009512014-11-20 21:39:37 +10002025 }
2026 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002027 return _PyStatus_ERR("Failed to initialize __main__.__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10002028 }
2029 Py_DECREF(loader);
2030 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002031 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002032}
2033
Nick Coghland6009512014-11-20 21:39:37 +10002034/* Import the site module (not into __main__ though) */
2035
Victor Stinner331a6a52019-05-27 16:39:22 +02002036static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002037init_import_site(void)
Nick Coghland6009512014-11-20 21:39:37 +10002038{
2039 PyObject *m;
2040 m = PyImport_ImportModule("site");
2041 if (m == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002042 return _PyStatus_ERR("Failed to import the site module");
Nick Coghland6009512014-11-20 21:39:37 +10002043 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002044 Py_DECREF(m);
Victor Stinner331a6a52019-05-27 16:39:22 +02002045 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002046}
2047
Victor Stinner874dbe82015-09-04 17:29:57 +02002048/* Check if a file descriptor is valid or not.
2049 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
2050static int
2051is_valid_fd(int fd)
2052{
Victor Stinner3092d6b2019-04-17 18:09:12 +02002053/* dup() is faster than fstat(): fstat() can require input/output operations,
2054 whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
2055 startup. Problem: dup() doesn't check if the file descriptor is valid on
2056 some platforms.
2057
2058 bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
2059 side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
2060 EBADF. FreeBSD has similar issue (bpo-32849).
2061
2062 Only use dup() on platforms where dup() is enough to detect invalid FD in
2063 corner cases: on Linux and Windows (bpo-32849). */
2064#if defined(__linux__) || defined(MS_WINDOWS)
2065 if (fd < 0) {
2066 return 0;
2067 }
2068 int fd2;
2069
2070 _Py_BEGIN_SUPPRESS_IPH
2071 fd2 = dup(fd);
2072 if (fd2 >= 0) {
2073 close(fd2);
2074 }
2075 _Py_END_SUPPRESS_IPH
2076
2077 return (fd2 >= 0);
2078#else
Victor Stinner1c4670e2017-05-04 00:45:56 +02002079 struct stat st;
2080 return (fstat(fd, &st) == 0);
Victor Stinner1c4670e2017-05-04 00:45:56 +02002081#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02002082}
2083
2084/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10002085static PyObject*
Victor Stinner331a6a52019-05-27 16:39:22 +02002086create_stdio(const PyConfig *config, PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002087 int fd, int write_mode, const char* name,
Victor Stinner709d23d2019-05-02 14:56:30 -04002088 const wchar_t* encoding, const wchar_t* errors)
Nick Coghland6009512014-11-20 21:39:37 +10002089{
2090 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
2091 const char* mode;
2092 const char* newline;
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002093 PyObject *line_buffering, *write_through;
Nick Coghland6009512014-11-20 21:39:37 +10002094 int buffering, isatty;
2095 _Py_IDENTIFIER(open);
2096 _Py_IDENTIFIER(isatty);
2097 _Py_IDENTIFIER(TextIOWrapper);
2098 _Py_IDENTIFIER(mode);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002099 const int buffered_stdio = config->buffered_stdio;
Nick Coghland6009512014-11-20 21:39:37 +10002100
Victor Stinner874dbe82015-09-04 17:29:57 +02002101 if (!is_valid_fd(fd))
2102 Py_RETURN_NONE;
2103
Nick Coghland6009512014-11-20 21:39:37 +10002104 /* stdin is always opened in buffered mode, first because it shouldn't
2105 make a difference in common use cases, second because TextIOWrapper
2106 depends on the presence of a read1() method which only exists on
2107 buffered streams.
2108 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002109 if (!buffered_stdio && write_mode)
Nick Coghland6009512014-11-20 21:39:37 +10002110 buffering = 0;
2111 else
2112 buffering = -1;
2113 if (write_mode)
2114 mode = "wb";
2115 else
2116 mode = "rb";
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002117 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
Nick Coghland6009512014-11-20 21:39:37 +10002118 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002119 Py_None, Py_None, /* encoding, errors */
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002120 Py_None, Py_False); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10002121 if (buf == NULL)
2122 goto error;
2123
2124 if (buffering) {
2125 _Py_IDENTIFIER(raw);
2126 raw = _PyObject_GetAttrId(buf, &PyId_raw);
2127 if (raw == NULL)
2128 goto error;
2129 }
2130 else {
2131 raw = buf;
2132 Py_INCREF(raw);
2133 }
2134
Steve Dower39294992016-08-30 21:22:36 -07002135#ifdef MS_WINDOWS
2136 /* Windows console IO is always UTF-8 encoded */
2137 if (PyWindowsConsoleIO_Check(raw))
Victor Stinner709d23d2019-05-02 14:56:30 -04002138 encoding = L"utf-8";
Steve Dower39294992016-08-30 21:22:36 -07002139#endif
2140
Nick Coghland6009512014-11-20 21:39:37 +10002141 text = PyUnicode_FromString(name);
2142 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
2143 goto error;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002144 res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
Nick Coghland6009512014-11-20 21:39:37 +10002145 if (res == NULL)
2146 goto error;
2147 isatty = PyObject_IsTrue(res);
2148 Py_DECREF(res);
2149 if (isatty == -1)
2150 goto error;
Victor Stinnerfbca9082018-08-30 00:50:45 +02002151 if (!buffered_stdio)
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002152 write_through = Py_True;
2153 else
2154 write_through = Py_False;
Jendrik Seipp5b907712020-01-01 23:21:43 +01002155 if (buffered_stdio && (isatty || fd == fileno(stderr)))
Nick Coghland6009512014-11-20 21:39:37 +10002156 line_buffering = Py_True;
2157 else
2158 line_buffering = Py_False;
2159
2160 Py_CLEAR(raw);
2161 Py_CLEAR(text);
2162
2163#ifdef MS_WINDOWS
2164 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
2165 newlines to "\n".
2166 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
2167 newline = NULL;
2168#else
2169 /* sys.stdin: split lines at "\n".
2170 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
2171 newline = "\n";
2172#endif
2173
Victor Stinner709d23d2019-05-02 14:56:30 -04002174 PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
2175 if (encoding_str == NULL) {
2176 Py_CLEAR(buf);
2177 goto error;
2178 }
2179
2180 PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
2181 if (errors_str == NULL) {
2182 Py_CLEAR(buf);
2183 Py_CLEAR(encoding_str);
2184 goto error;
2185 }
2186
2187 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
2188 buf, encoding_str, errors_str,
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002189 newline, line_buffering, write_through);
Nick Coghland6009512014-11-20 21:39:37 +10002190 Py_CLEAR(buf);
Victor Stinner709d23d2019-05-02 14:56:30 -04002191 Py_CLEAR(encoding_str);
2192 Py_CLEAR(errors_str);
Nick Coghland6009512014-11-20 21:39:37 +10002193 if (stream == NULL)
2194 goto error;
2195
2196 if (write_mode)
2197 mode = "w";
2198 else
2199 mode = "r";
2200 text = PyUnicode_FromString(mode);
2201 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
2202 goto error;
2203 Py_CLEAR(text);
2204 return stream;
2205
2206error:
2207 Py_XDECREF(buf);
2208 Py_XDECREF(stream);
2209 Py_XDECREF(text);
2210 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10002211
Victor Stinner874dbe82015-09-04 17:29:57 +02002212 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
2213 /* Issue #24891: the file descriptor was closed after the first
2214 is_valid_fd() check was called. Ignore the OSError and set the
2215 stream to None. */
2216 PyErr_Clear();
2217 Py_RETURN_NONE;
2218 }
2219 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002220}
2221
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002222/* Set builtins.open to io.OpenWrapper */
2223static PyStatus
Andy Lester75cd5bf2020-03-12 02:49:05 -05002224init_set_builtins_open(void)
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002225{
2226 PyObject *iomod = NULL, *wrapper;
2227 PyObject *bimod = NULL;
2228 PyStatus res = _PyStatus_OK();
2229
2230 if (!(iomod = PyImport_ImportModule("io"))) {
2231 goto error;
2232 }
2233
2234 if (!(bimod = PyImport_ImportModule("builtins"))) {
2235 goto error;
2236 }
2237
2238 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
2239 goto error;
2240 }
2241
2242 /* Set builtins.open */
2243 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
2244 Py_DECREF(wrapper);
2245 goto error;
2246 }
2247 Py_DECREF(wrapper);
2248 goto done;
2249
2250error:
2251 res = _PyStatus_ERR("can't initialize io.open");
2252
2253done:
2254 Py_XDECREF(bimod);
2255 Py_XDECREF(iomod);
2256 return res;
2257}
2258
2259
Nick Coghland6009512014-11-20 21:39:37 +10002260/* Initialize sys.stdin, stdout, stderr and builtins.open */
Victor Stinner331a6a52019-05-27 16:39:22 +02002261static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002262init_sys_streams(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002263{
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002264 PyObject *iomod = NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002265 PyObject *std = NULL;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002266 int fd;
Nick Coghland6009512014-11-20 21:39:37 +10002267 PyObject * encoding_attr;
Victor Stinner331a6a52019-05-27 16:39:22 +02002268 PyStatus res = _PyStatus_OK();
Victor Stinnerda7933e2020-04-13 03:04:28 +02002269 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002270
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002271 /* Check that stdin is not a directory
2272 Using shell redirection, you can redirect stdin to a directory,
2273 crashing the Python interpreter. Catch this common mistake here
2274 and output a useful error message. Note that under MS Windows,
2275 the shell already prevents that. */
2276#ifndef MS_WINDOWS
2277 struct _Py_stat_struct sb;
2278 if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
2279 S_ISDIR(sb.st_mode)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002280 return _PyStatus_ERR("<stdin> is a directory, cannot continue");
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002281 }
2282#endif
2283
Nick Coghland6009512014-11-20 21:39:37 +10002284 if (!(iomod = PyImport_ImportModule("io"))) {
2285 goto error;
2286 }
Nick Coghland6009512014-11-20 21:39:37 +10002287
Nick Coghland6009512014-11-20 21:39:37 +10002288 /* Set sys.stdin */
2289 fd = fileno(stdin);
2290 /* Under some conditions stdin, stdout and stderr may not be connected
2291 * and fileno() may point to an invalid file descriptor. For example
2292 * GUI apps don't have valid standard streams by default.
2293 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002294 std = create_stdio(config, iomod, fd, 0, "<stdin>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002295 config->stdio_encoding,
2296 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002297 if (std == NULL)
2298 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002299 PySys_SetObject("__stdin__", std);
2300 _PySys_SetObjectId(&PyId_stdin, std);
2301 Py_DECREF(std);
2302
2303 /* Set sys.stdout */
2304 fd = fileno(stdout);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002305 std = create_stdio(config, iomod, fd, 1, "<stdout>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002306 config->stdio_encoding,
2307 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002308 if (std == NULL)
2309 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002310 PySys_SetObject("__stdout__", std);
2311 _PySys_SetObjectId(&PyId_stdout, std);
2312 Py_DECREF(std);
2313
2314#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2315 /* Set sys.stderr, replaces the preliminary stderr */
2316 fd = fileno(stderr);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002317 std = create_stdio(config, iomod, fd, 1, "<stderr>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002318 config->stdio_encoding,
Victor Stinner709d23d2019-05-02 14:56:30 -04002319 L"backslashreplace");
Victor Stinner874dbe82015-09-04 17:29:57 +02002320 if (std == NULL)
2321 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002322
2323 /* Same as hack above, pre-import stderr's codec to avoid recursion
2324 when import.c tries to write to stderr in verbose mode. */
2325 encoding_attr = PyObject_GetAttrString(std, "encoding");
2326 if (encoding_attr != NULL) {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002327 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10002328 if (std_encoding != NULL) {
2329 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2330 Py_XDECREF(codec_info);
2331 }
2332 Py_DECREF(encoding_attr);
2333 }
Victor Stinnerb45d2592019-06-20 00:05:23 +02002334 _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
Nick Coghland6009512014-11-20 21:39:37 +10002335
2336 if (PySys_SetObject("__stderr__", std) < 0) {
2337 Py_DECREF(std);
2338 goto error;
2339 }
2340 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2341 Py_DECREF(std);
2342 goto error;
2343 }
2344 Py_DECREF(std);
2345#endif
2346
Victor Stinnera7368ac2017-11-15 18:11:45 -08002347 goto done;
Nick Coghland6009512014-11-20 21:39:37 +10002348
Victor Stinnera7368ac2017-11-15 18:11:45 -08002349error:
Victor Stinner331a6a52019-05-27 16:39:22 +02002350 res = _PyStatus_ERR("can't initialize sys standard streams");
Victor Stinnera7368ac2017-11-15 18:11:45 -08002351
2352done:
Victor Stinner124b9eb2018-08-29 01:29:06 +02002353 _Py_ClearStandardStreamEncoding();
Nick Coghland6009512014-11-20 21:39:37 +10002354 Py_XDECREF(iomod);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002355 return res;
Nick Coghland6009512014-11-20 21:39:37 +10002356}
2357
2358
Victor Stinner10dc4842015-03-24 12:01:30 +01002359static void
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002360_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2361 PyThreadState *tstate)
Victor Stinner10dc4842015-03-24 12:01:30 +01002362{
Victor Stinner10dc4842015-03-24 12:01:30 +01002363 fputc('\n', stderr);
2364 fflush(stderr);
2365
2366 /* display the current Python stack */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002367 _Py_DumpTracebackThreads(fd, interp, tstate);
Victor Stinner10dc4842015-03-24 12:01:30 +01002368}
Victor Stinner791da1c2016-03-14 16:53:12 +01002369
2370/* Print the current exception (if an exception is set) with its traceback,
2371 or display the current Python stack.
2372
2373 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2374 called on catastrophic cases.
2375
2376 Return 1 if the traceback was displayed, 0 otherwise. */
2377
2378static int
Andy Lester75cd5bf2020-03-12 02:49:05 -05002379_Py_FatalError_PrintExc(PyThreadState *tstate)
Victor Stinner791da1c2016-03-14 16:53:12 +01002380{
2381 PyObject *ferr, *res;
2382 PyObject *exception, *v, *tb;
2383 int has_tb;
2384
Victor Stinnerb45d2592019-06-20 00:05:23 +02002385 _PyErr_Fetch(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002386 if (exception == NULL) {
2387 /* No current exception */
2388 return 0;
2389 }
2390
2391 ferr = _PySys_GetObjectId(&PyId_stderr);
2392 if (ferr == NULL || ferr == Py_None) {
2393 /* sys.stderr is not set yet or set to None,
2394 no need to try to display the exception */
2395 return 0;
2396 }
2397
Victor Stinnerb45d2592019-06-20 00:05:23 +02002398 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002399 if (tb == NULL) {
2400 tb = Py_None;
2401 Py_INCREF(tb);
2402 }
2403 PyException_SetTraceback(v, tb);
2404 if (exception == NULL) {
2405 /* PyErr_NormalizeException() failed */
2406 return 0;
2407 }
2408
2409 has_tb = (tb != Py_None);
2410 PyErr_Display(exception, v, tb);
2411 Py_XDECREF(exception);
2412 Py_XDECREF(v);
2413 Py_XDECREF(tb);
2414
2415 /* sys.stderr may be buffered: call sys.stderr.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002416 res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002417 if (res == NULL) {
2418 _PyErr_Clear(tstate);
2419 }
2420 else {
Victor Stinner791da1c2016-03-14 16:53:12 +01002421 Py_DECREF(res);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002422 }
Victor Stinner791da1c2016-03-14 16:53:12 +01002423
2424 return has_tb;
2425}
2426
Nick Coghland6009512014-11-20 21:39:37 +10002427/* Print fatal error message and abort */
2428
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002429#ifdef MS_WINDOWS
2430static void
2431fatal_output_debug(const char *msg)
2432{
2433 /* buffer of 256 bytes allocated on the stack */
2434 WCHAR buffer[256 / sizeof(WCHAR)];
2435 size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2436 size_t msglen;
2437
2438 OutputDebugStringW(L"Fatal Python error: ");
2439
2440 msglen = strlen(msg);
2441 while (msglen) {
2442 size_t i;
2443
2444 if (buflen > msglen) {
2445 buflen = msglen;
2446 }
2447
2448 /* Convert the message to wchar_t. This uses a simple one-to-one
2449 conversion, assuming that the this error message actually uses
2450 ASCII only. If this ceases to be true, we will have to convert. */
2451 for (i=0; i < buflen; ++i) {
2452 buffer[i] = msg[i];
2453 }
2454 buffer[i] = L'\0';
2455 OutputDebugStringW(buffer);
2456
2457 msg += buflen;
2458 msglen -= buflen;
2459 }
2460 OutputDebugStringW(L"\n");
2461}
2462#endif
2463
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002464
2465static void
2466fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
2467{
2468 fprintf(stream, "Python runtime state: ");
Victor Stinner7b3c2522020-03-07 00:24:23 +01002469 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2470 if (finalizing) {
2471 fprintf(stream, "finalizing (tstate=%p)", finalizing);
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002472 }
2473 else if (runtime->initialized) {
2474 fprintf(stream, "initialized");
2475 }
2476 else if (runtime->core_initialized) {
2477 fprintf(stream, "core initialized");
2478 }
2479 else if (runtime->preinitialized) {
2480 fprintf(stream, "preinitialized");
2481 }
2482 else if (runtime->preinitializing) {
2483 fprintf(stream, "preinitializing");
2484 }
2485 else {
2486 fprintf(stream, "unknown");
2487 }
2488 fprintf(stream, "\n");
2489 fflush(stream);
2490}
2491
2492
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002493static inline void _Py_NO_RETURN
2494fatal_error_exit(int status)
Nick Coghland6009512014-11-20 21:39:37 +10002495{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002496 if (status < 0) {
2497#if defined(MS_WINDOWS) && defined(_DEBUG)
2498 DebugBreak();
2499#endif
2500 abort();
2501 }
2502 else {
2503 exit(status);
2504 }
2505}
2506
2507
2508static void _Py_NO_RETURN
2509fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2510 int status)
2511{
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002512 const int fd = fileno(stream);
Victor Stinner53345a42015-03-25 01:55:14 +01002513 static int reentrant = 0;
Victor Stinner53345a42015-03-25 01:55:14 +01002514
2515 if (reentrant) {
2516 /* Py_FatalError() caused a second fatal error.
2517 Example: flush_std_files() raises a recursion error. */
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002518 fatal_error_exit(status);
Victor Stinner53345a42015-03-25 01:55:14 +01002519 }
2520 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10002521
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002522 if (header) {
2523 fprintf(stream, "Fatal Python error: ");
2524 if (prefix) {
2525 fputs(prefix, stream);
2526 fputs(": ", stream);
2527 }
2528 if (msg) {
2529 fputs(msg, stream);
2530 }
2531 else {
2532 fprintf(stream, "<message not set>");
2533 }
2534 fputs("\n", stream);
2535 fflush(stream);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002536 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002537
2538 _PyRuntimeState *runtime = &_PyRuntime;
2539 fatal_error_dump_runtime(stream, runtime);
2540
2541 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2542 PyInterpreterState *interp = NULL;
2543 if (tstate != NULL) {
2544 interp = tstate->interp;
2545 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002546
Victor Stinner3a228ab2018-11-01 00:26:41 +01002547 /* Check if the current thread has a Python thread state
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002548 and holds the GIL.
Victor Stinner3a228ab2018-11-01 00:26:41 +01002549
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002550 tss_tstate is NULL if Py_FatalError() is called from a C thread which
2551 has no Python thread state.
2552
2553 tss_tstate != tstate if the current Python thread does not hold the GIL.
2554 */
2555 PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
2556 int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002557 if (has_tstate_and_gil) {
2558 /* If an exception is set, print the exception with its traceback */
Andy Lester75cd5bf2020-03-12 02:49:05 -05002559 if (!_Py_FatalError_PrintExc(tss_tstate)) {
Victor Stinner3a228ab2018-11-01 00:26:41 +01002560 /* No exception is set, or an exception is set without traceback */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002561 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002562 }
2563 }
2564 else {
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002565 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002566 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002567
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002568 /* The main purpose of faulthandler is to display the traceback.
2569 This function already did its best to display a traceback.
2570 Disable faulthandler to prevent writing a second traceback
2571 on abort(). */
Victor Stinner2025d782016-03-16 23:19:15 +01002572 _PyFaulthandler_Fini();
2573
Victor Stinner791da1c2016-03-14 16:53:12 +01002574 /* Check if the current Python thread hold the GIL */
Victor Stinner3a228ab2018-11-01 00:26:41 +01002575 if (has_tstate_and_gil) {
Victor Stinner791da1c2016-03-14 16:53:12 +01002576 /* Flush sys.stdout and sys.stderr */
2577 flush_std_files();
2578 }
Victor Stinnere0deff32015-03-24 13:46:18 +01002579
Nick Coghland6009512014-11-20 21:39:37 +10002580#ifdef MS_WINDOWS
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002581 fatal_output_debug(msg);
Victor Stinner53345a42015-03-25 01:55:14 +01002582#endif /* MS_WINDOWS */
2583
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002584 fatal_error_exit(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002585}
2586
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002587
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002588#undef Py_FatalError
2589
Victor Stinner19760862017-12-20 01:41:59 +01002590void _Py_NO_RETURN
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002591Py_FatalError(const char *msg)
2592{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002593 fatal_error(stderr, 1, NULL, msg, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002594}
2595
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002596
Victor Stinner19760862017-12-20 01:41:59 +01002597void _Py_NO_RETURN
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002598_Py_FatalErrorFunc(const char *func, const char *msg)
2599{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002600 fatal_error(stderr, 1, func, msg, -1);
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002601}
2602
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002603
2604void _Py_NO_RETURN
2605_Py_FatalErrorFormat(const char *func, const char *format, ...)
2606{
2607 static int reentrant = 0;
2608 if (reentrant) {
2609 /* _Py_FatalErrorFormat() caused a second fatal error */
2610 fatal_error_exit(-1);
2611 }
2612 reentrant = 1;
2613
2614 FILE *stream = stderr;
2615 fprintf(stream, "Fatal Python error: ");
2616 if (func) {
2617 fputs(func, stream);
2618 fputs(": ", stream);
2619 }
2620 fflush(stream);
2621
2622 va_list vargs;
2623#ifdef HAVE_STDARG_PROTOTYPES
2624 va_start(vargs, format);
2625#else
2626 va_start(vargs);
2627#endif
2628 vfprintf(stream, format, vargs);
2629 va_end(vargs);
2630
2631 fputs("\n", stream);
2632 fflush(stream);
2633
2634 fatal_error(stream, 0, NULL, NULL, -1);
2635}
2636
2637
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002638void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +02002639Py_ExitStatusException(PyStatus status)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002640{
Victor Stinner331a6a52019-05-27 16:39:22 +02002641 if (_PyStatus_IS_EXIT(status)) {
2642 exit(status.exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +02002643 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002644 else if (_PyStatus_IS_ERROR(status)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002645 fatal_error(stderr, 1, status.func, status.err_msg, 1);
Victor Stinnerdfe88472019-03-01 12:14:41 +01002646 }
2647 else {
Victor Stinner331a6a52019-05-27 16:39:22 +02002648 Py_FatalError("Py_ExitStatusException() must not be called on success");
Victor Stinnerdfe88472019-03-01 12:14:41 +01002649 }
Nick Coghland6009512014-11-20 21:39:37 +10002650}
2651
2652/* Clean up and exit */
2653
Nick Coghland6009512014-11-20 21:39:37 +10002654/* For the atexit module. */
Marcel Plch776407f2017-12-20 11:17:58 +01002655void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
Nick Coghland6009512014-11-20 21:39:37 +10002656{
Victor Stinner81a7be32020-04-14 15:14:01 +02002657 PyInterpreterState *is = _PyInterpreterState_GET();
Marcel Plch776407f2017-12-20 11:17:58 +01002658
Antoine Pitroufc5db952017-12-13 02:29:07 +01002659 /* Guard against API misuse (see bpo-17852) */
Marcel Plch776407f2017-12-20 11:17:58 +01002660 assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
2661
2662 is->pyexitfunc = func;
2663 is->pyexitmodule = module;
Nick Coghland6009512014-11-20 21:39:37 +10002664}
2665
2666static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002667call_py_exitfuncs(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002668{
Victor Stinnerb45d2592019-06-20 00:05:23 +02002669 PyInterpreterState *interp = tstate->interp;
2670 if (interp->pyexitfunc == NULL)
Nick Coghland6009512014-11-20 21:39:37 +10002671 return;
2672
Victor Stinnerb45d2592019-06-20 00:05:23 +02002673 (*interp->pyexitfunc)(interp->pyexitmodule);
2674 _PyErr_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10002675}
2676
2677/* Wait until threading._shutdown completes, provided
2678 the threading module was imported in the first place.
2679 The shutdown routine will wait until all non-daemon
2680 "threading" threads have completed. */
2681static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002682wait_for_thread_shutdown(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002683{
Nick Coghland6009512014-11-20 21:39:37 +10002684 _Py_IDENTIFIER(_shutdown);
2685 PyObject *result;
Eric Snow3f9eee62017-09-15 16:35:20 -06002686 PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
Nick Coghland6009512014-11-20 21:39:37 +10002687 if (threading == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +02002688 if (_PyErr_Occurred(tstate)) {
Stefan Krah027b09c2019-03-25 21:50:58 +01002689 PyErr_WriteUnraisable(NULL);
2690 }
2691 /* else: threading not imported */
Nick Coghland6009512014-11-20 21:39:37 +10002692 return;
2693 }
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002694 result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
Nick Coghland6009512014-11-20 21:39:37 +10002695 if (result == NULL) {
2696 PyErr_WriteUnraisable(threading);
2697 }
2698 else {
2699 Py_DECREF(result);
2700 }
2701 Py_DECREF(threading);
Nick Coghland6009512014-11-20 21:39:37 +10002702}
2703
2704#define NEXITFUNCS 32
Nick Coghland6009512014-11-20 21:39:37 +10002705int Py_AtExit(void (*func)(void))
2706{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002707 if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
Nick Coghland6009512014-11-20 21:39:37 +10002708 return -1;
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002709 _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
Nick Coghland6009512014-11-20 21:39:37 +10002710 return 0;
2711}
2712
2713static void
Victor Stinner8e91c242019-04-24 17:24:01 +02002714call_ll_exitfuncs(_PyRuntimeState *runtime)
Nick Coghland6009512014-11-20 21:39:37 +10002715{
Victor Stinner8e91c242019-04-24 17:24:01 +02002716 while (runtime->nexitfuncs > 0) {
Victor Stinner87d23a02019-04-26 05:49:26 +02002717 /* pop last function from the list */
2718 runtime->nexitfuncs--;
2719 void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2720 runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2721
2722 exitfunc();
Victor Stinner8e91c242019-04-24 17:24:01 +02002723 }
Nick Coghland6009512014-11-20 21:39:37 +10002724
2725 fflush(stdout);
2726 fflush(stderr);
2727}
2728
Victor Stinnercfc88312018-08-01 16:41:25 +02002729void _Py_NO_RETURN
Nick Coghland6009512014-11-20 21:39:37 +10002730Py_Exit(int sts)
2731{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002732 if (Py_FinalizeEx() < 0) {
2733 sts = 120;
2734 }
Nick Coghland6009512014-11-20 21:39:37 +10002735
2736 exit(sts);
2737}
2738
Victor Stinner331a6a52019-05-27 16:39:22 +02002739static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002740init_signals(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002741{
2742#ifdef SIGPIPE
2743 PyOS_setsig(SIGPIPE, SIG_IGN);
2744#endif
2745#ifdef SIGXFZ
2746 PyOS_setsig(SIGXFZ, SIG_IGN);
2747#endif
2748#ifdef SIGXFSZ
2749 PyOS_setsig(SIGXFSZ, SIG_IGN);
2750#endif
Victor Stinner400e1db2020-03-31 19:13:10 +02002751 PyOS_InitInterrupts(); /* May imply init_signals() */
Victor Stinnerb45d2592019-06-20 00:05:23 +02002752 if (_PyErr_Occurred(tstate)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002753 return _PyStatus_ERR("can't import signal");
Nick Coghland6009512014-11-20 21:39:37 +10002754 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002755 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002756}
2757
2758
2759/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2760 *
2761 * All of the code in this function must only use async-signal-safe functions,
2762 * listed at `man 7 signal` or
2763 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
Victor Stinnerefc28bb2020-03-05 18:13:56 +01002764 *
2765 * If this function is updated, update also _posix_spawn() of subprocess.py.
Nick Coghland6009512014-11-20 21:39:37 +10002766 */
2767void
2768_Py_RestoreSignals(void)
2769{
2770#ifdef SIGPIPE
2771 PyOS_setsig(SIGPIPE, SIG_DFL);
2772#endif
2773#ifdef SIGXFZ
2774 PyOS_setsig(SIGXFZ, SIG_DFL);
2775#endif
2776#ifdef SIGXFSZ
2777 PyOS_setsig(SIGXFSZ, SIG_DFL);
2778#endif
2779}
2780
2781
2782/*
2783 * The file descriptor fd is considered ``interactive'' if either
2784 * a) isatty(fd) is TRUE, or
2785 * b) the -i flag was given, and the filename associated with
2786 * the descriptor is NULL or "<stdin>" or "???".
2787 */
2788int
2789Py_FdIsInteractive(FILE *fp, const char *filename)
2790{
2791 if (isatty((int)fileno(fp)))
2792 return 1;
2793 if (!Py_InteractiveFlag)
2794 return 0;
2795 return (filename == NULL) ||
2796 (strcmp(filename, "<stdin>") == 0) ||
2797 (strcmp(filename, "???") == 0);
2798}
2799
2800
Nick Coghland6009512014-11-20 21:39:37 +10002801/* Wrappers around sigaction() or signal(). */
2802
2803PyOS_sighandler_t
2804PyOS_getsig(int sig)
2805{
2806#ifdef HAVE_SIGACTION
2807 struct sigaction context;
2808 if (sigaction(sig, NULL, &context) == -1)
2809 return SIG_ERR;
2810 return context.sa_handler;
2811#else
2812 PyOS_sighandler_t handler;
2813/* Special signal handling for the secure CRT in Visual Studio 2005 */
2814#if defined(_MSC_VER) && _MSC_VER >= 1400
2815 switch (sig) {
2816 /* Only these signals are valid */
2817 case SIGINT:
2818 case SIGILL:
2819 case SIGFPE:
2820 case SIGSEGV:
2821 case SIGTERM:
2822 case SIGBREAK:
2823 case SIGABRT:
2824 break;
2825 /* Don't call signal() with other values or it will assert */
2826 default:
2827 return SIG_ERR;
2828 }
2829#endif /* _MSC_VER && _MSC_VER >= 1400 */
2830 handler = signal(sig, SIG_IGN);
2831 if (handler != SIG_ERR)
2832 signal(sig, handler);
2833 return handler;
2834#endif
2835}
2836
2837/*
2838 * All of the code in this function must only use async-signal-safe functions,
2839 * listed at `man 7 signal` or
2840 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2841 */
2842PyOS_sighandler_t
2843PyOS_setsig(int sig, PyOS_sighandler_t handler)
2844{
2845#ifdef HAVE_SIGACTION
2846 /* Some code in Modules/signalmodule.c depends on sigaction() being
2847 * used here if HAVE_SIGACTION is defined. Fix that if this code
2848 * changes to invalidate that assumption.
2849 */
2850 struct sigaction context, ocontext;
2851 context.sa_handler = handler;
2852 sigemptyset(&context.sa_mask);
2853 context.sa_flags = 0;
2854 if (sigaction(sig, &context, &ocontext) == -1)
2855 return SIG_ERR;
2856 return ocontext.sa_handler;
2857#else
2858 PyOS_sighandler_t oldhandler;
2859 oldhandler = signal(sig, handler);
2860#ifdef HAVE_SIGINTERRUPT
2861 siginterrupt(sig, 1);
2862#endif
2863 return oldhandler;
2864#endif
2865}
2866
2867#ifdef __cplusplus
2868}
2869#endif