blob: e9df8fb5d273a77c191c52375e75c3acac9c3da1 [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 Stinner62230712020-11-18 23:18:29 +010011#include "pycore_import.h" // _PyImport_BootstrapImp()
Victor Stinner4f98f462020-04-15 04:01:58 +020012#include "pycore_initconfig.h" // _PyStatus_OK()
13#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
14#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
15#include "pycore_pyerrors.h" // _PyErr_Occurred()
16#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere5014be2020-04-14 17:52:15 +020017#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner4f98f462020-04-15 04:01:58 +020018#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
19#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
20
Victor Stinner66f77ca2021-01-19 23:35:27 +010021#include "module_names.h" // _Py_module_names
22
Victor Stinner4f98f462020-04-15 04:01:58 +020023#include <locale.h> // setlocale()
Nick Coghland6009512014-11-20 21:39:37 +100024
25#ifdef HAVE_SIGNAL_H
Victor Stinner4f98f462020-04-15 04:01:58 +020026# include <signal.h> // SIG_IGN
Nick Coghland6009512014-11-20 21:39:37 +100027#endif
28
29#ifdef HAVE_LANGINFO_H
Victor Stinner4f98f462020-04-15 04:01:58 +020030# include <langinfo.h> // nl_langinfo(CODESET)
Nick Coghland6009512014-11-20 21:39:37 +100031#endif
32
33#ifdef MS_WINDOWS
Victor Stinner4f98f462020-04-15 04:01:58 +020034# undef BYTE
35# include "windows.h"
Steve Dower39294992016-08-30 21:22:36 -070036
Victor Stinner4f98f462020-04-15 04:01:58 +020037 extern PyTypeObject PyWindowsConsoleIO_Type;
38# define PyWindowsConsoleIO_Check(op) \
39 (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
Nick Coghland6009512014-11-20 21:39:37 +100040#endif
41
Victor Stinner4f98f462020-04-15 04:01:58 +020042
Victor Stinner314b8782021-01-18 18:34:56 +010043#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
44
45
Nick Coghland6009512014-11-20 21:39:37 +100046_Py_IDENTIFIER(flush);
47_Py_IDENTIFIER(name);
48_Py_IDENTIFIER(stdin);
49_Py_IDENTIFIER(stdout);
50_Py_IDENTIFIER(stderr);
Eric Snow3f9eee62017-09-15 16:35:20 -060051_Py_IDENTIFIER(threading);
Nick Coghland6009512014-11-20 21:39:37 +100052
53#ifdef __cplusplus
54extern "C" {
55#endif
56
Nick Coghland6009512014-11-20 21:39:37 +100057
Victor Stinnerb45d2592019-06-20 00:05:23 +020058/* Forward declarations */
Victor Stinner331a6a52019-05-27 16:39:22 +020059static PyStatus add_main_module(PyInterpreterState *interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +020060static PyStatus init_import_site(void);
Andy Lester75cd5bf2020-03-12 02:49:05 -050061static PyStatus init_set_builtins_open(void);
Victor Stinnerb45d2592019-06-20 00:05:23 +020062static PyStatus init_sys_streams(PyThreadState *tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +020063static void wait_for_thread_shutdown(PyThreadState *tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +020064static void call_ll_exitfuncs(_PyRuntimeState *runtime);
Nick Coghland6009512014-11-20 21:39:37 +100065
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080066int _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080067_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010068static int runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060069
Victor Stinner331a6a52019-05-27 16:39:22 +020070PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -060071_PyRuntime_Initialize(void)
72{
73 /* XXX We only initialize once in the process, which aligns with
74 the static initialization of the former globals now found in
75 _PyRuntime. However, _PyRuntime *should* be initialized with
76 every Py_Initialize() call, but doing so breaks the runtime.
77 This is because the runtime state is not properly finalized
78 currently. */
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010079 if (runtime_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +020080 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080081 }
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010082 runtime_initialized = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080083
84 return _PyRuntimeState_Init(&_PyRuntime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060085}
86
87void
88_PyRuntime_Finalize(void)
89{
90 _PyRuntimeState_Fini(&_PyRuntime);
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010091 runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060092}
93
94int
95_Py_IsFinalizing(void)
96{
Victor Stinner7b3c2522020-03-07 00:24:23 +010097 return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098}
99
Nick Coghland6009512014-11-20 21:39:37 +1000100/* Hack to force loading of object files */
101int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
102 PyOS_mystrnicmp; /* Python/pystrcmp.o */
103
Eric Snowc7ec9982017-05-23 23:00:52 -0700104
Eric Snow1abcf672017-05-23 21:46:51 -0700105/* APIs to access the initialization flags
106 *
107 * Can be called prior to Py_Initialize.
108 */
Nick Coghland6009512014-11-20 21:39:37 +1000109
Eric Snow1abcf672017-05-23 21:46:51 -0700110int
111_Py_IsCoreInitialized(void)
112{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600113 return _PyRuntime.core_initialized;
Eric Snow1abcf672017-05-23 21:46:51 -0700114}
Nick Coghland6009512014-11-20 21:39:37 +1000115
116int
117Py_IsInitialized(void)
118{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600119 return _PyRuntime.initialized;
Nick Coghland6009512014-11-20 21:39:37 +1000120}
121
Nick Coghlan6ea41862017-06-11 13:16:15 +1000122
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000123/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
124 call this twice without an intervening Py_FinalizeEx() call. When
Nick Coghland6009512014-11-20 21:39:37 +1000125 initializations fail, a fatal error is issued and the function does
126 not return. On return, the first thread and interpreter state have
127 been created.
128
129 Locking: you must hold the interpreter lock while calling this.
130 (If the lock has not yet been initialized, that's equivalent to
131 having the lock, but you cannot use multiple threads.)
132
133*/
Victor Stinneref75a622020-11-12 15:14:13 +0100134static int
Victor Stinnerb45d2592019-06-20 00:05:23 +0200135init_importlib(PyThreadState *tstate, PyObject *sysmod)
Nick Coghland6009512014-11-20 21:39:37 +1000136{
Victor Stinneref75a622020-11-12 15:14:13 +0100137 assert(!_PyErr_Occurred(tstate));
138
Victor Stinnerb45d2592019-06-20 00:05:23 +0200139 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200140 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
Nick Coghland6009512014-11-20 21:39:37 +1000141
Victor Stinneref75a622020-11-12 15:14:13 +0100142 // Import _importlib through its frozen version, _frozen_importlib.
143 if (verbose) {
Nick Coghland6009512014-11-20 21:39:37 +1000144 PySys_FormatStderr("import _frozen_importlib # frozen\n");
145 }
Victor Stinneref75a622020-11-12 15:14:13 +0100146 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
147 return -1;
148 }
149 PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed
Nick Coghland6009512014-11-20 21:39:37 +1000150 if (importlib == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100151 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000152 }
Victor Stinneref75a622020-11-12 15:14:13 +0100153 interp->importlib = Py_NewRef(importlib);
Nick Coghland6009512014-11-20 21:39:37 +1000154
Victor Stinneref75a622020-11-12 15:14:13 +0100155 // Import the _imp module
156 if (verbose) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200157 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000158 }
Victor Stinner62230712020-11-18 23:18:29 +0100159 PyObject *imp_mod = _PyImport_BootstrapImp(tstate);
Victor Stinneref75a622020-11-12 15:14:13 +0100160 if (imp_mod == NULL) {
161 return -1;
162 }
163 if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
164 Py_DECREF(imp_mod);
165 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000166 }
167
Victor Stinneref75a622020-11-12 15:14:13 +0100168 // Install importlib as the implementation of import
169 PyObject *value = PyObject_CallMethod(importlib, "_install",
170 "OO", sysmod, imp_mod);
171 Py_DECREF(imp_mod);
Nick Coghland6009512014-11-20 21:39:37 +1000172 if (value == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100173 return -1;
Nick Coghland6009512014-11-20 21:39:37 +1000174 }
175 Py_DECREF(value);
Nick Coghland6009512014-11-20 21:39:37 +1000176
Victor Stinneref75a622020-11-12 15:14:13 +0100177 assert(!_PyErr_Occurred(tstate));
178 return 0;
Nick Coghland6009512014-11-20 21:39:37 +1000179}
180
Victor Stinneref75a622020-11-12 15:14:13 +0100181
Victor Stinner331a6a52019-05-27 16:39:22 +0200182static PyStatus
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200183init_importlib_external(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -0700184{
185 PyObject *value;
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200186 value = PyObject_CallMethod(tstate->interp->importlib,
Eric Snow1abcf672017-05-23 21:46:51 -0700187 "_install_external_importers", "");
188 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200189 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200190 return _PyStatus_ERR("external importer setup failed");
Eric Snow1abcf672017-05-23 21:46:51 -0700191 }
Stéphane Wirtelab1cb802017-06-08 13:13:20 +0200192 Py_DECREF(value);
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200193 return _PyImportZip_Init(tstate);
Eric Snow1abcf672017-05-23 21:46:51 -0700194}
Nick Coghland6009512014-11-20 21:39:37 +1000195
Nick Coghlan6ea41862017-06-11 13:16:15 +1000196/* Helper functions to better handle the legacy C locale
197 *
198 * The legacy C locale assumes ASCII as the default text encoding, which
199 * causes problems not only for the CPython runtime, but also other
200 * components like GNU readline.
201 *
202 * Accordingly, when the CLI detects it, it attempts to coerce it to a
203 * more capable UTF-8 based alternative as follows:
204 *
205 * if (_Py_LegacyLocaleDetected()) {
206 * _Py_CoerceLegacyLocale();
207 * }
208 *
209 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
210 *
211 * Locale coercion also impacts the default error handler for the standard
212 * streams: while the usual default is "strict", the default for the legacy
213 * C locale and for any of the coercion target locales is "surrogateescape".
214 */
215
216int
Victor Stinner0f721472019-05-20 17:16:38 +0200217_Py_LegacyLocaleDetected(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000218{
219#ifndef MS_WINDOWS
Victor Stinner0f721472019-05-20 17:16:38 +0200220 if (!warn) {
221 const char *locale_override = getenv("LC_ALL");
222 if (locale_override != NULL && *locale_override != '\0') {
223 /* Don't coerce C locale if the LC_ALL environment variable
224 is set */
225 return 0;
226 }
227 }
228
Nick Coghlan6ea41862017-06-11 13:16:15 +1000229 /* On non-Windows systems, the C locale is considered a legacy locale */
Nick Coghlaneb817952017-06-18 12:29:42 +1000230 /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
231 * the POSIX locale as a simple alias for the C locale, so
232 * we may also want to check for that explicitly.
233 */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000234 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
235 return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
236#else
237 /* Windows uses code pages instead of locales, so no locale is legacy */
238 return 0;
239#endif
240}
241
Victor Stinnerb0051362019-11-22 17:52:42 +0100242#ifndef MS_WINDOWS
Nick Coghlaneb817952017-06-18 12:29:42 +1000243static const char *_C_LOCALE_WARNING =
244 "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
245 "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
246 "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
247 "locales is recommended.\n";
248
Nick Coghlaneb817952017-06-18 12:29:42 +1000249static void
Victor Stinner43125222019-04-24 18:23:53 +0200250emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
Nick Coghlaneb817952017-06-18 12:29:42 +1000251{
Victor Stinner331a6a52019-05-27 16:39:22 +0200252 const PyPreConfig *preconfig = &runtime->preconfig;
Victor Stinner0f721472019-05-20 17:16:38 +0200253 if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
Victor Stinnercf215042018-08-29 22:56:06 +0200254 PySys_FormatStderr("%s", _C_LOCALE_WARNING);
Nick Coghlaneb817952017-06-18 12:29:42 +1000255 }
256}
Victor Stinnerb0051362019-11-22 17:52:42 +0100257#endif /* !defined(MS_WINDOWS) */
Nick Coghlaneb817952017-06-18 12:29:42 +1000258
Nick Coghlan6ea41862017-06-11 13:16:15 +1000259typedef struct _CandidateLocale {
260 const char *locale_name; /* The locale to try as a coercion target */
261} _LocaleCoercionTarget;
262
263static _LocaleCoercionTarget _TARGET_LOCALES[] = {
264 {"C.UTF-8"},
265 {"C.utf8"},
Nick Coghlan18974c32017-06-30 00:48:14 +1000266 {"UTF-8"},
Nick Coghlan6ea41862017-06-11 13:16:15 +1000267 {NULL}
268};
269
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200270
271int
272_Py_IsLocaleCoercionTarget(const char *ctype_loc)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000273{
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200274 const _LocaleCoercionTarget *target = NULL;
275 for (target = _TARGET_LOCALES; target->locale_name; target++) {
276 if (strcmp(ctype_loc, target->locale_name) == 0) {
277 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000278 }
Victor Stinner124b9eb2018-08-29 01:29:06 +0200279 }
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200280 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000281}
282
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200283
Nick Coghlan6ea41862017-06-11 13:16:15 +1000284#ifdef PY_COERCE_C_LOCALE
Victor Stinner94540602017-12-16 04:54:22 +0100285static const char C_LOCALE_COERCION_WARNING[] =
Nick Coghlan6ea41862017-06-11 13:16:15 +1000286 "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
287 "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
288
Victor Stinner0f721472019-05-20 17:16:38 +0200289static int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200290_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000291{
292 const char *newloc = target->locale_name;
293
294 /* Reset locale back to currently configured defaults */
xdegaye1588be62017-11-12 12:45:59 +0100295 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000296
297 /* Set the relevant locale environment variable */
298 if (setenv("LC_CTYPE", newloc, 1)) {
299 fprintf(stderr,
300 "Error setting LC_CTYPE, skipping C locale coercion\n");
Victor Stinner0f721472019-05-20 17:16:38 +0200301 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000302 }
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200303 if (warn) {
Victor Stinner94540602017-12-16 04:54:22 +0100304 fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
Nick Coghlaneb817952017-06-18 12:29:42 +1000305 }
Nick Coghlan6ea41862017-06-11 13:16:15 +1000306
307 /* Reconfigure with the overridden environment variables */
xdegaye1588be62017-11-12 12:45:59 +0100308 _Py_SetLocaleFromEnv(LC_ALL);
Victor Stinner0f721472019-05-20 17:16:38 +0200309 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000310}
311#endif
312
Victor Stinner0f721472019-05-20 17:16:38 +0200313int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200314_Py_CoerceLegacyLocale(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000315{
Victor Stinner0f721472019-05-20 17:16:38 +0200316 int coerced = 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000317#ifdef PY_COERCE_C_LOCALE
Victor Stinner8ea09112018-09-03 17:05:18 +0200318 char *oldloc = NULL;
319
320 oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
321 if (oldloc == NULL) {
Victor Stinner0f721472019-05-20 17:16:38 +0200322 return coerced;
Victor Stinner8ea09112018-09-03 17:05:18 +0200323 }
324
Victor Stinner94540602017-12-16 04:54:22 +0100325 const char *locale_override = getenv("LC_ALL");
326 if (locale_override == NULL || *locale_override == '\0') {
327 /* LC_ALL is also not set (or is set to an empty string) */
328 const _LocaleCoercionTarget *target = NULL;
329 for (target = _TARGET_LOCALES; target->locale_name; target++) {
330 const char *new_locale = setlocale(LC_CTYPE,
331 target->locale_name);
332 if (new_locale != NULL) {
Victor Stinnere2510952019-05-02 11:28:57 -0400333#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94540602017-12-16 04:54:22 +0100334 /* Also ensure that nl_langinfo works in this locale */
335 char *codeset = nl_langinfo(CODESET);
336 if (!codeset || *codeset == '\0') {
337 /* CODESET is not set or empty, so skip coercion */
338 new_locale = NULL;
339 _Py_SetLocaleFromEnv(LC_CTYPE);
340 continue;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000341 }
Victor Stinner94540602017-12-16 04:54:22 +0100342#endif
343 /* Successfully configured locale, so make it the default */
Victor Stinner0f721472019-05-20 17:16:38 +0200344 coerced = _coerce_default_locale_settings(warn, target);
Victor Stinner8ea09112018-09-03 17:05:18 +0200345 goto done;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000346 }
347 }
348 }
349 /* No C locale warning here, as Py_Initialize will emit one later */
Victor Stinner8ea09112018-09-03 17:05:18 +0200350
351 setlocale(LC_CTYPE, oldloc);
352
353done:
354 PyMem_RawFree(oldloc);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000355#endif
Victor Stinner0f721472019-05-20 17:16:38 +0200356 return coerced;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000357}
358
xdegaye1588be62017-11-12 12:45:59 +0100359/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
360 * isolate the idiosyncrasies of different libc implementations. It reads the
361 * appropriate environment variable and uses its value to select the locale for
362 * 'category'. */
363char *
364_Py_SetLocaleFromEnv(int category)
365{
Victor Stinner353933e2018-11-23 13:08:26 +0100366 char *res;
xdegaye1588be62017-11-12 12:45:59 +0100367#ifdef __ANDROID__
368 const char *locale;
369 const char **pvar;
370#ifdef PY_COERCE_C_LOCALE
371 const char *coerce_c_locale;
372#endif
373 const char *utf8_locale = "C.UTF-8";
374 const char *env_var_set[] = {
375 "LC_ALL",
376 "LC_CTYPE",
377 "LANG",
378 NULL,
379 };
380
381 /* Android setlocale(category, "") doesn't check the environment variables
382 * and incorrectly sets the "C" locale at API 24 and older APIs. We only
383 * check the environment variables listed in env_var_set. */
384 for (pvar=env_var_set; *pvar; pvar++) {
385 locale = getenv(*pvar);
386 if (locale != NULL && *locale != '\0') {
387 if (strcmp(locale, utf8_locale) == 0 ||
388 strcmp(locale, "en_US.UTF-8") == 0) {
389 return setlocale(category, utf8_locale);
390 }
391 return setlocale(category, "C");
392 }
393 }
394
395 /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
396 * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
397 * Quote from POSIX section "8.2 Internationalization Variables":
398 * "4. If the LANG environment variable is not set or is set to the empty
399 * string, the implementation-defined default locale shall be used." */
400
401#ifdef PY_COERCE_C_LOCALE
402 coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
403 if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
404 /* Some other ported code may check the environment variables (e.g. in
405 * extension modules), so we make sure that they match the locale
406 * configuration */
407 if (setenv("LC_CTYPE", utf8_locale, 1)) {
408 fprintf(stderr, "Warning: failed setting the LC_CTYPE "
409 "environment variable to %s\n", utf8_locale);
410 }
411 }
412#endif
Victor Stinner353933e2018-11-23 13:08:26 +0100413 res = setlocale(category, utf8_locale);
414#else /* !defined(__ANDROID__) */
415 res = setlocale(category, "");
416#endif
417 _Py_ResetForceASCII();
418 return res;
xdegaye1588be62017-11-12 12:45:59 +0100419}
420
Nick Coghlan6ea41862017-06-11 13:16:15 +1000421
Victor Stinner048a3562020-11-05 00:45:56 +0100422static int
Victor Stinner9e1b8282020-11-10 13:21:52 +0100423interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
Victor Stinner048a3562020-11-05 00:45:56 +0100424{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100425 const PyConfig *config = &tstate->interp->config;
Victor Stinner048a3562020-11-05 00:45:56 +0100426
Victor Stinner9e1b8282020-11-10 13:21:52 +0100427 if (!only_update_path_config) {
428 PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
429 if (_PyStatus_EXCEPTION(status)) {
430 _PyErr_SetFromPyStatus(status);
431 return -1;
432 }
Victor Stinner048a3562020-11-05 00:45:56 +0100433 }
434
Victor Stinner9e1b8282020-11-10 13:21:52 +0100435 if (_Py_IsMainInterpreter(tstate)) {
436 PyStatus status = _PyConfig_WritePathConfig(config);
Victor Stinner048a3562020-11-05 00:45:56 +0100437 if (_PyStatus_EXCEPTION(status)) {
438 _PyErr_SetFromPyStatus(status);
439 return -1;
440 }
441 }
442
443 // Update the sys module for the new configuration
444 if (_PySys_UpdateConfig(tstate) < 0) {
445 return -1;
446 }
447 return 0;
448}
449
450
451int
452_PyInterpreterState_SetConfig(const PyConfig *src_config)
453{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100454 PyThreadState *tstate = PyThreadState_Get();
Victor Stinner048a3562020-11-05 00:45:56 +0100455 int res = -1;
456
457 PyConfig config;
458 PyConfig_InitPythonConfig(&config);
459 PyStatus status = _PyConfig_Copy(&config, src_config);
460 if (_PyStatus_EXCEPTION(status)) {
461 _PyErr_SetFromPyStatus(status);
462 goto done;
463 }
464
465 status = PyConfig_Read(&config);
466 if (_PyStatus_EXCEPTION(status)) {
467 _PyErr_SetFromPyStatus(status);
468 goto done;
469 }
470
Victor Stinner9e1b8282020-11-10 13:21:52 +0100471 status = _PyConfig_Copy(&tstate->interp->config, &config);
472 if (_PyStatus_EXCEPTION(status)) {
473 _PyErr_SetFromPyStatus(status);
474 goto done;
475 }
476
477 res = interpreter_update_config(tstate, 0);
Victor Stinner048a3562020-11-05 00:45:56 +0100478
479done:
480 PyConfig_Clear(&config);
481 return res;
482}
483
484
Eric Snow1abcf672017-05-23 21:46:51 -0700485/* Global initializations. Can be undone by Py_Finalize(). Don't
486 call this twice without an intervening Py_Finalize() call.
487
Victor Stinner331a6a52019-05-27 16:39:22 +0200488 Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
Eric Snow1abcf672017-05-23 21:46:51 -0700489 must have a corresponding call to Py_Finalize.
490
491 Locking: you must hold the interpreter lock while calling these APIs.
492 (If the lock has not yet been initialized, that's equivalent to
493 having the lock, but you cannot use multiple threads.)
494
495*/
496
Victor Stinner331a6a52019-05-27 16:39:22 +0200497static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200498pyinit_core_reconfigure(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200499 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200500 const PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200501{
Victor Stinner331a6a52019-05-27 16:39:22 +0200502 PyStatus status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100503 PyThreadState *tstate = _PyThreadState_GET();
504 if (!tstate) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200505 return _PyStatus_ERR("failed to read thread state");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100506 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200507 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100508
509 PyInterpreterState *interp = tstate->interp;
510 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200511 return _PyStatus_ERR("can't make main interpreter");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100512 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100513
Victor Stinnere81f6e62020-06-08 18:12:59 +0200514 status = _PyConfig_Write(config, runtime);
515 if (_PyStatus_EXCEPTION(status)) {
516 return status;
517 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200518
Victor Stinner048a3562020-11-05 00:45:56 +0100519 status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200520 if (_PyStatus_EXCEPTION(status)) {
521 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200522 }
Victor Stinnerda7933e2020-04-13 03:04:28 +0200523 config = _PyInterpreterState_GetConfig(interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +0200524
Victor Stinner331a6a52019-05-27 16:39:22 +0200525 if (config->_install_importlib) {
Victor Stinner12f2f172019-09-26 15:51:50 +0200526 status = _PyConfig_WritePathConfig(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200527 if (_PyStatus_EXCEPTION(status)) {
528 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200529 }
530 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200531 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200532}
533
534
Victor Stinner331a6a52019-05-27 16:39:22 +0200535static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200536pycore_init_runtime(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200537 const PyConfig *config)
Nick Coghland6009512014-11-20 21:39:37 +1000538{
Victor Stinner43125222019-04-24 18:23:53 +0200539 if (runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200540 return _PyStatus_ERR("main interpreter already initialized");
Victor Stinner1dc6e392018-07-25 02:49:17 +0200541 }
Victor Stinnerda273412017-12-15 01:46:02 +0100542
Victor Stinnere81f6e62020-06-08 18:12:59 +0200543 PyStatus status = _PyConfig_Write(config, runtime);
544 if (_PyStatus_EXCEPTION(status)) {
545 return status;
546 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600547
Eric Snow1abcf672017-05-23 21:46:51 -0700548 /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
549 * threads behave a little more gracefully at interpreter shutdown.
550 * We clobber it here so the new interpreter can start with a clean
551 * slate.
552 *
553 * However, this may still lead to misbehaviour if there are daemon
554 * threads still hanging around from a previous Py_Initialize/Finalize
555 * pair :(
556 */
Victor Stinner7b3c2522020-03-07 00:24:23 +0100557 _PyRuntimeState_SetFinalizing(runtime, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600558
Victor Stinnere81f6e62020-06-08 18:12:59 +0200559 status = _Py_HashRandomization_Init(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200560 if (_PyStatus_EXCEPTION(status)) {
561 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800562 }
563
Victor Stinner331a6a52019-05-27 16:39:22 +0200564 status = _PyInterpreterState_Enable(runtime);
565 if (_PyStatus_EXCEPTION(status)) {
566 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800567 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200568 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100569}
Victor Stinnera7368ac2017-11-15 18:11:45 -0800570
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100571
Victor Stinner331a6a52019-05-27 16:39:22 +0200572static PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200573init_interp_create_gil(PyThreadState *tstate)
574{
575 PyStatus status;
576
577 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
578 only called here. */
579 _PyEval_FiniGIL(tstate);
580
581 /* Auto-thread-state API */
582 status = _PyGILState_Init(tstate);
583 if (_PyStatus_EXCEPTION(status)) {
584 return status;
585 }
586
587 /* Create the GIL and take it */
588 status = _PyEval_InitGIL(tstate);
589 if (_PyStatus_EXCEPTION(status)) {
590 return status;
591 }
592
593 return _PyStatus_OK();
594}
595
596
597static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200598pycore_create_interpreter(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200599 const PyConfig *config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200600 PyThreadState **tstate_p)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100601{
602 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnerda273412017-12-15 01:46:02 +0100603 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200604 return _PyStatus_ERR("can't make main interpreter");
Victor Stinnerda273412017-12-15 01:46:02 +0100605 }
606
Victor Stinner048a3562020-11-05 00:45:56 +0100607 PyStatus status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200608 if (_PyStatus_EXCEPTION(status)) {
609 return status;
Victor Stinnerda273412017-12-15 01:46:02 +0100610 }
Nick Coghland6009512014-11-20 21:39:37 +1000611
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200612 PyThreadState *tstate = PyThreadState_New(interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +0200613 if (tstate == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200614 return _PyStatus_ERR("can't make first thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +0200615 }
Nick Coghland6009512014-11-20 21:39:37 +1000616 (void) PyThreadState_Swap(tstate);
617
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200618 status = init_interp_create_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100619 if (_PyStatus_EXCEPTION(status)) {
620 return status;
621 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100622
Victor Stinnerb45d2592019-06-20 00:05:23 +0200623 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +0200624 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100625}
Nick Coghland6009512014-11-20 21:39:37 +1000626
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100627
Victor Stinner331a6a52019-05-27 16:39:22 +0200628static PyStatus
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100629pycore_init_types(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100630{
Victor Stinner444b39b2019-11-20 01:18:11 +0100631 PyStatus status;
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100632 int is_main_interp = _Py_IsMainInterpreter(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100633
Victor Stinner01b1cc12019-11-20 02:27:56 +0100634 status = _PyGC_Init(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100635 if (_PyStatus_EXCEPTION(status)) {
636 return status;
637 }
638
Victor Stinner0430dfa2020-06-24 15:21:54 +0200639 // Create the empty tuple singleton. It must be created before the first
640 // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
641 // for example.
642 status = _PyTuple_Init(tstate);
643 if (_PyStatus_EXCEPTION(status)) {
644 return status;
645 }
646
Victor Stinnere7e699e2019-11-20 12:08:13 +0100647 if (is_main_interp) {
648 status = _PyTypes_Init();
649 if (_PyStatus_EXCEPTION(status)) {
650 return status;
651 }
Victor Stinner630c8df2019-12-17 13:02:18 +0100652 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100653
Victor Stinner630c8df2019-12-17 13:02:18 +0100654 if (!_PyLong_Init(tstate)) {
655 return _PyStatus_ERR("can't init longs");
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100656 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100657
Victor Stinnerf363d0a2020-06-24 00:10:40 +0200658 status = _PyUnicode_Init(tstate);
659 if (_PyStatus_EXCEPTION(status)) {
660 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100661 }
662
Victor Stinner91698d82020-06-25 14:07:40 +0200663 status = _PyBytes_Init(tstate);
664 if (_PyStatus_EXCEPTION(status)) {
665 return status;
666 }
667
Victor Stinner281cce12020-06-23 22:55:46 +0200668 status = _PyExc_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200669 if (_PyStatus_EXCEPTION(status)) {
670 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100671 }
672
Victor Stinnere7e699e2019-11-20 12:08:13 +0100673 if (is_main_interp) {
674 if (!_PyFloat_Init()) {
675 return _PyStatus_ERR("can't init float");
676 }
Nick Coghland6009512014-11-20 21:39:37 +1000677
Victor Stinnere7e699e2019-11-20 12:08:13 +0100678 if (_PyStructSequence_Init() < 0) {
679 return _PyStatus_ERR("can't initialize structseq");
680 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100681 }
Victor Stinneref9d9b62019-05-22 11:28:22 +0200682
Victor Stinner331a6a52019-05-27 16:39:22 +0200683 status = _PyErr_Init();
684 if (_PyStatus_EXCEPTION(status)) {
685 return status;
Victor Stinneref9d9b62019-05-22 11:28:22 +0200686 }
687
Victor Stinnere7e699e2019-11-20 12:08:13 +0100688 if (is_main_interp) {
689 if (!_PyContext_Init()) {
690 return _PyStatus_ERR("can't init context");
691 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100692 }
693
Victor Stinneref75a622020-11-12 15:14:13 +0100694 if (_PyWarnings_InitState(tstate) < 0) {
695 return _PyStatus_ERR("can't initialize warnings");
696 }
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100697
698 status = _PyAtExit_Init(tstate);
699 if (_PyStatus_EXCEPTION(status)) {
700 return status;
701 }
702
Victor Stinner331a6a52019-05-27 16:39:22 +0200703 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100704}
705
706
Victor Stinner331a6a52019-05-27 16:39:22 +0200707static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200708pycore_init_builtins(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100709{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100710 assert(!_PyErr_Occurred(tstate));
711
Victor Stinnerb45d2592019-06-20 00:05:23 +0200712 PyObject *bimod = _PyBuiltin_Init(tstate);
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100713 if (bimod == NULL) {
Victor Stinner2582d462019-11-22 19:24:49 +0100714 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100715 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100716
Victor Stinner2582d462019-11-22 19:24:49 +0100717 PyInterpreterState *interp = tstate->interp;
718 if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
719 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100720 }
Victor Stinner2582d462019-11-22 19:24:49 +0100721
722 PyObject *builtins_dict = PyModule_GetDict(bimod);
723 if (builtins_dict == NULL) {
724 goto error;
725 }
726 Py_INCREF(builtins_dict);
727 interp->builtins = builtins_dict;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100728
Victor Stinner331a6a52019-05-27 16:39:22 +0200729 PyStatus status = _PyBuiltins_AddExceptions(bimod);
730 if (_PyStatus_EXCEPTION(status)) {
731 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100732 }
Victor Stinner2582d462019-11-22 19:24:49 +0100733
734 interp->builtins_copy = PyDict_Copy(interp->builtins);
735 if (interp->builtins_copy == NULL) {
736 goto error;
737 }
Pablo Galindob96c6b02019-12-04 11:19:59 +0000738 Py_DECREF(bimod);
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100739
Victor Stinner62230712020-11-18 23:18:29 +0100740 // Get the __import__ function
741 PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
742 "__import__");
743 if (import_func == NULL) {
744 goto error;
745 }
746 interp->import_func = Py_NewRef(import_func);
747
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100748 assert(!_PyErr_Occurred(tstate));
749
Victor Stinner331a6a52019-05-27 16:39:22 +0200750 return _PyStatus_OK();
Victor Stinner2582d462019-11-22 19:24:49 +0100751
752error:
Pablo Galindob96c6b02019-12-04 11:19:59 +0000753 Py_XDECREF(bimod);
Victor Stinner2582d462019-11-22 19:24:49 +0100754 return _PyStatus_ERR("can't initialize builtins module");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100755}
756
757
Victor Stinner331a6a52019-05-27 16:39:22 +0200758static PyStatus
Victor Stinnerd863ade2019-12-06 03:37:07 +0100759pycore_interp_init(PyThreadState *tstate)
760{
761 PyStatus status;
Victor Stinner080ee5a2019-12-08 21:55:58 +0100762 PyObject *sysmod = NULL;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100763
764 status = pycore_init_types(tstate);
765 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100766 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100767 }
768
Victor Stinnerd863ade2019-12-06 03:37:07 +0100769 status = _PySys_Create(tstate, &sysmod);
770 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100771 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100772 }
773
774 status = pycore_init_builtins(tstate);
775 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100776 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100777 }
778
Victor Stinneref75a622020-11-12 15:14:13 +0100779 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
780 if (config->_install_importlib) {
781 /* This call sets up builtin and frozen import support */
782 if (init_importlib(tstate, sysmod) < 0) {
783 return _PyStatus_ERR("failed to initialize importlib");
784 }
785 }
Victor Stinner080ee5a2019-12-08 21:55:58 +0100786
787done:
788 /* sys.modules['sys'] contains a strong reference to the module */
789 Py_XDECREF(sysmod);
790 return status;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100791}
792
793
794static PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +0200795pyinit_config(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200796 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200797 const PyConfig *config)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100798{
Victor Stinner331a6a52019-05-27 16:39:22 +0200799 PyStatus status = pycore_init_runtime(runtime, config);
800 if (_PyStatus_EXCEPTION(status)) {
801 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100802 }
803
Victor Stinnerb45d2592019-06-20 00:05:23 +0200804 PyThreadState *tstate;
805 status = pycore_create_interpreter(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200806 if (_PyStatus_EXCEPTION(status)) {
807 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100808 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200809 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100810
Victor Stinnerd863ade2019-12-06 03:37:07 +0100811 status = pycore_interp_init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200812 if (_PyStatus_EXCEPTION(status)) {
813 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100814 }
Eric Snow1abcf672017-05-23 21:46:51 -0700815
816 /* Only when we get here is the runtime core fully initialized */
Victor Stinner43125222019-04-24 18:23:53 +0200817 runtime->core_initialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200818 return _PyStatus_OK();
Eric Snow1abcf672017-05-23 21:46:51 -0700819}
820
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100821
Victor Stinner331a6a52019-05-27 16:39:22 +0200822PyStatus
823_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100824{
Victor Stinner331a6a52019-05-27 16:39:22 +0200825 PyStatus status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100826
Victor Stinner6d1c4672019-05-20 11:02:00 +0200827 if (src_config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200828 return _PyStatus_ERR("preinitialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +0200829 }
830
Victor Stinner331a6a52019-05-27 16:39:22 +0200831 status = _PyRuntime_Initialize();
832 if (_PyStatus_EXCEPTION(status)) {
833 return status;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100834 }
Victor Stinner43125222019-04-24 18:23:53 +0200835 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100836
Victor Stinnerd3b90412019-09-17 23:59:51 +0200837 if (runtime->preinitialized) {
Victor Stinnerf72346c2019-03-25 17:54:58 +0100838 /* If it's already configured: ignored the new configuration */
Victor Stinner331a6a52019-05-27 16:39:22 +0200839 return _PyStatus_OK();
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100840 }
841
Victor Stinnerd3b90412019-09-17 23:59:51 +0200842 /* Note: preinitialized remains 1 on error, it is only set to 0
843 at exit on success. */
844 runtime->preinitializing = 1;
845
Victor Stinner331a6a52019-05-27 16:39:22 +0200846 PyPreConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200847
848 status = _PyPreConfig_InitFromPreConfig(&config, src_config);
849 if (_PyStatus_EXCEPTION(status)) {
850 return status;
851 }
Victor Stinnerf72346c2019-03-25 17:54:58 +0100852
Victor Stinner331a6a52019-05-27 16:39:22 +0200853 status = _PyPreConfig_Read(&config, args);
854 if (_PyStatus_EXCEPTION(status)) {
855 return status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100856 }
857
Victor Stinner331a6a52019-05-27 16:39:22 +0200858 status = _PyPreConfig_Write(&config);
859 if (_PyStatus_EXCEPTION(status)) {
860 return status;
Victor Stinnerf72346c2019-03-25 17:54:58 +0100861 }
862
Victor Stinnerd3b90412019-09-17 23:59:51 +0200863 runtime->preinitializing = 0;
864 runtime->preinitialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200865 return _PyStatus_OK();
Victor Stinnerf72346c2019-03-25 17:54:58 +0100866}
867
Victor Stinner70005ac2019-05-02 15:25:34 -0400868
Victor Stinner331a6a52019-05-27 16:39:22 +0200869PyStatus
870Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
Victor Stinnerf72346c2019-03-25 17:54:58 +0100871{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100872 _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400873 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinnerf29084d2019-03-20 02:20:13 +0100874}
875
876
Victor Stinner331a6a52019-05-27 16:39:22 +0200877PyStatus
878Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
Victor Stinner20004952019-03-26 02:31:11 +0100879{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100880 _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400881 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinner20004952019-03-26 02:31:11 +0100882}
883
884
Victor Stinner331a6a52019-05-27 16:39:22 +0200885PyStatus
886Py_PreInitialize(const PyPreConfig *src_config)
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100887{
Victor Stinner70005ac2019-05-02 15:25:34 -0400888 return _Py_PreInitializeFromPyArgv(src_config, NULL);
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100889}
890
891
Victor Stinner331a6a52019-05-27 16:39:22 +0200892PyStatus
893_Py_PreInitializeFromConfig(const PyConfig *config,
894 const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100895{
Victor Stinner331a6a52019-05-27 16:39:22 +0200896 assert(config != NULL);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200897
Victor Stinner331a6a52019-05-27 16:39:22 +0200898 PyStatus status = _PyRuntime_Initialize();
899 if (_PyStatus_EXCEPTION(status)) {
900 return status;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200901 }
902 _PyRuntimeState *runtime = &_PyRuntime;
903
Victor Stinnerd3b90412019-09-17 23:59:51 +0200904 if (runtime->preinitialized) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200905 /* Already initialized: do nothing */
Victor Stinner331a6a52019-05-27 16:39:22 +0200906 return _PyStatus_OK();
Victor Stinner70005ac2019-05-02 15:25:34 -0400907 }
Victor Stinnercab5d072019-05-17 19:01:14 +0200908
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200910
Victor Stinner3c30a762019-10-01 10:56:37 +0200911 _PyPreConfig_InitFromConfig(&preconfig, config);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200912
Victor Stinner331a6a52019-05-27 16:39:22 +0200913 if (!config->parse_argv) {
914 return Py_PreInitialize(&preconfig);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200915 }
916 else if (args == NULL) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200917 _PyArgv config_args = {
918 .use_bytes_argv = 0,
Victor Stinner331a6a52019-05-27 16:39:22 +0200919 .argc = config->argv.length,
920 .wchar_argv = config->argv.items};
Victor Stinner6d1c4672019-05-20 11:02:00 +0200921 return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200922 }
923 else {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200924 return _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200925 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100926}
927
928
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100929/* Begin interpreter initialization
930 *
931 * On return, the first thread and interpreter state have been created,
932 * but the compiler, signal handling, multithreading and
933 * multiple interpreter support, and codec infrastructure are not yet
934 * available.
935 *
936 * The import system will support builtin and frozen modules only.
937 * The only supported io is writing to sys.stderr
938 *
939 * If any operation invoked by this function fails, a fatal error is
940 * issued and the function does not return.
941 *
942 * Any code invoked from this function should *not* assume it has access
943 * to the Python C API (unless the API is explicitly listed as being
944 * safe to call without calling Py_Initialize first)
945 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200946static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200947pyinit_core(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200948 const PyConfig *src_config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200949 PyThreadState **tstate_p)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200950{
Victor Stinner331a6a52019-05-27 16:39:22 +0200951 PyStatus status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200952
Victor Stinner331a6a52019-05-27 16:39:22 +0200953 status = _Py_PreInitializeFromConfig(src_config, NULL);
954 if (_PyStatus_EXCEPTION(status)) {
955 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200956 }
957
Victor Stinner331a6a52019-05-27 16:39:22 +0200958 PyConfig config;
Victor Stinner048a3562020-11-05 00:45:56 +0100959 PyConfig_InitPythonConfig(&config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200960
Victor Stinner331a6a52019-05-27 16:39:22 +0200961 status = _PyConfig_Copy(&config, src_config);
962 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200963 goto done;
964 }
965
Victor Stinner9e1b8282020-11-10 13:21:52 +0100966 // Read the configuration, but don't compute the path configuration
967 // (it is computed in the main init).
968 status = _PyConfig_Read(&config, 0);
Victor Stinner331a6a52019-05-27 16:39:22 +0200969 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200970 goto done;
971 }
972
973 if (!runtime->core_initialized) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200974 status = pyinit_config(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200975 }
976 else {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200977 status = pyinit_core_reconfigure(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200978 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200979 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200980 goto done;
981 }
982
983done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200984 PyConfig_Clear(&config);
985 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200986}
987
Victor Stinner5ac27a52019-03-27 13:40:14 +0100988
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200989/* Py_Initialize() has already been called: update the main interpreter
990 configuration. Example of bpo-34008: Py_Main() called after
991 Py_Initialize(). */
Victor Stinner331a6a52019-05-27 16:39:22 +0200992static PyStatus
Victor Stinneraf1d64d2020-11-04 17:34:34 +0100993pyinit_main_reconfigure(PyThreadState *tstate)
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200994{
Victor Stinner9e1b8282020-11-10 13:21:52 +0100995 if (interpreter_update_config(tstate, 0) < 0) {
996 return _PyStatus_ERR("fail to reconfigure Python");
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200997 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200998 return _PyStatus_OK();
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200999}
1000
Victor Stinnerb0051362019-11-22 17:52:42 +01001001
1002static PyStatus
1003init_interp_main(PyThreadState *tstate)
1004{
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001005 assert(!_PyErr_Occurred(tstate));
1006
Victor Stinnerb0051362019-11-22 17:52:42 +01001007 PyStatus status;
1008 int is_main_interp = _Py_IsMainInterpreter(tstate);
1009 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +02001010 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
Victor Stinnerb0051362019-11-22 17:52:42 +01001011
1012 if (!config->_install_importlib) {
1013 /* Special mode for freeze_importlib: run with no import system
1014 *
1015 * This means anything which needs support from extension modules
1016 * or pure Python code in the standard library won't work.
1017 */
1018 if (is_main_interp) {
1019 interp->runtime->initialized = 1;
1020 }
1021 return _PyStatus_OK();
1022 }
1023
Victor Stinner9e1b8282020-11-10 13:21:52 +01001024 // Compute the path configuration
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001025 status = _PyConfig_InitPathConfig(&interp->config, 1);
Victor Stinner9e1b8282020-11-10 13:21:52 +01001026 if (_PyStatus_EXCEPTION(status)) {
1027 return status;
1028 }
1029
Victor Stinner9e1b8282020-11-10 13:21:52 +01001030 if (interpreter_update_config(tstate, 1) < 0) {
1031 return _PyStatus_ERR("failed to update the Python config");
Victor Stinnerb0051362019-11-22 17:52:42 +01001032 }
1033
1034 status = init_importlib_external(tstate);
1035 if (_PyStatus_EXCEPTION(status)) {
1036 return status;
1037 }
1038
1039 if (is_main_interp) {
1040 /* initialize the faulthandler module */
1041 status = _PyFaulthandler_Init(config->faulthandler);
1042 if (_PyStatus_EXCEPTION(status)) {
1043 return status;
1044 }
1045 }
1046
1047 status = _PyUnicode_InitEncodings(tstate);
1048 if (_PyStatus_EXCEPTION(status)) {
1049 return status;
1050 }
1051
1052 if (is_main_interp) {
Victor Stinner296a7962020-11-17 16:22:23 +01001053 if (_PySignal_Init(config->install_signal_handlers) < 0) {
1054 return _PyStatus_ERR("can't initialize signals");
Victor Stinnerb0051362019-11-22 17:52:42 +01001055 }
1056
1057 if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1058 return _PyStatus_ERR("can't initialize tracemalloc");
1059 }
1060 }
1061
1062 status = init_sys_streams(tstate);
1063 if (_PyStatus_EXCEPTION(status)) {
1064 return status;
1065 }
1066
Andy Lester75cd5bf2020-03-12 02:49:05 -05001067 status = init_set_builtins_open();
Victor Stinnerb0051362019-11-22 17:52:42 +01001068 if (_PyStatus_EXCEPTION(status)) {
1069 return status;
1070 }
1071
1072 status = add_main_module(interp);
1073 if (_PyStatus_EXCEPTION(status)) {
1074 return status;
1075 }
1076
1077 if (is_main_interp) {
1078 /* Initialize warnings. */
1079 PyObject *warnoptions = PySys_GetObject("warnoptions");
1080 if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1081 {
1082 PyObject *warnings_module = PyImport_ImportModule("warnings");
1083 if (warnings_module == NULL) {
1084 fprintf(stderr, "'import warnings' failed; traceback:\n");
1085 _PyErr_Print(tstate);
1086 }
1087 Py_XDECREF(warnings_module);
1088 }
1089
1090 interp->runtime->initialized = 1;
1091 }
1092
1093 if (config->site_import) {
1094 status = init_import_site();
1095 if (_PyStatus_EXCEPTION(status)) {
1096 return status;
1097 }
1098 }
1099
1100 if (is_main_interp) {
1101#ifndef MS_WINDOWS
1102 emit_stderr_warning_for_legacy_locale(interp->runtime);
1103#endif
1104 }
1105
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001106 assert(!_PyErr_Occurred(tstate));
1107
Victor Stinnerb0051362019-11-22 17:52:42 +01001108 return _PyStatus_OK();
1109}
1110
1111
Eric Snowc7ec9982017-05-23 23:00:52 -07001112/* Update interpreter state based on supplied configuration settings
1113 *
1114 * After calling this function, most of the restrictions on the interpreter
1115 * are lifted. The only remaining incomplete settings are those related
1116 * to the main module (sys.argv[0], __main__ metadata)
1117 *
1118 * Calling this when the interpreter is not initializing, is already
1119 * initialized or without a valid current thread state is a fatal error.
1120 * Other errors should be reported as normal Python exceptions with a
1121 * non-zero return code.
1122 */
Victor Stinner331a6a52019-05-27 16:39:22 +02001123static PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +01001124pyinit_main(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -07001125{
Victor Stinnerb0051362019-11-22 17:52:42 +01001126 PyInterpreterState *interp = tstate->interp;
1127 if (!interp->runtime->core_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001128 return _PyStatus_ERR("runtime core not initialized");
Eric Snowc7ec9982017-05-23 23:00:52 -07001129 }
Eric Snowc7ec9982017-05-23 23:00:52 -07001130
Victor Stinnerb0051362019-11-22 17:52:42 +01001131 if (interp->runtime->initialized) {
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001132 return pyinit_main_reconfigure(tstate);
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001133 }
1134
Victor Stinnerb0051362019-11-22 17:52:42 +01001135 PyStatus status = init_interp_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001136 if (_PyStatus_EXCEPTION(status)) {
1137 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001138 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001139 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001140}
1141
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001142
Victor Stinner331a6a52019-05-27 16:39:22 +02001143PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +02001144Py_InitializeFromConfig(const PyConfig *config)
Eric Snow1abcf672017-05-23 21:46:51 -07001145{
Victor Stinner6d1c4672019-05-20 11:02:00 +02001146 if (config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001147 return _PyStatus_ERR("initialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +02001148 }
1149
Victor Stinner331a6a52019-05-27 16:39:22 +02001150 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001151
Victor Stinner331a6a52019-05-27 16:39:22 +02001152 status = _PyRuntime_Initialize();
1153 if (_PyStatus_EXCEPTION(status)) {
1154 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001155 }
1156 _PyRuntimeState *runtime = &_PyRuntime;
1157
Victor Stinnerb45d2592019-06-20 00:05:23 +02001158 PyThreadState *tstate = NULL;
1159 status = pyinit_core(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001160 if (_PyStatus_EXCEPTION(status)) {
1161 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001162 }
Victor Stinnerda7933e2020-04-13 03:04:28 +02001163 config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerbc8ac6b2017-11-30 18:03:55 +01001164
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001165 if (config->_init_main) {
Victor Stinner01b1cc12019-11-20 02:27:56 +01001166 status = pyinit_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001167 if (_PyStatus_EXCEPTION(status)) {
1168 return status;
Victor Stinner484f20d2019-03-27 02:04:16 +01001169 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001170 }
Victor Stinner484f20d2019-03-27 02:04:16 +01001171
Victor Stinner331a6a52019-05-27 16:39:22 +02001172 return _PyStatus_OK();
Victor Stinner5ac27a52019-03-27 13:40:14 +01001173}
1174
1175
Eric Snow1abcf672017-05-23 21:46:51 -07001176void
Nick Coghland6009512014-11-20 21:39:37 +10001177Py_InitializeEx(int install_sigs)
1178{
Victor Stinner331a6a52019-05-27 16:39:22 +02001179 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001180
Victor Stinner331a6a52019-05-27 16:39:22 +02001181 status = _PyRuntime_Initialize();
1182 if (_PyStatus_EXCEPTION(status)) {
1183 Py_ExitStatusException(status);
Victor Stinner43125222019-04-24 18:23:53 +02001184 }
1185 _PyRuntimeState *runtime = &_PyRuntime;
1186
1187 if (runtime->initialized) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001188 /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1189 return;
1190 }
1191
Victor Stinner331a6a52019-05-27 16:39:22 +02001192 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001193 _PyConfig_InitCompatConfig(&config);
Victor Stinner441b10c2019-09-28 04:28:35 +02001194
Victor Stinner1dc6e392018-07-25 02:49:17 +02001195 config.install_signal_handlers = install_sigs;
1196
Victor Stinner331a6a52019-05-27 16:39:22 +02001197 status = Py_InitializeFromConfig(&config);
1198 if (_PyStatus_EXCEPTION(status)) {
1199 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001200 }
Nick Coghland6009512014-11-20 21:39:37 +10001201}
1202
1203void
1204Py_Initialize(void)
1205{
1206 Py_InitializeEx(1);
1207}
1208
1209
Victor Stinneraf1d64d2020-11-04 17:34:34 +01001210PyStatus
1211_Py_InitializeMain(void)
1212{
1213 PyStatus status = _PyRuntime_Initialize();
1214 if (_PyStatus_EXCEPTION(status)) {
1215 return status;
1216 }
1217 _PyRuntimeState *runtime = &_PyRuntime;
1218 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1219 return pyinit_main(tstate);
1220}
1221
1222
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001223static void
1224finalize_modules_delete_special(PyThreadState *tstate, int verbose)
1225{
1226 // List of names to clear in sys
1227 static const char * const sys_deletes[] = {
1228 "path", "argv", "ps1", "ps2",
1229 "last_type", "last_value", "last_traceback",
1230 "path_hooks", "path_importer_cache", "meta_path",
1231 "__interactivehook__",
1232 NULL
1233 };
1234
1235 static const char * const sys_files[] = {
1236 "stdin", "__stdin__",
1237 "stdout", "__stdout__",
1238 "stderr", "__stderr__",
1239 NULL
1240 };
1241
1242 PyInterpreterState *interp = tstate->interp;
1243 if (verbose) {
1244 PySys_WriteStderr("# clear builtins._\n");
1245 }
1246 if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
1247 PyErr_WriteUnraisable(NULL);
1248 }
1249
1250 const char * const *p;
1251 for (p = sys_deletes; *p != NULL; p++) {
1252 if (verbose) {
1253 PySys_WriteStderr("# clear sys.%s\n", *p);
1254 }
1255 if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
1256 PyErr_WriteUnraisable(NULL);
1257 }
1258 }
1259 for (p = sys_files; *p != NULL; p+=2) {
1260 const char *name = p[0];
1261 const char *orig_name = p[1];
1262 if (verbose) {
1263 PySys_WriteStderr("# restore sys.%s\n", name);
1264 }
1265 PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
1266 orig_name);
1267 if (value == NULL) {
1268 if (_PyErr_Occurred(tstate)) {
1269 PyErr_WriteUnraisable(NULL);
1270 }
1271 value = Py_None;
1272 }
1273 if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
1274 PyErr_WriteUnraisable(NULL);
1275 }
1276 }
1277}
1278
1279
1280static PyObject*
1281finalize_remove_modules(PyObject *modules, int verbose)
1282{
1283 PyObject *weaklist = PyList_New(0);
1284 if (weaklist == NULL) {
1285 PyErr_WriteUnraisable(NULL);
1286 }
1287
1288#define STORE_MODULE_WEAKREF(name, mod) \
1289 if (weaklist != NULL) { \
1290 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1291 if (wr) { \
1292 PyObject *tup = PyTuple_Pack(2, name, wr); \
1293 if (!tup || PyList_Append(weaklist, tup) < 0) { \
1294 PyErr_WriteUnraisable(NULL); \
1295 } \
1296 Py_XDECREF(tup); \
1297 Py_DECREF(wr); \
1298 } \
1299 else { \
1300 PyErr_WriteUnraisable(NULL); \
1301 } \
1302 }
1303
1304#define CLEAR_MODULE(name, mod) \
1305 if (PyModule_Check(mod)) { \
1306 if (verbose && PyUnicode_Check(name)) { \
1307 PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
1308 } \
1309 STORE_MODULE_WEAKREF(name, mod); \
1310 if (PyObject_SetItem(modules, name, Py_None) < 0) { \
1311 PyErr_WriteUnraisable(NULL); \
1312 } \
1313 }
1314
1315 if (PyDict_CheckExact(modules)) {
1316 Py_ssize_t pos = 0;
1317 PyObject *key, *value;
1318 while (PyDict_Next(modules, &pos, &key, &value)) {
1319 CLEAR_MODULE(key, value);
1320 }
1321 }
1322 else {
1323 PyObject *iterator = PyObject_GetIter(modules);
1324 if (iterator == NULL) {
1325 PyErr_WriteUnraisable(NULL);
1326 }
1327 else {
1328 PyObject *key;
1329 while ((key = PyIter_Next(iterator))) {
1330 PyObject *value = PyObject_GetItem(modules, key);
1331 if (value == NULL) {
1332 PyErr_WriteUnraisable(NULL);
1333 continue;
1334 }
1335 CLEAR_MODULE(key, value);
1336 Py_DECREF(value);
1337 Py_DECREF(key);
1338 }
1339 if (PyErr_Occurred()) {
1340 PyErr_WriteUnraisable(NULL);
1341 }
1342 Py_DECREF(iterator);
1343 }
1344 }
1345#undef CLEAR_MODULE
1346#undef STORE_MODULE_WEAKREF
1347
1348 return weaklist;
1349}
1350
1351
1352static void
1353finalize_clear_modules_dict(PyObject *modules)
1354{
1355 if (PyDict_CheckExact(modules)) {
1356 PyDict_Clear(modules);
1357 }
1358 else {
1359 _Py_IDENTIFIER(clear);
1360 if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) {
1361 PyErr_WriteUnraisable(NULL);
1362 }
1363 }
1364}
1365
1366
1367static void
1368finalize_restore_builtins(PyThreadState *tstate)
1369{
1370 PyInterpreterState *interp = tstate->interp;
1371 PyObject *dict = PyDict_Copy(interp->builtins);
1372 if (dict == NULL) {
1373 PyErr_WriteUnraisable(NULL);
1374 }
1375 PyDict_Clear(interp->builtins);
1376 if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
1377 _PyErr_Clear(tstate);
1378 }
1379 Py_XDECREF(dict);
1380}
1381
1382
1383static void
1384finalize_modules_clear_weaklist(PyInterpreterState *interp,
1385 PyObject *weaklist, int verbose)
1386{
1387 // First clear modules imported later
1388 for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
1389 PyObject *tup = PyList_GET_ITEM(weaklist, i);
1390 PyObject *name = PyTuple_GET_ITEM(tup, 0);
1391 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
1392 if (mod == Py_None) {
1393 continue;
1394 }
1395 assert(PyModule_Check(mod));
1396 PyObject *dict = PyModule_GetDict(mod);
1397 if (dict == interp->builtins || dict == interp->sysdict) {
1398 continue;
1399 }
1400 Py_INCREF(mod);
1401 if (verbose && PyUnicode_Check(name)) {
1402 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
1403 }
1404 _PyModule_Clear(mod);
1405 Py_DECREF(mod);
1406 }
1407}
1408
1409
1410static void
1411finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
1412{
1413 // Clear sys dict
1414 if (verbose) {
1415 PySys_FormatStderr("# cleanup[3] wiping sys\n");
1416 }
1417 _PyModule_ClearDict(interp->sysdict);
1418
1419 // Clear builtins dict
1420 if (verbose) {
1421 PySys_FormatStderr("# cleanup[3] wiping builtins\n");
1422 }
1423 _PyModule_ClearDict(interp->builtins);
1424}
1425
1426
1427/* Clear modules, as good as we can */
1428static void
1429finalize_modules(PyThreadState *tstate)
1430{
1431 PyInterpreterState *interp = tstate->interp;
1432 PyObject *modules = interp->modules;
1433 if (modules == NULL) {
1434 // Already done
1435 return;
1436 }
1437 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
1438
1439 // Delete some special builtins._ and sys attributes first. These are
1440 // common places where user values hide and people complain when their
1441 // destructors fail. Since the modules containing them are
1442 // deleted *last* of all, they would come too late in the normal
1443 // destruction order. Sigh.
1444 //
1445 // XXX Perhaps these precautions are obsolete. Who knows?
1446 finalize_modules_delete_special(tstate, verbose);
1447
1448 // Remove all modules from sys.modules, hoping that garbage collection
1449 // can reclaim most of them: set all sys.modules values to None.
1450 //
1451 // We prepare a list which will receive (name, weakref) tuples of
1452 // modules when they are removed from sys.modules. The name is used
1453 // for diagnosis messages (in verbose mode), while the weakref helps
1454 // detect those modules which have been held alive.
1455 PyObject *weaklist = finalize_remove_modules(modules, verbose);
1456
1457 // Clear the modules dict
1458 finalize_clear_modules_dict(modules);
1459
1460 // Restore the original builtins dict, to ensure that any
1461 // user data gets cleared.
1462 finalize_restore_builtins(tstate);
1463
1464 // Collect garbage
1465 _PyGC_CollectNoFail(tstate);
1466
1467 // Dump GC stats before it's too late, since it uses the warnings
1468 // machinery.
1469 _PyGC_DumpShutdownStats(tstate);
1470
1471 if (weaklist != NULL) {
1472 // Now, if there are any modules left alive, clear their globals to
1473 // minimize potential leaks. All C extension modules actually end
1474 // up here, since they are kept alive in the interpreter state.
1475 //
1476 // The special treatment of "builtins" here is because even
1477 // when it's not referenced as a module, its dictionary is
1478 // referenced by almost every module's __builtins__. Since
1479 // deleting a module clears its dictionary (even if there are
1480 // references left to it), we need to delete the "builtins"
1481 // module last. Likewise, we don't delete sys until the very
1482 // end because it is implicitly referenced (e.g. by print).
1483 //
1484 // Since dict is ordered in CPython 3.6+, modules are saved in
1485 // importing order. First clear modules imported later.
1486 finalize_modules_clear_weaklist(interp, weaklist, verbose);
1487 Py_DECREF(weaklist);
1488 }
1489
1490 // Clear sys and builtins modules dict
1491 finalize_clear_sys_builtins_dict(interp, verbose);
1492
1493 // Clear module dict copies stored in the interpreter state:
1494 // clear PyInterpreterState.modules_by_index and
1495 // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
1496 // initialization API)
1497 _PyInterpreterState_ClearModules(interp);
1498
1499 // Clear and delete the modules directory. Actual modules will
1500 // still be there only if imported during the execution of some
1501 // destructor.
1502 Py_SETREF(interp->modules, NULL);
1503
1504 // Collect garbage once more
1505 _PyGC_CollectNoFail(tstate);
1506}
1507
1508
Nick Coghland6009512014-11-20 21:39:37 +10001509/* Flush stdout and stderr */
1510
1511static int
1512file_is_closed(PyObject *fobj)
1513{
1514 int r;
1515 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1516 if (tmp == NULL) {
1517 PyErr_Clear();
1518 return 0;
1519 }
1520 r = PyObject_IsTrue(tmp);
1521 Py_DECREF(tmp);
1522 if (r < 0)
1523 PyErr_Clear();
1524 return r > 0;
1525}
1526
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001527
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001528static int
Nick Coghland6009512014-11-20 21:39:37 +10001529flush_std_files(void)
1530{
1531 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1532 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1533 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001534 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001535
1536 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001537 tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001538 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001539 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001540 status = -1;
1541 }
Nick Coghland6009512014-11-20 21:39:37 +10001542 else
1543 Py_DECREF(tmp);
1544 }
1545
1546 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001547 tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001548 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001549 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001550 status = -1;
1551 }
Nick Coghland6009512014-11-20 21:39:37 +10001552 else
1553 Py_DECREF(tmp);
1554 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001555
1556 return status;
Nick Coghland6009512014-11-20 21:39:37 +10001557}
1558
1559/* Undo the effect of Py_Initialize().
1560
1561 Beware: if multiple interpreter and/or thread states exist, these
1562 are not wiped out; only the current thread and interpreter state
1563 are deleted. But since everything else is deleted, those other
1564 interpreter and thread states should no longer be used.
1565
1566 (XXX We should do better, e.g. wipe out all interpreters and
1567 threads.)
1568
1569 Locking: as above.
1570
1571*/
1572
Victor Stinner7eee5be2019-11-20 10:38:34 +01001573
1574static void
Victor Stinner90db4652020-07-01 23:21:36 +02001575finalize_interp_types(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001576{
Victor Stinner281cce12020-06-23 22:55:46 +02001577 _PyExc_Fini(tstate);
Victor Stinner3744ed22020-06-05 01:39:24 +02001578 _PyFrame_Fini(tstate);
Victor Stinner78a02c22020-06-05 02:34:14 +02001579 _PyAsyncGen_Fini(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001580 _PyContext_Fini(tstate);
Victor Stinnerf4507232020-12-26 20:26:08 +01001581 _PyType_Fini(tstate);
Victor Stinnerea251802020-12-26 02:58:33 +01001582 // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
1583 // a dict internally.
Victor Stinner666ecfb2020-07-02 01:19:57 +02001584 _PyUnicode_ClearInterned(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001585
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001586 _PyDict_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001587 _PyList_Fini(tstate);
1588 _PyTuple_Fini(tstate);
1589
1590 _PySlice_Fini(tstate);
Victor Stinner3d483342019-11-22 12:27:50 +01001591
Victor Stinnerc41eed12020-06-23 15:54:35 +02001592 _PyBytes_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001593 _PyUnicode_Fini(tstate);
1594 _PyFloat_Fini(tstate);
1595 _PyLong_Fini(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001596}
1597
1598
1599static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001600finalize_interp_clear(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001601{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001602 int is_main_interp = _Py_IsMainInterpreter(tstate);
1603
Victor Stinner7eee5be2019-11-20 10:38:34 +01001604 /* Clear interpreter state and all thread states */
Victor Stinnereba5bf22020-10-30 22:51:02 +01001605 _PyInterpreterState_Clear(tstate);
Pablo Galindoac0e1c22019-12-04 11:51:03 +00001606
Kongedaa0fe02020-07-04 05:06:46 +08001607 /* Clear all loghooks */
1608 /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1609 Call _PySys_ClearAuditHooks when PyObject available. */
1610 if (is_main_interp) {
1611 _PySys_ClearAuditHooks(tstate);
1612 }
1613
Victor Stinner7907f8c2020-06-08 01:22:36 +02001614 if (is_main_interp) {
1615 _Py_HashRandomization_Fini();
1616 _PyArg_Fini();
1617 _Py_ClearFileSystemEncoding();
1618 }
1619
Victor Stinner90db4652020-07-01 23:21:36 +02001620 finalize_interp_types(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001621}
1622
1623
1624static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001625finalize_interp_delete(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001626{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001627 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001628 /* Cleanup auto-thread-state */
1629 _PyGILState_Fini(tstate);
1630 }
1631
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001632 /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1633 fail when it is being awaited by another running daemon thread (see
1634 bpo-9901). Instead pycore_create_interpreter() destroys the previously
1635 created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1636 called multiple times. */
1637
Victor Stinner7eee5be2019-11-20 10:38:34 +01001638 PyInterpreterState_Delete(tstate->interp);
1639}
1640
1641
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001642int
1643Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +10001644{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001645 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001646
Victor Stinner8e91c242019-04-24 17:24:01 +02001647 _PyRuntimeState *runtime = &_PyRuntime;
1648 if (!runtime->initialized) {
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001649 return status;
Victor Stinner8e91c242019-04-24 17:24:01 +02001650 }
Nick Coghland6009512014-11-20 21:39:37 +10001651
Victor Stinnere225beb2019-06-03 18:14:24 +02001652 /* Get current thread state and interpreter pointer */
1653 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner8e91c242019-04-24 17:24:01 +02001654
Victor Stinnerb45d2592019-06-20 00:05:23 +02001655 // Wrap up existing "threading"-module-created, non-daemon threads.
1656 wait_for_thread_shutdown(tstate);
1657
1658 // Make any remaining pending calls.
Victor Stinner2b1df452020-01-13 18:46:59 +01001659 _Py_FinishPendingCalls(tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001660
Nick Coghland6009512014-11-20 21:39:37 +10001661 /* The interpreter is still entirely intact at this point, and the
1662 * exit funcs may be relying on that. In particular, if some thread
1663 * or exit func is still waiting to do an import, the import machinery
1664 * expects Py_IsInitialized() to return true. So don't say the
Eric Snow842a2f02019-03-15 15:47:51 -06001665 * runtime is uninitialized until after the exit funcs have run.
Nick Coghland6009512014-11-20 21:39:37 +10001666 * Note that Threading.py uses an exit func to do a join on all the
1667 * threads created thru it, so this also protects pending imports in
1668 * the threads created via Threading.
1669 */
Nick Coghland6009512014-11-20 21:39:37 +10001670
Victor Stinnerb8fa1352020-12-15 14:34:19 +01001671 _PyAtExit_Call(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001672
Victor Stinnerda273412017-12-15 01:46:02 +01001673 /* Copy the core config, PyInterpreterState_Delete() free
1674 the core config memory */
Victor Stinner5d862462017-12-19 11:35:58 +01001675#ifdef Py_REF_DEBUG
Christian Heimes07f2ade2020-11-18 16:38:53 +01001676 int show_ref_count = tstate->interp->config.show_ref_count;
Victor Stinner5d862462017-12-19 11:35:58 +01001677#endif
1678#ifdef Py_TRACE_REFS
Christian Heimes07f2ade2020-11-18 16:38:53 +01001679 int dump_refs = tstate->interp->config.dump_refs;
Victor Stinner5d862462017-12-19 11:35:58 +01001680#endif
1681#ifdef WITH_PYMALLOC
Christian Heimes07f2ade2020-11-18 16:38:53 +01001682 int malloc_stats = tstate->interp->config.malloc_stats;
Victor Stinner5d862462017-12-19 11:35:58 +01001683#endif
Victor Stinner6bf992a2017-12-06 17:26:10 +01001684
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001685 /* Remaining daemon threads will automatically exit
1686 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
Victor Stinner7b3c2522020-03-07 00:24:23 +01001687 _PyRuntimeState_SetFinalizing(runtime, tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +02001688 runtime->initialized = 0;
1689 runtime->core_initialized = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001690
Victor Stinner9ad58ac2020-03-09 23:37:49 +01001691 /* Destroy the state of all threads of the interpreter, except of the
1692 current thread. In practice, only daemon threads should still be alive,
1693 except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1694 Clear frames of other threads to call objects destructors. Destructors
1695 will be called in the current Python thread. Since
1696 _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1697 can take the GIL at this point: if they try, they will exit
1698 immediately. */
1699 _PyThreadState_DeleteExcept(runtime, tstate);
1700
Victor Stinnere0deff32015-03-24 13:46:18 +01001701 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001702 if (flush_std_files() < 0) {
1703 status = -1;
1704 }
Nick Coghland6009512014-11-20 21:39:37 +10001705
1706 /* Disable signal handling */
Victor Stinner296a7962020-11-17 16:22:23 +01001707 _PySignal_Fini();
Nick Coghland6009512014-11-20 21:39:37 +10001708
1709 /* Collect garbage. This may call finalizers; it's nice to call these
1710 * before all modules are destroyed.
1711 * XXX If a __del__ or weakref callback is triggered here, and tries to
1712 * XXX import a module, bad things can happen, because Python no
1713 * XXX longer believes it's initialized.
1714 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
1715 * XXX is easy to provoke that way. I've also seen, e.g.,
1716 * XXX Exception exceptions.ImportError: 'No module named sha'
1717 * XXX in <function callback at 0x008F5718> ignored
1718 * XXX but I'm unclear on exactly how that one happens. In any case,
1719 * XXX I haven't seen a real-life report of either of these.
1720 */
Victor Stinner8b341482020-10-30 17:00:00 +01001721 PyGC_Collect();
Eric Snowdae02762017-09-14 00:35:58 -07001722
Nick Coghland6009512014-11-20 21:39:37 +10001723 /* Destroy all modules */
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001724 finalize_modules(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001725
Inada Naoki91234a12019-06-03 21:30:58 +09001726 /* Print debug stats if any */
1727 _PyEval_Fini();
1728
Victor Stinnere0deff32015-03-24 13:46:18 +01001729 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001730 if (flush_std_files() < 0) {
1731 status = -1;
1732 }
Nick Coghland6009512014-11-20 21:39:37 +10001733
1734 /* Collect final garbage. This disposes of cycles created by
1735 * class definitions, for example.
1736 * XXX This is disabled because it caused too many problems. If
1737 * XXX a __del__ or weakref callback triggers here, Python code has
1738 * XXX a hard time running, because even the sys module has been
1739 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1740 * XXX One symptom is a sequence of information-free messages
1741 * XXX coming from threads (if a __del__ or callback is invoked,
1742 * XXX other threads can execute too, and any exception they encounter
1743 * XXX triggers a comedy of errors as subsystem after subsystem
1744 * XXX fails to find what it *expects* to find in sys to help report
1745 * XXX the exception and consequent unexpected failures). I've also
1746 * XXX seen segfaults then, after adding print statements to the
1747 * XXX Python code getting called.
1748 */
1749#if 0
Łukasz Langafef7e942016-09-09 21:47:46 -07001750 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +10001751#endif
1752
1753 /* Disable tracemalloc after all Python objects have been destroyed,
1754 so it is possible to use tracemalloc in objects destructor. */
1755 _PyTraceMalloc_Fini();
1756
1757 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1758 _PyImport_Fini();
1759
Nick Coghland6009512014-11-20 21:39:37 +10001760 /* unload faulthandler module */
1761 _PyFaulthandler_Fini();
1762
Nick Coghland6009512014-11-20 21:39:37 +10001763 /* dump hash stats */
1764 _PyHash_Fini();
1765
Eric Snowdae02762017-09-14 00:35:58 -07001766#ifdef Py_REF_DEBUG
Victor Stinnerda273412017-12-15 01:46:02 +01001767 if (show_ref_count) {
Victor Stinner25420fe2017-11-20 18:12:22 -08001768 _PyDebug_PrintTotalRefs();
1769 }
Eric Snowdae02762017-09-14 00:35:58 -07001770#endif
Nick Coghland6009512014-11-20 21:39:37 +10001771
1772#ifdef Py_TRACE_REFS
1773 /* Display all objects still alive -- this can invoke arbitrary
1774 * __repr__ overrides, so requires a mostly-intact interpreter.
1775 * Alas, a lot of stuff may still be alive now that will be cleaned
1776 * up later.
1777 */
Victor Stinnerda273412017-12-15 01:46:02 +01001778 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001779 _Py_PrintReferences(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001780 }
Nick Coghland6009512014-11-20 21:39:37 +10001781#endif /* Py_TRACE_REFS */
1782
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001783 finalize_interp_clear(tstate);
1784 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001785
1786#ifdef Py_TRACE_REFS
1787 /* Display addresses (& refcnts) of all objects still alive.
1788 * An address can be used to find the repr of the object, printed
1789 * above by _Py_PrintReferences.
1790 */
Victor Stinnerda273412017-12-15 01:46:02 +01001791 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001792 _Py_PrintReferenceAddresses(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001793 }
Nick Coghland6009512014-11-20 21:39:37 +10001794#endif /* Py_TRACE_REFS */
Victor Stinner34be8072016-03-14 12:04:26 +01001795#ifdef WITH_PYMALLOC
Victor Stinnerda273412017-12-15 01:46:02 +01001796 if (malloc_stats) {
Victor Stinner6bf992a2017-12-06 17:26:10 +01001797 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be8072016-03-14 12:04:26 +01001798 }
Nick Coghland6009512014-11-20 21:39:37 +10001799#endif
1800
Victor Stinner8e91c242019-04-24 17:24:01 +02001801 call_ll_exitfuncs(runtime);
Victor Stinner9316ee42017-11-25 03:17:57 +01001802
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001803 _PyRuntime_Finalize();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001804 return status;
1805}
1806
1807void
1808Py_Finalize(void)
1809{
1810 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +10001811}
1812
Victor Stinnerb0051362019-11-22 17:52:42 +01001813
Nick Coghland6009512014-11-20 21:39:37 +10001814/* Create and initialize a new interpreter and thread, and return the
1815 new thread. This requires that Py_Initialize() has been called
1816 first.
1817
1818 Unsuccessful initialization yields a NULL pointer. Note that *no*
1819 exception information is available even in this case -- the
1820 exception information is held in the thread, and there is no
1821 thread.
1822
1823 Locking: as above.
1824
1825*/
1826
Victor Stinner331a6a52019-05-27 16:39:22 +02001827static PyStatus
Victor Stinner252346a2020-05-01 11:33:44 +02001828new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
Nick Coghland6009512014-11-20 21:39:37 +10001829{
Victor Stinner331a6a52019-05-27 16:39:22 +02001830 PyStatus status;
Nick Coghland6009512014-11-20 21:39:37 +10001831
Victor Stinner331a6a52019-05-27 16:39:22 +02001832 status = _PyRuntime_Initialize();
1833 if (_PyStatus_EXCEPTION(status)) {
1834 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001835 }
1836 _PyRuntimeState *runtime = &_PyRuntime;
1837
1838 if (!runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001839 return _PyStatus_ERR("Py_Initialize must be called first");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001840 }
Nick Coghland6009512014-11-20 21:39:37 +10001841
Victor Stinner8a1be612016-03-14 22:07:55 +01001842 /* Issue #10915, #15751: The GIL API doesn't work with multiple
1843 interpreters: disable PyGILState_Check(). */
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001844 runtime->gilstate.check_enabled = 0;
Victor Stinner8a1be612016-03-14 22:07:55 +01001845
Victor Stinner43125222019-04-24 18:23:53 +02001846 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001847 if (interp == NULL) {
1848 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001849 return _PyStatus_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001850 }
Nick Coghland6009512014-11-20 21:39:37 +10001851
Victor Stinner43125222019-04-24 18:23:53 +02001852 PyThreadState *tstate = PyThreadState_New(interp);
Nick Coghland6009512014-11-20 21:39:37 +10001853 if (tstate == NULL) {
1854 PyInterpreterState_Delete(interp);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001855 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001856 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001857 }
1858
Victor Stinner43125222019-04-24 18:23:53 +02001859 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001860
Eric Snow1abcf672017-05-23 21:46:51 -07001861 /* Copy the current interpreter config into the new interpreter */
Victor Stinnerda7933e2020-04-13 03:04:28 +02001862 const PyConfig *config;
Victor Stinner7be4e352020-05-05 20:27:47 +02001863#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Eric Snow1abcf672017-05-23 21:46:51 -07001864 if (save_tstate != NULL) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001865 config = _PyInterpreterState_GetConfig(save_tstate->interp);
Victor Stinner7be4e352020-05-05 20:27:47 +02001866 }
1867 else
1868#endif
1869 {
Eric Snow1abcf672017-05-23 21:46:51 -07001870 /* No current thread state, copy from the main interpreter */
1871 PyInterpreterState *main_interp = PyInterpreterState_Main();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001872 config = _PyInterpreterState_GetConfig(main_interp);
Victor Stinnerda273412017-12-15 01:46:02 +01001873 }
1874
Victor Stinner048a3562020-11-05 00:45:56 +01001875
1876 status = _PyConfig_Copy(&interp->config, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001877 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001878 goto error;
Victor Stinnerda273412017-12-15 01:46:02 +01001879 }
Victor Stinner252346a2020-05-01 11:33:44 +02001880 interp->config._isolated_interpreter = isolated_subinterpreter;
Eric Snow1abcf672017-05-23 21:46:51 -07001881
Victor Stinner0dd5e7a2020-05-05 20:16:37 +02001882 status = init_interp_create_gil(tstate);
1883 if (_PyStatus_EXCEPTION(status)) {
1884 goto error;
1885 }
1886
Victor Stinnerd863ade2019-12-06 03:37:07 +01001887 status = pycore_interp_init(tstate);
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001888 if (_PyStatus_EXCEPTION(status)) {
1889 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001890 }
1891
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001892 status = init_interp_main(tstate);
1893 if (_PyStatus_EXCEPTION(status)) {
1894 goto error;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001895 }
Nick Coghland6009512014-11-20 21:39:37 +10001896
Victor Stinnera7368ac2017-11-15 18:11:45 -08001897 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +02001898 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001899
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001900error:
Victor Stinnerb0051362019-11-22 17:52:42 +01001901 *tstate_p = NULL;
1902
1903 /* Oops, it didn't work. Undo it all. */
Nick Coghland6009512014-11-20 21:39:37 +10001904 PyErr_PrintEx(0);
1905 PyThreadState_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001906 PyThreadState_Delete(tstate);
1907 PyInterpreterState_Delete(interp);
Victor Stinner9da74302019-11-20 11:17:17 +01001908 PyThreadState_Swap(save_tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001909
Victor Stinnerb0051362019-11-22 17:52:42 +01001910 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001911}
1912
1913PyThreadState *
Victor Stinner252346a2020-05-01 11:33:44 +02001914_Py_NewInterpreter(int isolated_subinterpreter)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001915{
Stéphane Wirtel9e06d2b2019-03-18 17:10:29 +01001916 PyThreadState *tstate = NULL;
Victor Stinner252346a2020-05-01 11:33:44 +02001917 PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
Victor Stinner331a6a52019-05-27 16:39:22 +02001918 if (_PyStatus_EXCEPTION(status)) {
1919 Py_ExitStatusException(status);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001920 }
1921 return tstate;
1922
Nick Coghland6009512014-11-20 21:39:37 +10001923}
1924
Victor Stinner252346a2020-05-01 11:33:44 +02001925PyThreadState *
1926Py_NewInterpreter(void)
1927{
1928 return _Py_NewInterpreter(0);
1929}
1930
Nick Coghland6009512014-11-20 21:39:37 +10001931/* Delete an interpreter and its last thread. This requires that the
1932 given thread state is current, that the thread has no remaining
1933 frames, and that it is its interpreter's only remaining thread.
1934 It is a fatal error to violate these constraints.
1935
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001936 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +10001937 everything, regardless.)
1938
1939 Locking: as above.
1940
1941*/
1942
1943void
1944Py_EndInterpreter(PyThreadState *tstate)
1945{
1946 PyInterpreterState *interp = tstate->interp;
1947
Victor Stinnerb45d2592019-06-20 00:05:23 +02001948 if (tstate != _PyThreadState_GET()) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001949 Py_FatalError("thread is not current");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001950 }
1951 if (tstate->frame != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001952 Py_FatalError("thread still has a frame");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001953 }
Eric Snow5be45a62019-03-08 22:47:07 -07001954 interp->finalizing = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001955
Eric Snow842a2f02019-03-15 15:47:51 -06001956 // Wrap up existing "threading"-module-created, non-daemon threads.
Victor Stinnerb45d2592019-06-20 00:05:23 +02001957 wait_for_thread_shutdown(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001958
Victor Stinnerb8fa1352020-12-15 14:34:19 +01001959 _PyAtExit_Call(tstate);
Marcel Plch776407f2017-12-20 11:17:58 +01001960
Victor Stinnerb45d2592019-06-20 00:05:23 +02001961 if (tstate != interp->tstate_head || tstate->next != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001962 Py_FatalError("not the last thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001963 }
Nick Coghland6009512014-11-20 21:39:37 +10001964
Victor Stinnerdff1ad52020-10-30 18:03:28 +01001965 finalize_modules(tstate);
1966
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001967 finalize_interp_clear(tstate);
1968 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001969}
1970
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001971/* Add the __main__ module */
Nick Coghland6009512014-11-20 21:39:37 +10001972
Victor Stinner331a6a52019-05-27 16:39:22 +02001973static PyStatus
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001974add_main_module(PyInterpreterState *interp)
Nick Coghland6009512014-11-20 21:39:37 +10001975{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001976 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +10001977 m = PyImport_AddModule("__main__");
1978 if (m == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +02001979 return _PyStatus_ERR("can't create __main__ module");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001980
Nick Coghland6009512014-11-20 21:39:37 +10001981 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001982 ann_dict = PyDict_New();
1983 if ((ann_dict == NULL) ||
1984 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001985 return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001986 }
1987 Py_DECREF(ann_dict);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001988
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001989 if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
1990 if (PyErr_Occurred()) {
1991 return _PyStatus_ERR("Failed to test __main__.__builtins__");
1992 }
Nick Coghland6009512014-11-20 21:39:37 +10001993 PyObject *bimod = PyImport_ImportModule("builtins");
1994 if (bimod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001995 return _PyStatus_ERR("Failed to retrieve builtins module");
Nick Coghland6009512014-11-20 21:39:37 +10001996 }
1997 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001998 return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
Nick Coghland6009512014-11-20 21:39:37 +10001999 }
2000 Py_DECREF(bimod);
2001 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002002
Nick Coghland6009512014-11-20 21:39:37 +10002003 /* Main is a little special - imp.is_builtin("__main__") will return
2004 * False, but BuiltinImporter is still the most appropriate initial
2005 * setting for its __loader__ attribute. A more suitable value will
2006 * be set if __main__ gets further initialized later in the startup
2007 * process.
2008 */
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002009 loader = _PyDict_GetItemStringWithError(d, "__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10002010 if (loader == NULL || loader == Py_None) {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002011 if (PyErr_Occurred()) {
2012 return _PyStatus_ERR("Failed to test __main__.__loader__");
2013 }
Nick Coghland6009512014-11-20 21:39:37 +10002014 PyObject *loader = PyObject_GetAttrString(interp->importlib,
2015 "BuiltinImporter");
2016 if (loader == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002017 return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
Nick Coghland6009512014-11-20 21:39:37 +10002018 }
2019 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002020 return _PyStatus_ERR("Failed to initialize __main__.__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10002021 }
2022 Py_DECREF(loader);
2023 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002024 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002025}
2026
Nick Coghland6009512014-11-20 21:39:37 +10002027/* Import the site module (not into __main__ though) */
2028
Victor Stinner331a6a52019-05-27 16:39:22 +02002029static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002030init_import_site(void)
Nick Coghland6009512014-11-20 21:39:37 +10002031{
2032 PyObject *m;
2033 m = PyImport_ImportModule("site");
2034 if (m == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002035 return _PyStatus_ERR("Failed to import the site module");
Nick Coghland6009512014-11-20 21:39:37 +10002036 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002037 Py_DECREF(m);
Victor Stinner331a6a52019-05-27 16:39:22 +02002038 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002039}
2040
Victor Stinner874dbe82015-09-04 17:29:57 +02002041/* Check if a file descriptor is valid or not.
2042 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
2043static int
2044is_valid_fd(int fd)
2045{
Victor Stinner3092d6b2019-04-17 18:09:12 +02002046/* dup() is faster than fstat(): fstat() can require input/output operations,
2047 whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
2048 startup. Problem: dup() doesn't check if the file descriptor is valid on
2049 some platforms.
2050
2051 bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
2052 side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
2053 EBADF. FreeBSD has similar issue (bpo-32849).
2054
2055 Only use dup() on platforms where dup() is enough to detect invalid FD in
2056 corner cases: on Linux and Windows (bpo-32849). */
2057#if defined(__linux__) || defined(MS_WINDOWS)
2058 if (fd < 0) {
2059 return 0;
2060 }
2061 int fd2;
2062
2063 _Py_BEGIN_SUPPRESS_IPH
2064 fd2 = dup(fd);
2065 if (fd2 >= 0) {
2066 close(fd2);
2067 }
2068 _Py_END_SUPPRESS_IPH
2069
2070 return (fd2 >= 0);
2071#else
Victor Stinner1c4670e2017-05-04 00:45:56 +02002072 struct stat st;
2073 return (fstat(fd, &st) == 0);
Victor Stinner1c4670e2017-05-04 00:45:56 +02002074#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02002075}
2076
2077/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10002078static PyObject*
Victor Stinner331a6a52019-05-27 16:39:22 +02002079create_stdio(const PyConfig *config, PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002080 int fd, int write_mode, const char* name,
Victor Stinner709d23d2019-05-02 14:56:30 -04002081 const wchar_t* encoding, const wchar_t* errors)
Nick Coghland6009512014-11-20 21:39:37 +10002082{
2083 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
2084 const char* mode;
2085 const char* newline;
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002086 PyObject *line_buffering, *write_through;
Nick Coghland6009512014-11-20 21:39:37 +10002087 int buffering, isatty;
2088 _Py_IDENTIFIER(open);
2089 _Py_IDENTIFIER(isatty);
2090 _Py_IDENTIFIER(TextIOWrapper);
2091 _Py_IDENTIFIER(mode);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002092 const int buffered_stdio = config->buffered_stdio;
Nick Coghland6009512014-11-20 21:39:37 +10002093
Victor Stinner874dbe82015-09-04 17:29:57 +02002094 if (!is_valid_fd(fd))
2095 Py_RETURN_NONE;
2096
Nick Coghland6009512014-11-20 21:39:37 +10002097 /* stdin is always opened in buffered mode, first because it shouldn't
2098 make a difference in common use cases, second because TextIOWrapper
2099 depends on the presence of a read1() method which only exists on
2100 buffered streams.
2101 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002102 if (!buffered_stdio && write_mode)
Nick Coghland6009512014-11-20 21:39:37 +10002103 buffering = 0;
2104 else
2105 buffering = -1;
2106 if (write_mode)
2107 mode = "wb";
2108 else
2109 mode = "rb";
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002110 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
Nick Coghland6009512014-11-20 21:39:37 +10002111 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002112 Py_None, Py_None, /* encoding, errors */
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03002113 Py_None, Py_False); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10002114 if (buf == NULL)
2115 goto error;
2116
2117 if (buffering) {
2118 _Py_IDENTIFIER(raw);
2119 raw = _PyObject_GetAttrId(buf, &PyId_raw);
2120 if (raw == NULL)
2121 goto error;
2122 }
2123 else {
2124 raw = buf;
2125 Py_INCREF(raw);
2126 }
2127
Steve Dower39294992016-08-30 21:22:36 -07002128#ifdef MS_WINDOWS
2129 /* Windows console IO is always UTF-8 encoded */
2130 if (PyWindowsConsoleIO_Check(raw))
Victor Stinner709d23d2019-05-02 14:56:30 -04002131 encoding = L"utf-8";
Steve Dower39294992016-08-30 21:22:36 -07002132#endif
2133
Nick Coghland6009512014-11-20 21:39:37 +10002134 text = PyUnicode_FromString(name);
2135 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
2136 goto error;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002137 res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
Nick Coghland6009512014-11-20 21:39:37 +10002138 if (res == NULL)
2139 goto error;
2140 isatty = PyObject_IsTrue(res);
2141 Py_DECREF(res);
2142 if (isatty == -1)
2143 goto error;
Victor Stinnerfbca9082018-08-30 00:50:45 +02002144 if (!buffered_stdio)
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002145 write_through = Py_True;
2146 else
2147 write_through = Py_False;
Jendrik Seipp5b907712020-01-01 23:21:43 +01002148 if (buffered_stdio && (isatty || fd == fileno(stderr)))
Nick Coghland6009512014-11-20 21:39:37 +10002149 line_buffering = Py_True;
2150 else
2151 line_buffering = Py_False;
2152
2153 Py_CLEAR(raw);
2154 Py_CLEAR(text);
2155
2156#ifdef MS_WINDOWS
2157 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
2158 newlines to "\n".
2159 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
2160 newline = NULL;
2161#else
2162 /* sys.stdin: split lines at "\n".
2163 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
2164 newline = "\n";
2165#endif
2166
Victor Stinner709d23d2019-05-02 14:56:30 -04002167 PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
2168 if (encoding_str == NULL) {
2169 Py_CLEAR(buf);
2170 goto error;
2171 }
2172
2173 PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
2174 if (errors_str == NULL) {
2175 Py_CLEAR(buf);
2176 Py_CLEAR(encoding_str);
2177 goto error;
2178 }
2179
2180 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
2181 buf, encoding_str, errors_str,
Serhiy Storchaka77732be2017-10-04 20:25:40 +03002182 newline, line_buffering, write_through);
Nick Coghland6009512014-11-20 21:39:37 +10002183 Py_CLEAR(buf);
Victor Stinner709d23d2019-05-02 14:56:30 -04002184 Py_CLEAR(encoding_str);
2185 Py_CLEAR(errors_str);
Nick Coghland6009512014-11-20 21:39:37 +10002186 if (stream == NULL)
2187 goto error;
2188
2189 if (write_mode)
2190 mode = "w";
2191 else
2192 mode = "r";
2193 text = PyUnicode_FromString(mode);
2194 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
2195 goto error;
2196 Py_CLEAR(text);
2197 return stream;
2198
2199error:
2200 Py_XDECREF(buf);
2201 Py_XDECREF(stream);
2202 Py_XDECREF(text);
2203 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10002204
Victor Stinner874dbe82015-09-04 17:29:57 +02002205 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
2206 /* Issue #24891: the file descriptor was closed after the first
2207 is_valid_fd() check was called. Ignore the OSError and set the
2208 stream to None. */
2209 PyErr_Clear();
2210 Py_RETURN_NONE;
2211 }
2212 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002213}
2214
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002215/* Set builtins.open to io.OpenWrapper */
2216static PyStatus
Andy Lester75cd5bf2020-03-12 02:49:05 -05002217init_set_builtins_open(void)
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002218{
2219 PyObject *iomod = NULL, *wrapper;
2220 PyObject *bimod = NULL;
2221 PyStatus res = _PyStatus_OK();
2222
2223 if (!(iomod = PyImport_ImportModule("io"))) {
2224 goto error;
2225 }
2226
2227 if (!(bimod = PyImport_ImportModule("builtins"))) {
2228 goto error;
2229 }
2230
2231 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
2232 goto error;
2233 }
2234
2235 /* Set builtins.open */
2236 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
2237 Py_DECREF(wrapper);
2238 goto error;
2239 }
2240 Py_DECREF(wrapper);
2241 goto done;
2242
2243error:
2244 res = _PyStatus_ERR("can't initialize io.open");
2245
2246done:
2247 Py_XDECREF(bimod);
2248 Py_XDECREF(iomod);
2249 return res;
2250}
2251
2252
Nick Coghland6009512014-11-20 21:39:37 +10002253/* Initialize sys.stdin, stdout, stderr and builtins.open */
Victor Stinner331a6a52019-05-27 16:39:22 +02002254static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002255init_sys_streams(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002256{
Victor Stinnere0c9ab82019-11-22 16:19:14 +01002257 PyObject *iomod = NULL;
Nick Coghland6009512014-11-20 21:39:37 +10002258 PyObject *std = NULL;
Victor Stinnera7368ac2017-11-15 18:11:45 -08002259 int fd;
Nick Coghland6009512014-11-20 21:39:37 +10002260 PyObject * encoding_attr;
Victor Stinner331a6a52019-05-27 16:39:22 +02002261 PyStatus res = _PyStatus_OK();
Victor Stinnerda7933e2020-04-13 03:04:28 +02002262 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002263
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002264 /* Check that stdin is not a directory
2265 Using shell redirection, you can redirect stdin to a directory,
2266 crashing the Python interpreter. Catch this common mistake here
2267 and output a useful error message. Note that under MS Windows,
2268 the shell already prevents that. */
2269#ifndef MS_WINDOWS
2270 struct _Py_stat_struct sb;
2271 if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
2272 S_ISDIR(sb.st_mode)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002273 return _PyStatus_ERR("<stdin> is a directory, cannot continue");
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01002274 }
2275#endif
2276
Nick Coghland6009512014-11-20 21:39:37 +10002277 if (!(iomod = PyImport_ImportModule("io"))) {
2278 goto error;
2279 }
Nick Coghland6009512014-11-20 21:39:37 +10002280
Nick Coghland6009512014-11-20 21:39:37 +10002281 /* Set sys.stdin */
2282 fd = fileno(stdin);
2283 /* Under some conditions stdin, stdout and stderr may not be connected
2284 * and fileno() may point to an invalid file descriptor. For example
2285 * GUI apps don't have valid standard streams by default.
2286 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02002287 std = create_stdio(config, iomod, fd, 0, "<stdin>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002288 config->stdio_encoding,
2289 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002290 if (std == NULL)
2291 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002292 PySys_SetObject("__stdin__", std);
2293 _PySys_SetObjectId(&PyId_stdin, std);
2294 Py_DECREF(std);
2295
2296 /* Set sys.stdout */
2297 fd = fileno(stdout);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002298 std = create_stdio(config, iomod, fd, 1, "<stdout>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002299 config->stdio_encoding,
2300 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002301 if (std == NULL)
2302 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002303 PySys_SetObject("__stdout__", std);
2304 _PySys_SetObjectId(&PyId_stdout, std);
2305 Py_DECREF(std);
2306
2307#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2308 /* Set sys.stderr, replaces the preliminary stderr */
2309 fd = fileno(stderr);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002310 std = create_stdio(config, iomod, fd, 1, "<stderr>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002311 config->stdio_encoding,
Victor Stinner709d23d2019-05-02 14:56:30 -04002312 L"backslashreplace");
Victor Stinner874dbe82015-09-04 17:29:57 +02002313 if (std == NULL)
2314 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002315
2316 /* Same as hack above, pre-import stderr's codec to avoid recursion
2317 when import.c tries to write to stderr in verbose mode. */
2318 encoding_attr = PyObject_GetAttrString(std, "encoding");
2319 if (encoding_attr != NULL) {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002320 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10002321 if (std_encoding != NULL) {
2322 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2323 Py_XDECREF(codec_info);
2324 }
2325 Py_DECREF(encoding_attr);
2326 }
Victor Stinnerb45d2592019-06-20 00:05:23 +02002327 _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
Nick Coghland6009512014-11-20 21:39:37 +10002328
2329 if (PySys_SetObject("__stderr__", std) < 0) {
2330 Py_DECREF(std);
2331 goto error;
2332 }
2333 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2334 Py_DECREF(std);
2335 goto error;
2336 }
2337 Py_DECREF(std);
2338#endif
2339
Victor Stinnera7368ac2017-11-15 18:11:45 -08002340 goto done;
Nick Coghland6009512014-11-20 21:39:37 +10002341
Victor Stinnera7368ac2017-11-15 18:11:45 -08002342error:
Victor Stinner331a6a52019-05-27 16:39:22 +02002343 res = _PyStatus_ERR("can't initialize sys standard streams");
Victor Stinnera7368ac2017-11-15 18:11:45 -08002344
2345done:
Victor Stinner124b9eb2018-08-29 01:29:06 +02002346 _Py_ClearStandardStreamEncoding();
Nick Coghland6009512014-11-20 21:39:37 +10002347 Py_XDECREF(iomod);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002348 return res;
Nick Coghland6009512014-11-20 21:39:37 +10002349}
2350
2351
Victor Stinner10dc4842015-03-24 12:01:30 +01002352static void
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002353_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2354 PyThreadState *tstate)
Victor Stinner10dc4842015-03-24 12:01:30 +01002355{
Victor Stinner314b8782021-01-18 18:34:56 +01002356 PUTS(fd, "\n");
Victor Stinner10dc4842015-03-24 12:01:30 +01002357
2358 /* display the current Python stack */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002359 _Py_DumpTracebackThreads(fd, interp, tstate);
Victor Stinner10dc4842015-03-24 12:01:30 +01002360}
Victor Stinner791da1c2016-03-14 16:53:12 +01002361
2362/* Print the current exception (if an exception is set) with its traceback,
2363 or display the current Python stack.
2364
2365 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2366 called on catastrophic cases.
2367
2368 Return 1 if the traceback was displayed, 0 otherwise. */
2369
2370static int
Andy Lester75cd5bf2020-03-12 02:49:05 -05002371_Py_FatalError_PrintExc(PyThreadState *tstate)
Victor Stinner791da1c2016-03-14 16:53:12 +01002372{
2373 PyObject *ferr, *res;
2374 PyObject *exception, *v, *tb;
2375 int has_tb;
2376
Victor Stinnerb45d2592019-06-20 00:05:23 +02002377 _PyErr_Fetch(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002378 if (exception == NULL) {
2379 /* No current exception */
2380 return 0;
2381 }
2382
2383 ferr = _PySys_GetObjectId(&PyId_stderr);
2384 if (ferr == NULL || ferr == Py_None) {
2385 /* sys.stderr is not set yet or set to None,
2386 no need to try to display the exception */
2387 return 0;
2388 }
2389
Victor Stinnerb45d2592019-06-20 00:05:23 +02002390 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002391 if (tb == NULL) {
2392 tb = Py_None;
2393 Py_INCREF(tb);
2394 }
2395 PyException_SetTraceback(v, tb);
2396 if (exception == NULL) {
2397 /* PyErr_NormalizeException() failed */
2398 return 0;
2399 }
2400
2401 has_tb = (tb != Py_None);
2402 PyErr_Display(exception, v, tb);
2403 Py_XDECREF(exception);
2404 Py_XDECREF(v);
2405 Py_XDECREF(tb);
2406
2407 /* sys.stderr may be buffered: call sys.stderr.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002408 res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002409 if (res == NULL) {
2410 _PyErr_Clear(tstate);
2411 }
2412 else {
Victor Stinner791da1c2016-03-14 16:53:12 +01002413 Py_DECREF(res);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002414 }
Victor Stinner791da1c2016-03-14 16:53:12 +01002415
2416 return has_tb;
2417}
2418
Nick Coghland6009512014-11-20 21:39:37 +10002419/* Print fatal error message and abort */
2420
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002421#ifdef MS_WINDOWS
2422static void
2423fatal_output_debug(const char *msg)
2424{
2425 /* buffer of 256 bytes allocated on the stack */
2426 WCHAR buffer[256 / sizeof(WCHAR)];
2427 size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2428 size_t msglen;
2429
2430 OutputDebugStringW(L"Fatal Python error: ");
2431
2432 msglen = strlen(msg);
2433 while (msglen) {
2434 size_t i;
2435
2436 if (buflen > msglen) {
2437 buflen = msglen;
2438 }
2439
2440 /* Convert the message to wchar_t. This uses a simple one-to-one
2441 conversion, assuming that the this error message actually uses
2442 ASCII only. If this ceases to be true, we will have to convert. */
2443 for (i=0; i < buflen; ++i) {
2444 buffer[i] = msg[i];
2445 }
2446 buffer[i] = L'\0';
2447 OutputDebugStringW(buffer);
2448
2449 msg += buflen;
2450 msglen -= buflen;
2451 }
2452 OutputDebugStringW(L"\n");
2453}
2454#endif
2455
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002456
2457static void
Victor Stinner314b8782021-01-18 18:34:56 +01002458fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime)
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002459{
Victor Stinner314b8782021-01-18 18:34:56 +01002460 PUTS(fd, "Python runtime state: ");
Victor Stinner7b3c2522020-03-07 00:24:23 +01002461 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2462 if (finalizing) {
Victor Stinner314b8782021-01-18 18:34:56 +01002463 PUTS(fd, "finalizing (tstate=0x");
2464 _Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2);
2465 PUTS(fd, ")");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002466 }
2467 else if (runtime->initialized) {
Victor Stinner314b8782021-01-18 18:34:56 +01002468 PUTS(fd, "initialized");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002469 }
2470 else if (runtime->core_initialized) {
Victor Stinner314b8782021-01-18 18:34:56 +01002471 PUTS(fd, "core initialized");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002472 }
2473 else if (runtime->preinitialized) {
Victor Stinner314b8782021-01-18 18:34:56 +01002474 PUTS(fd, "preinitialized");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002475 }
2476 else if (runtime->preinitializing) {
Victor Stinner314b8782021-01-18 18:34:56 +01002477 PUTS(fd, "preinitializing");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002478 }
2479 else {
Victor Stinner314b8782021-01-18 18:34:56 +01002480 PUTS(fd, "unknown");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002481 }
Victor Stinner314b8782021-01-18 18:34:56 +01002482 PUTS(fd, "\n");
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002483}
2484
2485
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002486static inline void _Py_NO_RETURN
2487fatal_error_exit(int status)
Nick Coghland6009512014-11-20 21:39:37 +10002488{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002489 if (status < 0) {
2490#if defined(MS_WINDOWS) && defined(_DEBUG)
2491 DebugBreak();
2492#endif
2493 abort();
2494 }
2495 else {
2496 exit(status);
2497 }
2498}
2499
2500
Victor Stinner66f77ca2021-01-19 23:35:27 +01002501// Dump the list of extension modules of sys.modules, excluding stdlib modules
2502// (_Py_module_names), into fd file descriptor.
2503//
Victor Stinner250035d2021-01-18 20:47:13 +01002504// This function is called by a signal handler in faulthandler: avoid memory
Victor Stinner66f77ca2021-01-19 23:35:27 +01002505// allocations and keep the implementation simple. For example, the list is not
2506// sorted on purpose.
Victor Stinner250035d2021-01-18 20:47:13 +01002507void
2508_Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
2509{
2510 if (interp == NULL) {
2511 return;
2512 }
2513 PyObject *modules = interp->modules;
Victor Stinner66f77ca2021-01-19 23:35:27 +01002514 if (modules == NULL || !PyDict_Check(modules)) {
Victor Stinner250035d2021-01-18 20:47:13 +01002515 return;
2516 }
2517
Victor Stinner66f77ca2021-01-19 23:35:27 +01002518 int header = 1;
2519 Py_ssize_t count = 0;
Victor Stinner250035d2021-01-18 20:47:13 +01002520 Py_ssize_t pos = 0;
2521 PyObject *key, *value;
Victor Stinner250035d2021-01-18 20:47:13 +01002522 while (PyDict_Next(modules, &pos, &key, &value)) {
2523 if (!PyUnicode_Check(key)) {
2524 continue;
2525 }
2526 if (!_PyModule_IsExtension(value)) {
2527 continue;
2528 }
2529
Victor Stinner66f77ca2021-01-19 23:35:27 +01002530 // Check if it is a stdlib extension module.
2531 // Use the module name from the sys.modules key,
2532 // don't attempt to get the module object name.
2533 const Py_ssize_t names_len = Py_ARRAY_LENGTH(_Py_module_names);
2534 int is_stdlib_mod = 0;
2535 for (Py_ssize_t i=0; i < names_len; i++) {
2536 const char *name = _Py_module_names[i];
2537 if (PyUnicode_CompareWithASCIIString(key, name) == 0) {
2538 is_stdlib_mod = 1;
2539 break;
2540 }
2541 }
2542 if (is_stdlib_mod) {
2543 // Ignore stdlib extension module.
2544 continue;
2545 }
2546
2547 if (header) {
2548 PUTS(fd, "\nExtension modules: ");
2549 header = 0;
2550 }
2551 else {
Victor Stinner250035d2021-01-18 20:47:13 +01002552 PUTS(fd, ", ");
2553 }
Victor Stinner250035d2021-01-18 20:47:13 +01002554
2555 _Py_DumpASCII(fd, key);
Victor Stinner66f77ca2021-01-19 23:35:27 +01002556 count++;
Victor Stinner250035d2021-01-18 20:47:13 +01002557 }
Victor Stinner66f77ca2021-01-19 23:35:27 +01002558
2559 if (count) {
2560 PUTS(fd, " (total: ");
2561 _Py_DumpDecimal(fd, count);
2562 PUTS(fd, ")");
2563 PUTS(fd, "\n");
2564 }
Victor Stinner250035d2021-01-18 20:47:13 +01002565}
2566
2567
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002568static void _Py_NO_RETURN
Victor Stinner314b8782021-01-18 18:34:56 +01002569fatal_error(int fd, int header, const char *prefix, const char *msg,
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002570 int status)
2571{
Victor Stinner53345a42015-03-25 01:55:14 +01002572 static int reentrant = 0;
Victor Stinner53345a42015-03-25 01:55:14 +01002573
2574 if (reentrant) {
2575 /* Py_FatalError() caused a second fatal error.
2576 Example: flush_std_files() raises a recursion error. */
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002577 fatal_error_exit(status);
Victor Stinner53345a42015-03-25 01:55:14 +01002578 }
2579 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10002580
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002581 if (header) {
Victor Stinner314b8782021-01-18 18:34:56 +01002582 PUTS(fd, "Fatal Python error: ");
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002583 if (prefix) {
Victor Stinner314b8782021-01-18 18:34:56 +01002584 PUTS(fd, prefix);
2585 PUTS(fd, ": ");
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002586 }
2587 if (msg) {
Victor Stinner314b8782021-01-18 18:34:56 +01002588 PUTS(fd, msg);
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002589 }
2590 else {
Victor Stinner314b8782021-01-18 18:34:56 +01002591 PUTS(fd, "<message not set>");
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002592 }
Victor Stinner314b8782021-01-18 18:34:56 +01002593 PUTS(fd, "\n");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002594 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002595
2596 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner314b8782021-01-18 18:34:56 +01002597 fatal_error_dump_runtime(fd, runtime);
Victor Stinner10dc4842015-03-24 12:01:30 +01002598
Victor Stinner3a228ab2018-11-01 00:26:41 +01002599 /* Check if the current thread has a Python thread state
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002600 and holds the GIL.
Victor Stinner3a228ab2018-11-01 00:26:41 +01002601
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002602 tss_tstate is NULL if Py_FatalError() is called from a C thread which
2603 has no Python thread state.
2604
2605 tss_tstate != tstate if the current Python thread does not hold the GIL.
2606 */
Victor Stinner314b8782021-01-18 18:34:56 +01002607 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2608 PyInterpreterState *interp = NULL;
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002609 PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
Victor Stinner314b8782021-01-18 18:34:56 +01002610 if (tstate != NULL) {
2611 interp = tstate->interp;
2612 }
2613 else if (tss_tstate != NULL) {
2614 interp = tss_tstate->interp;
2615 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002616 int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
Victor Stinner314b8782021-01-18 18:34:56 +01002617
Victor Stinner3a228ab2018-11-01 00:26:41 +01002618 if (has_tstate_and_gil) {
2619 /* If an exception is set, print the exception with its traceback */
Andy Lester75cd5bf2020-03-12 02:49:05 -05002620 if (!_Py_FatalError_PrintExc(tss_tstate)) {
Victor Stinner3a228ab2018-11-01 00:26:41 +01002621 /* No exception is set, or an exception is set without traceback */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002622 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002623 }
2624 }
2625 else {
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002626 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002627 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002628
Victor Stinner250035d2021-01-18 20:47:13 +01002629 _Py_DumpExtensionModules(fd, interp);
2630
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002631 /* The main purpose of faulthandler is to display the traceback.
2632 This function already did its best to display a traceback.
2633 Disable faulthandler to prevent writing a second traceback
2634 on abort(). */
Victor Stinner2025d782016-03-16 23:19:15 +01002635 _PyFaulthandler_Fini();
2636
Victor Stinner791da1c2016-03-14 16:53:12 +01002637 /* Check if the current Python thread hold the GIL */
Victor Stinner3a228ab2018-11-01 00:26:41 +01002638 if (has_tstate_and_gil) {
Victor Stinner791da1c2016-03-14 16:53:12 +01002639 /* Flush sys.stdout and sys.stderr */
2640 flush_std_files();
2641 }
Victor Stinnere0deff32015-03-24 13:46:18 +01002642
Nick Coghland6009512014-11-20 21:39:37 +10002643#ifdef MS_WINDOWS
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002644 fatal_output_debug(msg);
Victor Stinner53345a42015-03-25 01:55:14 +01002645#endif /* MS_WINDOWS */
2646
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002647 fatal_error_exit(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002648}
2649
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002650
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002651#undef Py_FatalError
2652
Victor Stinner19760862017-12-20 01:41:59 +01002653void _Py_NO_RETURN
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002654Py_FatalError(const char *msg)
2655{
Victor Stinner314b8782021-01-18 18:34:56 +01002656 fatal_error(fileno(stderr), 1, NULL, msg, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002657}
2658
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002659
Victor Stinner19760862017-12-20 01:41:59 +01002660void _Py_NO_RETURN
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002661_Py_FatalErrorFunc(const char *func, const char *msg)
2662{
Victor Stinner314b8782021-01-18 18:34:56 +01002663 fatal_error(fileno(stderr), 1, func, msg, -1);
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002664}
2665
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002666
2667void _Py_NO_RETURN
2668_Py_FatalErrorFormat(const char *func, const char *format, ...)
2669{
2670 static int reentrant = 0;
2671 if (reentrant) {
2672 /* _Py_FatalErrorFormat() caused a second fatal error */
2673 fatal_error_exit(-1);
2674 }
2675 reentrant = 1;
2676
2677 FILE *stream = stderr;
Victor Stinner314b8782021-01-18 18:34:56 +01002678 const int fd = fileno(stream);
2679 PUTS(fd, "Fatal Python error: ");
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002680 if (func) {
Victor Stinner314b8782021-01-18 18:34:56 +01002681 PUTS(fd, func);
2682 PUTS(fd, ": ");
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002683 }
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002684
2685 va_list vargs;
2686#ifdef HAVE_STDARG_PROTOTYPES
2687 va_start(vargs, format);
2688#else
2689 va_start(vargs);
2690#endif
2691 vfprintf(stream, format, vargs);
2692 va_end(vargs);
2693
2694 fputs("\n", stream);
2695 fflush(stream);
2696
Victor Stinner314b8782021-01-18 18:34:56 +01002697 fatal_error(fd, 0, NULL, NULL, -1);
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002698}
2699
2700
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002701void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +02002702Py_ExitStatusException(PyStatus status)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002703{
Victor Stinner331a6a52019-05-27 16:39:22 +02002704 if (_PyStatus_IS_EXIT(status)) {
2705 exit(status.exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +02002706 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002707 else if (_PyStatus_IS_ERROR(status)) {
Victor Stinner314b8782021-01-18 18:34:56 +01002708 fatal_error(fileno(stderr), 1, status.func, status.err_msg, 1);
Victor Stinnerdfe88472019-03-01 12:14:41 +01002709 }
2710 else {
Victor Stinner331a6a52019-05-27 16:39:22 +02002711 Py_FatalError("Py_ExitStatusException() must not be called on success");
Victor Stinnerdfe88472019-03-01 12:14:41 +01002712 }
Nick Coghland6009512014-11-20 21:39:37 +10002713}
2714
Victor Stinner357704c2020-12-14 23:07:54 +01002715
Nick Coghland6009512014-11-20 21:39:37 +10002716/* Wait until threading._shutdown completes, provided
2717 the threading module was imported in the first place.
2718 The shutdown routine will wait until all non-daemon
2719 "threading" threads have completed. */
2720static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002721wait_for_thread_shutdown(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002722{
Nick Coghland6009512014-11-20 21:39:37 +10002723 _Py_IDENTIFIER(_shutdown);
2724 PyObject *result;
Eric Snow3f9eee62017-09-15 16:35:20 -06002725 PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
Nick Coghland6009512014-11-20 21:39:37 +10002726 if (threading == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +02002727 if (_PyErr_Occurred(tstate)) {
Stefan Krah027b09c2019-03-25 21:50:58 +01002728 PyErr_WriteUnraisable(NULL);
2729 }
2730 /* else: threading not imported */
Nick Coghland6009512014-11-20 21:39:37 +10002731 return;
2732 }
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002733 result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
Nick Coghland6009512014-11-20 21:39:37 +10002734 if (result == NULL) {
2735 PyErr_WriteUnraisable(threading);
2736 }
2737 else {
2738 Py_DECREF(result);
2739 }
2740 Py_DECREF(threading);
Nick Coghland6009512014-11-20 21:39:37 +10002741}
2742
2743#define NEXITFUNCS 32
Nick Coghland6009512014-11-20 21:39:37 +10002744int Py_AtExit(void (*func)(void))
2745{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002746 if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
Nick Coghland6009512014-11-20 21:39:37 +10002747 return -1;
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002748 _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
Nick Coghland6009512014-11-20 21:39:37 +10002749 return 0;
2750}
2751
2752static void
Victor Stinner8e91c242019-04-24 17:24:01 +02002753call_ll_exitfuncs(_PyRuntimeState *runtime)
Nick Coghland6009512014-11-20 21:39:37 +10002754{
Victor Stinner8e91c242019-04-24 17:24:01 +02002755 while (runtime->nexitfuncs > 0) {
Victor Stinner87d23a02019-04-26 05:49:26 +02002756 /* pop last function from the list */
2757 runtime->nexitfuncs--;
2758 void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2759 runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2760
2761 exitfunc();
Victor Stinner8e91c242019-04-24 17:24:01 +02002762 }
Nick Coghland6009512014-11-20 21:39:37 +10002763
2764 fflush(stdout);
2765 fflush(stderr);
2766}
2767
Victor Stinnercfc88312018-08-01 16:41:25 +02002768void _Py_NO_RETURN
Nick Coghland6009512014-11-20 21:39:37 +10002769Py_Exit(int sts)
2770{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002771 if (Py_FinalizeEx() < 0) {
2772 sts = 120;
2773 }
Nick Coghland6009512014-11-20 21:39:37 +10002774
2775 exit(sts);
2776}
2777
Nick Coghland6009512014-11-20 21:39:37 +10002778
Nick Coghland6009512014-11-20 21:39:37 +10002779/*
2780 * The file descriptor fd is considered ``interactive'' if either
2781 * a) isatty(fd) is TRUE, or
2782 * b) the -i flag was given, and the filename associated with
2783 * the descriptor is NULL or "<stdin>" or "???".
2784 */
2785int
2786Py_FdIsInteractive(FILE *fp, const char *filename)
2787{
2788 if (isatty((int)fileno(fp)))
2789 return 1;
2790 if (!Py_InteractiveFlag)
2791 return 0;
2792 return (filename == NULL) ||
2793 (strcmp(filename, "<stdin>") == 0) ||
2794 (strcmp(filename, "???") == 0);
2795}
2796
2797
Victor Stinnera82f63f2020-12-09 22:37:27 +01002798int
2799_Py_FdIsInteractive(FILE *fp, PyObject *filename)
2800{
2801 if (isatty((int)fileno(fp))) {
2802 return 1;
2803 }
2804 if (!Py_InteractiveFlag) {
2805 return 0;
2806 }
2807 return (filename == NULL) ||
2808 (PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) ||
2809 (PyUnicode_CompareWithASCIIString(filename, "???") == 0);
2810}
2811
2812
Nick Coghland6009512014-11-20 21:39:37 +10002813/* Wrappers around sigaction() or signal(). */
2814
2815PyOS_sighandler_t
2816PyOS_getsig(int sig)
2817{
2818#ifdef HAVE_SIGACTION
2819 struct sigaction context;
2820 if (sigaction(sig, NULL, &context) == -1)
2821 return SIG_ERR;
2822 return context.sa_handler;
2823#else
2824 PyOS_sighandler_t handler;
2825/* Special signal handling for the secure CRT in Visual Studio 2005 */
2826#if defined(_MSC_VER) && _MSC_VER >= 1400
2827 switch (sig) {
2828 /* Only these signals are valid */
2829 case SIGINT:
2830 case SIGILL:
2831 case SIGFPE:
2832 case SIGSEGV:
2833 case SIGTERM:
2834 case SIGBREAK:
2835 case SIGABRT:
2836 break;
2837 /* Don't call signal() with other values or it will assert */
2838 default:
2839 return SIG_ERR;
2840 }
2841#endif /* _MSC_VER && _MSC_VER >= 1400 */
2842 handler = signal(sig, SIG_IGN);
2843 if (handler != SIG_ERR)
2844 signal(sig, handler);
2845 return handler;
2846#endif
2847}
2848
2849/*
2850 * All of the code in this function must only use async-signal-safe functions,
2851 * listed at `man 7 signal` or
2852 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2853 */
2854PyOS_sighandler_t
2855PyOS_setsig(int sig, PyOS_sighandler_t handler)
2856{
2857#ifdef HAVE_SIGACTION
2858 /* Some code in Modules/signalmodule.c depends on sigaction() being
2859 * used here if HAVE_SIGACTION is defined. Fix that if this code
2860 * changes to invalidate that assumption.
2861 */
2862 struct sigaction context, ocontext;
2863 context.sa_handler = handler;
2864 sigemptyset(&context.sa_mask);
2865 context.sa_flags = 0;
2866 if (sigaction(sig, &context, &ocontext) == -1)
2867 return SIG_ERR;
2868 return ocontext.sa_handler;
2869#else
2870 PyOS_sighandler_t oldhandler;
2871 oldhandler = signal(sig, handler);
2872#ifdef HAVE_SIGINTERRUPT
2873 siginterrupt(sig, 1);
2874#endif
2875 return oldhandler;
2876#endif
2877}
2878
2879#ifdef __cplusplus
2880}
2881#endif