blob: 2d219a4a3a8b0db5a9c4fd8497565491aa413dd2 [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
Victor Stinner4f98f462020-04-15 04:01:58 +020021#include <locale.h> // setlocale()
Nick Coghland6009512014-11-20 21:39:37 +100022
23#ifdef HAVE_SIGNAL_H
Victor Stinner4f98f462020-04-15 04:01:58 +020024# include <signal.h> // SIG_IGN
Nick Coghland6009512014-11-20 21:39:37 +100025#endif
26
27#ifdef HAVE_LANGINFO_H
Victor Stinner4f98f462020-04-15 04:01:58 +020028# include <langinfo.h> // nl_langinfo(CODESET)
Nick Coghland6009512014-11-20 21:39:37 +100029#endif
30
31#ifdef MS_WINDOWS
Victor Stinner4f98f462020-04-15 04:01:58 +020032# undef BYTE
33# include "windows.h"
Steve Dower39294992016-08-30 21:22:36 -070034
Victor Stinner4f98f462020-04-15 04:01:58 +020035 extern PyTypeObject PyWindowsConsoleIO_Type;
36# define PyWindowsConsoleIO_Check(op) \
37 (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
Nick Coghland6009512014-11-20 21:39:37 +100038#endif
39
Victor Stinner4f98f462020-04-15 04:01:58 +020040
Nick Coghland6009512014-11-20 21:39:37 +100041_Py_IDENTIFIER(flush);
42_Py_IDENTIFIER(name);
43_Py_IDENTIFIER(stdin);
44_Py_IDENTIFIER(stdout);
45_Py_IDENTIFIER(stderr);
Eric Snow3f9eee62017-09-15 16:35:20 -060046_Py_IDENTIFIER(threading);
Nick Coghland6009512014-11-20 21:39:37 +100047
48#ifdef __cplusplus
49extern "C" {
50#endif
51
Nick Coghland6009512014-11-20 21:39:37 +100052
Victor Stinnerb45d2592019-06-20 00:05:23 +020053/* Forward declarations */
Victor Stinner331a6a52019-05-27 16:39:22 +020054static PyStatus add_main_module(PyInterpreterState *interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +020055static PyStatus init_import_site(void);
Andy Lester75cd5bf2020-03-12 02:49:05 -050056static PyStatus init_set_builtins_open(void);
Victor Stinnerb45d2592019-06-20 00:05:23 +020057static PyStatus init_sys_streams(PyThreadState *tstate);
58static PyStatus init_signals(PyThreadState *tstate);
59static void call_py_exitfuncs(PyThreadState *tstate);
60static void wait_for_thread_shutdown(PyThreadState *tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +020061static void call_ll_exitfuncs(_PyRuntimeState *runtime);
Nick Coghland6009512014-11-20 21:39:37 +100062
Gregory P. Smith38f11cc2019-02-16 12:57:40 -080063int _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080064_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010065static int runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066
Victor Stinner331a6a52019-05-27 16:39:22 +020067PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068_PyRuntime_Initialize(void)
69{
70 /* XXX We only initialize once in the process, which aligns with
71 the static initialization of the former globals now found in
72 _PyRuntime. However, _PyRuntime *should* be initialized with
73 every Py_Initialize() call, but doing so breaks the runtime.
74 This is because the runtime state is not properly finalized
75 currently. */
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010076 if (runtime_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +020077 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080078 }
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010079 runtime_initialized = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080080
81 return _PyRuntimeState_Init(&_PyRuntime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060082}
83
84void
85_PyRuntime_Finalize(void)
86{
87 _PyRuntimeState_Fini(&_PyRuntime);
Victor Stinnerfd23cfa2019-03-20 00:03:01 +010088 runtime_initialized = 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060089}
90
91int
92_Py_IsFinalizing(void)
93{
Victor Stinner7b3c2522020-03-07 00:24:23 +010094 return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060095}
96
Nick Coghland6009512014-11-20 21:39:37 +100097/* Hack to force loading of object files */
98int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
99 PyOS_mystrnicmp; /* Python/pystrcmp.o */
100
101/* PyModule_GetWarningsModule is no longer necessary as of 2.6
102since _warnings is builtin. This API should not be used. */
103PyObject *
104PyModule_GetWarningsModule(void)
105{
106 return PyImport_ImportModule("warnings");
107}
108
Eric Snowc7ec9982017-05-23 23:00:52 -0700109
Eric Snow1abcf672017-05-23 21:46:51 -0700110/* APIs to access the initialization flags
111 *
112 * Can be called prior to Py_Initialize.
113 */
Nick Coghland6009512014-11-20 21:39:37 +1000114
Eric Snow1abcf672017-05-23 21:46:51 -0700115int
116_Py_IsCoreInitialized(void)
117{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600118 return _PyRuntime.core_initialized;
Eric Snow1abcf672017-05-23 21:46:51 -0700119}
Nick Coghland6009512014-11-20 21:39:37 +1000120
121int
122Py_IsInitialized(void)
123{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600124 return _PyRuntime.initialized;
Nick Coghland6009512014-11-20 21:39:37 +1000125}
126
Nick Coghlan6ea41862017-06-11 13:16:15 +1000127
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000128/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
129 call this twice without an intervening Py_FinalizeEx() call. When
Nick Coghland6009512014-11-20 21:39:37 +1000130 initializations fail, a fatal error is issued and the function does
131 not return. On return, the first thread and interpreter state have
132 been created.
133
134 Locking: you must hold the interpreter lock while calling this.
135 (If the lock has not yet been initialized, that's equivalent to
136 having the lock, but you cannot use multiple threads.)
137
138*/
139
Victor Stinner331a6a52019-05-27 16:39:22 +0200140static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200141init_importlib(PyThreadState *tstate, PyObject *sysmod)
Nick Coghland6009512014-11-20 21:39:37 +1000142{
143 PyObject *importlib;
144 PyObject *impmod;
Nick Coghland6009512014-11-20 21:39:37 +1000145 PyObject *value;
Victor Stinnerb45d2592019-06-20 00:05:23 +0200146 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200147 int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
Nick Coghland6009512014-11-20 21:39:37 +1000148
149 /* Import _importlib through its frozen version, _frozen_importlib. */
150 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200151 return _PyStatus_ERR("can't import _frozen_importlib");
Nick Coghland6009512014-11-20 21:39:37 +1000152 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200153 else if (verbose) {
Nick Coghland6009512014-11-20 21:39:37 +1000154 PySys_FormatStderr("import _frozen_importlib # frozen\n");
155 }
156 importlib = PyImport_AddModule("_frozen_importlib");
157 if (importlib == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200158 return _PyStatus_ERR("couldn't get _frozen_importlib from sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000159 }
160 interp->importlib = importlib;
161 Py_INCREF(interp->importlib);
162
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300163 interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
164 if (interp->import_func == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +0200165 return _PyStatus_ERR("__import__ not found");
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300166 Py_INCREF(interp->import_func);
167
Victor Stinnercd6e6942015-09-18 09:11:57 +0200168 /* Import the _imp module */
Benjamin Petersonc65ef772018-01-29 11:33:57 -0800169 impmod = PyInit__imp();
Nick Coghland6009512014-11-20 21:39:37 +1000170 if (impmod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200171 return _PyStatus_ERR("can't import _imp");
Nick Coghland6009512014-11-20 21:39:37 +1000172 }
Victor Stinnerc96be812019-05-14 17:34:56 +0200173 else if (verbose) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200174 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000175 }
Eric Snow3f9eee62017-09-15 16:35:20 -0600176 if (_PyImport_SetModuleString("_imp", impmod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200177 return _PyStatus_ERR("can't save _imp to sys.modules");
Nick Coghland6009512014-11-20 21:39:37 +1000178 }
179
Victor Stinnercd6e6942015-09-18 09:11:57 +0200180 /* Install importlib as the implementation of import */
Nick Coghland6009512014-11-20 21:39:37 +1000181 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
182 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200183 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200184 return _PyStatus_ERR("importlib install failed");
Nick Coghland6009512014-11-20 21:39:37 +1000185 }
186 Py_DECREF(value);
187 Py_DECREF(impmod);
188
Victor Stinner331a6a52019-05-27 16:39:22 +0200189 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +1000190}
191
Victor Stinner331a6a52019-05-27 16:39:22 +0200192static PyStatus
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200193init_importlib_external(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -0700194{
195 PyObject *value;
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200196 value = PyObject_CallMethod(tstate->interp->importlib,
Eric Snow1abcf672017-05-23 21:46:51 -0700197 "_install_external_importers", "");
198 if (value == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200199 _PyErr_Print(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200200 return _PyStatus_ERR("external importer setup failed");
Eric Snow1abcf672017-05-23 21:46:51 -0700201 }
Stéphane Wirtelab1cb802017-06-08 13:13:20 +0200202 Py_DECREF(value);
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200203 return _PyImportZip_Init(tstate);
Eric Snow1abcf672017-05-23 21:46:51 -0700204}
Nick Coghland6009512014-11-20 21:39:37 +1000205
Nick Coghlan6ea41862017-06-11 13:16:15 +1000206/* Helper functions to better handle the legacy C locale
207 *
208 * The legacy C locale assumes ASCII as the default text encoding, which
209 * causes problems not only for the CPython runtime, but also other
210 * components like GNU readline.
211 *
212 * Accordingly, when the CLI detects it, it attempts to coerce it to a
213 * more capable UTF-8 based alternative as follows:
214 *
215 * if (_Py_LegacyLocaleDetected()) {
216 * _Py_CoerceLegacyLocale();
217 * }
218 *
219 * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
220 *
221 * Locale coercion also impacts the default error handler for the standard
222 * streams: while the usual default is "strict", the default for the legacy
223 * C locale and for any of the coercion target locales is "surrogateescape".
224 */
225
226int
Victor Stinner0f721472019-05-20 17:16:38 +0200227_Py_LegacyLocaleDetected(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000228{
229#ifndef MS_WINDOWS
Victor Stinner0f721472019-05-20 17:16:38 +0200230 if (!warn) {
231 const char *locale_override = getenv("LC_ALL");
232 if (locale_override != NULL && *locale_override != '\0') {
233 /* Don't coerce C locale if the LC_ALL environment variable
234 is set */
235 return 0;
236 }
237 }
238
Nick Coghlan6ea41862017-06-11 13:16:15 +1000239 /* On non-Windows systems, the C locale is considered a legacy locale */
Nick Coghlaneb817952017-06-18 12:29:42 +1000240 /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
241 * the POSIX locale as a simple alias for the C locale, so
242 * we may also want to check for that explicitly.
243 */
Nick Coghlan6ea41862017-06-11 13:16:15 +1000244 const char *ctype_loc = setlocale(LC_CTYPE, NULL);
245 return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
246#else
247 /* Windows uses code pages instead of locales, so no locale is legacy */
248 return 0;
249#endif
250}
251
Victor Stinnerb0051362019-11-22 17:52:42 +0100252#ifndef MS_WINDOWS
Nick Coghlaneb817952017-06-18 12:29:42 +1000253static const char *_C_LOCALE_WARNING =
254 "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
255 "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
256 "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
257 "locales is recommended.\n";
258
Nick Coghlaneb817952017-06-18 12:29:42 +1000259static void
Victor Stinner43125222019-04-24 18:23:53 +0200260emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
Nick Coghlaneb817952017-06-18 12:29:42 +1000261{
Victor Stinner331a6a52019-05-27 16:39:22 +0200262 const PyPreConfig *preconfig = &runtime->preconfig;
Victor Stinner0f721472019-05-20 17:16:38 +0200263 if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
Victor Stinnercf215042018-08-29 22:56:06 +0200264 PySys_FormatStderr("%s", _C_LOCALE_WARNING);
Nick Coghlaneb817952017-06-18 12:29:42 +1000265 }
266}
Victor Stinnerb0051362019-11-22 17:52:42 +0100267#endif /* !defined(MS_WINDOWS) */
Nick Coghlaneb817952017-06-18 12:29:42 +1000268
Nick Coghlan6ea41862017-06-11 13:16:15 +1000269typedef struct _CandidateLocale {
270 const char *locale_name; /* The locale to try as a coercion target */
271} _LocaleCoercionTarget;
272
273static _LocaleCoercionTarget _TARGET_LOCALES[] = {
274 {"C.UTF-8"},
275 {"C.utf8"},
Nick Coghlan18974c32017-06-30 00:48:14 +1000276 {"UTF-8"},
Nick Coghlan6ea41862017-06-11 13:16:15 +1000277 {NULL}
278};
279
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200280
281int
282_Py_IsLocaleCoercionTarget(const char *ctype_loc)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000283{
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200284 const _LocaleCoercionTarget *target = NULL;
285 for (target = _TARGET_LOCALES; target->locale_name; target++) {
286 if (strcmp(ctype_loc, target->locale_name) == 0) {
287 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000288 }
Victor Stinner124b9eb2018-08-29 01:29:06 +0200289 }
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200290 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000291}
292
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200293
Nick Coghlan6ea41862017-06-11 13:16:15 +1000294#ifdef PY_COERCE_C_LOCALE
Victor Stinner94540602017-12-16 04:54:22 +0100295static const char C_LOCALE_COERCION_WARNING[] =
Nick Coghlan6ea41862017-06-11 13:16:15 +1000296 "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
297 "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
298
Victor Stinner0f721472019-05-20 17:16:38 +0200299static int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200300_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000301{
302 const char *newloc = target->locale_name;
303
304 /* Reset locale back to currently configured defaults */
xdegaye1588be62017-11-12 12:45:59 +0100305 _Py_SetLocaleFromEnv(LC_ALL);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000306
307 /* Set the relevant locale environment variable */
308 if (setenv("LC_CTYPE", newloc, 1)) {
309 fprintf(stderr,
310 "Error setting LC_CTYPE, skipping C locale coercion\n");
Victor Stinner0f721472019-05-20 17:16:38 +0200311 return 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000312 }
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200313 if (warn) {
Victor Stinner94540602017-12-16 04:54:22 +0100314 fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
Nick Coghlaneb817952017-06-18 12:29:42 +1000315 }
Nick Coghlan6ea41862017-06-11 13:16:15 +1000316
317 /* Reconfigure with the overridden environment variables */
xdegaye1588be62017-11-12 12:45:59 +0100318 _Py_SetLocaleFromEnv(LC_ALL);
Victor Stinner0f721472019-05-20 17:16:38 +0200319 return 1;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000320}
321#endif
322
Victor Stinner0f721472019-05-20 17:16:38 +0200323int
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200324_Py_CoerceLegacyLocale(int warn)
Nick Coghlan6ea41862017-06-11 13:16:15 +1000325{
Victor Stinner0f721472019-05-20 17:16:38 +0200326 int coerced = 0;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000327#ifdef PY_COERCE_C_LOCALE
Victor Stinner8ea09112018-09-03 17:05:18 +0200328 char *oldloc = NULL;
329
330 oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
331 if (oldloc == NULL) {
Victor Stinner0f721472019-05-20 17:16:38 +0200332 return coerced;
Victor Stinner8ea09112018-09-03 17:05:18 +0200333 }
334
Victor Stinner94540602017-12-16 04:54:22 +0100335 const char *locale_override = getenv("LC_ALL");
336 if (locale_override == NULL || *locale_override == '\0') {
337 /* LC_ALL is also not set (or is set to an empty string) */
338 const _LocaleCoercionTarget *target = NULL;
339 for (target = _TARGET_LOCALES; target->locale_name; target++) {
340 const char *new_locale = setlocale(LC_CTYPE,
341 target->locale_name);
342 if (new_locale != NULL) {
Victor Stinnere2510952019-05-02 11:28:57 -0400343#if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94540602017-12-16 04:54:22 +0100344 /* Also ensure that nl_langinfo works in this locale */
345 char *codeset = nl_langinfo(CODESET);
346 if (!codeset || *codeset == '\0') {
347 /* CODESET is not set or empty, so skip coercion */
348 new_locale = NULL;
349 _Py_SetLocaleFromEnv(LC_CTYPE);
350 continue;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000351 }
Victor Stinner94540602017-12-16 04:54:22 +0100352#endif
353 /* Successfully configured locale, so make it the default */
Victor Stinner0f721472019-05-20 17:16:38 +0200354 coerced = _coerce_default_locale_settings(warn, target);
Victor Stinner8ea09112018-09-03 17:05:18 +0200355 goto done;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000356 }
357 }
358 }
359 /* No C locale warning here, as Py_Initialize will emit one later */
Victor Stinner8ea09112018-09-03 17:05:18 +0200360
361 setlocale(LC_CTYPE, oldloc);
362
363done:
364 PyMem_RawFree(oldloc);
Nick Coghlan6ea41862017-06-11 13:16:15 +1000365#endif
Victor Stinner0f721472019-05-20 17:16:38 +0200366 return coerced;
Nick Coghlan6ea41862017-06-11 13:16:15 +1000367}
368
xdegaye1588be62017-11-12 12:45:59 +0100369/* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
370 * isolate the idiosyncrasies of different libc implementations. It reads the
371 * appropriate environment variable and uses its value to select the locale for
372 * 'category'. */
373char *
374_Py_SetLocaleFromEnv(int category)
375{
Victor Stinner353933e2018-11-23 13:08:26 +0100376 char *res;
xdegaye1588be62017-11-12 12:45:59 +0100377#ifdef __ANDROID__
378 const char *locale;
379 const char **pvar;
380#ifdef PY_COERCE_C_LOCALE
381 const char *coerce_c_locale;
382#endif
383 const char *utf8_locale = "C.UTF-8";
384 const char *env_var_set[] = {
385 "LC_ALL",
386 "LC_CTYPE",
387 "LANG",
388 NULL,
389 };
390
391 /* Android setlocale(category, "") doesn't check the environment variables
392 * and incorrectly sets the "C" locale at API 24 and older APIs. We only
393 * check the environment variables listed in env_var_set. */
394 for (pvar=env_var_set; *pvar; pvar++) {
395 locale = getenv(*pvar);
396 if (locale != NULL && *locale != '\0') {
397 if (strcmp(locale, utf8_locale) == 0 ||
398 strcmp(locale, "en_US.UTF-8") == 0) {
399 return setlocale(category, utf8_locale);
400 }
401 return setlocale(category, "C");
402 }
403 }
404
405 /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
406 * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
407 * Quote from POSIX section "8.2 Internationalization Variables":
408 * "4. If the LANG environment variable is not set or is set to the empty
409 * string, the implementation-defined default locale shall be used." */
410
411#ifdef PY_COERCE_C_LOCALE
412 coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
413 if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
414 /* Some other ported code may check the environment variables (e.g. in
415 * extension modules), so we make sure that they match the locale
416 * configuration */
417 if (setenv("LC_CTYPE", utf8_locale, 1)) {
418 fprintf(stderr, "Warning: failed setting the LC_CTYPE "
419 "environment variable to %s\n", utf8_locale);
420 }
421 }
422#endif
Victor Stinner353933e2018-11-23 13:08:26 +0100423 res = setlocale(category, utf8_locale);
424#else /* !defined(__ANDROID__) */
425 res = setlocale(category, "");
426#endif
427 _Py_ResetForceASCII();
428 return res;
xdegaye1588be62017-11-12 12:45:59 +0100429}
430
Nick Coghlan6ea41862017-06-11 13:16:15 +1000431
Eric Snow1abcf672017-05-23 21:46:51 -0700432/* Global initializations. Can be undone by Py_Finalize(). Don't
433 call this twice without an intervening Py_Finalize() call.
434
Victor Stinner331a6a52019-05-27 16:39:22 +0200435 Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
Eric Snow1abcf672017-05-23 21:46:51 -0700436 must have a corresponding call to Py_Finalize.
437
438 Locking: you must hold the interpreter lock while calling these APIs.
439 (If the lock has not yet been initialized, that's equivalent to
440 having the lock, but you cannot use multiple threads.)
441
442*/
443
Victor Stinner331a6a52019-05-27 16:39:22 +0200444static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200445pyinit_core_reconfigure(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200446 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200447 const PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200448{
Victor Stinner331a6a52019-05-27 16:39:22 +0200449 PyStatus status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100450 PyThreadState *tstate = _PyThreadState_GET();
451 if (!tstate) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200452 return _PyStatus_ERR("failed to read thread state");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100453 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200454 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100455
456 PyInterpreterState *interp = tstate->interp;
457 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200458 return _PyStatus_ERR("can't make main interpreter");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100459 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100460
Victor Stinnere81f6e62020-06-08 18:12:59 +0200461 status = _PyConfig_Write(config, runtime);
462 if (_PyStatus_EXCEPTION(status)) {
463 return status;
464 }
Victor Stinner1dc6e392018-07-25 02:49:17 +0200465
Victor Stinnerda7933e2020-04-13 03:04:28 +0200466 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200467 if (_PyStatus_EXCEPTION(status)) {
468 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200469 }
Victor Stinnerda7933e2020-04-13 03:04:28 +0200470 config = _PyInterpreterState_GetConfig(interp);
Victor Stinner1dc6e392018-07-25 02:49:17 +0200471
Victor Stinner331a6a52019-05-27 16:39:22 +0200472 if (config->_install_importlib) {
Victor Stinner12f2f172019-09-26 15:51:50 +0200473 status = _PyConfig_WritePathConfig(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200474 if (_PyStatus_EXCEPTION(status)) {
475 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200476 }
477 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200478 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200479}
480
481
Victor Stinner331a6a52019-05-27 16:39:22 +0200482static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200483pycore_init_runtime(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200484 const PyConfig *config)
Nick Coghland6009512014-11-20 21:39:37 +1000485{
Victor Stinner43125222019-04-24 18:23:53 +0200486 if (runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200487 return _PyStatus_ERR("main interpreter already initialized");
Victor Stinner1dc6e392018-07-25 02:49:17 +0200488 }
Victor Stinnerda273412017-12-15 01:46:02 +0100489
Victor Stinnere81f6e62020-06-08 18:12:59 +0200490 PyStatus status = _PyConfig_Write(config, runtime);
491 if (_PyStatus_EXCEPTION(status)) {
492 return status;
493 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600494
Eric Snow1abcf672017-05-23 21:46:51 -0700495 /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
496 * threads behave a little more gracefully at interpreter shutdown.
497 * We clobber it here so the new interpreter can start with a clean
498 * slate.
499 *
500 * However, this may still lead to misbehaviour if there are daemon
501 * threads still hanging around from a previous Py_Initialize/Finalize
502 * pair :(
503 */
Victor Stinner7b3c2522020-03-07 00:24:23 +0100504 _PyRuntimeState_SetFinalizing(runtime, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600505
Victor Stinnere81f6e62020-06-08 18:12:59 +0200506 status = _Py_HashRandomization_Init(config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200507 if (_PyStatus_EXCEPTION(status)) {
508 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800509 }
510
Victor Stinner331a6a52019-05-27 16:39:22 +0200511 status = _PyInterpreterState_Enable(runtime);
512 if (_PyStatus_EXCEPTION(status)) {
513 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800514 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200515 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100516}
Victor Stinnera7368ac2017-11-15 18:11:45 -0800517
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100518
Victor Stinner331a6a52019-05-27 16:39:22 +0200519static PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200520init_interp_create_gil(PyThreadState *tstate)
521{
522 PyStatus status;
523
524 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
525 only called here. */
526 _PyEval_FiniGIL(tstate);
527
528 /* Auto-thread-state API */
529 status = _PyGILState_Init(tstate);
530 if (_PyStatus_EXCEPTION(status)) {
531 return status;
532 }
533
534 /* Create the GIL and take it */
535 status = _PyEval_InitGIL(tstate);
536 if (_PyStatus_EXCEPTION(status)) {
537 return status;
538 }
539
540 return _PyStatus_OK();
541}
542
543
544static PyStatus
Victor Stinner43125222019-04-24 18:23:53 +0200545pycore_create_interpreter(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200546 const PyConfig *config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200547 PyThreadState **tstate_p)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100548{
549 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnerda273412017-12-15 01:46:02 +0100550 if (interp == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200551 return _PyStatus_ERR("can't make main interpreter");
Victor Stinnerda273412017-12-15 01:46:02 +0100552 }
553
Victor Stinnerda7933e2020-04-13 03:04:28 +0200554 PyStatus status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200555 if (_PyStatus_EXCEPTION(status)) {
556 return status;
Victor Stinnerda273412017-12-15 01:46:02 +0100557 }
Nick Coghland6009512014-11-20 21:39:37 +1000558
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200559 PyThreadState *tstate = PyThreadState_New(interp);
Victor Stinnerb45d2592019-06-20 00:05:23 +0200560 if (tstate == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200561 return _PyStatus_ERR("can't make first thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +0200562 }
Nick Coghland6009512014-11-20 21:39:37 +1000563 (void) PyThreadState_Swap(tstate);
564
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200565 status = init_interp_create_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100566 if (_PyStatus_EXCEPTION(status)) {
567 return status;
568 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100569
Victor Stinnerb45d2592019-06-20 00:05:23 +0200570 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +0200571 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100572}
Nick Coghland6009512014-11-20 21:39:37 +1000573
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100574
Victor Stinner331a6a52019-05-27 16:39:22 +0200575static PyStatus
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100576pycore_init_types(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100577{
Victor Stinner444b39b2019-11-20 01:18:11 +0100578 PyStatus status;
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100579 int is_main_interp = _Py_IsMainInterpreter(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100580
Victor Stinner01b1cc12019-11-20 02:27:56 +0100581 status = _PyGC_Init(tstate);
Victor Stinner444b39b2019-11-20 01:18:11 +0100582 if (_PyStatus_EXCEPTION(status)) {
583 return status;
584 }
585
Victor Stinner0430dfa2020-06-24 15:21:54 +0200586 // Create the empty tuple singleton. It must be created before the first
587 // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
588 // for example.
589 status = _PyTuple_Init(tstate);
590 if (_PyStatus_EXCEPTION(status)) {
591 return status;
592 }
593
Victor Stinnere7e699e2019-11-20 12:08:13 +0100594 if (is_main_interp) {
595 status = _PyTypes_Init();
596 if (_PyStatus_EXCEPTION(status)) {
597 return status;
598 }
Victor Stinner630c8df2019-12-17 13:02:18 +0100599 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100600
Victor Stinner630c8df2019-12-17 13:02:18 +0100601 if (!_PyLong_Init(tstate)) {
602 return _PyStatus_ERR("can't init longs");
Victor Stinnerb93f31f2019-11-20 18:39:12 +0100603 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100604
Victor Stinnerf363d0a2020-06-24 00:10:40 +0200605 status = _PyUnicode_Init(tstate);
606 if (_PyStatus_EXCEPTION(status)) {
607 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100608 }
609
Victor Stinner91698d82020-06-25 14:07:40 +0200610 status = _PyBytes_Init(tstate);
611 if (_PyStatus_EXCEPTION(status)) {
612 return status;
613 }
614
Victor Stinner281cce12020-06-23 22:55:46 +0200615 status = _PyExc_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200616 if (_PyStatus_EXCEPTION(status)) {
617 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100618 }
619
Victor Stinnere7e699e2019-11-20 12:08:13 +0100620 if (is_main_interp) {
621 if (!_PyFloat_Init()) {
622 return _PyStatus_ERR("can't init float");
623 }
Nick Coghland6009512014-11-20 21:39:37 +1000624
Victor Stinnere7e699e2019-11-20 12:08:13 +0100625 if (_PyStructSequence_Init() < 0) {
626 return _PyStatus_ERR("can't initialize structseq");
627 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100628 }
Victor Stinneref9d9b62019-05-22 11:28:22 +0200629
Victor Stinner331a6a52019-05-27 16:39:22 +0200630 status = _PyErr_Init();
631 if (_PyStatus_EXCEPTION(status)) {
632 return status;
Victor Stinneref9d9b62019-05-22 11:28:22 +0200633 }
634
Victor Stinnere7e699e2019-11-20 12:08:13 +0100635 if (is_main_interp) {
636 if (!_PyContext_Init()) {
637 return _PyStatus_ERR("can't init context");
638 }
Victor Stinneref5aa9a2019-11-20 00:38:03 +0100639 }
640
Victor Stinner331a6a52019-05-27 16:39:22 +0200641 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100642}
643
644
Victor Stinner331a6a52019-05-27 16:39:22 +0200645static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200646pycore_init_builtins(PyThreadState *tstate)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100647{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100648 assert(!_PyErr_Occurred(tstate));
649
Victor Stinnerb45d2592019-06-20 00:05:23 +0200650 PyObject *bimod = _PyBuiltin_Init(tstate);
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100651 if (bimod == NULL) {
Victor Stinner2582d462019-11-22 19:24:49 +0100652 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100653 }
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100654
Victor Stinner2582d462019-11-22 19:24:49 +0100655 PyInterpreterState *interp = tstate->interp;
656 if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
657 goto error;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100658 }
Victor Stinner2582d462019-11-22 19:24:49 +0100659
660 PyObject *builtins_dict = PyModule_GetDict(bimod);
661 if (builtins_dict == NULL) {
662 goto error;
663 }
664 Py_INCREF(builtins_dict);
665 interp->builtins = builtins_dict;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100666
Victor Stinner331a6a52019-05-27 16:39:22 +0200667 PyStatus status = _PyBuiltins_AddExceptions(bimod);
668 if (_PyStatus_EXCEPTION(status)) {
669 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100670 }
Victor Stinner2582d462019-11-22 19:24:49 +0100671
672 interp->builtins_copy = PyDict_Copy(interp->builtins);
673 if (interp->builtins_copy == NULL) {
674 goto error;
675 }
Pablo Galindob96c6b02019-12-04 11:19:59 +0000676 Py_DECREF(bimod);
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100677
678 assert(!_PyErr_Occurred(tstate));
679
Victor Stinner331a6a52019-05-27 16:39:22 +0200680 return _PyStatus_OK();
Victor Stinner2582d462019-11-22 19:24:49 +0100681
682error:
Pablo Galindob96c6b02019-12-04 11:19:59 +0000683 Py_XDECREF(bimod);
Victor Stinner2582d462019-11-22 19:24:49 +0100684 return _PyStatus_ERR("can't initialize builtins module");
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100685}
686
687
Victor Stinner331a6a52019-05-27 16:39:22 +0200688static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +0200689pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100690{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100691 assert(!_PyErr_Occurred(tstate));
Victor Stinnerb45d2592019-06-20 00:05:23 +0200692
Victor Stinner2582d462019-11-22 19:24:49 +0100693 PyStatus status = _PyImportHooks_Init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200694 if (_PyStatus_EXCEPTION(status)) {
695 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800696 }
Nick Coghland6009512014-11-20 21:39:37 +1000697
Victor Stinner30a89332020-06-23 15:55:45 +0200698 /* Initialize _warnings. */
699 status = _PyWarnings_InitState(tstate);
700 if (_PyStatus_EXCEPTION(status)) {
701 return status;
702 }
Nick Coghland6009512014-11-20 21:39:37 +1000703
Victor Stinner30a89332020-06-23 15:55:45 +0200704 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
705 if (config->_install_importlib) {
706 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner2ec1a1b2019-11-22 21:54:33 +0100707 status = _PyConfig_WritePathConfig(config);
708 if (_PyStatus_EXCEPTION(status)) {
709 return status;
710 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200711 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200712
Victor Stinner30a89332020-06-23 15:55:45 +0200713 /* This call sets up builtin and frozen import support */
Victor Stinnerb45d2592019-06-20 00:05:23 +0200714 status = init_importlib(tstate, sysmod);
Victor Stinner331a6a52019-05-27 16:39:22 +0200715 if (_PyStatus_EXCEPTION(status)) {
716 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800717 }
Eric Snow1abcf672017-05-23 21:46:51 -0700718 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100719
720 assert(!_PyErr_Occurred(tstate));
721
Victor Stinner331a6a52019-05-27 16:39:22 +0200722 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100723}
724
725
Victor Stinner331a6a52019-05-27 16:39:22 +0200726static PyStatus
Victor Stinnerd863ade2019-12-06 03:37:07 +0100727pycore_interp_init(PyThreadState *tstate)
728{
729 PyStatus status;
Victor Stinner080ee5a2019-12-08 21:55:58 +0100730 PyObject *sysmod = NULL;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100731
732 status = pycore_init_types(tstate);
733 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100734 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100735 }
736
Victor Stinnerd863ade2019-12-06 03:37:07 +0100737 status = _PySys_Create(tstate, &sysmod);
738 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100739 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100740 }
741
742 status = pycore_init_builtins(tstate);
743 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner080ee5a2019-12-08 21:55:58 +0100744 goto done;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100745 }
746
Victor Stinner080ee5a2019-12-08 21:55:58 +0100747 status = pycore_init_import_warnings(tstate, sysmod);
748
749done:
750 /* sys.modules['sys'] contains a strong reference to the module */
751 Py_XDECREF(sysmod);
752 return status;
Victor Stinnerd863ade2019-12-06 03:37:07 +0100753}
754
755
756static PyStatus
Victor Stinner331a6a52019-05-27 16:39:22 +0200757pyinit_config(_PyRuntimeState *runtime,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200758 PyThreadState **tstate_p,
Victor Stinner331a6a52019-05-27 16:39:22 +0200759 const PyConfig *config)
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100760{
Victor Stinner331a6a52019-05-27 16:39:22 +0200761 PyStatus status = pycore_init_runtime(runtime, config);
762 if (_PyStatus_EXCEPTION(status)) {
763 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100764 }
765
Victor Stinnerb45d2592019-06-20 00:05:23 +0200766 PyThreadState *tstate;
767 status = pycore_create_interpreter(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200768 if (_PyStatus_EXCEPTION(status)) {
769 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100770 }
Victor Stinnerb45d2592019-06-20 00:05:23 +0200771 *tstate_p = tstate;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100772
Victor Stinnerd863ade2019-12-06 03:37:07 +0100773 status = pycore_interp_init(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +0200774 if (_PyStatus_EXCEPTION(status)) {
775 return status;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100776 }
Eric Snow1abcf672017-05-23 21:46:51 -0700777
778 /* Only when we get here is the runtime core fully initialized */
Victor Stinner43125222019-04-24 18:23:53 +0200779 runtime->core_initialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200780 return _PyStatus_OK();
Eric Snow1abcf672017-05-23 21:46:51 -0700781}
782
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100783
Victor Stinner331a6a52019-05-27 16:39:22 +0200784PyStatus
785_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100786{
Victor Stinner331a6a52019-05-27 16:39:22 +0200787 PyStatus status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100788
Victor Stinner6d1c4672019-05-20 11:02:00 +0200789 if (src_config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200790 return _PyStatus_ERR("preinitialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +0200791 }
792
Victor Stinner331a6a52019-05-27 16:39:22 +0200793 status = _PyRuntime_Initialize();
794 if (_PyStatus_EXCEPTION(status)) {
795 return status;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100796 }
Victor Stinner43125222019-04-24 18:23:53 +0200797 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100798
Victor Stinnerd3b90412019-09-17 23:59:51 +0200799 if (runtime->preinitialized) {
Victor Stinnerf72346c2019-03-25 17:54:58 +0100800 /* If it's already configured: ignored the new configuration */
Victor Stinner331a6a52019-05-27 16:39:22 +0200801 return _PyStatus_OK();
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100802 }
803
Victor Stinnerd3b90412019-09-17 23:59:51 +0200804 /* Note: preinitialized remains 1 on error, it is only set to 0
805 at exit on success. */
806 runtime->preinitializing = 1;
807
Victor Stinner331a6a52019-05-27 16:39:22 +0200808 PyPreConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200809
810 status = _PyPreConfig_InitFromPreConfig(&config, src_config);
811 if (_PyStatus_EXCEPTION(status)) {
812 return status;
813 }
Victor Stinnerf72346c2019-03-25 17:54:58 +0100814
Victor Stinner331a6a52019-05-27 16:39:22 +0200815 status = _PyPreConfig_Read(&config, args);
816 if (_PyStatus_EXCEPTION(status)) {
817 return status;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100818 }
819
Victor Stinner331a6a52019-05-27 16:39:22 +0200820 status = _PyPreConfig_Write(&config);
821 if (_PyStatus_EXCEPTION(status)) {
822 return status;
Victor Stinnerf72346c2019-03-25 17:54:58 +0100823 }
824
Victor Stinnerd3b90412019-09-17 23:59:51 +0200825 runtime->preinitializing = 0;
826 runtime->preinitialized = 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200827 return _PyStatus_OK();
Victor Stinnerf72346c2019-03-25 17:54:58 +0100828}
829
Victor Stinner70005ac2019-05-02 15:25:34 -0400830
Victor Stinner331a6a52019-05-27 16:39:22 +0200831PyStatus
832Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
Victor Stinnerf72346c2019-03-25 17:54:58 +0100833{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100834 _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400835 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinnerf29084d2019-03-20 02:20:13 +0100836}
837
838
Victor Stinner331a6a52019-05-27 16:39:22 +0200839PyStatus
840Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
Victor Stinner20004952019-03-26 02:31:11 +0100841{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100842 _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
Victor Stinner70005ac2019-05-02 15:25:34 -0400843 return _Py_PreInitializeFromPyArgv(src_config, &args);
Victor Stinner20004952019-03-26 02:31:11 +0100844}
845
846
Victor Stinner331a6a52019-05-27 16:39:22 +0200847PyStatus
848Py_PreInitialize(const PyPreConfig *src_config)
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100849{
Victor Stinner70005ac2019-05-02 15:25:34 -0400850 return _Py_PreInitializeFromPyArgv(src_config, NULL);
Victor Stinnera6fbc4e2019-03-25 18:37:10 +0100851}
852
853
Victor Stinner331a6a52019-05-27 16:39:22 +0200854PyStatus
855_Py_PreInitializeFromConfig(const PyConfig *config,
856 const _PyArgv *args)
Victor Stinnerf29084d2019-03-20 02:20:13 +0100857{
Victor Stinner331a6a52019-05-27 16:39:22 +0200858 assert(config != NULL);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200859
Victor Stinner331a6a52019-05-27 16:39:22 +0200860 PyStatus status = _PyRuntime_Initialize();
861 if (_PyStatus_EXCEPTION(status)) {
862 return status;
Victor Stinner6d1c4672019-05-20 11:02:00 +0200863 }
864 _PyRuntimeState *runtime = &_PyRuntime;
865
Victor Stinnerd3b90412019-09-17 23:59:51 +0200866 if (runtime->preinitialized) {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200867 /* Already initialized: do nothing */
Victor Stinner331a6a52019-05-27 16:39:22 +0200868 return _PyStatus_OK();
Victor Stinner70005ac2019-05-02 15:25:34 -0400869 }
Victor Stinnercab5d072019-05-17 19:01:14 +0200870
Victor Stinner331a6a52019-05-27 16:39:22 +0200871 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200872
Victor Stinner3c30a762019-10-01 10:56:37 +0200873 _PyPreConfig_InitFromConfig(&preconfig, config);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200874
Victor Stinner331a6a52019-05-27 16:39:22 +0200875 if (!config->parse_argv) {
876 return Py_PreInitialize(&preconfig);
Victor Stinner6d1c4672019-05-20 11:02:00 +0200877 }
878 else if (args == NULL) {
Victor Stinnercab5d072019-05-17 19:01:14 +0200879 _PyArgv config_args = {
880 .use_bytes_argv = 0,
Victor Stinner331a6a52019-05-27 16:39:22 +0200881 .argc = config->argv.length,
882 .wchar_argv = config->argv.items};
Victor Stinner6d1c4672019-05-20 11:02:00 +0200883 return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200884 }
885 else {
Victor Stinner6d1c4672019-05-20 11:02:00 +0200886 return _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinnercab5d072019-05-17 19:01:14 +0200887 }
Victor Stinner7d2ef3e2019-03-06 00:36:56 +0100888}
889
890
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100891/* Begin interpreter initialization
892 *
893 * On return, the first thread and interpreter state have been created,
894 * but the compiler, signal handling, multithreading and
895 * multiple interpreter support, and codec infrastructure are not yet
896 * available.
897 *
898 * The import system will support builtin and frozen modules only.
899 * The only supported io is writing to sys.stderr
900 *
901 * If any operation invoked by this function fails, a fatal error is
902 * issued and the function does not return.
903 *
904 * Any code invoked from this function should *not* assume it has access
905 * to the Python C API (unless the API is explicitly listed as being
906 * safe to call without calling Py_Initialize first)
907 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200908static PyStatus
Victor Stinner5edcf262019-05-23 00:57:57 +0200909pyinit_core(_PyRuntimeState *runtime,
Victor Stinner331a6a52019-05-27 16:39:22 +0200910 const PyConfig *src_config,
Victor Stinnerb45d2592019-06-20 00:05:23 +0200911 PyThreadState **tstate_p)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200912{
Victor Stinner331a6a52019-05-27 16:39:22 +0200913 PyStatus status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200914
Victor Stinner331a6a52019-05-27 16:39:22 +0200915 status = _Py_PreInitializeFromConfig(src_config, NULL);
916 if (_PyStatus_EXCEPTION(status)) {
917 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200918 }
919
Victor Stinner331a6a52019-05-27 16:39:22 +0200920 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +0200921 _PyConfig_InitCompatConfig(&config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200922
Victor Stinner331a6a52019-05-27 16:39:22 +0200923 status = _PyConfig_Copy(&config, src_config);
924 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200925 goto done;
926 }
927
Victor Stinner331a6a52019-05-27 16:39:22 +0200928 status = PyConfig_Read(&config);
929 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200930 goto done;
931 }
932
933 if (!runtime->core_initialized) {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200934 status = pyinit_config(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200935 }
936 else {
Victor Stinnerb45d2592019-06-20 00:05:23 +0200937 status = pyinit_core_reconfigure(runtime, tstate_p, &config);
Victor Stinner5edcf262019-05-23 00:57:57 +0200938 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200939 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner5edcf262019-05-23 00:57:57 +0200940 goto done;
941 }
942
943done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200944 PyConfig_Clear(&config);
945 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200946}
947
Victor Stinner5ac27a52019-03-27 13:40:14 +0100948
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200949/* Py_Initialize() has already been called: update the main interpreter
950 configuration. Example of bpo-34008: Py_Main() called after
951 Py_Initialize(). */
Victor Stinner331a6a52019-05-27 16:39:22 +0200952static PyStatus
Victor Stinnerb0051362019-11-22 17:52:42 +0100953_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200954{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200955 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100956
Victor Stinner331a6a52019-05-27 16:39:22 +0200957 PyObject *argv = _PyWideStringList_AsList(&config->argv);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100958 if (argv == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200959 return _PyStatus_NO_MEMORY(); \
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100960 }
961
Victor Stinnerb0051362019-11-22 17:52:42 +0100962 int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);
Victor Stinner8b9dbc02019-03-27 01:36:16 +0100963 Py_DECREF(argv);
964 if (res < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200965 return _PyStatus_ERR("fail to set sys.argv");
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200966 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200967 return _PyStatus_OK();
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200968}
969
Victor Stinnerb0051362019-11-22 17:52:42 +0100970
971static PyStatus
972init_interp_main(PyThreadState *tstate)
973{
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100974 assert(!_PyErr_Occurred(tstate));
975
Victor Stinnerb0051362019-11-22 17:52:42 +0100976 PyStatus status;
977 int is_main_interp = _Py_IsMainInterpreter(tstate);
978 PyInterpreterState *interp = tstate->interp;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200979 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
Victor Stinnerb0051362019-11-22 17:52:42 +0100980
981 if (!config->_install_importlib) {
982 /* Special mode for freeze_importlib: run with no import system
983 *
984 * This means anything which needs support from extension modules
985 * or pure Python code in the standard library won't work.
986 */
987 if (is_main_interp) {
988 interp->runtime->initialized = 1;
989 }
990 return _PyStatus_OK();
991 }
992
993 if (is_main_interp) {
994 if (_PyTime_Init() < 0) {
995 return _PyStatus_ERR("can't initialize time");
996 }
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100997 }
Victor Stinnerb0051362019-11-22 17:52:42 +0100998
Victor Stinner81fe5bd2019-12-06 02:43:30 +0100999 if (_PySys_InitMain(tstate) < 0) {
1000 return _PyStatus_ERR("can't finish initializing sys");
Victor Stinnerb0051362019-11-22 17:52:42 +01001001 }
1002
1003 status = init_importlib_external(tstate);
1004 if (_PyStatus_EXCEPTION(status)) {
1005 return status;
1006 }
1007
1008 if (is_main_interp) {
1009 /* initialize the faulthandler module */
1010 status = _PyFaulthandler_Init(config->faulthandler);
1011 if (_PyStatus_EXCEPTION(status)) {
1012 return status;
1013 }
1014 }
1015
1016 status = _PyUnicode_InitEncodings(tstate);
1017 if (_PyStatus_EXCEPTION(status)) {
1018 return status;
1019 }
1020
1021 if (is_main_interp) {
1022 if (config->install_signal_handlers) {
1023 status = init_signals(tstate);
1024 if (_PyStatus_EXCEPTION(status)) {
1025 return status;
1026 }
1027 }
1028
1029 if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1030 return _PyStatus_ERR("can't initialize tracemalloc");
1031 }
1032 }
1033
1034 status = init_sys_streams(tstate);
1035 if (_PyStatus_EXCEPTION(status)) {
1036 return status;
1037 }
1038
Andy Lester75cd5bf2020-03-12 02:49:05 -05001039 status = init_set_builtins_open();
Victor Stinnerb0051362019-11-22 17:52:42 +01001040 if (_PyStatus_EXCEPTION(status)) {
1041 return status;
1042 }
1043
1044 status = add_main_module(interp);
1045 if (_PyStatus_EXCEPTION(status)) {
1046 return status;
1047 }
1048
1049 if (is_main_interp) {
1050 /* Initialize warnings. */
1051 PyObject *warnoptions = PySys_GetObject("warnoptions");
1052 if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1053 {
1054 PyObject *warnings_module = PyImport_ImportModule("warnings");
1055 if (warnings_module == NULL) {
1056 fprintf(stderr, "'import warnings' failed; traceback:\n");
1057 _PyErr_Print(tstate);
1058 }
1059 Py_XDECREF(warnings_module);
1060 }
1061
1062 interp->runtime->initialized = 1;
1063 }
1064
1065 if (config->site_import) {
1066 status = init_import_site();
1067 if (_PyStatus_EXCEPTION(status)) {
1068 return status;
1069 }
1070 }
1071
1072 if (is_main_interp) {
1073#ifndef MS_WINDOWS
1074 emit_stderr_warning_for_legacy_locale(interp->runtime);
1075#endif
1076 }
1077
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001078 assert(!_PyErr_Occurred(tstate));
1079
Victor Stinnerb0051362019-11-22 17:52:42 +01001080 return _PyStatus_OK();
1081}
1082
1083
Eric Snowc7ec9982017-05-23 23:00:52 -07001084/* Update interpreter state based on supplied configuration settings
1085 *
1086 * After calling this function, most of the restrictions on the interpreter
1087 * are lifted. The only remaining incomplete settings are those related
1088 * to the main module (sys.argv[0], __main__ metadata)
1089 *
1090 * Calling this when the interpreter is not initializing, is already
1091 * initialized or without a valid current thread state is a fatal error.
1092 * Other errors should be reported as normal Python exceptions with a
1093 * non-zero return code.
1094 */
Victor Stinner331a6a52019-05-27 16:39:22 +02001095static PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +01001096pyinit_main(PyThreadState *tstate)
Eric Snow1abcf672017-05-23 21:46:51 -07001097{
Victor Stinnerb0051362019-11-22 17:52:42 +01001098 PyInterpreterState *interp = tstate->interp;
1099 if (!interp->runtime->core_initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001100 return _PyStatus_ERR("runtime core not initialized");
Eric Snowc7ec9982017-05-23 23:00:52 -07001101 }
Eric Snowc7ec9982017-05-23 23:00:52 -07001102
Victor Stinnerb0051362019-11-22 17:52:42 +01001103 if (interp->runtime->initialized) {
1104 return _Py_ReconfigureMainInterpreter(tstate);
Victor Stinnerfb47bca2018-07-20 17:34:23 +02001105 }
1106
Victor Stinnerb0051362019-11-22 17:52:42 +01001107 PyStatus status = init_interp_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001108 if (_PyStatus_EXCEPTION(status)) {
1109 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001110 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001111 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001112}
1113
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001114
Victor Stinner331a6a52019-05-27 16:39:22 +02001115PyStatus
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001116_Py_InitializeMain(void)
1117{
Victor Stinner331a6a52019-05-27 16:39:22 +02001118 PyStatus status = _PyRuntime_Initialize();
1119 if (_PyStatus_EXCEPTION(status)) {
1120 return status;
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001121 }
1122 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnerb45d2592019-06-20 00:05:23 +02001123 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner01b1cc12019-11-20 02:27:56 +01001124 return pyinit_main(tstate);
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001125}
1126
1127
Victor Stinner331a6a52019-05-27 16:39:22 +02001128PyStatus
1129Py_InitializeFromConfig(const PyConfig *config)
Eric Snow1abcf672017-05-23 21:46:51 -07001130{
Victor Stinner6d1c4672019-05-20 11:02:00 +02001131 if (config == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001132 return _PyStatus_ERR("initialization config is NULL");
Victor Stinner6d1c4672019-05-20 11:02:00 +02001133 }
1134
Victor Stinner331a6a52019-05-27 16:39:22 +02001135 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001136
Victor Stinner331a6a52019-05-27 16:39:22 +02001137 status = _PyRuntime_Initialize();
1138 if (_PyStatus_EXCEPTION(status)) {
1139 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001140 }
1141 _PyRuntimeState *runtime = &_PyRuntime;
1142
Victor Stinnerb45d2592019-06-20 00:05:23 +02001143 PyThreadState *tstate = NULL;
1144 status = pyinit_core(runtime, config, &tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001145 if (_PyStatus_EXCEPTION(status)) {
1146 return status;
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001147 }
Victor Stinnerda7933e2020-04-13 03:04:28 +02001148 config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerbc8ac6b2017-11-30 18:03:55 +01001149
Victor Stinner9ef5dca2019-05-16 17:38:16 +02001150 if (config->_init_main) {
Victor Stinner01b1cc12019-11-20 02:27:56 +01001151 status = pyinit_main(tstate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001152 if (_PyStatus_EXCEPTION(status)) {
1153 return status;
Victor Stinner484f20d2019-03-27 02:04:16 +01001154 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001155 }
Victor Stinner484f20d2019-03-27 02:04:16 +01001156
Victor Stinner331a6a52019-05-27 16:39:22 +02001157 return _PyStatus_OK();
Victor Stinner5ac27a52019-03-27 13:40:14 +01001158}
1159
1160
Eric Snow1abcf672017-05-23 21:46:51 -07001161void
Nick Coghland6009512014-11-20 21:39:37 +10001162Py_InitializeEx(int install_sigs)
1163{
Victor Stinner331a6a52019-05-27 16:39:22 +02001164 PyStatus status;
Victor Stinner43125222019-04-24 18:23:53 +02001165
Victor Stinner331a6a52019-05-27 16:39:22 +02001166 status = _PyRuntime_Initialize();
1167 if (_PyStatus_EXCEPTION(status)) {
1168 Py_ExitStatusException(status);
Victor Stinner43125222019-04-24 18:23:53 +02001169 }
1170 _PyRuntimeState *runtime = &_PyRuntime;
1171
1172 if (runtime->initialized) {
Victor Stinner1dc6e392018-07-25 02:49:17 +02001173 /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1174 return;
1175 }
1176
Victor Stinner331a6a52019-05-27 16:39:22 +02001177 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001178 _PyConfig_InitCompatConfig(&config);
Victor Stinner441b10c2019-09-28 04:28:35 +02001179
Victor Stinner1dc6e392018-07-25 02:49:17 +02001180 config.install_signal_handlers = install_sigs;
1181
Victor Stinner331a6a52019-05-27 16:39:22 +02001182 status = Py_InitializeFromConfig(&config);
1183 if (_PyStatus_EXCEPTION(status)) {
1184 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001185 }
Nick Coghland6009512014-11-20 21:39:37 +10001186}
1187
1188void
1189Py_Initialize(void)
1190{
1191 Py_InitializeEx(1);
1192}
1193
1194
Nick Coghland6009512014-11-20 21:39:37 +10001195/* Flush stdout and stderr */
1196
1197static int
1198file_is_closed(PyObject *fobj)
1199{
1200 int r;
1201 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1202 if (tmp == NULL) {
1203 PyErr_Clear();
1204 return 0;
1205 }
1206 r = PyObject_IsTrue(tmp);
1207 Py_DECREF(tmp);
1208 if (r < 0)
1209 PyErr_Clear();
1210 return r > 0;
1211}
1212
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001213static int
Nick Coghland6009512014-11-20 21:39:37 +10001214flush_std_files(void)
1215{
1216 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1217 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1218 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001219 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001220
1221 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001222 tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001223 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001224 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001225 status = -1;
1226 }
Nick Coghland6009512014-11-20 21:39:37 +10001227 else
1228 Py_DECREF(tmp);
1229 }
1230
1231 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001232 tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001233 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +10001234 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001235 status = -1;
1236 }
Nick Coghland6009512014-11-20 21:39:37 +10001237 else
1238 Py_DECREF(tmp);
1239 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001240
1241 return status;
Nick Coghland6009512014-11-20 21:39:37 +10001242}
1243
1244/* Undo the effect of Py_Initialize().
1245
1246 Beware: if multiple interpreter and/or thread states exist, these
1247 are not wiped out; only the current thread and interpreter state
1248 are deleted. But since everything else is deleted, those other
1249 interpreter and thread states should no longer be used.
1250
1251 (XXX We should do better, e.g. wipe out all interpreters and
1252 threads.)
1253
1254 Locking: as above.
1255
1256*/
1257
Victor Stinner7eee5be2019-11-20 10:38:34 +01001258
1259static void
Victor Stinner90db4652020-07-01 23:21:36 +02001260finalize_interp_types(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001261{
Victor Stinner281cce12020-06-23 22:55:46 +02001262 _PyExc_Fini(tstate);
Victor Stinner3744ed22020-06-05 01:39:24 +02001263 _PyFrame_Fini(tstate);
Victor Stinner78a02c22020-06-05 02:34:14 +02001264 _PyAsyncGen_Fini(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001265 _PyContext_Fini(tstate);
Victor Stinner666ecfb2020-07-02 01:19:57 +02001266 _PyUnicode_ClearInterned(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001267
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001268 _PyDict_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001269 _PyList_Fini(tstate);
1270 _PyTuple_Fini(tstate);
1271
1272 _PySlice_Fini(tstate);
Victor Stinner3d483342019-11-22 12:27:50 +01001273
Victor Stinnerc41eed12020-06-23 15:54:35 +02001274 _PyBytes_Fini(tstate);
Victor Stinner7907f8c2020-06-08 01:22:36 +02001275 _PyUnicode_Fini(tstate);
1276 _PyFloat_Fini(tstate);
1277 _PyLong_Fini(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001278}
1279
1280
1281static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001282finalize_interp_clear(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001283{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001284 int is_main_interp = _Py_IsMainInterpreter(tstate);
1285
Victor Stinner7eee5be2019-11-20 10:38:34 +01001286 /* Clear interpreter state and all thread states */
1287 PyInterpreterState_Clear(tstate->interp);
1288
Pablo Galindoac0e1c22019-12-04 11:51:03 +00001289 /* Trigger a GC collection on subinterpreters*/
1290 if (!is_main_interp) {
1291 _PyGC_CollectNoFail();
1292 }
1293
Kongedaa0fe02020-07-04 05:06:46 +08001294 /* Clear all loghooks */
1295 /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1296 Call _PySys_ClearAuditHooks when PyObject available. */
1297 if (is_main_interp) {
1298 _PySys_ClearAuditHooks(tstate);
1299 }
1300
Victor Stinner88ec9192020-06-05 02:05:41 +02001301 _PyGC_Fini(tstate);
1302
Victor Stinner7907f8c2020-06-08 01:22:36 +02001303 if (is_main_interp) {
1304 _Py_HashRandomization_Fini();
1305 _PyArg_Fini();
1306 _Py_ClearFileSystemEncoding();
1307 }
1308
1309 _PyWarnings_Fini(tstate->interp);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001310
Victor Stinner90db4652020-07-01 23:21:36 +02001311 finalize_interp_types(tstate);
Victor Stinner7eee5be2019-11-20 10:38:34 +01001312}
1313
1314
1315static void
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001316finalize_interp_delete(PyThreadState *tstate)
Victor Stinner7eee5be2019-11-20 10:38:34 +01001317{
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001318 if (_Py_IsMainInterpreter(tstate)) {
Victor Stinner7eee5be2019-11-20 10:38:34 +01001319 /* Cleanup auto-thread-state */
1320 _PyGILState_Fini(tstate);
1321 }
1322
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001323 /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1324 fail when it is being awaited by another running daemon thread (see
1325 bpo-9901). Instead pycore_create_interpreter() destroys the previously
1326 created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1327 called multiple times. */
1328
Victor Stinner7eee5be2019-11-20 10:38:34 +01001329 PyInterpreterState_Delete(tstate->interp);
1330}
1331
1332
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001333int
1334Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +10001335{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001336 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001337
Victor Stinner8e91c242019-04-24 17:24:01 +02001338 _PyRuntimeState *runtime = &_PyRuntime;
1339 if (!runtime->initialized) {
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001340 return status;
Victor Stinner8e91c242019-04-24 17:24:01 +02001341 }
Nick Coghland6009512014-11-20 21:39:37 +10001342
Victor Stinnere225beb2019-06-03 18:14:24 +02001343 /* Get current thread state and interpreter pointer */
1344 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1345 PyInterpreterState *interp = tstate->interp;
Victor Stinner8e91c242019-04-24 17:24:01 +02001346
Victor Stinnerb45d2592019-06-20 00:05:23 +02001347 // Wrap up existing "threading"-module-created, non-daemon threads.
1348 wait_for_thread_shutdown(tstate);
1349
1350 // Make any remaining pending calls.
Victor Stinner2b1df452020-01-13 18:46:59 +01001351 _Py_FinishPendingCalls(tstate);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001352
Nick Coghland6009512014-11-20 21:39:37 +10001353 /* The interpreter is still entirely intact at this point, and the
1354 * exit funcs may be relying on that. In particular, if some thread
1355 * or exit func is still waiting to do an import, the import machinery
1356 * expects Py_IsInitialized() to return true. So don't say the
Eric Snow842a2f02019-03-15 15:47:51 -06001357 * runtime is uninitialized until after the exit funcs have run.
Nick Coghland6009512014-11-20 21:39:37 +10001358 * Note that Threading.py uses an exit func to do a join on all the
1359 * threads created thru it, so this also protects pending imports in
1360 * the threads created via Threading.
1361 */
Nick Coghland6009512014-11-20 21:39:37 +10001362
Victor Stinnerb45d2592019-06-20 00:05:23 +02001363 call_py_exitfuncs(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001364
Victor Stinnerda273412017-12-15 01:46:02 +01001365 /* Copy the core config, PyInterpreterState_Delete() free
1366 the core config memory */
Victor Stinner5d862462017-12-19 11:35:58 +01001367#ifdef Py_REF_DEBUG
Victor Stinner331a6a52019-05-27 16:39:22 +02001368 int show_ref_count = interp->config.show_ref_count;
Victor Stinner5d862462017-12-19 11:35:58 +01001369#endif
1370#ifdef Py_TRACE_REFS
Victor Stinner331a6a52019-05-27 16:39:22 +02001371 int dump_refs = interp->config.dump_refs;
Victor Stinner5d862462017-12-19 11:35:58 +01001372#endif
1373#ifdef WITH_PYMALLOC
Victor Stinner331a6a52019-05-27 16:39:22 +02001374 int malloc_stats = interp->config.malloc_stats;
Victor Stinner5d862462017-12-19 11:35:58 +01001375#endif
Victor Stinner6bf992a2017-12-06 17:26:10 +01001376
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001377 /* Remaining daemon threads will automatically exit
1378 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
Victor Stinner7b3c2522020-03-07 00:24:23 +01001379 _PyRuntimeState_SetFinalizing(runtime, tstate);
Victor Stinner8e91c242019-04-24 17:24:01 +02001380 runtime->initialized = 0;
1381 runtime->core_initialized = 0;
Nick Coghland6009512014-11-20 21:39:37 +10001382
Victor Stinner9ad58ac2020-03-09 23:37:49 +01001383 /* Destroy the state of all threads of the interpreter, except of the
1384 current thread. In practice, only daemon threads should still be alive,
1385 except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1386 Clear frames of other threads to call objects destructors. Destructors
1387 will be called in the current Python thread. Since
1388 _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1389 can take the GIL at this point: if they try, they will exit
1390 immediately. */
1391 _PyThreadState_DeleteExcept(runtime, tstate);
1392
Victor Stinnere0deff32015-03-24 13:46:18 +01001393 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001394 if (flush_std_files() < 0) {
1395 status = -1;
1396 }
Nick Coghland6009512014-11-20 21:39:37 +10001397
1398 /* Disable signal handling */
1399 PyOS_FiniInterrupts();
1400
1401 /* Collect garbage. This may call finalizers; it's nice to call these
1402 * before all modules are destroyed.
1403 * XXX If a __del__ or weakref callback is triggered here, and tries to
1404 * XXX import a module, bad things can happen, because Python no
1405 * XXX longer believes it's initialized.
1406 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
1407 * XXX is easy to provoke that way. I've also seen, e.g.,
1408 * XXX Exception exceptions.ImportError: 'No module named sha'
1409 * XXX in <function callback at 0x008F5718> ignored
1410 * XXX but I'm unclear on exactly how that one happens. In any case,
1411 * XXX I haven't seen a real-life report of either of these.
1412 */
Łukasz Langafef7e942016-09-09 21:47:46 -07001413 _PyGC_CollectIfEnabled();
Eric Snowdae02762017-09-14 00:35:58 -07001414
Nick Coghland6009512014-11-20 21:39:37 +10001415 /* Destroy all modules */
Victor Stinner987a0dc2019-06-19 10:36:10 +02001416 _PyImport_Cleanup(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001417
Inada Naoki91234a12019-06-03 21:30:58 +09001418 /* Print debug stats if any */
1419 _PyEval_Fini();
1420
Victor Stinnere0deff32015-03-24 13:46:18 +01001421 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001422 if (flush_std_files() < 0) {
1423 status = -1;
1424 }
Nick Coghland6009512014-11-20 21:39:37 +10001425
1426 /* Collect final garbage. This disposes of cycles created by
1427 * class definitions, for example.
1428 * XXX This is disabled because it caused too many problems. If
1429 * XXX a __del__ or weakref callback triggers here, Python code has
1430 * XXX a hard time running, because even the sys module has been
1431 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1432 * XXX One symptom is a sequence of information-free messages
1433 * XXX coming from threads (if a __del__ or callback is invoked,
1434 * XXX other threads can execute too, and any exception they encounter
1435 * XXX triggers a comedy of errors as subsystem after subsystem
1436 * XXX fails to find what it *expects* to find in sys to help report
1437 * XXX the exception and consequent unexpected failures). I've also
1438 * XXX seen segfaults then, after adding print statements to the
1439 * XXX Python code getting called.
1440 */
1441#if 0
Łukasz Langafef7e942016-09-09 21:47:46 -07001442 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +10001443#endif
1444
1445 /* Disable tracemalloc after all Python objects have been destroyed,
1446 so it is possible to use tracemalloc in objects destructor. */
1447 _PyTraceMalloc_Fini();
1448
1449 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1450 _PyImport_Fini();
1451
1452 /* Cleanup typeobject.c's internal caches. */
1453 _PyType_Fini();
1454
1455 /* unload faulthandler module */
1456 _PyFaulthandler_Fini();
1457
Nick Coghland6009512014-11-20 21:39:37 +10001458 /* dump hash stats */
1459 _PyHash_Fini();
1460
Eric Snowdae02762017-09-14 00:35:58 -07001461#ifdef Py_REF_DEBUG
Victor Stinnerda273412017-12-15 01:46:02 +01001462 if (show_ref_count) {
Victor Stinner25420fe2017-11-20 18:12:22 -08001463 _PyDebug_PrintTotalRefs();
1464 }
Eric Snowdae02762017-09-14 00:35:58 -07001465#endif
Nick Coghland6009512014-11-20 21:39:37 +10001466
1467#ifdef Py_TRACE_REFS
1468 /* Display all objects still alive -- this can invoke arbitrary
1469 * __repr__ overrides, so requires a mostly-intact interpreter.
1470 * Alas, a lot of stuff may still be alive now that will be cleaned
1471 * up later.
1472 */
Victor Stinnerda273412017-12-15 01:46:02 +01001473 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001474 _Py_PrintReferences(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001475 }
Nick Coghland6009512014-11-20 21:39:37 +10001476#endif /* Py_TRACE_REFS */
1477
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001478 finalize_interp_clear(tstate);
1479 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001480
1481#ifdef Py_TRACE_REFS
1482 /* Display addresses (& refcnts) of all objects still alive.
1483 * An address can be used to find the repr of the object, printed
1484 * above by _Py_PrintReferences.
1485 */
Victor Stinnerda273412017-12-15 01:46:02 +01001486 if (dump_refs) {
Nick Coghland6009512014-11-20 21:39:37 +10001487 _Py_PrintReferenceAddresses(stderr);
Victor Stinner6bf992a2017-12-06 17:26:10 +01001488 }
Nick Coghland6009512014-11-20 21:39:37 +10001489#endif /* Py_TRACE_REFS */
Victor Stinner34be8072016-03-14 12:04:26 +01001490#ifdef WITH_PYMALLOC
Victor Stinnerda273412017-12-15 01:46:02 +01001491 if (malloc_stats) {
Victor Stinner6bf992a2017-12-06 17:26:10 +01001492 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be8072016-03-14 12:04:26 +01001493 }
Nick Coghland6009512014-11-20 21:39:37 +10001494#endif
1495
Victor Stinner8e91c242019-04-24 17:24:01 +02001496 call_ll_exitfuncs(runtime);
Victor Stinner9316ee42017-11-25 03:17:57 +01001497
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001498 _PyRuntime_Finalize();
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001499 return status;
1500}
1501
1502void
1503Py_Finalize(void)
1504{
1505 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +10001506}
1507
Victor Stinnerb0051362019-11-22 17:52:42 +01001508
Nick Coghland6009512014-11-20 21:39:37 +10001509/* Create and initialize a new interpreter and thread, and return the
1510 new thread. This requires that Py_Initialize() has been called
1511 first.
1512
1513 Unsuccessful initialization yields a NULL pointer. Note that *no*
1514 exception information is available even in this case -- the
1515 exception information is held in the thread, and there is no
1516 thread.
1517
1518 Locking: as above.
1519
1520*/
1521
Victor Stinner331a6a52019-05-27 16:39:22 +02001522static PyStatus
Victor Stinner252346a2020-05-01 11:33:44 +02001523new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
Nick Coghland6009512014-11-20 21:39:37 +10001524{
Victor Stinner331a6a52019-05-27 16:39:22 +02001525 PyStatus status;
Nick Coghland6009512014-11-20 21:39:37 +10001526
Victor Stinner331a6a52019-05-27 16:39:22 +02001527 status = _PyRuntime_Initialize();
1528 if (_PyStatus_EXCEPTION(status)) {
1529 return status;
Victor Stinner43125222019-04-24 18:23:53 +02001530 }
1531 _PyRuntimeState *runtime = &_PyRuntime;
1532
1533 if (!runtime->initialized) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001534 return _PyStatus_ERR("Py_Initialize must be called first");
Victor Stinnera7368ac2017-11-15 18:11:45 -08001535 }
Nick Coghland6009512014-11-20 21:39:37 +10001536
Victor Stinner8a1be612016-03-14 22:07:55 +01001537 /* Issue #10915, #15751: The GIL API doesn't work with multiple
1538 interpreters: disable PyGILState_Check(). */
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001539 runtime->gilstate.check_enabled = 0;
Victor Stinner8a1be612016-03-14 22:07:55 +01001540
Victor Stinner43125222019-04-24 18:23:53 +02001541 PyInterpreterState *interp = PyInterpreterState_New();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001542 if (interp == NULL) {
1543 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001544 return _PyStatus_OK();
Victor Stinnera7368ac2017-11-15 18:11:45 -08001545 }
Nick Coghland6009512014-11-20 21:39:37 +10001546
Victor Stinner43125222019-04-24 18:23:53 +02001547 PyThreadState *tstate = PyThreadState_New(interp);
Nick Coghland6009512014-11-20 21:39:37 +10001548 if (tstate == NULL) {
1549 PyInterpreterState_Delete(interp);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001550 *tstate_p = NULL;
Victor Stinner331a6a52019-05-27 16:39:22 +02001551 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001552 }
1553
Victor Stinner43125222019-04-24 18:23:53 +02001554 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001555
Eric Snow1abcf672017-05-23 21:46:51 -07001556 /* Copy the current interpreter config into the new interpreter */
Victor Stinnerda7933e2020-04-13 03:04:28 +02001557 const PyConfig *config;
Victor Stinner7be4e352020-05-05 20:27:47 +02001558#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Eric Snow1abcf672017-05-23 21:46:51 -07001559 if (save_tstate != NULL) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001560 config = _PyInterpreterState_GetConfig(save_tstate->interp);
Victor Stinner7be4e352020-05-05 20:27:47 +02001561 }
1562 else
1563#endif
1564 {
Eric Snow1abcf672017-05-23 21:46:51 -07001565 /* No current thread state, copy from the main interpreter */
1566 PyInterpreterState *main_interp = PyInterpreterState_Main();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001567 config = _PyInterpreterState_GetConfig(main_interp);
Victor Stinnerda273412017-12-15 01:46:02 +01001568 }
1569
Victor Stinnerda7933e2020-04-13 03:04:28 +02001570 status = _PyInterpreterState_SetConfig(interp, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001571 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001572 goto error;
Victor Stinnerda273412017-12-15 01:46:02 +01001573 }
Victor Stinner252346a2020-05-01 11:33:44 +02001574 interp->config._isolated_interpreter = isolated_subinterpreter;
Eric Snow1abcf672017-05-23 21:46:51 -07001575
Victor Stinner0dd5e7a2020-05-05 20:16:37 +02001576 status = init_interp_create_gil(tstate);
1577 if (_PyStatus_EXCEPTION(status)) {
1578 goto error;
1579 }
1580
Victor Stinnerd863ade2019-12-06 03:37:07 +01001581 status = pycore_interp_init(tstate);
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001582 if (_PyStatus_EXCEPTION(status)) {
1583 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001584 }
1585
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001586 status = init_interp_main(tstate);
1587 if (_PyStatus_EXCEPTION(status)) {
1588 goto error;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001589 }
Nick Coghland6009512014-11-20 21:39:37 +10001590
Victor Stinnera7368ac2017-11-15 18:11:45 -08001591 *tstate_p = tstate;
Victor Stinner331a6a52019-05-27 16:39:22 +02001592 return _PyStatus_OK();
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001593
Victor Stinner81fe5bd2019-12-06 02:43:30 +01001594error:
Victor Stinnerb0051362019-11-22 17:52:42 +01001595 *tstate_p = NULL;
1596
1597 /* Oops, it didn't work. Undo it all. */
Nick Coghland6009512014-11-20 21:39:37 +10001598 PyErr_PrintEx(0);
1599 PyThreadState_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001600 PyThreadState_Delete(tstate);
1601 PyInterpreterState_Delete(interp);
Victor Stinner9da74302019-11-20 11:17:17 +01001602 PyThreadState_Swap(save_tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001603
Victor Stinnerb0051362019-11-22 17:52:42 +01001604 return status;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001605}
1606
1607PyThreadState *
Victor Stinner252346a2020-05-01 11:33:44 +02001608_Py_NewInterpreter(int isolated_subinterpreter)
Victor Stinnera7368ac2017-11-15 18:11:45 -08001609{
Stéphane Wirtel9e06d2b2019-03-18 17:10:29 +01001610 PyThreadState *tstate = NULL;
Victor Stinner252346a2020-05-01 11:33:44 +02001611 PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
Victor Stinner331a6a52019-05-27 16:39:22 +02001612 if (_PyStatus_EXCEPTION(status)) {
1613 Py_ExitStatusException(status);
Victor Stinnera7368ac2017-11-15 18:11:45 -08001614 }
1615 return tstate;
1616
Nick Coghland6009512014-11-20 21:39:37 +10001617}
1618
Victor Stinner252346a2020-05-01 11:33:44 +02001619PyThreadState *
1620Py_NewInterpreter(void)
1621{
1622 return _Py_NewInterpreter(0);
1623}
1624
Nick Coghland6009512014-11-20 21:39:37 +10001625/* Delete an interpreter and its last thread. This requires that the
1626 given thread state is current, that the thread has no remaining
1627 frames, and that it is its interpreter's only remaining thread.
1628 It is a fatal error to violate these constraints.
1629
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001630 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +10001631 everything, regardless.)
1632
1633 Locking: as above.
1634
1635*/
1636
1637void
1638Py_EndInterpreter(PyThreadState *tstate)
1639{
1640 PyInterpreterState *interp = tstate->interp;
1641
Victor Stinnerb45d2592019-06-20 00:05:23 +02001642 if (tstate != _PyThreadState_GET()) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001643 Py_FatalError("thread is not current");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001644 }
1645 if (tstate->frame != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001646 Py_FatalError("thread still has a frame");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001647 }
Eric Snow5be45a62019-03-08 22:47:07 -07001648 interp->finalizing = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001649
Eric Snow842a2f02019-03-15 15:47:51 -06001650 // Wrap up existing "threading"-module-created, non-daemon threads.
Victor Stinnerb45d2592019-06-20 00:05:23 +02001651 wait_for_thread_shutdown(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001652
Victor Stinnerb45d2592019-06-20 00:05:23 +02001653 call_py_exitfuncs(tstate);
Marcel Plch776407f2017-12-20 11:17:58 +01001654
Victor Stinnerb45d2592019-06-20 00:05:23 +02001655 if (tstate != interp->tstate_head || tstate->next != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001656 Py_FatalError("not the last thread");
Victor Stinnerb45d2592019-06-20 00:05:23 +02001657 }
Nick Coghland6009512014-11-20 21:39:37 +10001658
Victor Stinner987a0dc2019-06-19 10:36:10 +02001659 _PyImport_Cleanup(tstate);
Victor Stinnerb93f31f2019-11-20 18:39:12 +01001660 finalize_interp_clear(tstate);
1661 finalize_interp_delete(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10001662}
1663
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001664/* Add the __main__ module */
Nick Coghland6009512014-11-20 21:39:37 +10001665
Victor Stinner331a6a52019-05-27 16:39:22 +02001666static PyStatus
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001667add_main_module(PyInterpreterState *interp)
Nick Coghland6009512014-11-20 21:39:37 +10001668{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001669 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +10001670 m = PyImport_AddModule("__main__");
1671 if (m == NULL)
Victor Stinner331a6a52019-05-27 16:39:22 +02001672 return _PyStatus_ERR("can't create __main__ module");
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001673
Nick Coghland6009512014-11-20 21:39:37 +10001674 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001675 ann_dict = PyDict_New();
1676 if ((ann_dict == NULL) ||
1677 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001678 return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001679 }
1680 Py_DECREF(ann_dict);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001681
Nick Coghland6009512014-11-20 21:39:37 +10001682 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
1683 PyObject *bimod = PyImport_ImportModule("builtins");
1684 if (bimod == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001685 return _PyStatus_ERR("Failed to retrieve builtins module");
Nick Coghland6009512014-11-20 21:39:37 +10001686 }
1687 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001688 return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
Nick Coghland6009512014-11-20 21:39:37 +10001689 }
1690 Py_DECREF(bimod);
1691 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001692
Nick Coghland6009512014-11-20 21:39:37 +10001693 /* Main is a little special - imp.is_builtin("__main__") will return
1694 * False, but BuiltinImporter is still the most appropriate initial
1695 * setting for its __loader__ attribute. A more suitable value will
1696 * be set if __main__ gets further initialized later in the startup
1697 * process.
1698 */
1699 loader = PyDict_GetItemString(d, "__loader__");
1700 if (loader == NULL || loader == Py_None) {
1701 PyObject *loader = PyObject_GetAttrString(interp->importlib,
1702 "BuiltinImporter");
1703 if (loader == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001704 return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
Nick Coghland6009512014-11-20 21:39:37 +10001705 }
1706 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001707 return _PyStatus_ERR("Failed to initialize __main__.__loader__");
Nick Coghland6009512014-11-20 21:39:37 +10001708 }
1709 Py_DECREF(loader);
1710 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001711 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001712}
1713
Nick Coghland6009512014-11-20 21:39:37 +10001714/* Import the site module (not into __main__ though) */
1715
Victor Stinner331a6a52019-05-27 16:39:22 +02001716static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02001717init_import_site(void)
Nick Coghland6009512014-11-20 21:39:37 +10001718{
1719 PyObject *m;
1720 m = PyImport_ImportModule("site");
1721 if (m == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001722 return _PyStatus_ERR("Failed to import the site module");
Nick Coghland6009512014-11-20 21:39:37 +10001723 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -08001724 Py_DECREF(m);
Victor Stinner331a6a52019-05-27 16:39:22 +02001725 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10001726}
1727
Victor Stinner874dbe82015-09-04 17:29:57 +02001728/* Check if a file descriptor is valid or not.
1729 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
1730static int
1731is_valid_fd(int fd)
1732{
Victor Stinner3092d6b2019-04-17 18:09:12 +02001733/* dup() is faster than fstat(): fstat() can require input/output operations,
1734 whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
1735 startup. Problem: dup() doesn't check if the file descriptor is valid on
1736 some platforms.
1737
1738 bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
1739 side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
1740 EBADF. FreeBSD has similar issue (bpo-32849).
1741
1742 Only use dup() on platforms where dup() is enough to detect invalid FD in
1743 corner cases: on Linux and Windows (bpo-32849). */
1744#if defined(__linux__) || defined(MS_WINDOWS)
1745 if (fd < 0) {
1746 return 0;
1747 }
1748 int fd2;
1749
1750 _Py_BEGIN_SUPPRESS_IPH
1751 fd2 = dup(fd);
1752 if (fd2 >= 0) {
1753 close(fd2);
1754 }
1755 _Py_END_SUPPRESS_IPH
1756
1757 return (fd2 >= 0);
1758#else
Victor Stinner1c4670e2017-05-04 00:45:56 +02001759 struct stat st;
1760 return (fstat(fd, &st) == 0);
Victor Stinner1c4670e2017-05-04 00:45:56 +02001761#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02001762}
1763
1764/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10001765static PyObject*
Victor Stinner331a6a52019-05-27 16:39:22 +02001766create_stdio(const PyConfig *config, PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001767 int fd, int write_mode, const char* name,
Victor Stinner709d23d2019-05-02 14:56:30 -04001768 const wchar_t* encoding, const wchar_t* errors)
Nick Coghland6009512014-11-20 21:39:37 +10001769{
1770 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
1771 const char* mode;
1772 const char* newline;
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001773 PyObject *line_buffering, *write_through;
Nick Coghland6009512014-11-20 21:39:37 +10001774 int buffering, isatty;
1775 _Py_IDENTIFIER(open);
1776 _Py_IDENTIFIER(isatty);
1777 _Py_IDENTIFIER(TextIOWrapper);
1778 _Py_IDENTIFIER(mode);
Victor Stinnerfbca9082018-08-30 00:50:45 +02001779 const int buffered_stdio = config->buffered_stdio;
Nick Coghland6009512014-11-20 21:39:37 +10001780
Victor Stinner874dbe82015-09-04 17:29:57 +02001781 if (!is_valid_fd(fd))
1782 Py_RETURN_NONE;
1783
Nick Coghland6009512014-11-20 21:39:37 +10001784 /* stdin is always opened in buffered mode, first because it shouldn't
1785 make a difference in common use cases, second because TextIOWrapper
1786 depends on the presence of a read1() method which only exists on
1787 buffered streams.
1788 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02001789 if (!buffered_stdio && write_mode)
Nick Coghland6009512014-11-20 21:39:37 +10001790 buffering = 0;
1791 else
1792 buffering = -1;
1793 if (write_mode)
1794 mode = "wb";
1795 else
1796 mode = "rb";
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03001797 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
Nick Coghland6009512014-11-20 21:39:37 +10001798 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001799 Py_None, Py_None, /* encoding, errors */
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +03001800 Py_None, Py_False); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10001801 if (buf == NULL)
1802 goto error;
1803
1804 if (buffering) {
1805 _Py_IDENTIFIER(raw);
1806 raw = _PyObject_GetAttrId(buf, &PyId_raw);
1807 if (raw == NULL)
1808 goto error;
1809 }
1810 else {
1811 raw = buf;
1812 Py_INCREF(raw);
1813 }
1814
Steve Dower39294992016-08-30 21:22:36 -07001815#ifdef MS_WINDOWS
1816 /* Windows console IO is always UTF-8 encoded */
1817 if (PyWindowsConsoleIO_Check(raw))
Victor Stinner709d23d2019-05-02 14:56:30 -04001818 encoding = L"utf-8";
Steve Dower39294992016-08-30 21:22:36 -07001819#endif
1820
Nick Coghland6009512014-11-20 21:39:37 +10001821 text = PyUnicode_FromString(name);
1822 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
1823 goto error;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001824 res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
Nick Coghland6009512014-11-20 21:39:37 +10001825 if (res == NULL)
1826 goto error;
1827 isatty = PyObject_IsTrue(res);
1828 Py_DECREF(res);
1829 if (isatty == -1)
1830 goto error;
Victor Stinnerfbca9082018-08-30 00:50:45 +02001831 if (!buffered_stdio)
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001832 write_through = Py_True;
1833 else
1834 write_through = Py_False;
Jendrik Seipp5b907712020-01-01 23:21:43 +01001835 if (buffered_stdio && (isatty || fd == fileno(stderr)))
Nick Coghland6009512014-11-20 21:39:37 +10001836 line_buffering = Py_True;
1837 else
1838 line_buffering = Py_False;
1839
1840 Py_CLEAR(raw);
1841 Py_CLEAR(text);
1842
1843#ifdef MS_WINDOWS
1844 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1845 newlines to "\n".
1846 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1847 newline = NULL;
1848#else
1849 /* sys.stdin: split lines at "\n".
1850 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1851 newline = "\n";
1852#endif
1853
Victor Stinner709d23d2019-05-02 14:56:30 -04001854 PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
1855 if (encoding_str == NULL) {
1856 Py_CLEAR(buf);
1857 goto error;
1858 }
1859
1860 PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
1861 if (errors_str == NULL) {
1862 Py_CLEAR(buf);
1863 Py_CLEAR(encoding_str);
1864 goto error;
1865 }
1866
1867 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
1868 buf, encoding_str, errors_str,
Serhiy Storchaka77732be2017-10-04 20:25:40 +03001869 newline, line_buffering, write_through);
Nick Coghland6009512014-11-20 21:39:37 +10001870 Py_CLEAR(buf);
Victor Stinner709d23d2019-05-02 14:56:30 -04001871 Py_CLEAR(encoding_str);
1872 Py_CLEAR(errors_str);
Nick Coghland6009512014-11-20 21:39:37 +10001873 if (stream == NULL)
1874 goto error;
1875
1876 if (write_mode)
1877 mode = "w";
1878 else
1879 mode = "r";
1880 text = PyUnicode_FromString(mode);
1881 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
1882 goto error;
1883 Py_CLEAR(text);
1884 return stream;
1885
1886error:
1887 Py_XDECREF(buf);
1888 Py_XDECREF(stream);
1889 Py_XDECREF(text);
1890 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10001891
Victor Stinner874dbe82015-09-04 17:29:57 +02001892 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
1893 /* Issue #24891: the file descriptor was closed after the first
1894 is_valid_fd() check was called. Ignore the OSError and set the
1895 stream to None. */
1896 PyErr_Clear();
1897 Py_RETURN_NONE;
1898 }
1899 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001900}
1901
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001902/* Set builtins.open to io.OpenWrapper */
1903static PyStatus
Andy Lester75cd5bf2020-03-12 02:49:05 -05001904init_set_builtins_open(void)
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001905{
1906 PyObject *iomod = NULL, *wrapper;
1907 PyObject *bimod = NULL;
1908 PyStatus res = _PyStatus_OK();
1909
1910 if (!(iomod = PyImport_ImportModule("io"))) {
1911 goto error;
1912 }
1913
1914 if (!(bimod = PyImport_ImportModule("builtins"))) {
1915 goto error;
1916 }
1917
1918 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1919 goto error;
1920 }
1921
1922 /* Set builtins.open */
1923 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
1924 Py_DECREF(wrapper);
1925 goto error;
1926 }
1927 Py_DECREF(wrapper);
1928 goto done;
1929
1930error:
1931 res = _PyStatus_ERR("can't initialize io.open");
1932
1933done:
1934 Py_XDECREF(bimod);
1935 Py_XDECREF(iomod);
1936 return res;
1937}
1938
1939
Nick Coghland6009512014-11-20 21:39:37 +10001940/* Initialize sys.stdin, stdout, stderr and builtins.open */
Victor Stinner331a6a52019-05-27 16:39:22 +02001941static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02001942init_sys_streams(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10001943{
Victor Stinnere0c9ab82019-11-22 16:19:14 +01001944 PyObject *iomod = NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001945 PyObject *std = NULL;
Victor Stinnera7368ac2017-11-15 18:11:45 -08001946 int fd;
Nick Coghland6009512014-11-20 21:39:37 +10001947 PyObject * encoding_attr;
Victor Stinner331a6a52019-05-27 16:39:22 +02001948 PyStatus res = _PyStatus_OK();
Victor Stinnerda7933e2020-04-13 03:04:28 +02001949 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02001950
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01001951 /* Check that stdin is not a directory
1952 Using shell redirection, you can redirect stdin to a directory,
1953 crashing the Python interpreter. Catch this common mistake here
1954 and output a useful error message. Note that under MS Windows,
1955 the shell already prevents that. */
1956#ifndef MS_WINDOWS
1957 struct _Py_stat_struct sb;
1958 if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
1959 S_ISDIR(sb.st_mode)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001960 return _PyStatus_ERR("<stdin> is a directory, cannot continue");
Victor Stinnerbf4ac2d2019-01-22 17:39:03 +01001961 }
1962#endif
1963
Nick Coghland6009512014-11-20 21:39:37 +10001964 if (!(iomod = PyImport_ImportModule("io"))) {
1965 goto error;
1966 }
Nick Coghland6009512014-11-20 21:39:37 +10001967
Nick Coghland6009512014-11-20 21:39:37 +10001968 /* Set sys.stdin */
1969 fd = fileno(stdin);
1970 /* Under some conditions stdin, stdout and stderr may not be connected
1971 * and fileno() may point to an invalid file descriptor. For example
1972 * GUI apps don't have valid standard streams by default.
1973 */
Victor Stinnerfbca9082018-08-30 00:50:45 +02001974 std = create_stdio(config, iomod, fd, 0, "<stdin>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02001975 config->stdio_encoding,
1976 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02001977 if (std == NULL)
1978 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001979 PySys_SetObject("__stdin__", std);
1980 _PySys_SetObjectId(&PyId_stdin, std);
1981 Py_DECREF(std);
1982
1983 /* Set sys.stdout */
1984 fd = fileno(stdout);
Victor Stinnerfbca9082018-08-30 00:50:45 +02001985 std = create_stdio(config, iomod, fd, 1, "<stdout>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02001986 config->stdio_encoding,
1987 config->stdio_errors);
Victor Stinner874dbe82015-09-04 17:29:57 +02001988 if (std == NULL)
1989 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001990 PySys_SetObject("__stdout__", std);
1991 _PySys_SetObjectId(&PyId_stdout, std);
1992 Py_DECREF(std);
1993
1994#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
1995 /* Set sys.stderr, replaces the preliminary stderr */
1996 fd = fileno(stderr);
Victor Stinnerfbca9082018-08-30 00:50:45 +02001997 std = create_stdio(config, iomod, fd, 1, "<stderr>",
Victor Stinnerdfe0dc72018-08-29 11:47:29 +02001998 config->stdio_encoding,
Victor Stinner709d23d2019-05-02 14:56:30 -04001999 L"backslashreplace");
Victor Stinner874dbe82015-09-04 17:29:57 +02002000 if (std == NULL)
2001 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10002002
2003 /* Same as hack above, pre-import stderr's codec to avoid recursion
2004 when import.c tries to write to stderr in verbose mode. */
2005 encoding_attr = PyObject_GetAttrString(std, "encoding");
2006 if (encoding_attr != NULL) {
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002007 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10002008 if (std_encoding != NULL) {
2009 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2010 Py_XDECREF(codec_info);
2011 }
2012 Py_DECREF(encoding_attr);
2013 }
Victor Stinnerb45d2592019-06-20 00:05:23 +02002014 _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */
Nick Coghland6009512014-11-20 21:39:37 +10002015
2016 if (PySys_SetObject("__stderr__", std) < 0) {
2017 Py_DECREF(std);
2018 goto error;
2019 }
2020 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2021 Py_DECREF(std);
2022 goto error;
2023 }
2024 Py_DECREF(std);
2025#endif
2026
Victor Stinnera7368ac2017-11-15 18:11:45 -08002027 goto done;
Nick Coghland6009512014-11-20 21:39:37 +10002028
Victor Stinnera7368ac2017-11-15 18:11:45 -08002029error:
Victor Stinner331a6a52019-05-27 16:39:22 +02002030 res = _PyStatus_ERR("can't initialize sys standard streams");
Victor Stinnera7368ac2017-11-15 18:11:45 -08002031
2032done:
Victor Stinner124b9eb2018-08-29 01:29:06 +02002033 _Py_ClearStandardStreamEncoding();
Nick Coghland6009512014-11-20 21:39:37 +10002034 Py_XDECREF(iomod);
Victor Stinnera7368ac2017-11-15 18:11:45 -08002035 return res;
Nick Coghland6009512014-11-20 21:39:37 +10002036}
2037
2038
Victor Stinner10dc4842015-03-24 12:01:30 +01002039static void
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002040_Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2041 PyThreadState *tstate)
Victor Stinner10dc4842015-03-24 12:01:30 +01002042{
Victor Stinner10dc4842015-03-24 12:01:30 +01002043 fputc('\n', stderr);
2044 fflush(stderr);
2045
2046 /* display the current Python stack */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002047 _Py_DumpTracebackThreads(fd, interp, tstate);
Victor Stinner10dc4842015-03-24 12:01:30 +01002048}
Victor Stinner791da1c2016-03-14 16:53:12 +01002049
2050/* Print the current exception (if an exception is set) with its traceback,
2051 or display the current Python stack.
2052
2053 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2054 called on catastrophic cases.
2055
2056 Return 1 if the traceback was displayed, 0 otherwise. */
2057
2058static int
Andy Lester75cd5bf2020-03-12 02:49:05 -05002059_Py_FatalError_PrintExc(PyThreadState *tstate)
Victor Stinner791da1c2016-03-14 16:53:12 +01002060{
2061 PyObject *ferr, *res;
2062 PyObject *exception, *v, *tb;
2063 int has_tb;
2064
Victor Stinnerb45d2592019-06-20 00:05:23 +02002065 _PyErr_Fetch(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002066 if (exception == NULL) {
2067 /* No current exception */
2068 return 0;
2069 }
2070
2071 ferr = _PySys_GetObjectId(&PyId_stderr);
2072 if (ferr == NULL || ferr == Py_None) {
2073 /* sys.stderr is not set yet or set to None,
2074 no need to try to display the exception */
2075 return 0;
2076 }
2077
Victor Stinnerb45d2592019-06-20 00:05:23 +02002078 _PyErr_NormalizeException(tstate, &exception, &v, &tb);
Victor Stinner791da1c2016-03-14 16:53:12 +01002079 if (tb == NULL) {
2080 tb = Py_None;
2081 Py_INCREF(tb);
2082 }
2083 PyException_SetTraceback(v, tb);
2084 if (exception == NULL) {
2085 /* PyErr_NormalizeException() failed */
2086 return 0;
2087 }
2088
2089 has_tb = (tb != Py_None);
2090 PyErr_Display(exception, v, tb);
2091 Py_XDECREF(exception);
2092 Py_XDECREF(v);
2093 Py_XDECREF(tb);
2094
2095 /* sys.stderr may be buffered: call sys.stderr.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002096 res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002097 if (res == NULL) {
2098 _PyErr_Clear(tstate);
2099 }
2100 else {
Victor Stinner791da1c2016-03-14 16:53:12 +01002101 Py_DECREF(res);
Victor Stinnerb45d2592019-06-20 00:05:23 +02002102 }
Victor Stinner791da1c2016-03-14 16:53:12 +01002103
2104 return has_tb;
2105}
2106
Nick Coghland6009512014-11-20 21:39:37 +10002107/* Print fatal error message and abort */
2108
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002109#ifdef MS_WINDOWS
2110static void
2111fatal_output_debug(const char *msg)
2112{
2113 /* buffer of 256 bytes allocated on the stack */
2114 WCHAR buffer[256 / sizeof(WCHAR)];
2115 size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2116 size_t msglen;
2117
2118 OutputDebugStringW(L"Fatal Python error: ");
2119
2120 msglen = strlen(msg);
2121 while (msglen) {
2122 size_t i;
2123
2124 if (buflen > msglen) {
2125 buflen = msglen;
2126 }
2127
2128 /* Convert the message to wchar_t. This uses a simple one-to-one
2129 conversion, assuming that the this error message actually uses
2130 ASCII only. If this ceases to be true, we will have to convert. */
2131 for (i=0; i < buflen; ++i) {
2132 buffer[i] = msg[i];
2133 }
2134 buffer[i] = L'\0';
2135 OutputDebugStringW(buffer);
2136
2137 msg += buflen;
2138 msglen -= buflen;
2139 }
2140 OutputDebugStringW(L"\n");
2141}
2142#endif
2143
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002144
2145static void
2146fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime)
2147{
2148 fprintf(stream, "Python runtime state: ");
Victor Stinner7b3c2522020-03-07 00:24:23 +01002149 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2150 if (finalizing) {
2151 fprintf(stream, "finalizing (tstate=%p)", finalizing);
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002152 }
2153 else if (runtime->initialized) {
2154 fprintf(stream, "initialized");
2155 }
2156 else if (runtime->core_initialized) {
2157 fprintf(stream, "core initialized");
2158 }
2159 else if (runtime->preinitialized) {
2160 fprintf(stream, "preinitialized");
2161 }
2162 else if (runtime->preinitializing) {
2163 fprintf(stream, "preinitializing");
2164 }
2165 else {
2166 fprintf(stream, "unknown");
2167 }
2168 fprintf(stream, "\n");
2169 fflush(stream);
2170}
2171
2172
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002173static inline void _Py_NO_RETURN
2174fatal_error_exit(int status)
Nick Coghland6009512014-11-20 21:39:37 +10002175{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002176 if (status < 0) {
2177#if defined(MS_WINDOWS) && defined(_DEBUG)
2178 DebugBreak();
2179#endif
2180 abort();
2181 }
2182 else {
2183 exit(status);
2184 }
2185}
2186
2187
2188static void _Py_NO_RETURN
2189fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2190 int status)
2191{
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002192 const int fd = fileno(stream);
Victor Stinner53345a42015-03-25 01:55:14 +01002193 static int reentrant = 0;
Victor Stinner53345a42015-03-25 01:55:14 +01002194
2195 if (reentrant) {
2196 /* Py_FatalError() caused a second fatal error.
2197 Example: flush_std_files() raises a recursion error. */
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002198 fatal_error_exit(status);
Victor Stinner53345a42015-03-25 01:55:14 +01002199 }
2200 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10002201
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002202 if (header) {
2203 fprintf(stream, "Fatal Python error: ");
2204 if (prefix) {
2205 fputs(prefix, stream);
2206 fputs(": ", stream);
2207 }
2208 if (msg) {
2209 fputs(msg, stream);
2210 }
2211 else {
2212 fprintf(stream, "<message not set>");
2213 }
2214 fputs("\n", stream);
2215 fflush(stream);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002216 }
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002217
2218 _PyRuntimeState *runtime = &_PyRuntime;
2219 fatal_error_dump_runtime(stream, runtime);
2220
2221 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2222 PyInterpreterState *interp = NULL;
2223 if (tstate != NULL) {
2224 interp = tstate->interp;
2225 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002226
Victor Stinner3a228ab2018-11-01 00:26:41 +01002227 /* Check if the current thread has a Python thread state
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002228 and holds the GIL.
Victor Stinner3a228ab2018-11-01 00:26:41 +01002229
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002230 tss_tstate is NULL if Py_FatalError() is called from a C thread which
2231 has no Python thread state.
2232
2233 tss_tstate != tstate if the current Python thread does not hold the GIL.
2234 */
2235 PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
2236 int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002237 if (has_tstate_and_gil) {
2238 /* If an exception is set, print the exception with its traceback */
Andy Lester75cd5bf2020-03-12 02:49:05 -05002239 if (!_Py_FatalError_PrintExc(tss_tstate)) {
Victor Stinner3a228ab2018-11-01 00:26:41 +01002240 /* No exception is set, or an exception is set without traceback */
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002241 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner3a228ab2018-11-01 00:26:41 +01002242 }
2243 }
2244 else {
Victor Stinner1ce16fb2019-09-18 01:35:33 +02002245 _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002246 }
Victor Stinner10dc4842015-03-24 12:01:30 +01002247
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002248 /* The main purpose of faulthandler is to display the traceback.
2249 This function already did its best to display a traceback.
2250 Disable faulthandler to prevent writing a second traceback
2251 on abort(). */
Victor Stinner2025d782016-03-16 23:19:15 +01002252 _PyFaulthandler_Fini();
2253
Victor Stinner791da1c2016-03-14 16:53:12 +01002254 /* Check if the current Python thread hold the GIL */
Victor Stinner3a228ab2018-11-01 00:26:41 +01002255 if (has_tstate_and_gil) {
Victor Stinner791da1c2016-03-14 16:53:12 +01002256 /* Flush sys.stdout and sys.stderr */
2257 flush_std_files();
2258 }
Victor Stinnere0deff32015-03-24 13:46:18 +01002259
Nick Coghland6009512014-11-20 21:39:37 +10002260#ifdef MS_WINDOWS
Victor Stinner8d5a3aa2017-10-04 09:50:12 -07002261 fatal_output_debug(msg);
Victor Stinner53345a42015-03-25 01:55:14 +01002262#endif /* MS_WINDOWS */
2263
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002264 fatal_error_exit(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002265}
2266
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002267
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002268#undef Py_FatalError
2269
Victor Stinner19760862017-12-20 01:41:59 +01002270void _Py_NO_RETURN
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002271Py_FatalError(const char *msg)
2272{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002273 fatal_error(stderr, 1, NULL, msg, -1);
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002274}
2275
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002276
Victor Stinner19760862017-12-20 01:41:59 +01002277void _Py_NO_RETURN
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002278_Py_FatalErrorFunc(const char *func, const char *msg)
2279{
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002280 fatal_error(stderr, 1, func, msg, -1);
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002281}
2282
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002283
2284void _Py_NO_RETURN
2285_Py_FatalErrorFormat(const char *func, const char *format, ...)
2286{
2287 static int reentrant = 0;
2288 if (reentrant) {
2289 /* _Py_FatalErrorFormat() caused a second fatal error */
2290 fatal_error_exit(-1);
2291 }
2292 reentrant = 1;
2293
2294 FILE *stream = stderr;
2295 fprintf(stream, "Fatal Python error: ");
2296 if (func) {
2297 fputs(func, stream);
2298 fputs(": ", stream);
2299 }
2300 fflush(stream);
2301
2302 va_list vargs;
2303#ifdef HAVE_STDARG_PROTOTYPES
2304 va_start(vargs, format);
2305#else
2306 va_start(vargs);
2307#endif
2308 vfprintf(stream, format, vargs);
2309 va_end(vargs);
2310
2311 fputs("\n", stream);
2312 fflush(stream);
2313
2314 fatal_error(stream, 0, NULL, NULL, -1);
2315}
2316
2317
Victor Stinner9e5d30c2020-03-07 00:54:20 +01002318void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +02002319Py_ExitStatusException(PyStatus status)
Victor Stinnerf7e5b562017-11-15 15:48:08 -08002320{
Victor Stinner331a6a52019-05-27 16:39:22 +02002321 if (_PyStatus_IS_EXIT(status)) {
2322 exit(status.exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +02002323 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002324 else if (_PyStatus_IS_ERROR(status)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01002325 fatal_error(stderr, 1, status.func, status.err_msg, 1);
Victor Stinnerdfe88472019-03-01 12:14:41 +01002326 }
2327 else {
Victor Stinner331a6a52019-05-27 16:39:22 +02002328 Py_FatalError("Py_ExitStatusException() must not be called on success");
Victor Stinnerdfe88472019-03-01 12:14:41 +01002329 }
Nick Coghland6009512014-11-20 21:39:37 +10002330}
2331
2332/* Clean up and exit */
2333
Nick Coghland6009512014-11-20 21:39:37 +10002334/* For the atexit module. */
Marcel Plch776407f2017-12-20 11:17:58 +01002335void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
Nick Coghland6009512014-11-20 21:39:37 +10002336{
Victor Stinner81a7be32020-04-14 15:14:01 +02002337 PyInterpreterState *is = _PyInterpreterState_GET();
Marcel Plch776407f2017-12-20 11:17:58 +01002338
Antoine Pitroufc5db952017-12-13 02:29:07 +01002339 /* Guard against API misuse (see bpo-17852) */
Marcel Plch776407f2017-12-20 11:17:58 +01002340 assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
2341
2342 is->pyexitfunc = func;
2343 is->pyexitmodule = module;
Nick Coghland6009512014-11-20 21:39:37 +10002344}
2345
2346static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002347call_py_exitfuncs(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002348{
Victor Stinnerb45d2592019-06-20 00:05:23 +02002349 PyInterpreterState *interp = tstate->interp;
2350 if (interp->pyexitfunc == NULL)
Nick Coghland6009512014-11-20 21:39:37 +10002351 return;
2352
Victor Stinnerb45d2592019-06-20 00:05:23 +02002353 (*interp->pyexitfunc)(interp->pyexitmodule);
2354 _PyErr_Clear(tstate);
Nick Coghland6009512014-11-20 21:39:37 +10002355}
2356
2357/* Wait until threading._shutdown completes, provided
2358 the threading module was imported in the first place.
2359 The shutdown routine will wait until all non-daemon
2360 "threading" threads have completed. */
2361static void
Victor Stinnerb45d2592019-06-20 00:05:23 +02002362wait_for_thread_shutdown(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002363{
Nick Coghland6009512014-11-20 21:39:37 +10002364 _Py_IDENTIFIER(_shutdown);
2365 PyObject *result;
Eric Snow3f9eee62017-09-15 16:35:20 -06002366 PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
Nick Coghland6009512014-11-20 21:39:37 +10002367 if (threading == NULL) {
Victor Stinnerb45d2592019-06-20 00:05:23 +02002368 if (_PyErr_Occurred(tstate)) {
Stefan Krah027b09c2019-03-25 21:50:58 +01002369 PyErr_WriteUnraisable(NULL);
2370 }
2371 /* else: threading not imported */
Nick Coghland6009512014-11-20 21:39:37 +10002372 return;
2373 }
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02002374 result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
Nick Coghland6009512014-11-20 21:39:37 +10002375 if (result == NULL) {
2376 PyErr_WriteUnraisable(threading);
2377 }
2378 else {
2379 Py_DECREF(result);
2380 }
2381 Py_DECREF(threading);
Nick Coghland6009512014-11-20 21:39:37 +10002382}
2383
2384#define NEXITFUNCS 32
Nick Coghland6009512014-11-20 21:39:37 +10002385int Py_AtExit(void (*func)(void))
2386{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002387 if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
Nick Coghland6009512014-11-20 21:39:37 +10002388 return -1;
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002389 _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
Nick Coghland6009512014-11-20 21:39:37 +10002390 return 0;
2391}
2392
2393static void
Victor Stinner8e91c242019-04-24 17:24:01 +02002394call_ll_exitfuncs(_PyRuntimeState *runtime)
Nick Coghland6009512014-11-20 21:39:37 +10002395{
Victor Stinner8e91c242019-04-24 17:24:01 +02002396 while (runtime->nexitfuncs > 0) {
Victor Stinner87d23a02019-04-26 05:49:26 +02002397 /* pop last function from the list */
2398 runtime->nexitfuncs--;
2399 void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2400 runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2401
2402 exitfunc();
Victor Stinner8e91c242019-04-24 17:24:01 +02002403 }
Nick Coghland6009512014-11-20 21:39:37 +10002404
2405 fflush(stdout);
2406 fflush(stderr);
2407}
2408
Victor Stinnercfc88312018-08-01 16:41:25 +02002409void _Py_NO_RETURN
Nick Coghland6009512014-11-20 21:39:37 +10002410Py_Exit(int sts)
2411{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00002412 if (Py_FinalizeEx() < 0) {
2413 sts = 120;
2414 }
Nick Coghland6009512014-11-20 21:39:37 +10002415
2416 exit(sts);
2417}
2418
Victor Stinner331a6a52019-05-27 16:39:22 +02002419static PyStatus
Victor Stinnerb45d2592019-06-20 00:05:23 +02002420init_signals(PyThreadState *tstate)
Nick Coghland6009512014-11-20 21:39:37 +10002421{
2422#ifdef SIGPIPE
2423 PyOS_setsig(SIGPIPE, SIG_IGN);
2424#endif
2425#ifdef SIGXFZ
2426 PyOS_setsig(SIGXFZ, SIG_IGN);
2427#endif
2428#ifdef SIGXFSZ
2429 PyOS_setsig(SIGXFSZ, SIG_IGN);
2430#endif
Victor Stinner400e1db2020-03-31 19:13:10 +02002431 PyOS_InitInterrupts(); /* May imply init_signals() */
Victor Stinnerb45d2592019-06-20 00:05:23 +02002432 if (_PyErr_Occurred(tstate)) {
Victor Stinner331a6a52019-05-27 16:39:22 +02002433 return _PyStatus_ERR("can't import signal");
Nick Coghland6009512014-11-20 21:39:37 +10002434 }
Victor Stinner331a6a52019-05-27 16:39:22 +02002435 return _PyStatus_OK();
Nick Coghland6009512014-11-20 21:39:37 +10002436}
2437
2438
2439/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2440 *
2441 * All of the code in this function must only use async-signal-safe functions,
2442 * listed at `man 7 signal` or
2443 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
Victor Stinnerefc28bb2020-03-05 18:13:56 +01002444 *
2445 * If this function is updated, update also _posix_spawn() of subprocess.py.
Nick Coghland6009512014-11-20 21:39:37 +10002446 */
2447void
2448_Py_RestoreSignals(void)
2449{
2450#ifdef SIGPIPE
2451 PyOS_setsig(SIGPIPE, SIG_DFL);
2452#endif
2453#ifdef SIGXFZ
2454 PyOS_setsig(SIGXFZ, SIG_DFL);
2455#endif
2456#ifdef SIGXFSZ
2457 PyOS_setsig(SIGXFSZ, SIG_DFL);
2458#endif
2459}
2460
2461
2462/*
2463 * The file descriptor fd is considered ``interactive'' if either
2464 * a) isatty(fd) is TRUE, or
2465 * b) the -i flag was given, and the filename associated with
2466 * the descriptor is NULL or "<stdin>" or "???".
2467 */
2468int
2469Py_FdIsInteractive(FILE *fp, const char *filename)
2470{
2471 if (isatty((int)fileno(fp)))
2472 return 1;
2473 if (!Py_InteractiveFlag)
2474 return 0;
2475 return (filename == NULL) ||
2476 (strcmp(filename, "<stdin>") == 0) ||
2477 (strcmp(filename, "???") == 0);
2478}
2479
2480
Nick Coghland6009512014-11-20 21:39:37 +10002481/* Wrappers around sigaction() or signal(). */
2482
2483PyOS_sighandler_t
2484PyOS_getsig(int sig)
2485{
2486#ifdef HAVE_SIGACTION
2487 struct sigaction context;
2488 if (sigaction(sig, NULL, &context) == -1)
2489 return SIG_ERR;
2490 return context.sa_handler;
2491#else
2492 PyOS_sighandler_t handler;
2493/* Special signal handling for the secure CRT in Visual Studio 2005 */
2494#if defined(_MSC_VER) && _MSC_VER >= 1400
2495 switch (sig) {
2496 /* Only these signals are valid */
2497 case SIGINT:
2498 case SIGILL:
2499 case SIGFPE:
2500 case SIGSEGV:
2501 case SIGTERM:
2502 case SIGBREAK:
2503 case SIGABRT:
2504 break;
2505 /* Don't call signal() with other values or it will assert */
2506 default:
2507 return SIG_ERR;
2508 }
2509#endif /* _MSC_VER && _MSC_VER >= 1400 */
2510 handler = signal(sig, SIG_IGN);
2511 if (handler != SIG_ERR)
2512 signal(sig, handler);
2513 return handler;
2514#endif
2515}
2516
2517/*
2518 * All of the code in this function must only use async-signal-safe functions,
2519 * listed at `man 7 signal` or
2520 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2521 */
2522PyOS_sighandler_t
2523PyOS_setsig(int sig, PyOS_sighandler_t handler)
2524{
2525#ifdef HAVE_SIGACTION
2526 /* Some code in Modules/signalmodule.c depends on sigaction() being
2527 * used here if HAVE_SIGACTION is defined. Fix that if this code
2528 * changes to invalidate that assumption.
2529 */
2530 struct sigaction context, ocontext;
2531 context.sa_handler = handler;
2532 sigemptyset(&context.sa_mask);
2533 context.sa_flags = 0;
2534 if (sigaction(sig, &context, &ocontext) == -1)
2535 return SIG_ERR;
2536 return ocontext.sa_handler;
2537#else
2538 PyOS_sighandler_t oldhandler;
2539 oldhandler = signal(sig, handler);
2540#ifdef HAVE_SIGINTERRUPT
2541 siginterrupt(sig, 1);
2542#endif
2543 return oldhandler;
2544#endif
2545}
2546
2547#ifdef __cplusplus
2548}
2549#endif