blob: 00a9b99be4aa3f9d66d0539d58a7264a09600b14 [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 Stinnere5014be2020-04-14 17:52:15 +020011#include "pycore_import.h" // _PyImport_Cleanup()
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
21#include "grammar.h" // PyGrammar_RemoveAccelerators()
22#include <locale.h> // setlocale()
Nick Coghland6009512014-11-20 21:39:37 +100023
24#ifdef HAVE_SIGNAL_H
Victor Stinner4f98f462020-04-15 04:01:58 +020025# include <signal.h> // SIG_IGN
Nick Coghland6009512014-11-20 21:39:37 +100026#endif
27
28#ifdef HAVE_LANGINFO_H
Victor Stinner4f98f462020-04-15 04:01:58 +020029# include <langinfo.h> // nl_langinfo(CODESET)
Nick Coghland6009512014-11-20 21:39:37 +100030#endif
31
32#ifdef MS_WINDOWS
Victor Stinner4f98f462020-04-15 04:01:58 +020033# undef BYTE
34# include "windows.h"
Steve Dower39294992016-08-30 21:22:36 -070035
Victor Stinner4f98f462020-04-15 04:01:58 +020036 extern PyTypeObject PyWindowsConsoleIO_Type;
37# define PyWindowsConsoleIO_Check(op) \
38 (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
Nick Coghland6009512014-11-20 21:39:37 +100039#endif
40
Victor Stinner4f98f462020-04-15 04:01:58 +020041
Nick Coghland6009512014-11-20 21:39:37 +100042_Py_IDENTIFIER(flush);
43_Py_IDENTIFIER(name);
44_Py_IDENTIFIER(stdin);
45_Py_IDENTIFIER(stdout);
46_Py_IDENTIFIER(stderr);
Eric Snow3f9eee62017-09-15 16:35:20 -060047_Py_IDENTIFIER(threading);
Nick Coghland6009512014-11-20 21:39:37 +100048
49#ifdef __cplusplus
50extern "C" {
51#endif
52
Nick Coghland6009512014-11-20 21:39:37 +100053extern grammar _PyParser_Grammar; /* From graminit.c */
54
Victor Stinnerb45d2592019-06-20 00:05:23 +020055/* Forward declarations */
Victor Stinner331a6a52019-05-27 16:39:22 +020056static PyStatus add_main_module(PyInterpreterState *interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +020057static PyStatus init_import_site(void);
Andy Lester75cd5bf2020-03-12 02:49:05 -050058static PyStatus init_set_builtins_open(void);
Victor Stinnerb45d2592019-06-20 00:05:23 +020059static PyStatus init_sys_streams(PyThreadState *tstate);
60static PyStatus init_signals(PyThreadState *tstate);
61static void call_py_exitfuncs(PyThreadState *tstate);
62static void wait_for_thread_shutdown(PyThreadState *tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +020063static void call_ll_exitfuncs(_PyRuntimeState *runtime);
Nick Coghland6009512014-11-20 21:39:37 +100064
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080065int _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080066_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010067static int runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068
Victor Stinner331a6a52019-05-27 16:39:22 +020069PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -060070_PyRuntime_Initialize(void)
71{
72 /* XXX We only initialize once in the process, which aligns with
73 the static initialization of the former globals now found in
74 _PyRuntime. However, _PyRuntime *should* be initialized with
75 every Py_Initialize() call, but doing so breaks the runtime.
76 This is because the runtime state is not properly finalized
77 currently. */
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010078 if (runtime_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +020079 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080080 }
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010081 runtime_initialized = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080082
83 return _PyRuntimeState_Init(&_PyRuntime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060084}
85
86void
87_PyRuntime_Finalize(void)
88{
89 _PyRuntimeState_Fini(&_PyRuntime);
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010090 runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060091}
92
93int
94_Py_IsFinalizing(void)
95{
Victor Stinner7b3c2522020-03-07 00:24:23 +010096 return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060097}
98
Nick Coghland6009512014-11-20 21:39:37 +100099/* Hack to force loading of object files */
100int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
101 PyOS_mystrnicmp; /* Python/pystrcmp.o */
102
103/* PyModule_GetWarningsModule is no longer necessary as of 2.6
104since _warnings is builtin. This API should not be used. */
105PyObject *
106PyModule_GetWarningsModule(void)
107{
108 return PyImport_ImportModule("warnings");
109}
110
Eric Snowc7ec9982017-05-23 23:00:52 -0700111
Eric Snow1abcf672017-05-23 21:46:51 -0700112/* APIs to access the initialization flags
113 *
114 * Can be called prior to Py_Initialize.
115 */
Nick Coghland6009512014-11-20 21:39:37 +1000116
Eric Snow1abcf672017-05-23 21:46:51 -0700117int
118_Py_IsCoreInitialized(void)
119{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600120 return _PyRuntime.core_initialized;
Eric Snow1abcf672017-05-23 21:46:51 -0700121}
Nick Coghland6009512014-11-20 21:39:37 +1000122
123int
124Py_IsInitialized(void)
125{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600126 return _PyRuntime.initialized;
Nick Coghland6009512014-11-20 21:39:37 +1000127}
128
Nick Coghlan6ea41862017-06-11 13:16:15 +1000129
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000130/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
131 call this twice without an intervening Py_FinalizeEx() call. When
Nick Coghland6009512014-11-20 21:39:37 +1000132 initializations fail, a fatal error is issued and the function does
133 not return. On return, the first thread and interpreter state have
134 been created.
135
136 Locking: you must hold the interpreter lock while calling this.
137 (If the lock has not yet been initialized, that's equivalent to
138 having the lock, but you cannot use multiple threads.)
139
140*/
141
Victor Stinner331a6a52019-05-27 16:39:22 +0200142static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200143init_importlib(PyThreadState *tstate, PyObject *sysmod)
Nick Coghland6009512014-11-20 21:39:37 +1000144{
145 PyObject *importlib;
146 PyObject *impmod;
Nick Coghland6009512014-11-20 21:39:37 +1000147 PyObject *value;
Victor Stinnerb45d2592019-06-20 00:05:23 +0200148 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200149 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
Nick Coghland6009512014-11-20 21:39:37 +1000150
151 /* Import _importlib through its frozen version, _frozen_importlib. */
152 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200153 return _PyStatus_ERR("can't import _frozen_importlib");
Nick Coghland6009512014-11-20 21:39:37 +1000154 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200155 else if (verbose) {
Nick Coghland6009512014-11-20 21:39:37 +1000156 PySys_FormatStderr("import _frozen_importlib # frozen\n");
157 }
158 importlib = PyImport_AddModule("_frozen_importlib");
159 if (importlib == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200160 return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000161 }
162 interp->importlib = importlib;
163 Py_INCREF(interp->importlib);
164
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300165 interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
166 if (interp->import_func == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +0200167 return _PyStatus_ERR("__import__ not found");
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300168 Py_INCREF(interp->import_func);
169
Victor Stinnercd6e6942015-09-18 09:11:57 +0200170 /* Import the _imp module */
Benjamin Petersonc65ef772018-01-29 11:33:57 -0800171 impmod = PyInit__imp();
Nick Coghland6009512014-11-20 21:39:37 +1000172 if (impmod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200173 return _PyStatus_ERR("can't import _imp");
Nick Coghland6009512014-11-20 21:39:37 +1000174 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200175 else if (verbose) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200176 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000177 }
Eric Snow3f9eee62017-09-15 16:35:20 -0600178 if (_PyImport_SetModuleString("_imp", impmod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200179 return _PyStatus_ERR("can't save _imp to sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000180 }
181
Victor Stinnercd6e6942015-09-18 09:11:57 +0200182 /* Install importlib as the implementation of import */
Nick Coghland6009512014-11-20 21:39:37 +1000183 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
184 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200185 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200186 return _PyStatus_ERR("importlib install failed");
Nick Coghland6009512014-11-20 21:39:37 +1000187 }
188 Py_DECREF(value);
189 Py_DECREF(impmod);
190
Victor Stinner331a6a52019-05-27 16:39:22 +0200191 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +1000192}
193
Victor Stinner331a6a52019-05-27 16:39:22 +0200194static PyStatus
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200195init_importlib_external(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -0700196{
197 PyObject *value;
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200198 value = PyObject_CallMethod(tstate->interp->importlib,
Eric Snow1abcf672017-05-23 21:46:51 -0700199 "_install_external_importers", "");
200 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200201 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200202 return _PyStatus_ERR("external importer setup failed");
Eric Snow1abcf672017-05-23 21:46:51 -0700203 }
Stéphane Wirtelab1cb802017-06-08 13:13:20 +0200204 Py_DECREF(value);
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200205 return _PyImportZip_Init(tstate);
Eric Snow1abcf672017-05-23 21:46:51 -0700206}
Nick Coghland6009512014-11-20 21:39:37 +1000207
Nick Coghlan6ea41862017-06-11 13:16:15 +1000208/* Helper functions to better handle the legacy C locale
209 *
210 * The legacy C locale assumes ASCII as the default text encoding, which
211 * causes problems not only for the CPython runtime, but also other
212 * components like GNU readline.
213 *
214 * Accordingly, when the CLI detects it, it attempts to coerce it to a
215 * more capable UTF-8 based alternative as follows:
216 *
217 * if (_Py_LegacyLocaleDetected()) {
218 * _Py_CoerceLegacyLocale();
219 * }
220 *
221 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
222 *
223 * Locale coercion also impacts the default error handler for the standard
224 * streams: while the usual default is "strict", the default for the legacy
225 * C locale and for any of the coercion target locales is "surrogateescape".
226 */
227
228int
Victor Stinner0f721472019-05-20 17:16:38 +0200229_Py_LegacyLocaleDetected(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000230{
231#ifndef MS_WINDOWS
Victor Stinner0f721472019-05-20 17:16:38 +0200232 if (!warn) {
233 const char *locale_override = getenv("LC_ALL");
234 if (locale_override != NULL && *locale_override != '\0') {
235 /* Don't coerce C locale if the LC_ALL environment variable
236 is set */
237 return 0;
238 }
239 }
240
Nick Coghlan6ea41862017-06-11 13:16:15 +1000241 /* On non-Windows systems, the C locale is considered a legacy locale */
Nick Coghlaneb817952017-06-18 12:29:42 +1000242 /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
243 * the POSIX locale as a simple alias for the C locale, so
244 * we may also want to check for that explicitly.
245 */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000246 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
247 return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
248#else
249 /* Windows uses code pages instead of locales, so no locale is legacy */
250 return 0;
251#endif
252}
253
Victor Stinnerb0051362019-11-22 17:52:42 +0100254#ifndef MS_WINDOWS
Nick Coghlaneb817952017-06-18 12:29:42 +1000255static const char *_C_LOCALE_WARNING =
256 "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
257 "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
258 "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
259 "locales is recommended.\n";
260
Nick Coghlaneb817952017-06-18 12:29:42 +1000261static void
Victor Stinner43125222019-04-24 18:23:53 +0200262emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
Nick Coghlaneb817952017-06-18 12:29:42 +1000263{
Victor Stinner331a6a52019-05-27 16:39:22 +0200264 const PyPreConfig *preconfig = &runtime->preconfig;
Victor Stinner0f721472019-05-20 17:16:38 +0200265 if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
Victor Stinnercf215042018-08-29 22:56:06 +0200266 PySys_FormatStderr("%s", _C_LOCALE_WARNING);
Nick Coghlaneb817952017-06-18 12:29:42 +1000267 }
268}
Victor Stinnerb0051362019-11-22 17:52:42 +0100269#endif /* !defined(MS_WINDOWS) */
Nick Coghlaneb817952017-06-18 12:29:42 +1000270
Nick Coghlan6ea41862017-06-11 13:16:15 +1000271typedef struct _CandidateLocale {
272 const char *locale_name; /* The locale to try as a coercion target */
273} _LocaleCoercionTarget;
274
275static _LocaleCoercionTarget _TARGET_LOCALES[] = {
276 {"C.UTF-8"},
277 {"C.utf8"},
Nick Coghlan18974c32017-06-30 00:48:14 +1000278 {"UTF-8"},
Nick Coghlan6ea41862017-06-11 13:16:15 +1000279 {NULL}
280};
281
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200282
283int
284_Py_IsLocaleCoercionTarget(const char *ctype_loc)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000285{
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200286 const _LocaleCoercionTarget *target = NULL;
287 for (target = _TARGET_LOCALES; target->locale_name; target++) {
288 if (strcmp(ctype_loc, target->locale_name) == 0) {
289 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000290 }
Victor Stinner124b9eb2018-08-29 01:29:06 +0200291 }
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200292 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000293}
294
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200295
Nick Coghlan6ea41862017-06-11 13:16:15 +1000296#ifdef PY_COERCE_C_LOCALE
Victor Stinner94540602017-12-16 04:54:22 +0100297static const char C_LOCALE_COERCION_WARNING[] =
Nick Coghlan6ea41862017-06-11 13:16:15 +1000298 "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
299 "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
300
Victor Stinner0f721472019-05-20 17:16:38 +0200301static int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200302_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000303{
304 const char *newloc = target->locale_name;
305
306 /* Reset locale back to currently configured defaults */
xdegaye1588be62017-11-12 12:45:59 +0100307 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000308
309 /* Set the relevant locale environment variable */
310 if (setenv("LC_CTYPE", newloc, 1)) {
311 fprintf(stderr,
312 "Error setting LC_CTYPE, skipping C locale coercion\n");
Victor Stinner0f721472019-05-20 17:16:38 +0200313 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000314 }
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200315 if (warn) {
Victor Stinner94540602017-12-16 04:54:22 +0100316 fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
Nick Coghlaneb817952017-06-18 12:29:42 +1000317 }
Nick Coghlan6ea41862017-06-11 13:16:15 +1000318
319 /* Reconfigure with the overridden environment variables */
xdegaye1588be62017-11-12 12:45:59 +0100320 _Py_SetLocaleFromEnv(LC_ALL);
Victor Stinner0f721472019-05-20 17:16:38 +0200321 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000322}
323#endif
324
Victor Stinner0f721472019-05-20 17:16:38 +0200325int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200326_Py_CoerceLegacyLocale(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000327{
Victor Stinner0f721472019-05-20 17:16:38 +0200328 int coerced = 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000329#ifdef PY_COERCE_C_LOCALE
Victor Stinner8ea09112018-09-03 17:05:18 +0200330 char *oldloc = NULL;
331
332 oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
333 if (oldloc == NULL) {
Victor Stinner0f721472019-05-20 17:16:38 +0200334 return coerced;
Victor Stinner8ea09112018-09-03 17:05:18 +0200335 }
336
Victor Stinner94540602017-12-16 04:54:22 +0100337 const char *locale_override = getenv("LC_ALL");
338 if (locale_override == NULL || *locale_override == '\0') {
339 /* LC_ALL is also not set (or is set to an empty string) */
340 const _LocaleCoercionTarget *target = NULL;
341 for (target = _TARGET_LOCALES; target->locale_name; target++) {
342 const char *new_locale = setlocale(LC_CTYPE,
343 target->locale_name);
344 if (new_locale != NULL) {
Victor Stinnere2510952019-05-02 11:28:57 -0400345#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94540602017-12-16 04:54:22 +0100346 /* Also ensure that nl_langinfo works in this locale */
347 char *codeset = nl_langinfo(CODESET);
348 if (!codeset || *codeset == '\0') {
349 /* CODESET is not set or empty, so skip coercion */
350 new_locale = NULL;
351 _Py_SetLocaleFromEnv(LC_CTYPE);
352 continue;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000353 }
Victor Stinner94540602017-12-16 04:54:22 +0100354#endif
355 /* Successfully configured locale, so make it the default */
Victor Stinner0f721472019-05-20 17:16:38 +0200356 coerced = _coerce_default_locale_settings(warn, target);
Victor Stinner8ea09112018-09-03 17:05:18 +0200357 goto done;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000358 }
359 }
360 }
361 /* No C locale warning here, as Py_Initialize will emit one later */
Victor Stinner8ea09112018-09-03 17:05:18 +0200362
363 setlocale(LC_CTYPE, oldloc);
364
365done:
366 PyMem_RawFree(oldloc);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000367#endif
Victor Stinner0f721472019-05-20 17:16:38 +0200368 return coerced;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000369}
370
xdegaye1588be62017-11-12 12:45:59 +0100371/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
372 * isolate the idiosyncrasies of different libc implementations. It reads the
373 * appropriate environment variable and uses its value to select the locale for
374 * 'category'. */
375char *
376_Py_SetLocaleFromEnv(int category)
377{
Victor Stinner353933e2018-11-23 13:08:26 +0100378 char *res;
xdegaye1588be62017-11-12 12:45:59 +0100379#ifdef __ANDROID__
380 const char *locale;
381 const char **pvar;
382#ifdef PY_COERCE_C_LOCALE
383 const char *coerce_c_locale;
384#endif
385 const char *utf8_locale = "C.UTF-8";
386 const char *env_var_set[] = {
387 "LC_ALL",
388 "LC_CTYPE",
389 "LANG",
390 NULL,
391 };
392
393 /* Android setlocale(category, "") doesn't check the environment variables
394 * and incorrectly sets the "C" locale at API 24 and older APIs. We only
395 * check the environment variables listed in env_var_set. */
396 for (pvar=env_var_set; *pvar; pvar++) {
397 locale = getenv(*pvar);
398 if (locale != NULL && *locale != '\0') {
399 if (strcmp(locale, utf8_locale) == 0 ||
400 strcmp(locale, "en_US.UTF-8") == 0) {
401 return setlocale(category, utf8_locale);
402 }
403 return setlocale(category, "C");
404 }
405 }
406
407 /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
408 * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
409 * Quote from POSIX section "8.2 Internationalization Variables":
410 * "4. If the LANG environment variable is not set or is set to the empty
411 * string, the implementation-defined default locale shall be used." */
412
413#ifdef PY_COERCE_C_LOCALE
414 coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
415 if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
416 /* Some other ported code may check the environment variables (e.g. in
417 * extension modules), so we make sure that they match the locale
418 * configuration */
419 if (setenv("LC_CTYPE", utf8_locale, 1)) {
420 fprintf(stderr, "Warning: failed setting the LC_CTYPE "
421 "environment variable to %s\n", utf8_locale);
422 }
423 }
424#endif
Victor Stinner353933e2018-11-23 13:08:26 +0100425 res = setlocale(category, utf8_locale);
426#else /* !defined(__ANDROID__) */
427 res = setlocale(category, "");
428#endif
429 _Py_ResetForceASCII();
430 return res;
xdegaye1588be62017-11-12 12:45:59 +0100431}
432
Nick Coghlan6ea41862017-06-11 13:16:15 +1000433
Eric Snow1abcf672017-05-23 21:46:51 -0700434/* Global initializations. Can be undone by Py_Finalize(). Don't
435 call this twice without an intervening Py_Finalize() call.
436
Victor Stinner331a6a52019-05-27 16:39:22 +0200437 Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
Eric Snow1abcf672017-05-23 21:46:51 -0700438 must have a corresponding call to Py_Finalize.
439
440 Locking: you must hold the interpreter lock while calling these APIs.
441 (If the lock has not yet been initialized, that's equivalent to
442 having the lock, but you cannot use multiple threads.)
443
444*/
445
Victor Stinner331a6a52019-05-27 16:39:22 +0200446static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200447pyinit_core_reconfigure(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200448 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200449 const PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200450{
Victor Stinner331a6a52019-05-27 16:39:22 +0200451 PyStatus status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100452 PyThreadState *tstate = _PyThreadState_GET();
453 if (!tstate) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200454 return _PyStatus_ERR("failed to read thread state");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100455 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200456 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100457
458 PyInterpreterState *interp = tstate->interp;
459 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200460 return _PyStatus_ERR("can't make main interpreter");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100461 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100462
Victor Stinnerdedaac02020-06-08 18:44:50 +0200463 status = _PyConfig_Write(config, runtime);
464 if (_PyStatus_EXCEPTION(status)) {
465 return status;
466 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200467
Victor Stinnerda7933e2020-04-13 03:04:28 +0200468 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200469 if (_PyStatus_EXCEPTION(status)) {
470 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200471 }
Victor Stinnerda7933e2020-04-13 03:04:28 +0200472 config = _PyInterpreterState_GetConfig(interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +0200473
Victor Stinner331a6a52019-05-27 16:39:22 +0200474 if (config->_install_importlib) {
Victor Stinner12f2f172019-09-26 15:51:50 +0200475 status = _PyConfig_WritePathConfig(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200476 if (_PyStatus_EXCEPTION(status)) {
477 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200478 }
479 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200480 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200481}
482
483
Victor Stinner331a6a52019-05-27 16:39:22 +0200484static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200485pycore_init_runtime(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200486 const PyConfig *config)
Nick Coghland6009512014-11-20 21:39:37 +1000487{
Victor Stinner43125222019-04-24 18:23:53 +0200488 if (runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200489 return _PyStatus_ERR("main interpreter already initialized");
Victor Stinner1dc6e392018-07-25 02:49:17 +0200490 }
Victor Stinnerda273412017-12-15 01:46:02 +0100491
Victor Stinnerdedaac02020-06-08 18:44:50 +0200492 PyStatus status = _PyConfig_Write(config, runtime);
493 if (_PyStatus_EXCEPTION(status)) {
494 return status;
495 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600496
Eric Snow1abcf672017-05-23 21:46:51 -0700497 /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
498 * threads behave a little more gracefully at interpreter shutdown.
499 * We clobber it here so the new interpreter can start with a clean
500 * slate.
501 *
502 * However, this may still lead to misbehaviour if there are daemon
503 * threads still hanging around from a previous Py_Initialize/Finalize
504 * pair :(
505 */
Victor Stinner7b3c2522020-03-07 00:24:23 +0100506 _PyRuntimeState_SetFinalizing(runtime, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600507
Victor Stinnerdedaac02020-06-08 18:44:50 +0200508 status = _Py_HashRandomization_Init(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200509 if (_PyStatus_EXCEPTION(status)) {
510 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800511 }
512
Victor Stinner331a6a52019-05-27 16:39:22 +0200513 status = _PyInterpreterState_Enable(runtime);
514 if (_PyStatus_EXCEPTION(status)) {
515 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800516 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200517 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100518}
Victor Stinnera7368ac2017-11-15 18:11:45 -0800519
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100520
Victor Stinner331a6a52019-05-27 16:39:22 +0200521static PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200522init_interp_create_gil(PyThreadState *tstate)
523{
524 PyStatus status;
525
526 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
527 only called here. */
528 _PyEval_FiniGIL(tstate);
529
530 /* Auto-thread-state API */
531 status = _PyGILState_Init(tstate);
532 if (_PyStatus_EXCEPTION(status)) {
533 return status;
534 }
535
536 /* Create the GIL and take it */
537 status = _PyEval_InitGIL(tstate);
538 if (_PyStatus_EXCEPTION(status)) {
539 return status;
540 }
541
542 return _PyStatus_OK();
543}
544
545
546static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200547pycore_create_interpreter(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200548 const PyConfig *config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200549 PyThreadState **tstate_p)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100550{
551 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnerda273412017-12-15 01:46:02 +0100552 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200553 return _PyStatus_ERR("can't make main interpreter");
Victor Stinnerda273412017-12-15 01:46:02 +0100554 }
555
Victor Stinnerda7933e2020-04-13 03:04:28 +0200556 PyStatus status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200557 if (_PyStatus_EXCEPTION(status)) {
558 return status;
Victor Stinnerda273412017-12-15 01:46:02 +0100559 }
Nick Coghland6009512014-11-20 21:39:37 +1000560
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200561 PyThreadState *tstate = PyThreadState_New(interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +0200562 if (tstate == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200563 return _PyStatus_ERR("can't make first thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +0200564 }
Nick Coghland6009512014-11-20 21:39:37 +1000565 (void) PyThreadState_Swap(tstate);
566
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200567 status = init_interp_create_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100568 if (_PyStatus_EXCEPTION(status)) {
569 return status;
570 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100571
Victor Stinnerb45d2592019-06-20 00:05:23 +0200572 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +0200573 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100574}
Nick Coghland6009512014-11-20 21:39:37 +1000575
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100576
Victor Stinner331a6a52019-05-27 16:39:22 +0200577static PyStatus
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100578pycore_init_types(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100579{
Victor Stinner444b39b2019-11-20 01:18:11 +0100580 PyStatus status;
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100581 int is_main_interp = _Py_IsMainInterpreter(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100582
Victor Stinner01b1cc12019-11-20 02:27:56 +0100583 status = _PyGC_Init(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100584 if (_PyStatus_EXCEPTION(status)) {
585 return status;
586 }
587
Victor Stinnere7e699e2019-11-20 12:08:13 +0100588 if (is_main_interp) {
589 status = _PyTypes_Init();
590 if (_PyStatus_EXCEPTION(status)) {
591 return status;
592 }
Victor Stinner630c8df2019-12-17 13:02:18 +0100593 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100594
Victor Stinner630c8df2019-12-17 13:02:18 +0100595
596 if (!_PyLong_Init(tstate)) {
597 return _PyStatus_ERR("can't init longs");
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100598 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100599
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100600 if (is_main_interp) {
Victor Stinnere7e699e2019-11-20 12:08:13 +0100601 status = _PyUnicode_Init();
602 if (_PyStatus_EXCEPTION(status)) {
603 return status;
604 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100605 }
606
Victor Stinner331a6a52019-05-27 16:39:22 +0200607 status = _PyExc_Init();
608 if (_PyStatus_EXCEPTION(status)) {
609 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100610 }
611
Victor Stinnere7e699e2019-11-20 12:08:13 +0100612 if (is_main_interp) {
613 if (!_PyFloat_Init()) {
614 return _PyStatus_ERR("can't init float");
615 }
Nick Coghland6009512014-11-20 21:39:37 +1000616
Victor Stinnere7e699e2019-11-20 12:08:13 +0100617 if (_PyStructSequence_Init() < 0) {
618 return _PyStatus_ERR("can't initialize structseq");
619 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100620 }
Victor Stinneref9d9b62019-05-22 11:28:22 +0200621
Victor Stinner331a6a52019-05-27 16:39:22 +0200622 status = _PyErr_Init();
623 if (_PyStatus_EXCEPTION(status)) {
624 return status;
Victor Stinneref9d9b62019-05-22 11:28:22 +0200625 }
626
Victor Stinnere7e699e2019-11-20 12:08:13 +0100627 if (is_main_interp) {
628 if (!_PyContext_Init()) {
629 return _PyStatus_ERR("can't init context");
630 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100631 }
632
Victor Stinner331a6a52019-05-27 16:39:22 +0200633 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100634}
635
636
Victor Stinner331a6a52019-05-27 16:39:22 +0200637static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200638pycore_init_builtins(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100639{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100640 assert(!_PyErr_Occurred(tstate));
641
Victor Stinnerb45d2592019-06-20 00:05:23 +0200642 PyObject *bimod = _PyBuiltin_Init(tstate);
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100643 if (bimod == NULL) {
Victor Stinner2582d462019-11-22 19:24:49 +0100644 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100645 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100646
Victor Stinner2582d462019-11-22 19:24:49 +0100647 PyInterpreterState *interp = tstate->interp;
648 if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
649 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100650 }
Victor Stinner2582d462019-11-22 19:24:49 +0100651
652 PyObject *builtins_dict = PyModule_GetDict(bimod);
653 if (builtins_dict == NULL) {
654 goto error;
655 }
656 Py_INCREF(builtins_dict);
657 interp->builtins = builtins_dict;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100658
Victor Stinner331a6a52019-05-27 16:39:22 +0200659 PyStatus status = _PyBuiltins_AddExceptions(bimod);
660 if (_PyStatus_EXCEPTION(status)) {
661 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100662 }
Victor Stinner2582d462019-11-22 19:24:49 +0100663
664 interp->builtins_copy = PyDict_Copy(interp->builtins);
665 if (interp->builtins_copy == NULL) {
666 goto error;
667 }
Pablo Galindob96c6b02019-12-04 11:19:59 +0000668 Py_DECREF(bimod);
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100669
670 assert(!_PyErr_Occurred(tstate));
671
Victor Stinner331a6a52019-05-27 16:39:22 +0200672 return _PyStatus_OK();
Victor Stinner2582d462019-11-22 19:24:49 +0100673
674error:
Pablo Galindob96c6b02019-12-04 11:19:59 +0000675 Py_XDECREF(bimod);
Victor Stinner2582d462019-11-22 19:24:49 +0100676 return _PyStatus_ERR("can't initialize builtins module");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100677}
678
679
Victor Stinner331a6a52019-05-27 16:39:22 +0200680static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200681pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100682{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100683 assert(!_PyErr_Occurred(tstate));
Victor Stinnerb45d2592019-06-20 00:05:23 +0200684
Victor Stinner2582d462019-11-22 19:24:49 +0100685 PyStatus status = _PyImportHooks_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200686 if (_PyStatus_EXCEPTION(status)) {
687 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800688 }
Nick Coghland6009512014-11-20 21:39:37 +1000689
Victor Stinnerda7933e2020-04-13 03:04:28 +0200690 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinner2ec1a1b2019-11-22 21:54:33 +0100691 if (_Py_IsMainInterpreter(tstate)) {
692 /* Initialize _warnings. */
Victor Stinner66b79732020-03-02 15:02:18 +0100693 status = _PyWarnings_InitState(tstate);
694 if (_PyStatus_EXCEPTION(status)) {
695 return status;
Victor Stinner2ec1a1b2019-11-22 21:54:33 +0100696 }
Nick Coghland6009512014-11-20 21:39:37 +1000697
Victor Stinner2ec1a1b2019-11-22 21:54:33 +0100698 if (config->_install_importlib) {
699 status = _PyConfig_WritePathConfig(config);
700 if (_PyStatus_EXCEPTION(status)) {
701 return status;
702 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200703 }
704 }
705
Eric Snow1abcf672017-05-23 21:46:51 -0700706 /* This call sets up builtin and frozen import support */
Victor Stinnerb45d2592019-06-20 00:05:23 +0200707 if (config->_install_importlib) {
708 status = init_importlib(tstate, sysmod);
Victor Stinner331a6a52019-05-27 16:39:22 +0200709 if (_PyStatus_EXCEPTION(status)) {
710 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800711 }
Eric Snow1abcf672017-05-23 21:46:51 -0700712 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100713
714 assert(!_PyErr_Occurred(tstate));
715
Victor Stinner331a6a52019-05-27 16:39:22 +0200716 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100717}
718
719
Victor Stinner331a6a52019-05-27 16:39:22 +0200720static PyStatus
Victor Stinnerd863ade2019-12-06 03:37:07 +0100721pycore_interp_init(PyThreadState *tstate)
722{
723 PyStatus status;
Victor Stinner080ee5a2019-12-08 21:55:58 +0100724 PyObject *sysmod = NULL;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100725
726 status = pycore_init_types(tstate);
727 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100728 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100729 }
730
Victor Stinnerd863ade2019-12-06 03:37:07 +0100731 status = _PySys_Create(tstate, &sysmod);
732 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100733 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100734 }
735
736 status = pycore_init_builtins(tstate);
737 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100738 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100739 }
740
Victor Stinner080ee5a2019-12-08 21:55:58 +0100741 status = pycore_init_import_warnings(tstate, sysmod);
742
743done:
744 /* sys.modules['sys'] contains a strong reference to the module */
745 Py_XDECREF(sysmod);
746 return status;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100747}
748
749
750static PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +0200751pyinit_config(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200752 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200753 const PyConfig *config)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100754{
Victor Stinner331a6a52019-05-27 16:39:22 +0200755 PyStatus status = pycore_init_runtime(runtime, config);
756 if (_PyStatus_EXCEPTION(status)) {
757 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100758 }
759
Victor Stinnerb45d2592019-06-20 00:05:23 +0200760 PyThreadState *tstate;
761 status = pycore_create_interpreter(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200762 if (_PyStatus_EXCEPTION(status)) {
763 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100764 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200765 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100766
Victor Stinnerd863ade2019-12-06 03:37:07 +0100767 status = pycore_interp_init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200768 if (_PyStatus_EXCEPTION(status)) {
769 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100770 }
Eric Snow1abcf672017-05-23 21:46:51 -0700771
772 /* Only when we get here is the runtime core fully initialized */
Victor Stinner43125222019-04-24 18:23:53 +0200773 runtime->core_initialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200774 return _PyStatus_OK();
Eric Snow1abcf672017-05-23 21:46:51 -0700775}
776
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100777
Victor Stinner331a6a52019-05-27 16:39:22 +0200778PyStatus
779_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100780{
Victor Stinner331a6a52019-05-27 16:39:22 +0200781 PyStatus status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100782
Victor Stinner6d1c4672019-05-20 11:02:00 +0200783 if (src_config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200784 return _PyStatus_ERR("preinitialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +0200785 }
786
Victor Stinner331a6a52019-05-27 16:39:22 +0200787 status = _PyRuntime_Initialize();
788 if (_PyStatus_EXCEPTION(status)) {
789 return status;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100790 }
Victor Stinner43125222019-04-24 18:23:53 +0200791 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100792
Victor Stinnerd3b90412019-09-17 23:59:51 +0200793 if (runtime->preinitialized) {
Victor Stinnerf72346c2019-03-25 17:54:58 +0100794 /* If it's already configured: ignored the new configuration */
Victor Stinner331a6a52019-05-27 16:39:22 +0200795 return _PyStatus_OK();
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100796 }
797
Victor Stinnerd3b90412019-09-17 23:59:51 +0200798 /* Note: preinitialized remains 1 on error, it is only set to 0
799 at exit on success. */
800 runtime->preinitializing = 1;
801
Victor Stinner331a6a52019-05-27 16:39:22 +0200802 PyPreConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200803
804 status = _PyPreConfig_InitFromPreConfig(&config, src_config);
805 if (_PyStatus_EXCEPTION(status)) {
806 return status;
807 }
Victor Stinnerf72346c2019-03-25 17:54:58 +0100808
Victor Stinner331a6a52019-05-27 16:39:22 +0200809 status = _PyPreConfig_Read(&config, args);
810 if (_PyStatus_EXCEPTION(status)) {
811 return status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100812 }
813
Victor Stinner331a6a52019-05-27 16:39:22 +0200814 status = _PyPreConfig_Write(&config);
815 if (_PyStatus_EXCEPTION(status)) {
816 return status;
Victor Stinnerf72346c2019-03-25 17:54:58 +0100817 }
818
Victor Stinnerd3b90412019-09-17 23:59:51 +0200819 runtime->preinitializing = 0;
820 runtime->preinitialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200821 return _PyStatus_OK();
Victor Stinnerf72346c2019-03-25 17:54:58 +0100822}
823
Victor Stinner70005ac2019-05-02 15:25:34 -0400824
Victor Stinner331a6a52019-05-27 16:39:22 +0200825PyStatus
826Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
Victor Stinnerf72346c2019-03-25 17:54:58 +0100827{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100828 _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400829 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinnerf29084d2019-03-20 02:20:13 +0100830}
831
832
Victor Stinner331a6a52019-05-27 16:39:22 +0200833PyStatus
834Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
Victor Stinner20004952019-03-26 02:31:11 +0100835{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100836 _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400837 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinner20004952019-03-26 02:31:11 +0100838}
839
840
Victor Stinner331a6a52019-05-27 16:39:22 +0200841PyStatus
842Py_PreInitialize(const PyPreConfig *src_config)
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100843{
Victor Stinner70005ac2019-05-02 15:25:34 -0400844 return _Py_PreInitializeFromPyArgv(src_config, NULL);
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100845}
846
847
Victor Stinner331a6a52019-05-27 16:39:22 +0200848PyStatus
849_Py_PreInitializeFromConfig(const PyConfig *config,
850 const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100851{
Victor Stinner331a6a52019-05-27 16:39:22 +0200852 assert(config != NULL);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200853
Victor Stinner331a6a52019-05-27 16:39:22 +0200854 PyStatus status = _PyRuntime_Initialize();
855 if (_PyStatus_EXCEPTION(status)) {
856 return status;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200857 }
858 _PyRuntimeState *runtime = &_PyRuntime;
859
Victor Stinnerd3b90412019-09-17 23:59:51 +0200860 if (runtime->preinitialized) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200861 /* Already initialized: do nothing */
Victor Stinner331a6a52019-05-27 16:39:22 +0200862 return _PyStatus_OK();
Victor Stinner70005ac2019-05-02 15:25:34 -0400863 }
Victor Stinnercab5d072019-05-17 19:01:14 +0200864
Victor Stinner331a6a52019-05-27 16:39:22 +0200865 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200866
Victor Stinner3c30a762019-10-01 10:56:37 +0200867 _PyPreConfig_InitFromConfig(&preconfig, config);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200868
Victor Stinner331a6a52019-05-27 16:39:22 +0200869 if (!config->parse_argv) {
870 return Py_PreInitialize(&preconfig);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200871 }
872 else if (args == NULL) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200873 _PyArgv config_args = {
874 .use_bytes_argv = 0,
Victor Stinner331a6a52019-05-27 16:39:22 +0200875 .argc = config->argv.length,
876 .wchar_argv = config->argv.items};
Victor Stinner6d1c4672019-05-20 11:02:00 +0200877 return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200878 }
879 else {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200880 return _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200881 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100882}
883
884
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100885/* Begin interpreter initialization
886 *
887 * On return, the first thread and interpreter state have been created,
888 * but the compiler, signal handling, multithreading and
889 * multiple interpreter support, and codec infrastructure are not yet
890 * available.
891 *
892 * The import system will support builtin and frozen modules only.
893 * The only supported io is writing to sys.stderr
894 *
895 * If any operation invoked by this function fails, a fatal error is
896 * issued and the function does not return.
897 *
898 * Any code invoked from this function should *not* assume it has access
899 * to the Python C API (unless the API is explicitly listed as being
900 * safe to call without calling Py_Initialize first)
901 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200902static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200903pyinit_core(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200904 const PyConfig *src_config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200905 PyThreadState **tstate_p)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200906{
Victor Stinner331a6a52019-05-27 16:39:22 +0200907 PyStatus status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200908
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 status = _Py_PreInitializeFromConfig(src_config, NULL);
910 if (_PyStatus_EXCEPTION(status)) {
911 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200912 }
913
Victor Stinner331a6a52019-05-27 16:39:22 +0200914 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +0200915 _PyConfig_InitCompatConfig(&config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200916
Victor Stinner331a6a52019-05-27 16:39:22 +0200917 status = _PyConfig_Copy(&config, src_config);
918 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200919 goto done;
920 }
921
Victor Stinner331a6a52019-05-27 16:39:22 +0200922 status = PyConfig_Read(&config);
923 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200924 goto done;
925 }
926
927 if (!runtime->core_initialized) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200928 status = pyinit_config(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200929 }
930 else {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200931 status = pyinit_core_reconfigure(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200932 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200933 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200934 goto done;
935 }
936
937done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200938 PyConfig_Clear(&config);
939 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200940}
941
Victor Stinner5ac27a52019-03-27 13:40:14 +0100942
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200943/* Py_Initialize() has already been called: update the main interpreter
944 configuration. Example of bpo-34008: Py_Main() called after
945 Py_Initialize(). */
Victor Stinner331a6a52019-05-27 16:39:22 +0200946static PyStatus
Victor Stinnerb0051362019-11-22 17:52:42 +0100947_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200948{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200949 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100950
Victor Stinner331a6a52019-05-27 16:39:22 +0200951 PyObject *argv = _PyWideStringList_AsList(&config->argv);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100952 if (argv == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200953 return _PyStatus_NO_MEMORY(); \
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100954 }
955
Victor Stinnerb0051362019-11-22 17:52:42 +0100956 int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100957 Py_DECREF(argv);
958 if (res < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200959 return _PyStatus_ERR("fail to set sys.argv");
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200960 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200961 return _PyStatus_OK();
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200962}
963
Victor Stinnerb0051362019-11-22 17:52:42 +0100964
965static PyStatus
966init_interp_main(PyThreadState *tstate)
967{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100968 assert(!_PyErr_Occurred(tstate));
969
Victor Stinnerb0051362019-11-22 17:52:42 +0100970 PyStatus status;
971 int is_main_interp = _Py_IsMainInterpreter(tstate);
972 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200973 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
Victor Stinnerb0051362019-11-22 17:52:42 +0100974
975 if (!config->_install_importlib) {
976 /* Special mode for freeze_importlib: run with no import system
977 *
978 * This means anything which needs support from extension modules
979 * or pure Python code in the standard library won't work.
980 */
981 if (is_main_interp) {
982 interp->runtime->initialized = 1;
983 }
984 return _PyStatus_OK();
985 }
986
987 if (is_main_interp) {
988 if (_PyTime_Init() < 0) {
989 return _PyStatus_ERR("can't initialize time");
990 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100991 }
Victor Stinnerb0051362019-11-22 17:52:42 +0100992
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100993 if (_PySys_InitMain(tstate) < 0) {
994 return _PyStatus_ERR("can't finish initializing sys");
Victor Stinnerb0051362019-11-22 17:52:42 +0100995 }
996
997 status = init_importlib_external(tstate);
998 if (_PyStatus_EXCEPTION(status)) {
999 return status;
1000 }
1001
1002 if (is_main_interp) {
1003 /* initialize the faulthandler module */
1004 status = _PyFaulthandler_Init(config->faulthandler);
1005 if (_PyStatus_EXCEPTION(status)) {
1006 return status;
1007 }
1008 }
1009
1010 status = _PyUnicode_InitEncodings(tstate);
1011 if (_PyStatus_EXCEPTION(status)) {
1012 return status;
1013 }
1014
1015 if (is_main_interp) {
1016 if (config->install_signal_handlers) {
1017 status = init_signals(tstate);
1018 if (_PyStatus_EXCEPTION(status)) {
1019 return status;
1020 }
1021 }
1022
1023 if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1024 return _PyStatus_ERR("can't initialize tracemalloc");
1025 }
1026 }
1027
1028 status = init_sys_streams(tstate);
1029 if (_PyStatus_EXCEPTION(status)) {
1030 return status;
1031 }
1032
Andy Lester75cd5bf2020-03-12 02:49:05 -05001033 status = init_set_builtins_open();
Victor Stinnerb0051362019-11-22 17:52:42 +01001034 if (_PyStatus_EXCEPTION(status)) {
1035 return status;
1036 }
1037
1038 status = add_main_module(interp);
1039 if (_PyStatus_EXCEPTION(status)) {
1040 return status;
1041 }
1042
1043 if (is_main_interp) {
1044 /* Initialize warnings. */
1045 PyObject *warnoptions = PySys_GetObject("warnoptions");
1046 if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1047 {
1048 PyObject *warnings_module = PyImport_ImportModule("warnings");
1049 if (warnings_module == NULL) {
1050 fprintf(stderr, "'import warnings' failed; traceback:\n");
1051 _PyErr_Print(tstate);
1052 }
1053 Py_XDECREF(warnings_module);
1054 }
1055
1056 interp->runtime->initialized = 1;
1057 }
1058
1059 if (config->site_import) {
1060 status = init_import_site();
1061 if (_PyStatus_EXCEPTION(status)) {
1062 return status;
1063 }
1064 }
1065
1066 if (is_main_interp) {
1067#ifndef MS_WINDOWS
1068 emit_stderr_warning_for_legacy_locale(interp->runtime);
1069#endif
1070 }
1071
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001072 assert(!_PyErr_Occurred(tstate));
1073
Victor Stinnerb0051362019-11-22 17:52:42 +01001074 return _PyStatus_OK();
1075}
1076
1077
Eric Snowc7ec9982017-05-23 23:00:52 -07001078/* Update interpreter state based on supplied configuration settings
1079 *
1080 * After calling this function, most of the restrictions on the interpreter
1081 * are lifted. The only remaining incomplete settings are those related
1082 * to the main module (sys.argv[0], __main__ metadata)
1083 *
1084 * Calling this when the interpreter is not initializing, is already
1085 * initialized or without a valid current thread state is a fatal error.
1086 * Other errors should be reported as normal Python exceptions with a
1087 * non-zero return code.
1088 */
Victor Stinner331a6a52019-05-27 16:39:22 +02001089static PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +01001090pyinit_main(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -07001091{
Victor Stinnerb0051362019-11-22 17:52:42 +01001092 PyInterpreterState *interp = tstate->interp;
1093 if (!interp->runtime->core_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001094 return _PyStatus_ERR("runtime core not initialized");
Eric Snowc7ec9982017-05-23 23:00:52 -07001095 }
Eric Snowc7ec9982017-05-23 23:00:52 -07001096
Victor Stinnerb0051362019-11-22 17:52:42 +01001097 if (interp->runtime->initialized) {
1098 return _Py_ReconfigureMainInterpreter(tstate);
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001099 }
1100
Victor Stinnerb0051362019-11-22 17:52:42 +01001101 PyStatus status = init_interp_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001102 if (_PyStatus_EXCEPTION(status)) {
1103 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001104 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001105 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001106}
1107
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001108
Victor Stinner331a6a52019-05-27 16:39:22 +02001109PyStatus
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001110_Py_InitializeMain(void)
1111{
Victor Stinner331a6a52019-05-27 16:39:22 +02001112 PyStatus status = _PyRuntime_Initialize();
1113 if (_PyStatus_EXCEPTION(status)) {
1114 return status;
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001115 }
1116 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnerb45d2592019-06-20 00:05:23 +02001117 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner01b1cc12019-11-20 02:27:56 +01001118 return pyinit_main(tstate);
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001119}
1120
1121
Victor Stinner331a6a52019-05-27 16:39:22 +02001122PyStatus
1123Py_InitializeFromConfig(const PyConfig *config)
Eric Snow1abcf672017-05-23 21:46:51 -07001124{
Victor Stinner6d1c4672019-05-20 11:02:00 +02001125 if (config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001126 return _PyStatus_ERR("initialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +02001127 }
1128
Victor Stinner331a6a52019-05-27 16:39:22 +02001129 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001130
Victor Stinner331a6a52019-05-27 16:39:22 +02001131 status = _PyRuntime_Initialize();
1132 if (_PyStatus_EXCEPTION(status)) {
1133 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001134 }
1135 _PyRuntimeState *runtime = &_PyRuntime;
1136
Victor Stinnerb45d2592019-06-20 00:05:23 +02001137 PyThreadState *tstate = NULL;
1138 status = pyinit_core(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001139 if (_PyStatus_EXCEPTION(status)) {
1140 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001141 }
Victor Stinnerda7933e2020-04-13 03:04:28 +02001142 config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerbc8ac6b2017-11-30 18:03:55 +01001143
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001144 if (config->_init_main) {
Victor Stinner01b1cc12019-11-20 02:27:56 +01001145 status = pyinit_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001146 if (_PyStatus_EXCEPTION(status)) {
1147 return status;
Victor Stinner484f20d2019-03-27 02:04:16 +01001148 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001149 }
Victor Stinner484f20d2019-03-27 02:04:16 +01001150
Victor Stinner331a6a52019-05-27 16:39:22 +02001151 return _PyStatus_OK();
Victor Stinner5ac27a52019-03-27 13:40:14 +01001152}
1153
1154
Eric Snow1abcf672017-05-23 21:46:51 -07001155void
Nick Coghland6009512014-11-20 21:39:37 +10001156Py_InitializeEx(int install_sigs)
1157{
Victor Stinner331a6a52019-05-27 16:39:22 +02001158 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001159
Victor Stinner331a6a52019-05-27 16:39:22 +02001160 status = _PyRuntime_Initialize();
1161 if (_PyStatus_EXCEPTION(status)) {
1162 Py_ExitStatusException(status);
Victor Stinner43125222019-04-24 18:23:53 +02001163 }
1164 _PyRuntimeState *runtime = &_PyRuntime;
1165
1166 if (runtime->initialized) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001167 /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1168 return;
1169 }
1170
Victor Stinner331a6a52019-05-27 16:39:22 +02001171 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001172 _PyConfig_InitCompatConfig(&config);
Victor Stinner441b10c2019-09-28 04:28:35 +02001173
Victor Stinner1dc6e392018-07-25 02:49:17 +02001174 config.install_signal_handlers = install_sigs;
1175
Victor Stinner331a6a52019-05-27 16:39:22 +02001176 status = Py_InitializeFromConfig(&config);
1177 if (_PyStatus_EXCEPTION(status)) {
1178 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001179 }
Nick Coghland6009512014-11-20 21:39:37 +10001180}
1181
1182void
1183Py_Initialize(void)
1184{
1185 Py_InitializeEx(1);
1186}
1187
1188
Nick Coghland6009512014-11-20 21:39:37 +10001189/* Flush stdout and stderr */
1190
1191static int
1192file_is_closed(PyObject *fobj)
1193{
1194 int r;
1195 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1196 if (tmp == NULL) {
1197 PyErr_Clear();
1198 return 0;
1199 }
1200 r = PyObject_IsTrue(tmp);
1201 Py_DECREF(tmp);
1202 if (r < 0)
1203 PyErr_Clear();
1204 return r > 0;
1205}
1206
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001207static int
Nick Coghland6009512014-11-20 21:39:37 +10001208flush_std_files(void)
1209{
1210 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1211 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1212 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001213 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001214
1215 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001216 tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001217 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001218 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001219 status = -1;
1220 }
Nick Coghland6009512014-11-20 21:39:37 +10001221 else
1222 Py_DECREF(tmp);
1223 }
1224
1225 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001226 tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001227 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001228 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001229 status = -1;
1230 }
Nick Coghland6009512014-11-20 21:39:37 +10001231 else
1232 Py_DECREF(tmp);
1233 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001234
1235 return status;
Nick Coghland6009512014-11-20 21:39:37 +10001236}
1237
1238/* Undo the effect of Py_Initialize().
1239
1240 Beware: if multiple interpreter and/or thread states exist, these
1241 are not wiped out; only the current thread and interpreter state
1242 are deleted. But since everything else is deleted, those other
1243 interpreter and thread states should no longer be used.
1244
1245 (XXX We should do better, e.g. wipe out all interpreters and
1246 threads.)
1247
1248 Locking: as above.
1249
1250*/
1251
Victor Stinner7eee5be2019-11-20 10:38:34 +01001252
1253static void
1254finalize_interp_types(PyThreadState *tstate, int is_main_interp)
1255{
1256 if (is_main_interp) {
1257 /* Sundry finalizers */
Victor Stinner7eee5be2019-11-20 10:38:34 +01001258 _PyFrame_Fini();
Victor Stinner7eee5be2019-11-20 10:38:34 +01001259 _PyTuple_Fini();
1260 _PyList_Fini();
1261 _PySet_Fini();
1262 _PyBytes_Fini();
Victor Stinner630c8df2019-12-17 13:02:18 +01001263 }
1264
1265 _PyLong_Fini(tstate);
1266
1267 if (is_main_interp) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001268 _PyFloat_Fini();
1269 _PyDict_Fini();
1270 _PySlice_Fini();
1271 }
1272
1273 _PyWarnings_Fini(tstate->interp);
1274
1275 if (is_main_interp) {
1276 _Py_HashRandomization_Fini();
1277 _PyArg_Fini();
1278 _PyAsyncGen_Fini();
1279 _PyContext_Fini();
Victor Stinner3d483342019-11-22 12:27:50 +01001280 }
Victor Stinner7eee5be2019-11-20 10:38:34 +01001281
Victor Stinner3d483342019-11-22 12:27:50 +01001282 /* Cleanup Unicode implementation */
1283 _PyUnicode_Fini(tstate);
1284
1285 if (is_main_interp) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001286 _Py_ClearFileSystemEncoding();
1287 }
1288}
1289
1290
1291static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001292finalize_interp_clear(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001293{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001294 int is_main_interp = _Py_IsMainInterpreter(tstate);
1295
Victor Stinner7eee5be2019-11-20 10:38:34 +01001296 /* Clear interpreter state and all thread states */
1297 PyInterpreterState_Clear(tstate->interp);
1298
Pablo Galindoac0e1c22019-12-04 11:51:03 +00001299 /* Trigger a GC collection on subinterpreters*/
1300 if (!is_main_interp) {
1301 _PyGC_CollectNoFail();
1302 }
1303
Steve Dowere1d4fdc2020-07-03 22:58:29 +01001304 /* Clear all loghooks */
1305 /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1306 Call _PySys_ClearAuditHooks when PyObject available. */
1307 if (is_main_interp) {
1308 _PySys_ClearAuditHooks(tstate);
1309 }
1310
Steve Dower941117a2020-07-03 23:34:46 +01001311 finalize_interp_types(tstate, is_main_interp);
1312
Victor Stinner7eee5be2019-11-20 10:38:34 +01001313 if (is_main_interp) {
1314 /* XXX Still allocated:
1315 - various static ad-hoc pointers to interned strings
1316 - int and float free list blocks
1317 - whatever various modules and libraries allocate
1318 */
1319
1320 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
1321
1322 _PyExc_Fini();
Victor Stinner7eee5be2019-11-20 10:38:34 +01001323 }
Victor Stinner72474072019-11-20 12:25:50 +01001324
1325 _PyGC_Fini(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001326}
1327
1328
1329static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001330finalize_interp_delete(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001331{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001332 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001333 /* Cleanup auto-thread-state */
1334 _PyGILState_Fini(tstate);
1335 }
1336
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001337 /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1338 fail when it is being awaited by another running daemon thread (see
1339 bpo-9901). Instead pycore_create_interpreter() destroys the previously
1340 created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1341 called multiple times. */
1342
Victor Stinner7eee5be2019-11-20 10:38:34 +01001343 PyInterpreterState_Delete(tstate->interp);
1344}
1345
1346
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001347int
1348Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +10001349{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001350 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001351
Victor Stinner8e91c242019-04-24 17:24:01 +02001352 _PyRuntimeState *runtime = &_PyRuntime;
1353 if (!runtime->initialized) {
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001354 return status;
Victor Stinner8e91c242019-04-24 17:24:01 +02001355 }
Nick Coghland6009512014-11-20 21:39:37 +10001356
Victor Stinnere225beb2019-06-03 18:14:24 +02001357 /* Get current thread state and interpreter pointer */
1358 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1359 PyInterpreterState *interp = tstate->interp;
Victor Stinner8e91c242019-04-24 17:24:01 +02001360
Victor Stinnerb45d2592019-06-20 00:05:23 +02001361 // Wrap up existing "threading"-module-created, non-daemon threads.
1362 wait_for_thread_shutdown(tstate);
1363
1364 // Make any remaining pending calls.
Victor Stinner2b1df452020-01-13 18:46:59 +01001365 _Py_FinishPendingCalls(tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001366
Nick Coghland6009512014-11-20 21:39:37 +10001367 /* The interpreter is still entirely intact at this point, and the
1368 * exit funcs may be relying on that. In particular, if some thread
1369 * or exit func is still waiting to do an import, the import machinery
1370 * expects Py_IsInitialized() to return true. So don't say the
Eric Snow842a2f02019-03-15 15:47:51 -06001371 * runtime is uninitialized until after the exit funcs have run.
Nick Coghland6009512014-11-20 21:39:37 +10001372 * Note that Threading.py uses an exit func to do a join on all the
1373 * threads created thru it, so this also protects pending imports in
1374 * the threads created via Threading.
1375 */
Nick Coghland6009512014-11-20 21:39:37 +10001376
Victor Stinnerb45d2592019-06-20 00:05:23 +02001377 call_py_exitfuncs(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001378
Victor Stinnerda273412017-12-15 01:46:02 +01001379 /* Copy the core config, PyInterpreterState_Delete() free
1380 the core config memory */
Victor Stinner5d862462017-12-19 11:35:58 +01001381#ifdef Py_REF_DEBUG
Victor Stinner331a6a52019-05-27 16:39:22 +02001382 int show_ref_count = interp->config.show_ref_count;
Victor Stinner5d862462017-12-19 11:35:58 +01001383#endif
1384#ifdef Py_TRACE_REFS
Victor Stinner331a6a52019-05-27 16:39:22 +02001385 int dump_refs = interp->config.dump_refs;
Victor Stinner5d862462017-12-19 11:35:58 +01001386#endif
1387#ifdef WITH_PYMALLOC
Victor Stinner331a6a52019-05-27 16:39:22 +02001388 int malloc_stats = interp->config.malloc_stats;
Victor Stinner5d862462017-12-19 11:35:58 +01001389#endif
Victor Stinner6bf992a2017-12-06 17:26:10 +01001390
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001391 /* Remaining daemon threads will automatically exit
1392 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
Victor Stinner7b3c2522020-03-07 00:24:23 +01001393 _PyRuntimeState_SetFinalizing(runtime, tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +02001394 runtime->initialized = 0;
1395 runtime->core_initialized = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001396
Victor Stinner9ad58ac2020-03-09 23:37:49 +01001397 /* Destroy the state of all threads of the interpreter, except of the
1398 current thread. In practice, only daemon threads should still be alive,
1399 except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1400 Clear frames of other threads to call objects destructors. Destructors
1401 will be called in the current Python thread. Since
1402 _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1403 can take the GIL at this point: if they try, they will exit
1404 immediately. */
1405 _PyThreadState_DeleteExcept(runtime, tstate);
1406
Victor Stinnere0deff32015-03-24 13:46:18 +01001407 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001408 if (flush_std_files() < 0) {
1409 status = -1;
1410 }
Nick Coghland6009512014-11-20 21:39:37 +10001411
1412 /* Disable signal handling */
1413 PyOS_FiniInterrupts();
1414
1415 /* Collect garbage. This may call finalizers; it's nice to call these
1416 * before all modules are destroyed.
1417 * XXX If a __del__ or weakref callback is triggered here, and tries to
1418 * XXX import a module, bad things can happen, because Python no
1419 * XXX longer believes it's initialized.
1420 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
1421 * XXX is easy to provoke that way. I've also seen, e.g.,
1422 * XXX Exception exceptions.ImportError: 'No module named sha'
1423 * XXX in <function callback at 0x008F5718> ignored
1424 * XXX but I'm unclear on exactly how that one happens. In any case,
1425 * XXX I haven't seen a real-life report of either of these.
1426 */
Łukasz Langafef7e942016-09-09 21:47:46 -07001427 _PyGC_CollectIfEnabled();
Eric Snowdae02762017-09-14 00:35:58 -07001428
Nick Coghland6009512014-11-20 21:39:37 +10001429 /* Destroy all modules */
Victor Stinner987a0dc2019-06-19 10:36:10 +02001430 _PyImport_Cleanup(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001431
Inada Naoki91234a12019-06-03 21:30:58 +09001432 /* Print debug stats if any */
1433 _PyEval_Fini();
1434
Victor Stinnere0deff32015-03-24 13:46:18 +01001435 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001436 if (flush_std_files() < 0) {
1437 status = -1;
1438 }
Nick Coghland6009512014-11-20 21:39:37 +10001439
1440 /* Collect final garbage. This disposes of cycles created by
1441 * class definitions, for example.
1442 * XXX This is disabled because it caused too many problems. If
1443 * XXX a __del__ or weakref callback triggers here, Python code has
1444 * XXX a hard time running, because even the sys module has been
1445 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1446 * XXX One symptom is a sequence of information-free messages
1447 * XXX coming from threads (if a __del__ or callback is invoked,
1448 * XXX other threads can execute too, and any exception they encounter
1449 * XXX triggers a comedy of errors as subsystem after subsystem
1450 * XXX fails to find what it *expects* to find in sys to help report
1451 * XXX the exception and consequent unexpected failures). I've also
1452 * XXX seen segfaults then, after adding print statements to the
1453 * XXX Python code getting called.
1454 */
1455#if 0
Łukasz Langafef7e942016-09-09 21:47:46 -07001456 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +10001457#endif
1458
1459 /* Disable tracemalloc after all Python objects have been destroyed,
1460 so it is possible to use tracemalloc in objects destructor. */
1461 _PyTraceMalloc_Fini();
1462
1463 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1464 _PyImport_Fini();
1465
1466 /* Cleanup typeobject.c's internal caches. */
1467 _PyType_Fini();
1468
1469 /* unload faulthandler module */
1470 _PyFaulthandler_Fini();
1471
Nick Coghland6009512014-11-20 21:39:37 +10001472 /* dump hash stats */
1473 _PyHash_Fini();
1474
Eric Snowdae02762017-09-14 00:35:58 -07001475#ifdef Py_REF_DEBUG
Victor Stinnerda273412017-12-15 01:46:02 +01001476 if (show_ref_count) {
Victor Stinner25420fe2017-11-20 18:12:22 -08001477 _PyDebug_PrintTotalRefs();
1478 }
Eric Snowdae02762017-09-14 00:35:58 -07001479#endif
Nick Coghland6009512014-11-20 21:39:37 +10001480
1481#ifdef Py_TRACE_REFS
1482 /* Display all objects still alive -- this can invoke arbitrary
1483 * __repr__ overrides, so requires a mostly-intact interpreter.
1484 * Alas, a lot of stuff may still be alive now that will be cleaned
1485 * up later.
1486 */
Victor Stinnerda273412017-12-15 01:46:02 +01001487 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001488 _Py_PrintReferences(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001489 }
Nick Coghland6009512014-11-20 21:39:37 +10001490#endif /* Py_TRACE_REFS */
1491
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001492 finalize_interp_clear(tstate);
1493 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001494
1495#ifdef Py_TRACE_REFS
1496 /* Display addresses (& refcnts) of all objects still alive.
1497 * An address can be used to find the repr of the object, printed
1498 * above by _Py_PrintReferences.
1499 */
Victor Stinnerda273412017-12-15 01:46:02 +01001500 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001501 _Py_PrintReferenceAddresses(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001502 }
Nick Coghland6009512014-11-20 21:39:37 +10001503#endif /* Py_TRACE_REFS */
Victor Stinner34be8072016-03-14 12:04:26 +01001504#ifdef WITH_PYMALLOC
Victor Stinnerda273412017-12-15 01:46:02 +01001505 if (malloc_stats) {
Victor Stinner6bf992a2017-12-06 17:26:10 +01001506 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be8072016-03-14 12:04:26 +01001507 }
Nick Coghland6009512014-11-20 21:39:37 +10001508#endif
1509
Victor Stinner8e91c242019-04-24 17:24:01 +02001510 call_ll_exitfuncs(runtime);
Victor Stinner9316ee42017-11-25 03:17:57 +01001511
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001512 _PyRuntime_Finalize();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001513 return status;
1514}
1515
1516void
1517Py_Finalize(void)
1518{
1519 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +10001520}
1521
Victor Stinnerb0051362019-11-22 17:52:42 +01001522
Nick Coghland6009512014-11-20 21:39:37 +10001523/* Create and initialize a new interpreter and thread, and return the
1524 new thread. This requires that Py_Initialize() has been called
1525 first.
1526
1527 Unsuccessful initialization yields a NULL pointer. Note that *no*
1528 exception information is available even in this case -- the
1529 exception information is held in the thread, and there is no
1530 thread.
1531
1532 Locking: as above.
1533
1534*/
1535
Victor Stinner331a6a52019-05-27 16:39:22 +02001536static PyStatus
Victor Stinner252346a2020-05-01 11:33:44 +02001537new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
Nick Coghland6009512014-11-20 21:39:37 +10001538{
Victor Stinner331a6a52019-05-27 16:39:22 +02001539 PyStatus status;
Nick Coghland6009512014-11-20 21:39:37 +10001540
Victor Stinner331a6a52019-05-27 16:39:22 +02001541 status = _PyRuntime_Initialize();
1542 if (_PyStatus_EXCEPTION(status)) {
1543 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001544 }
1545 _PyRuntimeState *runtime = &_PyRuntime;
1546
1547 if (!runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001548 return _PyStatus_ERR("Py_Initialize must be called first");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001549 }
Nick Coghland6009512014-11-20 21:39:37 +10001550
Victor Stinner8a1be612016-03-14 22:07:55 +01001551 /* Issue #10915, #15751: The GIL API doesn't work with multiple
1552 interpreters: disable PyGILState_Check(). */
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001553 runtime->gilstate.check_enabled = 0;
Victor Stinner8a1be612016-03-14 22:07:55 +01001554
Victor Stinner43125222019-04-24 18:23:53 +02001555 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001556 if (interp == NULL) {
1557 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001558 return _PyStatus_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001559 }
Nick Coghland6009512014-11-20 21:39:37 +10001560
Victor Stinner43125222019-04-24 18:23:53 +02001561 PyThreadState *tstate = PyThreadState_New(interp);
Nick Coghland6009512014-11-20 21:39:37 +10001562 if (tstate == NULL) {
1563 PyInterpreterState_Delete(interp);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001564 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001565 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001566 }
1567
Victor Stinner43125222019-04-24 18:23:53 +02001568 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001569
Eric Snow1abcf672017-05-23 21:46:51 -07001570 /* Copy the current interpreter config into the new interpreter */
Victor Stinnerda7933e2020-04-13 03:04:28 +02001571 const PyConfig *config;
Eric Snow1abcf672017-05-23 21:46:51 -07001572 if (save_tstate != NULL) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001573 config = _PyInterpreterState_GetConfig(save_tstate->interp);
Victor Stinner7be4e352020-05-05 20:27:47 +02001574 }
1575 else
Victor Stinner7be4e352020-05-05 20:27:47 +02001576 {
Eric Snow1abcf672017-05-23 21:46:51 -07001577 /* No current thread state, copy from the main interpreter */
1578 PyInterpreterState *main_interp = PyInterpreterState_Main();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001579 config = _PyInterpreterState_GetConfig(main_interp);
Victor Stinnerda273412017-12-15 01:46:02 +01001580 }
1581
Victor Stinnerda7933e2020-04-13 03:04:28 +02001582 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001583 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001584 goto error;
Victor Stinnerda273412017-12-15 01:46:02 +01001585 }
Victor Stinner252346a2020-05-01 11:33:44 +02001586 interp->config._isolated_interpreter = isolated_subinterpreter;
Eric Snow1abcf672017-05-23 21:46:51 -07001587
Victor Stinner0dd5e7a2020-05-05 20:16:37 +02001588 status = init_interp_create_gil(tstate);
1589 if (_PyStatus_EXCEPTION(status)) {
1590 goto error;
1591 }
1592
Victor Stinnerd863ade2019-12-06 03:37:07 +01001593 status = pycore_interp_init(tstate);
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001594 if (_PyStatus_EXCEPTION(status)) {
1595 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001596 }
1597
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001598 status = init_interp_main(tstate);
1599 if (_PyStatus_EXCEPTION(status)) {
1600 goto error;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001601 }
Nick Coghland6009512014-11-20 21:39:37 +10001602
Victor Stinnera7368ac2017-11-15 18:11:45 -08001603 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +02001604 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001605
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001606error:
Victor Stinnerb0051362019-11-22 17:52:42 +01001607 *tstate_p = NULL;
1608
1609 /* Oops, it didn't work. Undo it all. */
Nick Coghland6009512014-11-20 21:39:37 +10001610 PyErr_PrintEx(0);
1611 PyThreadState_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001612 PyThreadState_Delete(tstate);
1613 PyInterpreterState_Delete(interp);
Victor Stinner9da74302019-11-20 11:17:17 +01001614 PyThreadState_Swap(save_tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001615
Victor Stinnerb0051362019-11-22 17:52:42 +01001616 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001617}
1618
1619PyThreadState *
Victor Stinner252346a2020-05-01 11:33:44 +02001620_Py_NewInterpreter(int isolated_subinterpreter)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001621{
Stéphane Wirtel9e06d2b2019-03-18 17:10:29 +01001622 PyThreadState *tstate = NULL;
Victor Stinner252346a2020-05-01 11:33:44 +02001623 PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
Victor Stinner331a6a52019-05-27 16:39:22 +02001624 if (_PyStatus_EXCEPTION(status)) {
1625 Py_ExitStatusException(status);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001626 }
1627 return tstate;
1628
Nick Coghland6009512014-11-20 21:39:37 +10001629}
1630
Victor Stinner252346a2020-05-01 11:33:44 +02001631PyThreadState *
1632Py_NewInterpreter(void)
1633{
1634 return _Py_NewInterpreter(0);
1635}
1636
Nick Coghland6009512014-11-20 21:39:37 +10001637/* Delete an interpreter and its last thread. This requires that the
1638 given thread state is current, that the thread has no remaining
1639 frames, and that it is its interpreter's only remaining thread.
1640 It is a fatal error to violate these constraints.
1641
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001642 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +10001643 everything, regardless.)
1644
1645 Locking: as above.
1646
1647*/
1648
1649void
1650Py_EndInterpreter(PyThreadState *tstate)
1651{
1652 PyInterpreterState *interp = tstate->interp;
1653
Victor Stinnerb45d2592019-06-20 00:05:23 +02001654 if (tstate != _PyThreadState_GET()) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001655 Py_FatalError("thread is not current");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001656 }
1657 if (tstate->frame != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001658 Py_FatalError("thread still has a frame");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001659 }
Eric Snow5be45a62019-03-08 22:47:07 -07001660 interp->finalizing = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001661
Eric Snow842a2f02019-03-15 15:47:51 -06001662 // Wrap up existing "threading"-module-created, non-daemon threads.
Victor Stinnerb45d2592019-06-20 00:05:23 +02001663 wait_for_thread_shutdown(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001664
Victor Stinnerb45d2592019-06-20 00:05:23 +02001665 call_py_exitfuncs(tstate);
Marcel Plch776407f2017-12-20 11:17:58 +01001666
Victor Stinnerb45d2592019-06-20 00:05:23 +02001667 if (tstate != interp->tstate_head || tstate->next != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001668 Py_FatalError("not the last thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001669 }
Nick Coghland6009512014-11-20 21:39:37 +10001670
Victor Stinner987a0dc2019-06-19 10:36:10 +02001671 _PyImport_Cleanup(tstate);
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001672 finalize_interp_clear(tstate);
1673 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001674}
1675
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001676/* Add the __main__ module */
Nick Coghland6009512014-11-20 21:39:37 +10001677
Victor Stinner331a6a52019-05-27 16:39:22 +02001678static PyStatus
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001679add_main_module(PyInterpreterState *interp)
Nick Coghland6009512014-11-20 21:39:37 +10001680{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001681 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +10001682 m = PyImport_AddModule("__main__");
1683 if (m == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +02001684 return _PyStatus_ERR("can't create __main__ module");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001685
Nick Coghland6009512014-11-20 21:39:37 +10001686 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001687 ann_dict = PyDict_New();
1688 if ((ann_dict == NULL) ||
1689 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001690 return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001691 }
1692 Py_DECREF(ann_dict);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001693
Nick Coghland6009512014-11-20 21:39:37 +10001694 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
1695 PyObject *bimod = PyImport_ImportModule("builtins");
1696 if (bimod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001697 return _PyStatus_ERR("Failed to retrieve builtins module");
Nick Coghland6009512014-11-20 21:39:37 +10001698 }
1699 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001700 return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
Nick Coghland6009512014-11-20 21:39:37 +10001701 }
1702 Py_DECREF(bimod);
1703 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001704
Nick Coghland6009512014-11-20 21:39:37 +10001705 /* Main is a little special - imp.is_builtin("__main__") will return
1706 * False, but BuiltinImporter is still the most appropriate initial
1707 * setting for its __loader__ attribute. A more suitable value will
1708 * be set if __main__ gets further initialized later in the startup
1709 * process.
1710 */
1711 loader = PyDict_GetItemString(d, "__loader__");
1712 if (loader == NULL || loader == Py_None) {
1713 PyObject *loader = PyObject_GetAttrString(interp->importlib,
1714 "BuiltinImporter");
1715 if (loader == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001716 return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
Nick Coghland6009512014-11-20 21:39:37 +10001717 }
1718 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001719 return _PyStatus_ERR("Failed to initialize __main__.__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10001720 }
1721 Py_DECREF(loader);
1722 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001723 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001724}
1725
Nick Coghland6009512014-11-20 21:39:37 +10001726/* Import the site module (not into __main__ though) */
1727
Victor Stinner331a6a52019-05-27 16:39:22 +02001728static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02001729init_import_site(void)
Nick Coghland6009512014-11-20 21:39:37 +10001730{
1731 PyObject *m;
1732 m = PyImport_ImportModule("site");
1733 if (m == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001734 return _PyStatus_ERR("Failed to import the site module");
Nick Coghland6009512014-11-20 21:39:37 +10001735 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001736 Py_DECREF(m);
Victor Stinner331a6a52019-05-27 16:39:22 +02001737 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001738}
1739
Victor Stinner874dbe82015-09-04 17:29:57 +02001740/* Check if a file descriptor is valid or not.
1741 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
1742static int
1743is_valid_fd(int fd)
1744{
Victor Stinner3092d6b2019-04-17 18:09:12 +02001745/* dup() is faster than fstat(): fstat() can require input/output operations,
1746 whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
1747 startup. Problem: dup() doesn't check if the file descriptor is valid on
1748 some platforms.
1749
1750 bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
1751 side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
1752 EBADF. FreeBSD has similar issue (bpo-32849).
1753
1754 Only use dup() on platforms where dup() is enough to detect invalid FD in
1755 corner cases: on Linux and Windows (bpo-32849). */
1756#if defined(__linux__) || defined(MS_WINDOWS)
1757 if (fd < 0) {
1758 return 0;
1759 }
1760 int fd2;
1761
1762 _Py_BEGIN_SUPPRESS_IPH
1763 fd2 = dup(fd);
1764 if (fd2 >= 0) {
1765 close(fd2);
1766 }
1767 _Py_END_SUPPRESS_IPH
1768
1769 return (fd2 >= 0);
1770#else
Victor Stinner1c4670e2017-05-04 00:45:56 +02001771 struct stat st;
1772 return (fstat(fd, &st) == 0);
Victor Stinner1c4670e2017-05-04 00:45:56 +02001773#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02001774}
1775
1776/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10001777static PyObject*
Victor Stinner331a6a52019-05-27 16:39:22 +02001778create_stdio(const PyConfig *config, PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001779 int fd, int write_mode, const char* name,
Victor Stinner709d23d2019-05-02 14:56:30 -04001780 const wchar_t* encoding, const wchar_t* errors)
Nick Coghland6009512014-11-20 21:39:37 +10001781{
1782 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
1783 const char* mode;
1784 const char* newline;
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001785 PyObject *line_buffering, *write_through;
Nick Coghland6009512014-11-20 21:39:37 +10001786 int buffering, isatty;
1787 _Py_IDENTIFIER(open);
1788 _Py_IDENTIFIER(isatty);
1789 _Py_IDENTIFIER(TextIOWrapper);
1790 _Py_IDENTIFIER(mode);
Victor Stinnerfbca9082018-08-30 00:50:45 +02001791 const int buffered_stdio = config->buffered_stdio;
Nick Coghland6009512014-11-20 21:39:37 +10001792
Victor Stinner874dbe82015-09-04 17:29:57 +02001793 if (!is_valid_fd(fd))
1794 Py_RETURN_NONE;
1795
Nick Coghland6009512014-11-20 21:39:37 +10001796 /* stdin is always opened in buffered mode, first because it shouldn't
1797 make a difference in common use cases, second because TextIOWrapper
1798 depends on the presence of a read1() method which only exists on
1799 buffered streams.
1800 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02001801 if (!buffered_stdio && write_mode)
Nick Coghland6009512014-11-20 21:39:37 +10001802 buffering = 0;
1803 else
1804 buffering = -1;
1805 if (write_mode)
1806 mode = "wb";
1807 else
1808 mode = "rb";
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03001809 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
Nick Coghland6009512014-11-20 21:39:37 +10001810 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001811 Py_None, Py_None, /* encoding, errors */
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03001812 Py_None, Py_False); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10001813 if (buf == NULL)
1814 goto error;
1815
1816 if (buffering) {
1817 _Py_IDENTIFIER(raw);
1818 raw = _PyObject_GetAttrId(buf, &PyId_raw);
1819 if (raw == NULL)
1820 goto error;
1821 }
1822 else {
1823 raw = buf;
1824 Py_INCREF(raw);
1825 }
1826
Steve Dower39294992016-08-30 21:22:36 -07001827#ifdef MS_WINDOWS
1828 /* Windows console IO is always UTF-8 encoded */
1829 if (PyWindowsConsoleIO_Check(raw))
Victor Stinner709d23d2019-05-02 14:56:30 -04001830 encoding = L"utf-8";
Steve Dower39294992016-08-30 21:22:36 -07001831#endif
1832
Nick Coghland6009512014-11-20 21:39:37 +10001833 text = PyUnicode_FromString(name);
1834 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
1835 goto error;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001836 res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
Nick Coghland6009512014-11-20 21:39:37 +10001837 if (res == NULL)
1838 goto error;
1839 isatty = PyObject_IsTrue(res);
1840 Py_DECREF(res);
1841 if (isatty == -1)
1842 goto error;
Victor Stinnerfbca9082018-08-30 00:50:45 +02001843 if (!buffered_stdio)
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001844 write_through = Py_True;
1845 else
1846 write_through = Py_False;
Jendrik Seipp5b907712020-01-01 23:21:43 +01001847 if (buffered_stdio && (isatty || fd == fileno(stderr)))
Nick Coghland6009512014-11-20 21:39:37 +10001848 line_buffering = Py_True;
1849 else
1850 line_buffering = Py_False;
1851
1852 Py_CLEAR(raw);
1853 Py_CLEAR(text);
1854
1855#ifdef MS_WINDOWS
1856 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1857 newlines to "\n".
1858 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1859 newline = NULL;
1860#else
1861 /* sys.stdin: split lines at "\n".
1862 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1863 newline = "\n";
1864#endif
1865
Victor Stinner709d23d2019-05-02 14:56:30 -04001866 PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
1867 if (encoding_str == NULL) {
1868 Py_CLEAR(buf);
1869 goto error;
1870 }
1871
1872 PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
1873 if (errors_str == NULL) {
1874 Py_CLEAR(buf);
1875 Py_CLEAR(encoding_str);
1876 goto error;
1877 }
1878
1879 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
1880 buf, encoding_str, errors_str,
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001881 newline, line_buffering, write_through);
Nick Coghland6009512014-11-20 21:39:37 +10001882 Py_CLEAR(buf);
Victor Stinner709d23d2019-05-02 14:56:30 -04001883 Py_CLEAR(encoding_str);
1884 Py_CLEAR(errors_str);
Nick Coghland6009512014-11-20 21:39:37 +10001885 if (stream == NULL)
1886 goto error;
1887
1888 if (write_mode)
1889 mode = "w";
1890 else
1891 mode = "r";
1892 text = PyUnicode_FromString(mode);
1893 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
1894 goto error;
1895 Py_CLEAR(text);
1896 return stream;
1897
1898error:
1899 Py_XDECREF(buf);
1900 Py_XDECREF(stream);
1901 Py_XDECREF(text);
1902 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10001903
Victor Stinner874dbe82015-09-04 17:29:57 +02001904 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
1905 /* Issue #24891: the file descriptor was closed after the first
1906 is_valid_fd() check was called. Ignore the OSError and set the
1907 stream to None. */
1908 PyErr_Clear();
1909 Py_RETURN_NONE;
1910 }
1911 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001912}
1913
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001914/* Set builtins.open to io.OpenWrapper */
1915static PyStatus
Andy Lester75cd5bf2020-03-12 02:49:05 -05001916init_set_builtins_open(void)
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001917{
1918 PyObject *iomod = NULL, *wrapper;
1919 PyObject *bimod = NULL;
1920 PyStatus res = _PyStatus_OK();
1921
1922 if (!(iomod = PyImport_ImportModule("io"))) {
1923 goto error;
1924 }
1925
1926 if (!(bimod = PyImport_ImportModule("builtins"))) {
1927 goto error;
1928 }
1929
1930 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1931 goto error;
1932 }
1933
1934 /* Set builtins.open */
1935 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
1936 Py_DECREF(wrapper);
1937 goto error;
1938 }
1939 Py_DECREF(wrapper);
1940 goto done;
1941
1942error:
1943 res = _PyStatus_ERR("can't initialize io.open");
1944
1945done:
1946 Py_XDECREF(bimod);
1947 Py_XDECREF(iomod);
1948 return res;
1949}
1950
1951
Nick Coghland6009512014-11-20 21:39:37 +10001952/* Initialize sys.stdin, stdout, stderr and builtins.open */
Victor Stinner331a6a52019-05-27 16:39:22 +02001953static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02001954init_sys_streams(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10001955{
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001956 PyObject *iomod = NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001957 PyObject *m;
1958 PyObject *std = NULL;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001959 int fd;
Nick Coghland6009512014-11-20 21:39:37 +10001960 PyObject * encoding_attr;
Victor Stinner331a6a52019-05-27 16:39:22 +02001961 PyStatus res = _PyStatus_OK();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001962 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02001963
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01001964 /* Check that stdin is not a directory
1965 Using shell redirection, you can redirect stdin to a directory,
1966 crashing the Python interpreter. Catch this common mistake here
1967 and output a useful error message. Note that under MS Windows,
1968 the shell already prevents that. */
1969#ifndef MS_WINDOWS
1970 struct _Py_stat_struct sb;
1971 if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
1972 S_ISDIR(sb.st_mode)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001973 return _PyStatus_ERR("<stdin> is a directory, cannot continue");
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01001974 }
1975#endif
1976
Nick Coghland6009512014-11-20 21:39:37 +10001977 /* Hack to avoid a nasty recursion issue when Python is invoked
1978 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1979 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1980 goto error;
1981 }
1982 Py_DECREF(m);
1983
1984 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1985 goto error;
1986 }
1987 Py_DECREF(m);
1988
Nick Coghland6009512014-11-20 21:39:37 +10001989 if (!(iomod = PyImport_ImportModule("io"))) {
1990 goto error;
1991 }
Nick Coghland6009512014-11-20 21:39:37 +10001992
Nick Coghland6009512014-11-20 21:39:37 +10001993 /* Set sys.stdin */
1994 fd = fileno(stdin);
1995 /* Under some conditions stdin, stdout and stderr may not be connected
1996 * and fileno() may point to an invalid file descriptor. For example
1997 * GUI apps don't have valid standard streams by default.
1998 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02001999 std = create_stdio(config, iomod, fd, 0, "<stdin>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002000 config->stdio_encoding,
2001 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002002 if (std == NULL)
2003 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002004 PySys_SetObject("__stdin__", std);
2005 _PySys_SetObjectId(&PyId_stdin, std);
2006 Py_DECREF(std);
2007
2008 /* Set sys.stdout */
2009 fd = fileno(stdout);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002010 std = create_stdio(config, iomod, fd, 1, "<stdout>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002011 config->stdio_encoding,
2012 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02002013 if (std == NULL)
2014 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002015 PySys_SetObject("__stdout__", std);
2016 _PySys_SetObjectId(&PyId_stdout, std);
2017 Py_DECREF(std);
2018
2019#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2020 /* Set sys.stderr, replaces the preliminary stderr */
2021 fd = fileno(stderr);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002022 std = create_stdio(config, iomod, fd, 1, "<stderr>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02002023 config->stdio_encoding,
Victor Stinner709d23d2019-05-02 14:56:30 -04002024 L"backslashreplace");
Victor Stinner874dbe82015-09-04 17:29:57 +02002025 if (std == NULL)
2026 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002027
2028 /* Same as hack above, pre-import stderr's codec to avoid recursion
2029 when import.c tries to write to stderr in verbose mode. */
2030 encoding_attr = PyObject_GetAttrString(std, "encoding");
2031 if (encoding_attr != NULL) {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002032 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10002033 if (std_encoding != NULL) {
2034 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2035 Py_XDECREF(codec_info);
2036 }
2037 Py_DECREF(encoding_attr);
2038 }
Victor Stinnerb45d2592019-06-20 00:05:23 +02002039 _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
Nick Coghland6009512014-11-20 21:39:37 +10002040
2041 if (PySys_SetObject("__stderr__", std) < 0) {
2042 Py_DECREF(std);
2043 goto error;
2044 }
2045 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2046 Py_DECREF(std);
2047 goto error;
2048 }
2049 Py_DECREF(std);
2050#endif
2051
Victor Stinnera7368ac2017-11-15 18:11:45 -08002052 goto done;
Nick Coghland6009512014-11-20 21:39:37 +10002053
Victor Stinnera7368ac2017-11-15 18:11:45 -08002054error:
Victor Stinner331a6a52019-05-27 16:39:22 +02002055 res = _PyStatus_ERR("can't initialize sys standard streams");
Victor Stinnera7368ac2017-11-15 18:11:45 -08002056
2057done:
Victor Stinner124b9eb2018-08-29 01:29:06 +02002058 _Py_ClearStandardStreamEncoding();
Nick Coghland6009512014-11-20 21:39:37 +10002059 Py_XDECREF(iomod);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002060 return res;
Nick Coghland6009512014-11-20 21:39:37 +10002061}
2062
2063
Victor Stinner10dc4842015-03-24 12:01:30 +01002064static void
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002065_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2066 PyThreadState *tstate)
Victor Stinner10dc4842015-03-24 12:01:30 +01002067{
Victor Stinner10dc4842015-03-24 12:01:30 +01002068 fputc('\n', stderr);
2069 fflush(stderr);
2070
2071 /* display the current Python stack */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002072 _Py_DumpTracebackThreads(fd, interp, tstate);
Victor Stinner10dc4842015-03-24 12:01:30 +01002073}
Victor Stinner791da1c2016-03-14 16:53:12 +01002074
2075/* Print the current exception (if an exception is set) with its traceback,
2076 or display the current Python stack.
2077
2078 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2079 called on catastrophic cases.
2080
2081 Return 1 if the traceback was displayed, 0 otherwise. */
2082
2083static int
Andy Lester75cd5bf2020-03-12 02:49:05 -05002084_Py_FatalError_PrintExc(PyThreadState *tstate)
Victor Stinner791da1c2016-03-14 16:53:12 +01002085{
2086 PyObject *ferr, *res;
2087 PyObject *exception, *v, *tb;
2088 int has_tb;
2089
Victor Stinnerb45d2592019-06-20 00:05:23 +02002090 _PyErr_Fetch(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002091 if (exception == NULL) {
2092 /* No current exception */
2093 return 0;
2094 }
2095
2096 ferr = _PySys_GetObjectId(&PyId_stderr);
2097 if (ferr == NULL || ferr == Py_None) {
2098 /* sys.stderr is not set yet or set to None,
2099 no need to try to display the exception */
2100 return 0;
2101 }
2102
Victor Stinnerb45d2592019-06-20 00:05:23 +02002103 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002104 if (tb == NULL) {
2105 tb = Py_None;
2106 Py_INCREF(tb);
2107 }
2108 PyException_SetTraceback(v, tb);
2109 if (exception == NULL) {
2110 /* PyErr_NormalizeException() failed */
2111 return 0;
2112 }
2113
2114 has_tb = (tb != Py_None);
2115 PyErr_Display(exception, v, tb);
2116 Py_XDECREF(exception);
2117 Py_XDECREF(v);
2118 Py_XDECREF(tb);
2119
2120 /* sys.stderr may be buffered: call sys.stderr.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002121 res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002122 if (res == NULL) {
2123 _PyErr_Clear(tstate);
2124 }
2125 else {
Victor Stinner791da1c2016-03-14 16:53:12 +01002126 Py_DECREF(res);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002127 }
Victor Stinner791da1c2016-03-14 16:53:12 +01002128
2129 return has_tb;
2130}
2131
Nick Coghland6009512014-11-20 21:39:37 +10002132/* Print fatal error message and abort */
2133
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002134#ifdef MS_WINDOWS
2135static void
2136fatal_output_debug(const char *msg)
2137{
2138 /* buffer of 256 bytes allocated on the stack */
2139 WCHAR buffer[256 / sizeof(WCHAR)];
2140 size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2141 size_t msglen;
2142
2143 OutputDebugStringW(L"Fatal Python error: ");
2144
2145 msglen = strlen(msg);
2146 while (msglen) {
2147 size_t i;
2148
2149 if (buflen > msglen) {
2150 buflen = msglen;
2151 }
2152
2153 /* Convert the message to wchar_t. This uses a simple one-to-one
2154 conversion, assuming that the this error message actually uses
2155 ASCII only. If this ceases to be true, we will have to convert. */
2156 for (i=0; i < buflen; ++i) {
2157 buffer[i] = msg[i];
2158 }
2159 buffer[i] = L'\0';
2160 OutputDebugStringW(buffer);
2161
2162 msg += buflen;
2163 msglen -= buflen;
2164 }
2165 OutputDebugStringW(L"\n");
2166}
2167#endif
2168
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002169
2170static void
2171fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
2172{
2173 fprintf(stream, "Python runtime state: ");
Victor Stinner7b3c2522020-03-07 00:24:23 +01002174 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2175 if (finalizing) {
2176 fprintf(stream, "finalizing (tstate=%p)", finalizing);
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002177 }
2178 else if (runtime->initialized) {
2179 fprintf(stream, "initialized");
2180 }
2181 else if (runtime->core_initialized) {
2182 fprintf(stream, "core initialized");
2183 }
2184 else if (runtime->preinitialized) {
2185 fprintf(stream, "preinitialized");
2186 }
2187 else if (runtime->preinitializing) {
2188 fprintf(stream, "preinitializing");
2189 }
2190 else {
2191 fprintf(stream, "unknown");
2192 }
2193 fprintf(stream, "\n");
2194 fflush(stream);
2195}
2196
2197
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002198static inline void _Py_NO_RETURN
2199fatal_error_exit(int status)
Nick Coghland6009512014-11-20 21:39:37 +10002200{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002201 if (status < 0) {
2202#if defined(MS_WINDOWS) && defined(_DEBUG)
2203 DebugBreak();
2204#endif
2205 abort();
2206 }
2207 else {
2208 exit(status);
2209 }
2210}
2211
2212
2213static void _Py_NO_RETURN
2214fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2215 int status)
2216{
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002217 const int fd = fileno(stream);
Victor Stinner53345a42015-03-25 01:55:14 +01002218 static int reentrant = 0;
Victor Stinner53345a42015-03-25 01:55:14 +01002219
2220 if (reentrant) {
2221 /* Py_FatalError() caused a second fatal error.
2222 Example: flush_std_files() raises a recursion error. */
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002223 fatal_error_exit(status);
Victor Stinner53345a42015-03-25 01:55:14 +01002224 }
2225 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10002226
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002227 if (header) {
2228 fprintf(stream, "Fatal Python error: ");
2229 if (prefix) {
2230 fputs(prefix, stream);
2231 fputs(": ", stream);
2232 }
2233 if (msg) {
2234 fputs(msg, stream);
2235 }
2236 else {
2237 fprintf(stream, "<message not set>");
2238 }
2239 fputs("\n", stream);
2240 fflush(stream);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002241 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002242
2243 _PyRuntimeState *runtime = &_PyRuntime;
2244 fatal_error_dump_runtime(stream, runtime);
2245
2246 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2247 PyInterpreterState *interp = NULL;
2248 if (tstate != NULL) {
2249 interp = tstate->interp;
2250 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002251
Victor Stinner3a228ab2018-11-01 00:26:41 +01002252 /* Check if the current thread has a Python thread state
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002253 and holds the GIL.
Victor Stinner3a228ab2018-11-01 00:26:41 +01002254
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002255 tss_tstate is NULL if Py_FatalError() is called from a C thread which
2256 has no Python thread state.
2257
2258 tss_tstate != tstate if the current Python thread does not hold the GIL.
2259 */
2260 PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
2261 int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002262 if (has_tstate_and_gil) {
2263 /* If an exception is set, print the exception with its traceback */
Andy Lester75cd5bf2020-03-12 02:49:05 -05002264 if (!_Py_FatalError_PrintExc(tss_tstate)) {
Victor Stinner3a228ab2018-11-01 00:26:41 +01002265 /* No exception is set, or an exception is set without traceback */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002266 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002267 }
2268 }
2269 else {
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002270 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002271 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002272
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002273 /* The main purpose of faulthandler is to display the traceback.
2274 This function already did its best to display a traceback.
2275 Disable faulthandler to prevent writing a second traceback
2276 on abort(). */
Victor Stinner2025d782016-03-16 23:19:15 +01002277 _PyFaulthandler_Fini();
2278
Victor Stinner791da1c2016-03-14 16:53:12 +01002279 /* Check if the current Python thread hold the GIL */
Victor Stinner3a228ab2018-11-01 00:26:41 +01002280 if (has_tstate_and_gil) {
Victor Stinner791da1c2016-03-14 16:53:12 +01002281 /* Flush sys.stdout and sys.stderr */
2282 flush_std_files();
2283 }
Victor Stinnere0deff32015-03-24 13:46:18 +01002284
Nick Coghland6009512014-11-20 21:39:37 +10002285#ifdef MS_WINDOWS
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002286 fatal_output_debug(msg);
Victor Stinner53345a42015-03-25 01:55:14 +01002287#endif /* MS_WINDOWS */
2288
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002289 fatal_error_exit(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002290}
2291
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002292
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002293#undef Py_FatalError
2294
Victor Stinner19760862017-12-20 01:41:59 +01002295void _Py_NO_RETURN
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002296Py_FatalError(const char *msg)
2297{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002298 fatal_error(stderr, 1, NULL, msg, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002299}
2300
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002301
Victor Stinner19760862017-12-20 01:41:59 +01002302void _Py_NO_RETURN
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002303_Py_FatalErrorFunc(const char *func, const char *msg)
2304{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002305 fatal_error(stderr, 1, func, msg, -1);
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002306}
2307
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002308
2309void _Py_NO_RETURN
2310_Py_FatalErrorFormat(const char *func, const char *format, ...)
2311{
2312 static int reentrant = 0;
2313 if (reentrant) {
2314 /* _Py_FatalErrorFormat() caused a second fatal error */
2315 fatal_error_exit(-1);
2316 }
2317 reentrant = 1;
2318
2319 FILE *stream = stderr;
2320 fprintf(stream, "Fatal Python error: ");
2321 if (func) {
2322 fputs(func, stream);
2323 fputs(": ", stream);
2324 }
2325 fflush(stream);
2326
2327 va_list vargs;
2328#ifdef HAVE_STDARG_PROTOTYPES
2329 va_start(vargs, format);
2330#else
2331 va_start(vargs);
2332#endif
2333 vfprintf(stream, format, vargs);
2334 va_end(vargs);
2335
2336 fputs("\n", stream);
2337 fflush(stream);
2338
2339 fatal_error(stream, 0, NULL, NULL, -1);
2340}
2341
2342
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002343void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +02002344Py_ExitStatusException(PyStatus status)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002345{
Victor Stinner331a6a52019-05-27 16:39:22 +02002346 if (_PyStatus_IS_EXIT(status)) {
2347 exit(status.exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +02002348 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002349 else if (_PyStatus_IS_ERROR(status)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002350 fatal_error(stderr, 1, status.func, status.err_msg, 1);
Victor Stinnerdfe88472019-03-01 12:14:41 +01002351 }
2352 else {
Victor Stinner331a6a52019-05-27 16:39:22 +02002353 Py_FatalError("Py_ExitStatusException() must not be called on success");
Victor Stinnerdfe88472019-03-01 12:14:41 +01002354 }
Nick Coghland6009512014-11-20 21:39:37 +10002355}
2356
2357/* Clean up and exit */
2358
Nick Coghland6009512014-11-20 21:39:37 +10002359/* For the atexit module. */
Marcel Plch776407f2017-12-20 11:17:58 +01002360void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
Nick Coghland6009512014-11-20 21:39:37 +10002361{
Victor Stinner81a7be32020-04-14 15:14:01 +02002362 PyInterpreterState *is = _PyInterpreterState_GET();
Marcel Plch776407f2017-12-20 11:17:58 +01002363
Antoine Pitroufc5db952017-12-13 02:29:07 +01002364 /* Guard against API misuse (see bpo-17852) */
Marcel Plch776407f2017-12-20 11:17:58 +01002365 assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
2366
2367 is->pyexitfunc = func;
2368 is->pyexitmodule = module;
Nick Coghland6009512014-11-20 21:39:37 +10002369}
2370
2371static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002372call_py_exitfuncs(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002373{
Victor Stinnerb45d2592019-06-20 00:05:23 +02002374 PyInterpreterState *interp = tstate->interp;
2375 if (interp->pyexitfunc == NULL)
Nick Coghland6009512014-11-20 21:39:37 +10002376 return;
2377
Victor Stinnerb45d2592019-06-20 00:05:23 +02002378 (*interp->pyexitfunc)(interp->pyexitmodule);
2379 _PyErr_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10002380}
2381
2382/* Wait until threading._shutdown completes, provided
2383 the threading module was imported in the first place.
2384 The shutdown routine will wait until all non-daemon
2385 "threading" threads have completed. */
2386static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002387wait_for_thread_shutdown(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002388{
Nick Coghland6009512014-11-20 21:39:37 +10002389 _Py_IDENTIFIER(_shutdown);
2390 PyObject *result;
Eric Snow3f9eee62017-09-15 16:35:20 -06002391 PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
Nick Coghland6009512014-11-20 21:39:37 +10002392 if (threading == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +02002393 if (_PyErr_Occurred(tstate)) {
Stefan Krah027b09c2019-03-25 21:50:58 +01002394 PyErr_WriteUnraisable(NULL);
2395 }
2396 /* else: threading not imported */
Nick Coghland6009512014-11-20 21:39:37 +10002397 return;
2398 }
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002399 result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
Nick Coghland6009512014-11-20 21:39:37 +10002400 if (result == NULL) {
2401 PyErr_WriteUnraisable(threading);
2402 }
2403 else {
2404 Py_DECREF(result);
2405 }
2406 Py_DECREF(threading);
Nick Coghland6009512014-11-20 21:39:37 +10002407}
2408
2409#define NEXITFUNCS 32
Nick Coghland6009512014-11-20 21:39:37 +10002410int Py_AtExit(void (*func)(void))
2411{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002412 if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
Nick Coghland6009512014-11-20 21:39:37 +10002413 return -1;
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002414 _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
Nick Coghland6009512014-11-20 21:39:37 +10002415 return 0;
2416}
2417
2418static void
Victor Stinner8e91c242019-04-24 17:24:01 +02002419call_ll_exitfuncs(_PyRuntimeState *runtime)
Nick Coghland6009512014-11-20 21:39:37 +10002420{
Victor Stinner8e91c242019-04-24 17:24:01 +02002421 while (runtime->nexitfuncs > 0) {
Victor Stinner87d23a02019-04-26 05:49:26 +02002422 /* pop last function from the list */
2423 runtime->nexitfuncs--;
2424 void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2425 runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2426
2427 exitfunc();
Victor Stinner8e91c242019-04-24 17:24:01 +02002428 }
Nick Coghland6009512014-11-20 21:39:37 +10002429
2430 fflush(stdout);
2431 fflush(stderr);
2432}
2433
Victor Stinnercfc88312018-08-01 16:41:25 +02002434void _Py_NO_RETURN
Nick Coghland6009512014-11-20 21:39:37 +10002435Py_Exit(int sts)
2436{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002437 if (Py_FinalizeEx() < 0) {
2438 sts = 120;
2439 }
Nick Coghland6009512014-11-20 21:39:37 +10002440
2441 exit(sts);
2442}
2443
Victor Stinner331a6a52019-05-27 16:39:22 +02002444static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002445init_signals(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002446{
2447#ifdef SIGPIPE
2448 PyOS_setsig(SIGPIPE, SIG_IGN);
2449#endif
2450#ifdef SIGXFZ
2451 PyOS_setsig(SIGXFZ, SIG_IGN);
2452#endif
2453#ifdef SIGXFSZ
2454 PyOS_setsig(SIGXFSZ, SIG_IGN);
2455#endif
Victor Stinner400e1db2020-03-31 19:13:10 +02002456 PyOS_InitInterrupts(); /* May imply init_signals() */
Victor Stinnerb45d2592019-06-20 00:05:23 +02002457 if (_PyErr_Occurred(tstate)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002458 return _PyStatus_ERR("can't import signal");
Nick Coghland6009512014-11-20 21:39:37 +10002459 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002460 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002461}
2462
2463
2464/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2465 *
2466 * All of the code in this function must only use async-signal-safe functions,
2467 * listed at `man 7 signal` or
2468 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
Victor Stinnerefc28bb2020-03-05 18:13:56 +01002469 *
2470 * If this function is updated, update also _posix_spawn() of subprocess.py.
Nick Coghland6009512014-11-20 21:39:37 +10002471 */
2472void
2473_Py_RestoreSignals(void)
2474{
2475#ifdef SIGPIPE
2476 PyOS_setsig(SIGPIPE, SIG_DFL);
2477#endif
2478#ifdef SIGXFZ
2479 PyOS_setsig(SIGXFZ, SIG_DFL);
2480#endif
2481#ifdef SIGXFSZ
2482 PyOS_setsig(SIGXFSZ, SIG_DFL);
2483#endif
2484}
2485
2486
2487/*
2488 * The file descriptor fd is considered ``interactive'' if either
2489 * a) isatty(fd) is TRUE, or
2490 * b) the -i flag was given, and the filename associated with
2491 * the descriptor is NULL or "<stdin>" or "???".
2492 */
2493int
2494Py_FdIsInteractive(FILE *fp, const char *filename)
2495{
2496 if (isatty((int)fileno(fp)))
2497 return 1;
2498 if (!Py_InteractiveFlag)
2499 return 0;
2500 return (filename == NULL) ||
2501 (strcmp(filename, "<stdin>") == 0) ||
2502 (strcmp(filename, "???") == 0);
2503}
2504
2505
Nick Coghland6009512014-11-20 21:39:37 +10002506/* Wrappers around sigaction() or signal(). */
2507
2508PyOS_sighandler_t
2509PyOS_getsig(int sig)
2510{
2511#ifdef HAVE_SIGACTION
2512 struct sigaction context;
2513 if (sigaction(sig, NULL, &context) == -1)
2514 return SIG_ERR;
2515 return context.sa_handler;
2516#else
2517 PyOS_sighandler_t handler;
2518/* Special signal handling for the secure CRT in Visual Studio 2005 */
2519#if defined(_MSC_VER) && _MSC_VER >= 1400
2520 switch (sig) {
2521 /* Only these signals are valid */
2522 case SIGINT:
2523 case SIGILL:
2524 case SIGFPE:
2525 case SIGSEGV:
2526 case SIGTERM:
2527 case SIGBREAK:
2528 case SIGABRT:
2529 break;
2530 /* Don't call signal() with other values or it will assert */
2531 default:
2532 return SIG_ERR;
2533 }
2534#endif /* _MSC_VER && _MSC_VER >= 1400 */
2535 handler = signal(sig, SIG_IGN);
2536 if (handler != SIG_ERR)
2537 signal(sig, handler);
2538 return handler;
2539#endif
2540}
2541
2542/*
2543 * All of the code in this function must only use async-signal-safe functions,
2544 * listed at `man 7 signal` or
2545 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2546 */
2547PyOS_sighandler_t
2548PyOS_setsig(int sig, PyOS_sighandler_t handler)
2549{
2550#ifdef HAVE_SIGACTION
2551 /* Some code in Modules/signalmodule.c depends on sigaction() being
2552 * used here if HAVE_SIGACTION is defined. Fix that if this code
2553 * changes to invalidate that assumption.
2554 */
2555 struct sigaction context, ocontext;
2556 context.sa_handler = handler;
2557 sigemptyset(&context.sa_mask);
2558 context.sa_flags = 0;
2559 if (sigaction(sig, &context, &ocontext) == -1)
2560 return SIG_ERR;
2561 return ocontext.sa_handler;
2562#else
2563 PyOS_sighandler_t oldhandler;
2564 oldhandler = signal(sig, handler);
2565#ifdef HAVE_SIGINTERRUPT
2566 siginterrupt(sig, 1);
2567#endif
2568 return oldhandler;
2569#endif
2570}
2571
2572#ifdef __cplusplus
2573}
2574#endif