Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1 | /* Python interpreter top-level routines, including init/exit */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 5 | #include "pycore_ceval.h" // _PyEval_FiniGIL() |
| 6 | #include "pycore_context.h" // _PyContext_Init() |
| 7 | #include "pycore_fileutils.h" // _Py_ResetForceASCII() |
Victor Stinner | 6223071 | 2020-11-18 23:18:29 +0100 | [diff] [blame] | 8 | #include "pycore_import.h" // _PyImport_BootstrapImp() |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 9 | #include "pycore_initconfig.h" // _PyStatus_OK() |
| 10 | #include "pycore_object.h" // _PyDebug_PrintTotalRefs() |
| 11 | #include "pycore_pathconfig.h" // _PyConfig_WritePathConfig() |
| 12 | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
| 13 | #include "pycore_pylifecycle.h" // _PyErr_Print() |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 14 | #include "pycore_pystate.h" // _PyThreadState_GET() |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 15 | #include "pycore_sysmodule.h" // _PySys_ClearAuditHooks() |
| 16 | #include "pycore_traceback.h" // _Py_DumpTracebackThreads() |
| 17 | |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 18 | #include <locale.h> // setlocale() |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 19 | |
Miss Islington (bot) | ebba286 | 2021-07-29 08:20:58 -0700 | [diff] [blame] | 20 | #if defined(__APPLE__) |
| 21 | #include <mach-o/loader.h> |
| 22 | #endif |
| 23 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 24 | #ifdef HAVE_SIGNAL_H |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 25 | # include <signal.h> // SIG_IGN |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #ifdef HAVE_LANGINFO_H |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 29 | # include <langinfo.h> // nl_langinfo(CODESET) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #ifdef MS_WINDOWS |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 33 | # undef BYTE |
| 34 | # include "windows.h" |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 35 | |
Victor Stinner | 4f98f46 | 2020-04-15 04:01:58 +0200 | [diff] [blame] | 36 | extern PyTypeObject PyWindowsConsoleIO_Type; |
| 37 | # define PyWindowsConsoleIO_Check(op) \ |
| 38 | (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type)) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 39 | #endif |
| 40 | |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 41 | #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str)) |
| 42 | |
| 43 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 44 | _Py_IDENTIFIER(flush); |
| 45 | _Py_IDENTIFIER(name); |
| 46 | _Py_IDENTIFIER(stdin); |
| 47 | _Py_IDENTIFIER(stdout); |
| 48 | _Py_IDENTIFIER(stderr); |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 49 | _Py_IDENTIFIER(threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 50 | |
| 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 55 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 56 | /* Forward declarations */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 57 | static PyStatus add_main_module(PyInterpreterState *interp); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 58 | static PyStatus init_import_site(void); |
Andy Lester | 75cd5bf | 2020-03-12 02:49:05 -0500 | [diff] [blame] | 59 | static PyStatus init_set_builtins_open(void); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 60 | static PyStatus init_sys_streams(PyThreadState *tstate); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 61 | static void wait_for_thread_shutdown(PyThreadState *tstate); |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 62 | static void call_ll_exitfuncs(_PyRuntimeState *runtime); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 63 | |
Gregory P. Smith | 38f11cc | 2019-02-16 12:57:40 -0800 | [diff] [blame] | 64 | int _Py_UnhandledKeyboardInterrupt = 0; |
Miss Islington (bot) | ebba286 | 2021-07-29 08:20:58 -0700 | [diff] [blame] | 65 | |
| 66 | /* The following places the `_PyRuntime` structure in a location that can be |
| 67 | * found without any external information. This is meant to ease access to the |
| 68 | * interpreter state for various runtime debugging tools, but is *not* an |
| 69 | * officially supported feature */ |
| 70 | |
| 71 | #if defined(MS_WINDOWS) |
| 72 | |
| 73 | #pragma section("PyRuntime", read, write) |
| 74 | __declspec(allocate("PyRuntime")) |
| 75 | |
| 76 | #elif defined(__APPLE__) |
| 77 | |
| 78 | __attribute__(( |
| 79 | section(SEG_DATA ",PyRuntime") |
| 80 | )) |
| 81 | |
| 82 | #endif |
| 83 | |
| 84 | _PyRuntimeState _PyRuntime |
| 85 | #if defined(__linux__) && (defined(__GNUC__) || defined(__clang__)) |
| 86 | __attribute__ ((section (".PyRuntime"))) |
| 87 | #endif |
| 88 | = _PyRuntimeState_INIT; |
Victor Stinner | fd23cfa | 2019-03-20 00:03:01 +0100 | [diff] [blame] | 89 | static int runtime_initialized = 0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 90 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 91 | PyStatus |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 92 | _PyRuntime_Initialize(void) |
| 93 | { |
| 94 | /* XXX We only initialize once in the process, which aligns with |
| 95 | the static initialization of the former globals now found in |
| 96 | _PyRuntime. However, _PyRuntime *should* be initialized with |
| 97 | every Py_Initialize() call, but doing so breaks the runtime. |
| 98 | This is because the runtime state is not properly finalized |
| 99 | currently. */ |
Victor Stinner | fd23cfa | 2019-03-20 00:03:01 +0100 | [diff] [blame] | 100 | if (runtime_initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 101 | return _PyStatus_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 102 | } |
Victor Stinner | fd23cfa | 2019-03-20 00:03:01 +0100 | [diff] [blame] | 103 | runtime_initialized = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 104 | |
| 105 | return _PyRuntimeState_Init(&_PyRuntime); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | _PyRuntime_Finalize(void) |
| 110 | { |
| 111 | _PyRuntimeState_Fini(&_PyRuntime); |
Victor Stinner | fd23cfa | 2019-03-20 00:03:01 +0100 | [diff] [blame] | 112 | runtime_initialized = 0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int |
| 116 | _Py_IsFinalizing(void) |
| 117 | { |
Victor Stinner | 7b3c252 | 2020-03-07 00:24:23 +0100 | [diff] [blame] | 118 | return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 121 | /* Hack to force loading of object files */ |
| 122 | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ |
| 123 | PyOS_mystrnicmp; /* Python/pystrcmp.o */ |
| 124 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 125 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 126 | /* APIs to access the initialization flags |
| 127 | * |
| 128 | * Can be called prior to Py_Initialize. |
| 129 | */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 130 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 131 | int |
| 132 | _Py_IsCoreInitialized(void) |
| 133 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 134 | return _PyRuntime.core_initialized; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 135 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 136 | |
| 137 | int |
| 138 | Py_IsInitialized(void) |
| 139 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 140 | return _PyRuntime.initialized; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 141 | } |
| 142 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 143 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 144 | /* Global initializations. Can be undone by Py_FinalizeEx(). Don't |
| 145 | call this twice without an intervening Py_FinalizeEx() call. When |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 146 | initializations fail, a fatal error is issued and the function does |
| 147 | not return. On return, the first thread and interpreter state have |
| 148 | been created. |
| 149 | |
| 150 | Locking: you must hold the interpreter lock while calling this. |
| 151 | (If the lock has not yet been initialized, that's equivalent to |
| 152 | having the lock, but you cannot use multiple threads.) |
| 153 | |
| 154 | */ |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 155 | static int |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 156 | init_importlib(PyThreadState *tstate, PyObject *sysmod) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 157 | { |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 158 | assert(!_PyErr_Occurred(tstate)); |
| 159 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 160 | PyInterpreterState *interp = tstate->interp; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 161 | int verbose = _PyInterpreterState_GetConfig(interp)->verbose; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 162 | |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 163 | // Import _importlib through its frozen version, _frozen_importlib. |
| 164 | if (verbose) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 165 | PySys_FormatStderr("import _frozen_importlib # frozen\n"); |
| 166 | } |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 167 | if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { |
| 168 | return -1; |
| 169 | } |
| 170 | PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 171 | if (importlib == NULL) { |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 172 | return -1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 173 | } |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 174 | interp->importlib = Py_NewRef(importlib); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 175 | |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 176 | // Import the _imp module |
| 177 | if (verbose) { |
Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 178 | PySys_FormatStderr("import _imp # builtin\n"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 179 | } |
Victor Stinner | 6223071 | 2020-11-18 23:18:29 +0100 | [diff] [blame] | 180 | PyObject *imp_mod = _PyImport_BootstrapImp(tstate); |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 181 | if (imp_mod == NULL) { |
| 182 | return -1; |
| 183 | } |
| 184 | if (_PyImport_SetModuleString("_imp", imp_mod) < 0) { |
| 185 | Py_DECREF(imp_mod); |
| 186 | return -1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 187 | } |
| 188 | |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 189 | // Install importlib as the implementation of import |
| 190 | PyObject *value = PyObject_CallMethod(importlib, "_install", |
| 191 | "OO", sysmod, imp_mod); |
| 192 | Py_DECREF(imp_mod); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 193 | if (value == NULL) { |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 194 | return -1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 195 | } |
| 196 | Py_DECREF(value); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 197 | |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 198 | assert(!_PyErr_Occurred(tstate)); |
| 199 | return 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 200 | } |
| 201 | |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 202 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 203 | static PyStatus |
Victor Stinner | 0a28f8d | 2019-06-19 02:54:39 +0200 | [diff] [blame] | 204 | init_importlib_external(PyThreadState *tstate) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 205 | { |
| 206 | PyObject *value; |
Victor Stinner | 0a28f8d | 2019-06-19 02:54:39 +0200 | [diff] [blame] | 207 | value = PyObject_CallMethod(tstate->interp->importlib, |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 208 | "_install_external_importers", ""); |
| 209 | if (value == NULL) { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 210 | _PyErr_Print(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 211 | return _PyStatus_ERR("external importer setup failed"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 212 | } |
Stéphane Wirtel | ab1cb80 | 2017-06-08 13:13:20 +0200 | [diff] [blame] | 213 | Py_DECREF(value); |
Victor Stinner | 0a28f8d | 2019-06-19 02:54:39 +0200 | [diff] [blame] | 214 | return _PyImportZip_Init(tstate); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 215 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 216 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 217 | /* Helper functions to better handle the legacy C locale |
| 218 | * |
| 219 | * The legacy C locale assumes ASCII as the default text encoding, which |
| 220 | * causes problems not only for the CPython runtime, but also other |
| 221 | * components like GNU readline. |
| 222 | * |
| 223 | * Accordingly, when the CLI detects it, it attempts to coerce it to a |
| 224 | * more capable UTF-8 based alternative as follows: |
| 225 | * |
| 226 | * if (_Py_LegacyLocaleDetected()) { |
| 227 | * _Py_CoerceLegacyLocale(); |
| 228 | * } |
| 229 | * |
| 230 | * See the documentation of the PYTHONCOERCECLOCALE setting for more details. |
| 231 | * |
| 232 | * Locale coercion also impacts the default error handler for the standard |
| 233 | * streams: while the usual default is "strict", the default for the legacy |
| 234 | * C locale and for any of the coercion target locales is "surrogateescape". |
| 235 | */ |
| 236 | |
| 237 | int |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 238 | _Py_LegacyLocaleDetected(int warn) |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 239 | { |
| 240 | #ifndef MS_WINDOWS |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 241 | if (!warn) { |
| 242 | const char *locale_override = getenv("LC_ALL"); |
| 243 | if (locale_override != NULL && *locale_override != '\0') { |
| 244 | /* Don't coerce C locale if the LC_ALL environment variable |
| 245 | is set */ |
| 246 | return 0; |
| 247 | } |
| 248 | } |
| 249 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 250 | /* On non-Windows systems, the C locale is considered a legacy locale */ |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 251 | /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat |
| 252 | * the POSIX locale as a simple alias for the C locale, so |
| 253 | * we may also want to check for that explicitly. |
| 254 | */ |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 255 | const char *ctype_loc = setlocale(LC_CTYPE, NULL); |
| 256 | return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0; |
| 257 | #else |
| 258 | /* Windows uses code pages instead of locales, so no locale is legacy */ |
| 259 | return 0; |
| 260 | #endif |
| 261 | } |
| 262 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 263 | #ifndef MS_WINDOWS |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 264 | static const char *_C_LOCALE_WARNING = |
| 265 | "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII " |
| 266 | "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, " |
| 267 | "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible " |
| 268 | "locales is recommended.\n"; |
| 269 | |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 270 | static void |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 271 | emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime) |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 272 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 273 | const PyPreConfig *preconfig = &runtime->preconfig; |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 274 | if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) { |
Victor Stinner | cf21504 | 2018-08-29 22:56:06 +0200 | [diff] [blame] | 275 | PySys_FormatStderr("%s", _C_LOCALE_WARNING); |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 276 | } |
| 277 | } |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 278 | #endif /* !defined(MS_WINDOWS) */ |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 279 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 280 | typedef struct _CandidateLocale { |
| 281 | const char *locale_name; /* The locale to try as a coercion target */ |
| 282 | } _LocaleCoercionTarget; |
| 283 | |
| 284 | static _LocaleCoercionTarget _TARGET_LOCALES[] = { |
| 285 | {"C.UTF-8"}, |
| 286 | {"C.utf8"}, |
Nick Coghlan | 18974c3 | 2017-06-30 00:48:14 +1000 | [diff] [blame] | 287 | {"UTF-8"}, |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 288 | {NULL} |
| 289 | }; |
| 290 | |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 291 | |
| 292 | int |
| 293 | _Py_IsLocaleCoercionTarget(const char *ctype_loc) |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 294 | { |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 295 | const _LocaleCoercionTarget *target = NULL; |
| 296 | for (target = _TARGET_LOCALES; target->locale_name; target++) { |
| 297 | if (strcmp(ctype_loc, target->locale_name) == 0) { |
| 298 | return 1; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 299 | } |
Victor Stinner | 124b9eb | 2018-08-29 01:29:06 +0200 | [diff] [blame] | 300 | } |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 301 | return 0; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 302 | } |
| 303 | |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 304 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 305 | #ifdef PY_COERCE_C_LOCALE |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 306 | static const char C_LOCALE_COERCION_WARNING[] = |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 307 | "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " |
| 308 | "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; |
| 309 | |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 310 | static int |
Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 311 | _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target) |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 312 | { |
| 313 | const char *newloc = target->locale_name; |
| 314 | |
| 315 | /* Reset locale back to currently configured defaults */ |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 316 | _Py_SetLocaleFromEnv(LC_ALL); |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 317 | |
| 318 | /* Set the relevant locale environment variable */ |
| 319 | if (setenv("LC_CTYPE", newloc, 1)) { |
| 320 | fprintf(stderr, |
| 321 | "Error setting LC_CTYPE, skipping C locale coercion\n"); |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 322 | return 0; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 323 | } |
Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 324 | if (warn) { |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 325 | fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc); |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 326 | } |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 327 | |
| 328 | /* Reconfigure with the overridden environment variables */ |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 329 | _Py_SetLocaleFromEnv(LC_ALL); |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 330 | return 1; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 331 | } |
| 332 | #endif |
| 333 | |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 334 | int |
Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 335 | _Py_CoerceLegacyLocale(int warn) |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 336 | { |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 337 | int coerced = 0; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 338 | #ifdef PY_COERCE_C_LOCALE |
Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 339 | char *oldloc = NULL; |
| 340 | |
| 341 | oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL)); |
| 342 | if (oldloc == NULL) { |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 343 | return coerced; |
Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 346 | const char *locale_override = getenv("LC_ALL"); |
| 347 | if (locale_override == NULL || *locale_override == '\0') { |
| 348 | /* LC_ALL is also not set (or is set to an empty string) */ |
| 349 | const _LocaleCoercionTarget *target = NULL; |
| 350 | for (target = _TARGET_LOCALES; target->locale_name; target++) { |
| 351 | const char *new_locale = setlocale(LC_CTYPE, |
| 352 | target->locale_name); |
| 353 | if (new_locale != NULL) { |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 354 | #if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 355 | /* Also ensure that nl_langinfo works in this locale */ |
| 356 | char *codeset = nl_langinfo(CODESET); |
| 357 | if (!codeset || *codeset == '\0') { |
| 358 | /* CODESET is not set or empty, so skip coercion */ |
| 359 | new_locale = NULL; |
| 360 | _Py_SetLocaleFromEnv(LC_CTYPE); |
| 361 | continue; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 362 | } |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 363 | #endif |
| 364 | /* Successfully configured locale, so make it the default */ |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 365 | coerced = _coerce_default_locale_settings(warn, target); |
Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 366 | goto done; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | /* No C locale warning here, as Py_Initialize will emit one later */ |
Victor Stinner | 8ea0911 | 2018-09-03 17:05:18 +0200 | [diff] [blame] | 371 | |
| 372 | setlocale(LC_CTYPE, oldloc); |
| 373 | |
| 374 | done: |
| 375 | PyMem_RawFree(oldloc); |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 376 | #endif |
Victor Stinner | 0f72147 | 2019-05-20 17:16:38 +0200 | [diff] [blame] | 377 | return coerced; |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 378 | } |
| 379 | |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 380 | /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to |
| 381 | * isolate the idiosyncrasies of different libc implementations. It reads the |
| 382 | * appropriate environment variable and uses its value to select the locale for |
| 383 | * 'category'. */ |
| 384 | char * |
| 385 | _Py_SetLocaleFromEnv(int category) |
| 386 | { |
Victor Stinner | 353933e | 2018-11-23 13:08:26 +0100 | [diff] [blame] | 387 | char *res; |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 388 | #ifdef __ANDROID__ |
| 389 | const char *locale; |
| 390 | const char **pvar; |
| 391 | #ifdef PY_COERCE_C_LOCALE |
| 392 | const char *coerce_c_locale; |
| 393 | #endif |
| 394 | const char *utf8_locale = "C.UTF-8"; |
| 395 | const char *env_var_set[] = { |
| 396 | "LC_ALL", |
| 397 | "LC_CTYPE", |
| 398 | "LANG", |
| 399 | NULL, |
| 400 | }; |
| 401 | |
| 402 | /* Android setlocale(category, "") doesn't check the environment variables |
| 403 | * and incorrectly sets the "C" locale at API 24 and older APIs. We only |
| 404 | * check the environment variables listed in env_var_set. */ |
| 405 | for (pvar=env_var_set; *pvar; pvar++) { |
| 406 | locale = getenv(*pvar); |
| 407 | if (locale != NULL && *locale != '\0') { |
| 408 | if (strcmp(locale, utf8_locale) == 0 || |
| 409 | strcmp(locale, "en_US.UTF-8") == 0) { |
| 410 | return setlocale(category, utf8_locale); |
| 411 | } |
| 412 | return setlocale(category, "C"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of |
| 417 | * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string. |
| 418 | * Quote from POSIX section "8.2 Internationalization Variables": |
| 419 | * "4. If the LANG environment variable is not set or is set to the empty |
| 420 | * string, the implementation-defined default locale shall be used." */ |
| 421 | |
| 422 | #ifdef PY_COERCE_C_LOCALE |
| 423 | coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); |
| 424 | if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) { |
| 425 | /* Some other ported code may check the environment variables (e.g. in |
| 426 | * extension modules), so we make sure that they match the locale |
| 427 | * configuration */ |
| 428 | if (setenv("LC_CTYPE", utf8_locale, 1)) { |
| 429 | fprintf(stderr, "Warning: failed setting the LC_CTYPE " |
| 430 | "environment variable to %s\n", utf8_locale); |
| 431 | } |
| 432 | } |
| 433 | #endif |
Victor Stinner | 353933e | 2018-11-23 13:08:26 +0100 | [diff] [blame] | 434 | res = setlocale(category, utf8_locale); |
| 435 | #else /* !defined(__ANDROID__) */ |
| 436 | res = setlocale(category, ""); |
| 437 | #endif |
| 438 | _Py_ResetForceASCII(); |
| 439 | return res; |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 442 | |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 443 | static int |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 444 | interpreter_update_config(PyThreadState *tstate, int only_update_path_config) |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 445 | { |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 446 | const PyConfig *config = &tstate->interp->config; |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 447 | |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 448 | if (!only_update_path_config) { |
| 449 | PyStatus status = _PyConfig_Write(config, tstate->interp->runtime); |
| 450 | if (_PyStatus_EXCEPTION(status)) { |
| 451 | _PyErr_SetFromPyStatus(status); |
| 452 | return -1; |
| 453 | } |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Victor Stinner | 101bf69 | 2021-02-19 13:33:31 +0100 | [diff] [blame] | 456 | if (_Py_IsMainInterpreter(tstate->interp)) { |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 457 | PyStatus status = _PyConfig_WritePathConfig(config); |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 458 | if (_PyStatus_EXCEPTION(status)) { |
| 459 | _PyErr_SetFromPyStatus(status); |
| 460 | return -1; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Update the sys module for the new configuration |
| 465 | if (_PySys_UpdateConfig(tstate) < 0) { |
| 466 | return -1; |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | int |
| 473 | _PyInterpreterState_SetConfig(const PyConfig *src_config) |
| 474 | { |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 475 | PyThreadState *tstate = PyThreadState_Get(); |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 476 | int res = -1; |
| 477 | |
| 478 | PyConfig config; |
| 479 | PyConfig_InitPythonConfig(&config); |
| 480 | PyStatus status = _PyConfig_Copy(&config, src_config); |
| 481 | if (_PyStatus_EXCEPTION(status)) { |
| 482 | _PyErr_SetFromPyStatus(status); |
| 483 | goto done; |
| 484 | } |
| 485 | |
| 486 | status = PyConfig_Read(&config); |
| 487 | if (_PyStatus_EXCEPTION(status)) { |
| 488 | _PyErr_SetFromPyStatus(status); |
| 489 | goto done; |
| 490 | } |
| 491 | |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 492 | status = _PyConfig_Copy(&tstate->interp->config, &config); |
| 493 | if (_PyStatus_EXCEPTION(status)) { |
| 494 | _PyErr_SetFromPyStatus(status); |
| 495 | goto done; |
| 496 | } |
| 497 | |
| 498 | res = interpreter_update_config(tstate, 0); |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 499 | |
| 500 | done: |
| 501 | PyConfig_Clear(&config); |
| 502 | return res; |
| 503 | } |
| 504 | |
| 505 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 506 | /* Global initializations. Can be undone by Py_Finalize(). Don't |
| 507 | call this twice without an intervening Py_Finalize() call. |
| 508 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 509 | Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 510 | must have a corresponding call to Py_Finalize. |
| 511 | |
| 512 | Locking: you must hold the interpreter lock while calling these APIs. |
| 513 | (If the lock has not yet been initialized, that's equivalent to |
| 514 | having the lock, but you cannot use multiple threads.) |
| 515 | |
| 516 | */ |
| 517 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 518 | static PyStatus |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 519 | pyinit_core_reconfigure(_PyRuntimeState *runtime, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 520 | PyThreadState **tstate_p, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 521 | const PyConfig *config) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 522 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 523 | PyStatus status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 524 | PyThreadState *tstate = _PyThreadState_GET(); |
| 525 | if (!tstate) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 526 | return _PyStatus_ERR("failed to read thread state"); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 527 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 528 | *tstate_p = tstate; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 529 | |
| 530 | PyInterpreterState *interp = tstate->interp; |
| 531 | if (interp == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 532 | return _PyStatus_ERR("can't make main interpreter"); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 533 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 534 | |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 535 | status = _PyConfig_Write(config, runtime); |
| 536 | if (_PyStatus_EXCEPTION(status)) { |
| 537 | return status; |
| 538 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 539 | |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 540 | status = _PyConfig_Copy(&interp->config, config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 541 | if (_PyStatus_EXCEPTION(status)) { |
| 542 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 543 | } |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 544 | config = _PyInterpreterState_GetConfig(interp); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 545 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 546 | if (config->_install_importlib) { |
Victor Stinner | 12f2f17 | 2019-09-26 15:51:50 +0200 | [diff] [blame] | 547 | status = _PyConfig_WritePathConfig(config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 548 | if (_PyStatus_EXCEPTION(status)) { |
| 549 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 550 | } |
| 551 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 552 | return _PyStatus_OK(); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 556 | static PyStatus |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 557 | pycore_init_runtime(_PyRuntimeState *runtime, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 558 | const PyConfig *config) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 559 | { |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 560 | if (runtime->initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 561 | return _PyStatus_ERR("main interpreter already initialized"); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 562 | } |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 563 | |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 564 | PyStatus status = _PyConfig_Write(config, runtime); |
| 565 | if (_PyStatus_EXCEPTION(status)) { |
| 566 | return status; |
| 567 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 568 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 569 | /* Py_Finalize leaves _Py_Finalizing set in order to help daemon |
| 570 | * threads behave a little more gracefully at interpreter shutdown. |
| 571 | * We clobber it here so the new interpreter can start with a clean |
| 572 | * slate. |
| 573 | * |
| 574 | * However, this may still lead to misbehaviour if there are daemon |
| 575 | * threads still hanging around from a previous Py_Initialize/Finalize |
| 576 | * pair :( |
| 577 | */ |
Victor Stinner | 7b3c252 | 2020-03-07 00:24:23 +0100 | [diff] [blame] | 578 | _PyRuntimeState_SetFinalizing(runtime, NULL); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 579 | |
Victor Stinner | e81f6e6 | 2020-06-08 18:12:59 +0200 | [diff] [blame] | 580 | status = _Py_HashRandomization_Init(config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 581 | if (_PyStatus_EXCEPTION(status)) { |
| 582 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 585 | status = _PyInterpreterState_Enable(runtime); |
| 586 | if (_PyStatus_EXCEPTION(status)) { |
| 587 | return status; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 588 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 589 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 590 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 591 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 592 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 593 | static PyStatus |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 594 | init_interp_create_gil(PyThreadState *tstate) |
| 595 | { |
| 596 | PyStatus status; |
| 597 | |
| 598 | /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is |
| 599 | only called here. */ |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 600 | _PyEval_FiniGIL(tstate->interp); |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 601 | |
| 602 | /* Auto-thread-state API */ |
Victor Stinner | 87f649a | 2021-03-10 20:00:46 +0100 | [diff] [blame] | 603 | status = _PyGILState_SetTstate(tstate); |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 604 | if (_PyStatus_EXCEPTION(status)) { |
| 605 | return status; |
| 606 | } |
| 607 | |
| 608 | /* Create the GIL and take it */ |
| 609 | status = _PyEval_InitGIL(tstate); |
| 610 | if (_PyStatus_EXCEPTION(status)) { |
| 611 | return status; |
| 612 | } |
| 613 | |
| 614 | return _PyStatus_OK(); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | static PyStatus |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 619 | pycore_create_interpreter(_PyRuntimeState *runtime, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 620 | const PyConfig *config, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 621 | PyThreadState **tstate_p) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 622 | { |
Victor Stinner | 87f649a | 2021-03-10 20:00:46 +0100 | [diff] [blame] | 623 | /* Auto-thread-state API */ |
| 624 | PyStatus status = _PyGILState_Init(runtime); |
| 625 | if (_PyStatus_EXCEPTION(status)) { |
| 626 | return status; |
| 627 | } |
| 628 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 629 | PyInterpreterState *interp = PyInterpreterState_New(); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 630 | if (interp == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 631 | return _PyStatus_ERR("can't make main interpreter"); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 632 | } |
Victor Stinner | 87f649a | 2021-03-10 20:00:46 +0100 | [diff] [blame] | 633 | assert(_Py_IsMainInterpreter(interp)); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 634 | |
Victor Stinner | 87f649a | 2021-03-10 20:00:46 +0100 | [diff] [blame] | 635 | status = _PyConfig_Copy(&interp->config, config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 636 | if (_PyStatus_EXCEPTION(status)) { |
| 637 | return status; |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 638 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 639 | |
Victor Stinner | d19d8d5 | 2018-07-24 13:55:48 +0200 | [diff] [blame] | 640 | PyThreadState *tstate = PyThreadState_New(interp); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 641 | if (tstate == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 642 | return _PyStatus_ERR("can't make first thread"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 643 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 644 | (void) PyThreadState_Swap(tstate); |
| 645 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 646 | status = init_interp_create_gil(tstate); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 647 | if (_PyStatus_EXCEPTION(status)) { |
| 648 | return status; |
| 649 | } |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 650 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 651 | *tstate_p = tstate; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 652 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 653 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 654 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 655 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 656 | static PyStatus |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 657 | pycore_init_singletons(PyInterpreterState *interp) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 658 | { |
Victor Stinner | 444b39b | 2019-11-20 01:18:11 +0100 | [diff] [blame] | 659 | PyStatus status; |
| 660 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 661 | if (_PyLong_Init(interp) < 0) { |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 662 | return _PyStatus_ERR("can't init longs"); |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 663 | } |
Victor Stinner | ef5aa9a | 2019-11-20 00:38:03 +0100 | [diff] [blame] | 664 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 665 | if (_Py_IsMainInterpreter(interp)) { |
| 666 | _PyFloat_Init(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 669 | status = _PyBytes_Init(interp); |
Victor Stinner | 91698d8 | 2020-06-25 14:07:40 +0200 | [diff] [blame] | 670 | if (_PyStatus_EXCEPTION(status)) { |
| 671 | return status; |
| 672 | } |
| 673 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 674 | status = _PyUnicode_Init(interp); |
| 675 | if (_PyStatus_EXCEPTION(status)) { |
| 676 | return status; |
| 677 | } |
| 678 | |
| 679 | status = _PyTuple_Init(interp); |
| 680 | if (_PyStatus_EXCEPTION(status)) { |
| 681 | return status; |
| 682 | } |
| 683 | |
| 684 | return _PyStatus_OK(); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | static PyStatus |
| 689 | pycore_init_types(PyInterpreterState *interp) |
| 690 | { |
| 691 | PyStatus status; |
| 692 | int is_main_interp = _Py_IsMainInterpreter(interp); |
| 693 | |
| 694 | if (is_main_interp) { |
| 695 | if (_PyStructSequence_Init() < 0) { |
| 696 | return _PyStatus_ERR("can't initialize structseq"); |
| 697 | } |
| 698 | |
| 699 | status = _PyTypes_Init(); |
| 700 | if (_PyStatus_EXCEPTION(status)) { |
| 701 | return status; |
| 702 | } |
| 703 | |
| 704 | if (_PyLong_InitTypes() < 0) { |
| 705 | return _PyStatus_ERR("can't init int type"); |
| 706 | } |
| 707 | |
| 708 | status = _PyUnicode_InitTypes(); |
| 709 | if (_PyStatus_EXCEPTION(status)) { |
| 710 | return status; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (is_main_interp) { |
| 715 | if (_PyFloat_InitTypes() < 0) { |
| 716 | return _PyStatus_ERR("can't init float"); |
| 717 | } |
| 718 | } |
| 719 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 720 | status = _PyExc_Init(interp); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 721 | if (_PyStatus_EXCEPTION(status)) { |
| 722 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 725 | status = _PyErr_InitTypes(); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 726 | if (_PyStatus_EXCEPTION(status)) { |
| 727 | return status; |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Victor Stinner | e7e699e | 2019-11-20 12:08:13 +0100 | [diff] [blame] | 730 | if (is_main_interp) { |
| 731 | if (!_PyContext_Init()) { |
| 732 | return _PyStatus_ERR("can't init context"); |
| 733 | } |
Victor Stinner | ef5aa9a | 2019-11-20 00:38:03 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 736 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 740 | static PyStatus |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 741 | pycore_init_builtins(PyThreadState *tstate) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 742 | { |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 743 | PyInterpreterState *interp = tstate->interp; |
| 744 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 745 | PyObject *bimod = _PyBuiltin_Init(interp); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 746 | if (bimod == NULL) { |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 747 | goto error; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 748 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 749 | |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 750 | if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) { |
| 751 | goto error; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 752 | } |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 753 | |
| 754 | PyObject *builtins_dict = PyModule_GetDict(bimod); |
| 755 | if (builtins_dict == NULL) { |
| 756 | goto error; |
| 757 | } |
| 758 | Py_INCREF(builtins_dict); |
| 759 | interp->builtins = builtins_dict; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 760 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 761 | PyStatus status = _PyBuiltins_AddExceptions(bimod); |
| 762 | if (_PyStatus_EXCEPTION(status)) { |
| 763 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 764 | } |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 765 | |
| 766 | interp->builtins_copy = PyDict_Copy(interp->builtins); |
| 767 | if (interp->builtins_copy == NULL) { |
| 768 | goto error; |
| 769 | } |
Pablo Galindo | b96c6b0 | 2019-12-04 11:19:59 +0000 | [diff] [blame] | 770 | Py_DECREF(bimod); |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 771 | |
Victor Stinner | 6223071 | 2020-11-18 23:18:29 +0100 | [diff] [blame] | 772 | // Get the __import__ function |
| 773 | PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins, |
| 774 | "__import__"); |
| 775 | if (import_func == NULL) { |
| 776 | goto error; |
| 777 | } |
| 778 | interp->import_func = Py_NewRef(import_func); |
| 779 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 780 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 781 | return _PyStatus_OK(); |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 782 | |
| 783 | error: |
Pablo Galindo | b96c6b0 | 2019-12-04 11:19:59 +0000 | [diff] [blame] | 784 | Py_XDECREF(bimod); |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 785 | return _PyStatus_ERR("can't initialize builtins module"); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 789 | static PyStatus |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 790 | pycore_interp_init(PyThreadState *tstate) |
| 791 | { |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 792 | PyInterpreterState *interp = tstate->interp; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 793 | PyStatus status; |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 794 | PyObject *sysmod = NULL; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 795 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 796 | // Create singletons before the first PyType_Ready() call, since |
| 797 | // PyType_Ready() uses singletons like the Unicode empty string (tp_doc) |
| 798 | // and the empty tuple singletons (tp_bases). |
| 799 | status = pycore_init_singletons(interp); |
| 800 | if (_PyStatus_EXCEPTION(status)) { |
| 801 | return status; |
| 802 | } |
| 803 | |
| 804 | // The GC must be initialized before the first GC collection. |
| 805 | status = _PyGC_Init(interp); |
| 806 | if (_PyStatus_EXCEPTION(status)) { |
| 807 | return status; |
| 808 | } |
| 809 | |
| 810 | status = pycore_init_types(interp); |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 811 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 812 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 815 | if (_PyWarnings_InitState(interp) < 0) { |
| 816 | return _PyStatus_ERR("can't initialize warnings"); |
| 817 | } |
| 818 | |
| 819 | status = _PyAtExit_Init(interp); |
| 820 | if (_PyStatus_EXCEPTION(status)) { |
| 821 | return status; |
| 822 | } |
| 823 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 824 | status = _PySys_Create(tstate, &sysmod); |
| 825 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 826 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 829 | status = pycore_init_builtins(tstate); |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 830 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 831 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Victor Stinner | 442ad74 | 2021-04-02 15:28:13 +0200 | [diff] [blame] | 834 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
Victor Stinner | ef75a62 | 2020-11-12 15:14:13 +0100 | [diff] [blame] | 835 | if (config->_install_importlib) { |
| 836 | /* This call sets up builtin and frozen import support */ |
| 837 | if (init_importlib(tstate, sysmod) < 0) { |
| 838 | return _PyStatus_ERR("failed to initialize importlib"); |
| 839 | } |
| 840 | } |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 841 | |
| 842 | done: |
| 843 | /* sys.modules['sys'] contains a strong reference to the module */ |
| 844 | Py_XDECREF(sysmod); |
| 845 | return status; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | |
| 849 | static PyStatus |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 850 | pyinit_config(_PyRuntimeState *runtime, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 851 | PyThreadState **tstate_p, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 852 | const PyConfig *config) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 853 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 854 | PyStatus status = pycore_init_runtime(runtime, config); |
| 855 | if (_PyStatus_EXCEPTION(status)) { |
| 856 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 859 | PyThreadState *tstate; |
| 860 | status = pycore_create_interpreter(runtime, config, &tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 861 | if (_PyStatus_EXCEPTION(status)) { |
| 862 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 863 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 864 | *tstate_p = tstate; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 865 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 866 | status = pycore_interp_init(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 867 | if (_PyStatus_EXCEPTION(status)) { |
| 868 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 869 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 870 | |
| 871 | /* Only when we get here is the runtime core fully initialized */ |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 872 | runtime->core_initialized = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 873 | return _PyStatus_OK(); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 876 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 877 | PyStatus |
| 878 | _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args) |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 879 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 880 | PyStatus status; |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 881 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 882 | if (src_config == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 883 | return _PyStatus_ERR("preinitialization config is NULL"); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 886 | status = _PyRuntime_Initialize(); |
| 887 | if (_PyStatus_EXCEPTION(status)) { |
| 888 | return status; |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 889 | } |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 890 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 891 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 892 | if (runtime->preinitialized) { |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 893 | /* If it's already configured: ignored the new configuration */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 894 | return _PyStatus_OK(); |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 897 | /* Note: preinitialized remains 1 on error, it is only set to 0 |
| 898 | at exit on success. */ |
| 899 | runtime->preinitializing = 1; |
| 900 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 901 | PyPreConfig config; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 902 | |
| 903 | status = _PyPreConfig_InitFromPreConfig(&config, src_config); |
| 904 | if (_PyStatus_EXCEPTION(status)) { |
| 905 | return status; |
| 906 | } |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 907 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 908 | status = _PyPreConfig_Read(&config, args); |
| 909 | if (_PyStatus_EXCEPTION(status)) { |
| 910 | return status; |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 913 | status = _PyPreConfig_Write(&config); |
| 914 | if (_PyStatus_EXCEPTION(status)) { |
| 915 | return status; |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 918 | runtime->preinitializing = 0; |
| 919 | runtime->preinitialized = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 920 | return _PyStatus_OK(); |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 923 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 924 | PyStatus |
| 925 | Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv) |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 926 | { |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 927 | _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv}; |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 928 | return _Py_PreInitializeFromPyArgv(src_config, &args); |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 932 | PyStatus |
| 933 | Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv) |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 934 | { |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 935 | _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv}; |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 936 | return _Py_PreInitializeFromPyArgv(src_config, &args); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 940 | PyStatus |
| 941 | Py_PreInitialize(const PyPreConfig *src_config) |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 942 | { |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 943 | return _Py_PreInitializeFromPyArgv(src_config, NULL); |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 947 | PyStatus |
| 948 | _Py_PreInitializeFromConfig(const PyConfig *config, |
| 949 | const _PyArgv *args) |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 950 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 951 | assert(config != NULL); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 952 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 953 | PyStatus status = _PyRuntime_Initialize(); |
| 954 | if (_PyStatus_EXCEPTION(status)) { |
| 955 | return status; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 956 | } |
| 957 | _PyRuntimeState *runtime = &_PyRuntime; |
| 958 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 959 | if (runtime->preinitialized) { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 960 | /* Already initialized: do nothing */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 961 | return _PyStatus_OK(); |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 962 | } |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 963 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 964 | PyPreConfig preconfig; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 965 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 966 | _PyPreConfig_InitFromConfig(&preconfig, config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 967 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 968 | if (!config->parse_argv) { |
| 969 | return Py_PreInitialize(&preconfig); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 970 | } |
| 971 | else if (args == NULL) { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 972 | _PyArgv config_args = { |
| 973 | .use_bytes_argv = 0, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 974 | .argc = config->argv.length, |
| 975 | .wchar_argv = config->argv.items}; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 976 | return _Py_PreInitializeFromPyArgv(&preconfig, &config_args); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 977 | } |
| 978 | else { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 979 | return _Py_PreInitializeFromPyArgv(&preconfig, args); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 980 | } |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 984 | /* Begin interpreter initialization |
| 985 | * |
| 986 | * On return, the first thread and interpreter state have been created, |
| 987 | * but the compiler, signal handling, multithreading and |
| 988 | * multiple interpreter support, and codec infrastructure are not yet |
| 989 | * available. |
| 990 | * |
| 991 | * The import system will support builtin and frozen modules only. |
| 992 | * The only supported io is writing to sys.stderr |
| 993 | * |
| 994 | * If any operation invoked by this function fails, a fatal error is |
| 995 | * issued and the function does not return. |
| 996 | * |
| 997 | * Any code invoked from this function should *not* assume it has access |
| 998 | * to the Python C API (unless the API is explicitly listed as being |
| 999 | * safe to call without calling Py_Initialize first) |
| 1000 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1001 | static PyStatus |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1002 | pyinit_core(_PyRuntimeState *runtime, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1003 | const PyConfig *src_config, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1004 | PyThreadState **tstate_p) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1005 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1006 | PyStatus status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1007 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1008 | status = _Py_PreInitializeFromConfig(src_config, NULL); |
| 1009 | if (_PyStatus_EXCEPTION(status)) { |
| 1010 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1013 | PyConfig config; |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 1014 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1015 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1016 | status = _PyConfig_Copy(&config, src_config); |
| 1017 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1018 | goto done; |
| 1019 | } |
| 1020 | |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 1021 | // Read the configuration, but don't compute the path configuration |
| 1022 | // (it is computed in the main init). |
| 1023 | status = _PyConfig_Read(&config, 0); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1024 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1025 | goto done; |
| 1026 | } |
| 1027 | |
| 1028 | if (!runtime->core_initialized) { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1029 | status = pyinit_config(runtime, tstate_p, &config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1030 | } |
| 1031 | else { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1032 | status = pyinit_core_reconfigure(runtime, tstate_p, &config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1033 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1034 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 1035 | goto done; |
| 1036 | } |
| 1037 | |
| 1038 | done: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1039 | PyConfig_Clear(&config); |
| 1040 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 1043 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1044 | /* Py_Initialize() has already been called: update the main interpreter |
| 1045 | configuration. Example of bpo-34008: Py_Main() called after |
| 1046 | Py_Initialize(). */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1047 | static PyStatus |
Victor Stinner | af1d64d | 2020-11-04 17:34:34 +0100 | [diff] [blame] | 1048 | pyinit_main_reconfigure(PyThreadState *tstate) |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1049 | { |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 1050 | if (interpreter_update_config(tstate, 0) < 0) { |
| 1051 | return _PyStatus_ERR("fail to reconfigure Python"); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1052 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1053 | return _PyStatus_OK(); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1056 | |
| 1057 | static PyStatus |
| 1058 | init_interp_main(PyThreadState *tstate) |
| 1059 | { |
Miss Islington (bot) | a11158e | 2021-08-06 04:32:37 -0700 | [diff] [blame] | 1060 | extern void _PyThread_debug_deprecation(void); |
| 1061 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1062 | assert(!_PyErr_Occurred(tstate)); |
| 1063 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1064 | PyStatus status; |
Victor Stinner | 101bf69 | 2021-02-19 13:33:31 +0100 | [diff] [blame] | 1065 | int is_main_interp = _Py_IsMainInterpreter(tstate->interp); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1066 | PyInterpreterState *interp = tstate->interp; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 1067 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1068 | |
| 1069 | if (!config->_install_importlib) { |
| 1070 | /* Special mode for freeze_importlib: run with no import system |
| 1071 | * |
| 1072 | * This means anything which needs support from extension modules |
| 1073 | * or pure Python code in the standard library won't work. |
| 1074 | */ |
| 1075 | if (is_main_interp) { |
| 1076 | interp->runtime->initialized = 1; |
| 1077 | } |
| 1078 | return _PyStatus_OK(); |
| 1079 | } |
| 1080 | |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 1081 | // Compute the path configuration |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 1082 | status = _PyConfig_InitPathConfig(&interp->config, 1); |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 1083 | if (_PyStatus_EXCEPTION(status)) { |
| 1084 | return status; |
| 1085 | } |
| 1086 | |
Victor Stinner | 9e1b828 | 2020-11-10 13:21:52 +0100 | [diff] [blame] | 1087 | if (interpreter_update_config(tstate, 1) < 0) { |
| 1088 | return _PyStatus_ERR("failed to update the Python config"); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | status = init_importlib_external(tstate); |
| 1092 | if (_PyStatus_EXCEPTION(status)) { |
| 1093 | return status; |
| 1094 | } |
| 1095 | |
| 1096 | if (is_main_interp) { |
| 1097 | /* initialize the faulthandler module */ |
| 1098 | status = _PyFaulthandler_Init(config->faulthandler); |
| 1099 | if (_PyStatus_EXCEPTION(status)) { |
| 1100 | return status; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | status = _PyUnicode_InitEncodings(tstate); |
| 1105 | if (_PyStatus_EXCEPTION(status)) { |
| 1106 | return status; |
| 1107 | } |
| 1108 | |
| 1109 | if (is_main_interp) { |
Victor Stinner | 296a796 | 2020-11-17 16:22:23 +0100 | [diff] [blame] | 1110 | if (_PySignal_Init(config->install_signal_handlers) < 0) { |
| 1111 | return _PyStatus_ERR("can't initialize signals"); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (_PyTraceMalloc_Init(config->tracemalloc) < 0) { |
| 1115 | return _PyStatus_ERR("can't initialize tracemalloc"); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | status = init_sys_streams(tstate); |
| 1120 | if (_PyStatus_EXCEPTION(status)) { |
| 1121 | return status; |
| 1122 | } |
| 1123 | |
Andy Lester | 75cd5bf | 2020-03-12 02:49:05 -0500 | [diff] [blame] | 1124 | status = init_set_builtins_open(); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1125 | if (_PyStatus_EXCEPTION(status)) { |
| 1126 | return status; |
| 1127 | } |
| 1128 | |
| 1129 | status = add_main_module(interp); |
| 1130 | if (_PyStatus_EXCEPTION(status)) { |
| 1131 | return status; |
| 1132 | } |
| 1133 | |
| 1134 | if (is_main_interp) { |
| 1135 | /* Initialize warnings. */ |
| 1136 | PyObject *warnoptions = PySys_GetObject("warnoptions"); |
| 1137 | if (warnoptions != NULL && PyList_Size(warnoptions) > 0) |
| 1138 | { |
| 1139 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 1140 | if (warnings_module == NULL) { |
| 1141 | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
| 1142 | _PyErr_Print(tstate); |
| 1143 | } |
| 1144 | Py_XDECREF(warnings_module); |
| 1145 | } |
| 1146 | |
| 1147 | interp->runtime->initialized = 1; |
| 1148 | } |
| 1149 | |
| 1150 | if (config->site_import) { |
| 1151 | status = init_import_site(); |
| 1152 | if (_PyStatus_EXCEPTION(status)) { |
| 1153 | return status; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | if (is_main_interp) { |
| 1158 | #ifndef MS_WINDOWS |
| 1159 | emit_stderr_warning_for_legacy_locale(interp->runtime); |
| 1160 | #endif |
| 1161 | } |
| 1162 | |
Miss Islington (bot) | a11158e | 2021-08-06 04:32:37 -0700 | [diff] [blame] | 1163 | // Warn about PYTHONTHREADDEBUG deprecation |
| 1164 | _PyThread_debug_deprecation(); |
| 1165 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1166 | assert(!_PyErr_Occurred(tstate)); |
| 1167 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1168 | return _PyStatus_OK(); |
| 1169 | } |
| 1170 | |
| 1171 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1172 | /* Update interpreter state based on supplied configuration settings |
| 1173 | * |
| 1174 | * After calling this function, most of the restrictions on the interpreter |
| 1175 | * are lifted. The only remaining incomplete settings are those related |
| 1176 | * to the main module (sys.argv[0], __main__ metadata) |
| 1177 | * |
| 1178 | * Calling this when the interpreter is not initializing, is already |
| 1179 | * initialized or without a valid current thread state is a fatal error. |
| 1180 | * Other errors should be reported as normal Python exceptions with a |
| 1181 | * non-zero return code. |
| 1182 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1183 | static PyStatus |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 1184 | pyinit_main(PyThreadState *tstate) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1185 | { |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1186 | PyInterpreterState *interp = tstate->interp; |
| 1187 | if (!interp->runtime->core_initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1188 | return _PyStatus_ERR("runtime core not initialized"); |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1189 | } |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1190 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1191 | if (interp->runtime->initialized) { |
Victor Stinner | af1d64d | 2020-11-04 17:34:34 +0100 | [diff] [blame] | 1192 | return pyinit_main_reconfigure(tstate); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1195 | PyStatus status = init_interp_main(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1196 | if (_PyStatus_EXCEPTION(status)) { |
| 1197 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1198 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1199 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1200 | } |
| 1201 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1202 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1203 | PyStatus |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1204 | Py_InitializeFromConfig(const PyConfig *config) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1205 | { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1206 | if (config == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1207 | return _PyStatus_ERR("initialization config is NULL"); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1210 | PyStatus status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1211 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1212 | status = _PyRuntime_Initialize(); |
| 1213 | if (_PyStatus_EXCEPTION(status)) { |
| 1214 | return status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1215 | } |
| 1216 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1217 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1218 | PyThreadState *tstate = NULL; |
| 1219 | status = pyinit_core(runtime, config, &tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1220 | if (_PyStatus_EXCEPTION(status)) { |
| 1221 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1222 | } |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 1223 | config = _PyInterpreterState_GetConfig(tstate->interp); |
Victor Stinner | bc8ac6b | 2017-11-30 18:03:55 +0100 | [diff] [blame] | 1224 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1225 | if (config->_init_main) { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 1226 | status = pyinit_main(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1227 | if (_PyStatus_EXCEPTION(status)) { |
| 1228 | return status; |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 1229 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1230 | } |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 1231 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1232 | return _PyStatus_OK(); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1236 | void |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1237 | Py_InitializeEx(int install_sigs) |
| 1238 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1239 | PyStatus status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1240 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1241 | status = _PyRuntime_Initialize(); |
| 1242 | if (_PyStatus_EXCEPTION(status)) { |
| 1243 | Py_ExitStatusException(status); |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1244 | } |
| 1245 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1246 | |
| 1247 | if (runtime->initialized) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1248 | /* bpo-33932: Calling Py_Initialize() twice does nothing. */ |
| 1249 | return; |
| 1250 | } |
| 1251 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1252 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1253 | _PyConfig_InitCompatConfig(&config); |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 1254 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1255 | config.install_signal_handlers = install_sigs; |
| 1256 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1257 | status = Py_InitializeFromConfig(&config); |
| 1258 | if (_PyStatus_EXCEPTION(status)) { |
| 1259 | Py_ExitStatusException(status); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1260 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void |
| 1264 | Py_Initialize(void) |
| 1265 | { |
| 1266 | Py_InitializeEx(1); |
| 1267 | } |
| 1268 | |
| 1269 | |
Victor Stinner | af1d64d | 2020-11-04 17:34:34 +0100 | [diff] [blame] | 1270 | PyStatus |
| 1271 | _Py_InitializeMain(void) |
| 1272 | { |
| 1273 | PyStatus status = _PyRuntime_Initialize(); |
| 1274 | if (_PyStatus_EXCEPTION(status)) { |
| 1275 | return status; |
| 1276 | } |
| 1277 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1278 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 1279 | return pyinit_main(tstate); |
| 1280 | } |
| 1281 | |
| 1282 | |
Victor Stinner | dff1ad5 | 2020-10-30 18:03:28 +0100 | [diff] [blame] | 1283 | static void |
| 1284 | finalize_modules_delete_special(PyThreadState *tstate, int verbose) |
| 1285 | { |
| 1286 | // List of names to clear in sys |
| 1287 | static const char * const sys_deletes[] = { |
| 1288 | "path", "argv", "ps1", "ps2", |
| 1289 | "last_type", "last_value", "last_traceback", |
| 1290 | "path_hooks", "path_importer_cache", "meta_path", |
| 1291 | "__interactivehook__", |
| 1292 | NULL |
| 1293 | }; |
| 1294 | |
| 1295 | static const char * const sys_files[] = { |
| 1296 | "stdin", "__stdin__", |
| 1297 | "stdout", "__stdout__", |
| 1298 | "stderr", "__stderr__", |
| 1299 | NULL |
| 1300 | }; |
| 1301 | |
| 1302 | PyInterpreterState *interp = tstate->interp; |
| 1303 | if (verbose) { |
| 1304 | PySys_WriteStderr("# clear builtins._\n"); |
| 1305 | } |
| 1306 | if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) { |
| 1307 | PyErr_WriteUnraisable(NULL); |
| 1308 | } |
| 1309 | |
| 1310 | const char * const *p; |
| 1311 | for (p = sys_deletes; *p != NULL; p++) { |
| 1312 | if (verbose) { |
| 1313 | PySys_WriteStderr("# clear sys.%s\n", *p); |
| 1314 | } |
| 1315 | if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) { |
| 1316 | PyErr_WriteUnraisable(NULL); |
| 1317 | } |
| 1318 | } |
| 1319 | for (p = sys_files; *p != NULL; p+=2) { |
| 1320 | const char *name = p[0]; |
| 1321 | const char *orig_name = p[1]; |
| 1322 | if (verbose) { |
| 1323 | PySys_WriteStderr("# restore sys.%s\n", name); |
| 1324 | } |
| 1325 | PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict, |
| 1326 | orig_name); |
| 1327 | if (value == NULL) { |
| 1328 | if (_PyErr_Occurred(tstate)) { |
| 1329 | PyErr_WriteUnraisable(NULL); |
| 1330 | } |
| 1331 | value = Py_None; |
| 1332 | } |
| 1333 | if (PyDict_SetItemString(interp->sysdict, name, value) < 0) { |
| 1334 | PyErr_WriteUnraisable(NULL); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | static PyObject* |
| 1341 | finalize_remove_modules(PyObject *modules, int verbose) |
| 1342 | { |
| 1343 | PyObject *weaklist = PyList_New(0); |
| 1344 | if (weaklist == NULL) { |
| 1345 | PyErr_WriteUnraisable(NULL); |
| 1346 | } |
| 1347 | |
| 1348 | #define STORE_MODULE_WEAKREF(name, mod) \ |
| 1349 | if (weaklist != NULL) { \ |
| 1350 | PyObject *wr = PyWeakref_NewRef(mod, NULL); \ |
| 1351 | if (wr) { \ |
| 1352 | PyObject *tup = PyTuple_Pack(2, name, wr); \ |
| 1353 | if (!tup || PyList_Append(weaklist, tup) < 0) { \ |
| 1354 | PyErr_WriteUnraisable(NULL); \ |
| 1355 | } \ |
| 1356 | Py_XDECREF(tup); \ |
| 1357 | Py_DECREF(wr); \ |
| 1358 | } \ |
| 1359 | else { \ |
| 1360 | PyErr_WriteUnraisable(NULL); \ |
| 1361 | } \ |
| 1362 | } |
| 1363 | |
| 1364 | #define CLEAR_MODULE(name, mod) \ |
| 1365 | if (PyModule_Check(mod)) { \ |
| 1366 | if (verbose && PyUnicode_Check(name)) { \ |
| 1367 | PySys_FormatStderr("# cleanup[2] removing %U\n", name); \ |
| 1368 | } \ |
| 1369 | STORE_MODULE_WEAKREF(name, mod); \ |
| 1370 | if (PyObject_SetItem(modules, name, Py_None) < 0) { \ |
| 1371 | PyErr_WriteUnraisable(NULL); \ |
| 1372 | } \ |
| 1373 | } |
| 1374 | |
| 1375 | if (PyDict_CheckExact(modules)) { |
| 1376 | Py_ssize_t pos = 0; |
| 1377 | PyObject *key, *value; |
| 1378 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 1379 | CLEAR_MODULE(key, value); |
| 1380 | } |
| 1381 | } |
| 1382 | else { |
| 1383 | PyObject *iterator = PyObject_GetIter(modules); |
| 1384 | if (iterator == NULL) { |
| 1385 | PyErr_WriteUnraisable(NULL); |
| 1386 | } |
| 1387 | else { |
| 1388 | PyObject *key; |
| 1389 | while ((key = PyIter_Next(iterator))) { |
| 1390 | PyObject *value = PyObject_GetItem(modules, key); |
| 1391 | if (value == NULL) { |
| 1392 | PyErr_WriteUnraisable(NULL); |
| 1393 | continue; |
| 1394 | } |
| 1395 | CLEAR_MODULE(key, value); |
| 1396 | Py_DECREF(value); |
| 1397 | Py_DECREF(key); |
| 1398 | } |
| 1399 | if (PyErr_Occurred()) { |
| 1400 | PyErr_WriteUnraisable(NULL); |
| 1401 | } |
| 1402 | Py_DECREF(iterator); |
| 1403 | } |
| 1404 | } |
| 1405 | #undef CLEAR_MODULE |
| 1406 | #undef STORE_MODULE_WEAKREF |
| 1407 | |
| 1408 | return weaklist; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | static void |
| 1413 | finalize_clear_modules_dict(PyObject *modules) |
| 1414 | { |
| 1415 | if (PyDict_CheckExact(modules)) { |
| 1416 | PyDict_Clear(modules); |
| 1417 | } |
| 1418 | else { |
| 1419 | _Py_IDENTIFIER(clear); |
| 1420 | if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) { |
| 1421 | PyErr_WriteUnraisable(NULL); |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | static void |
| 1428 | finalize_restore_builtins(PyThreadState *tstate) |
| 1429 | { |
| 1430 | PyInterpreterState *interp = tstate->interp; |
| 1431 | PyObject *dict = PyDict_Copy(interp->builtins); |
| 1432 | if (dict == NULL) { |
| 1433 | PyErr_WriteUnraisable(NULL); |
| 1434 | } |
| 1435 | PyDict_Clear(interp->builtins); |
| 1436 | if (PyDict_Update(interp->builtins, interp->builtins_copy)) { |
| 1437 | _PyErr_Clear(tstate); |
| 1438 | } |
| 1439 | Py_XDECREF(dict); |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | static void |
| 1444 | finalize_modules_clear_weaklist(PyInterpreterState *interp, |
| 1445 | PyObject *weaklist, int verbose) |
| 1446 | { |
| 1447 | // First clear modules imported later |
| 1448 | for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) { |
| 1449 | PyObject *tup = PyList_GET_ITEM(weaklist, i); |
| 1450 | PyObject *name = PyTuple_GET_ITEM(tup, 0); |
| 1451 | PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1)); |
| 1452 | if (mod == Py_None) { |
| 1453 | continue; |
| 1454 | } |
| 1455 | assert(PyModule_Check(mod)); |
| 1456 | PyObject *dict = PyModule_GetDict(mod); |
| 1457 | if (dict == interp->builtins || dict == interp->sysdict) { |
| 1458 | continue; |
| 1459 | } |
| 1460 | Py_INCREF(mod); |
| 1461 | if (verbose && PyUnicode_Check(name)) { |
| 1462 | PySys_FormatStderr("# cleanup[3] wiping %U\n", name); |
| 1463 | } |
| 1464 | _PyModule_Clear(mod); |
| 1465 | Py_DECREF(mod); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | static void |
| 1471 | finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose) |
| 1472 | { |
| 1473 | // Clear sys dict |
| 1474 | if (verbose) { |
| 1475 | PySys_FormatStderr("# cleanup[3] wiping sys\n"); |
| 1476 | } |
| 1477 | _PyModule_ClearDict(interp->sysdict); |
| 1478 | |
| 1479 | // Clear builtins dict |
| 1480 | if (verbose) { |
| 1481 | PySys_FormatStderr("# cleanup[3] wiping builtins\n"); |
| 1482 | } |
| 1483 | _PyModule_ClearDict(interp->builtins); |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | /* Clear modules, as good as we can */ |
| 1488 | static void |
| 1489 | finalize_modules(PyThreadState *tstate) |
| 1490 | { |
| 1491 | PyInterpreterState *interp = tstate->interp; |
| 1492 | PyObject *modules = interp->modules; |
| 1493 | if (modules == NULL) { |
| 1494 | // Already done |
| 1495 | return; |
| 1496 | } |
| 1497 | int verbose = _PyInterpreterState_GetConfig(interp)->verbose; |
| 1498 | |
| 1499 | // Delete some special builtins._ and sys attributes first. These are |
| 1500 | // common places where user values hide and people complain when their |
| 1501 | // destructors fail. Since the modules containing them are |
| 1502 | // deleted *last* of all, they would come too late in the normal |
| 1503 | // destruction order. Sigh. |
| 1504 | // |
| 1505 | // XXX Perhaps these precautions are obsolete. Who knows? |
| 1506 | finalize_modules_delete_special(tstate, verbose); |
| 1507 | |
| 1508 | // Remove all modules from sys.modules, hoping that garbage collection |
| 1509 | // can reclaim most of them: set all sys.modules values to None. |
| 1510 | // |
| 1511 | // We prepare a list which will receive (name, weakref) tuples of |
| 1512 | // modules when they are removed from sys.modules. The name is used |
| 1513 | // for diagnosis messages (in verbose mode), while the weakref helps |
| 1514 | // detect those modules which have been held alive. |
| 1515 | PyObject *weaklist = finalize_remove_modules(modules, verbose); |
| 1516 | |
| 1517 | // Clear the modules dict |
| 1518 | finalize_clear_modules_dict(modules); |
| 1519 | |
| 1520 | // Restore the original builtins dict, to ensure that any |
| 1521 | // user data gets cleared. |
| 1522 | finalize_restore_builtins(tstate); |
| 1523 | |
| 1524 | // Collect garbage |
| 1525 | _PyGC_CollectNoFail(tstate); |
| 1526 | |
| 1527 | // Dump GC stats before it's too late, since it uses the warnings |
| 1528 | // machinery. |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1529 | _PyGC_DumpShutdownStats(interp); |
Victor Stinner | dff1ad5 | 2020-10-30 18:03:28 +0100 | [diff] [blame] | 1530 | |
| 1531 | if (weaklist != NULL) { |
| 1532 | // Now, if there are any modules left alive, clear their globals to |
| 1533 | // minimize potential leaks. All C extension modules actually end |
| 1534 | // up here, since they are kept alive in the interpreter state. |
| 1535 | // |
| 1536 | // The special treatment of "builtins" here is because even |
| 1537 | // when it's not referenced as a module, its dictionary is |
| 1538 | // referenced by almost every module's __builtins__. Since |
| 1539 | // deleting a module clears its dictionary (even if there are |
| 1540 | // references left to it), we need to delete the "builtins" |
| 1541 | // module last. Likewise, we don't delete sys until the very |
| 1542 | // end because it is implicitly referenced (e.g. by print). |
| 1543 | // |
| 1544 | // Since dict is ordered in CPython 3.6+, modules are saved in |
| 1545 | // importing order. First clear modules imported later. |
| 1546 | finalize_modules_clear_weaklist(interp, weaklist, verbose); |
| 1547 | Py_DECREF(weaklist); |
| 1548 | } |
| 1549 | |
| 1550 | // Clear sys and builtins modules dict |
| 1551 | finalize_clear_sys_builtins_dict(interp, verbose); |
| 1552 | |
| 1553 | // Clear module dict copies stored in the interpreter state: |
| 1554 | // clear PyInterpreterState.modules_by_index and |
| 1555 | // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase |
| 1556 | // initialization API) |
| 1557 | _PyInterpreterState_ClearModules(interp); |
| 1558 | |
| 1559 | // Clear and delete the modules directory. Actual modules will |
| 1560 | // still be there only if imported during the execution of some |
| 1561 | // destructor. |
| 1562 | Py_SETREF(interp->modules, NULL); |
| 1563 | |
| 1564 | // Collect garbage once more |
| 1565 | _PyGC_CollectNoFail(tstate); |
| 1566 | } |
| 1567 | |
| 1568 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1569 | /* Flush stdout and stderr */ |
| 1570 | |
| 1571 | static int |
| 1572 | file_is_closed(PyObject *fobj) |
| 1573 | { |
| 1574 | int r; |
| 1575 | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
| 1576 | if (tmp == NULL) { |
| 1577 | PyErr_Clear(); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | r = PyObject_IsTrue(tmp); |
| 1581 | Py_DECREF(tmp); |
| 1582 | if (r < 0) |
| 1583 | PyErr_Clear(); |
| 1584 | return r > 0; |
| 1585 | } |
| 1586 | |
Victor Stinner | dff1ad5 | 2020-10-30 18:03:28 +0100 | [diff] [blame] | 1587 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1588 | static int |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1589 | flush_std_files(void) |
| 1590 | { |
| 1591 | PyObject *fout = _PySys_GetObjectId(&PyId_stdout); |
| 1592 | PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); |
| 1593 | PyObject *tmp; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1594 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1595 | |
| 1596 | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1597 | tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1598 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1599 | PyErr_WriteUnraisable(fout); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1600 | status = -1; |
| 1601 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1602 | else |
| 1603 | Py_DECREF(tmp); |
| 1604 | } |
| 1605 | |
| 1606 | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1607 | tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1608 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1609 | PyErr_Clear(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1610 | status = -1; |
| 1611 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1612 | else |
| 1613 | Py_DECREF(tmp); |
| 1614 | } |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1615 | |
| 1616 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* Undo the effect of Py_Initialize(). |
| 1620 | |
| 1621 | Beware: if multiple interpreter and/or thread states exist, these |
| 1622 | are not wiped out; only the current thread and interpreter state |
| 1623 | are deleted. But since everything else is deleted, those other |
| 1624 | interpreter and thread states should no longer be used. |
| 1625 | |
| 1626 | (XXX We should do better, e.g. wipe out all interpreters and |
| 1627 | threads.) |
| 1628 | |
| 1629 | Locking: as above. |
| 1630 | |
| 1631 | */ |
| 1632 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1633 | |
| 1634 | static void |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1635 | finalize_interp_types(PyInterpreterState *interp) |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1636 | { |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1637 | _PyExc_Fini(interp); |
| 1638 | _PyFrame_Fini(interp); |
| 1639 | _PyAsyncGen_Fini(interp); |
| 1640 | _PyContext_Fini(interp); |
| 1641 | _PyType_Fini(interp); |
Victor Stinner | ea25180 | 2020-12-26 02:58:33 +0100 | [diff] [blame] | 1642 | // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses |
| 1643 | // a dict internally. |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1644 | _PyUnicode_ClearInterned(interp); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1645 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1646 | _PyDict_Fini(interp); |
| 1647 | _PyList_Fini(interp); |
| 1648 | _PyTuple_Fini(interp); |
Victor Stinner | 7907f8c | 2020-06-08 01:22:36 +0200 | [diff] [blame] | 1649 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1650 | _PySlice_Fini(interp); |
Victor Stinner | 3d48334 | 2019-11-22 12:27:50 +0100 | [diff] [blame] | 1651 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1652 | _PyBytes_Fini(interp); |
| 1653 | _PyUnicode_Fini(interp); |
| 1654 | _PyFloat_Fini(interp); |
| 1655 | _PyLong_Fini(interp); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | static void |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1660 | finalize_interp_clear(PyThreadState *tstate) |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1661 | { |
Victor Stinner | 101bf69 | 2021-02-19 13:33:31 +0100 | [diff] [blame] | 1662 | int is_main_interp = _Py_IsMainInterpreter(tstate->interp); |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1663 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1664 | /* Clear interpreter state and all thread states */ |
Victor Stinner | eba5bf2 | 2020-10-30 22:51:02 +0100 | [diff] [blame] | 1665 | _PyInterpreterState_Clear(tstate); |
Pablo Galindo | ac0e1c2 | 2019-12-04 11:51:03 +0000 | [diff] [blame] | 1666 | |
Konge | daa0fe0 | 2020-07-04 05:06:46 +0800 | [diff] [blame] | 1667 | /* Clear all loghooks */ |
| 1668 | /* Both _PySys_Audit function and users still need PyObject, such as tuple. |
| 1669 | Call _PySys_ClearAuditHooks when PyObject available. */ |
| 1670 | if (is_main_interp) { |
| 1671 | _PySys_ClearAuditHooks(tstate); |
| 1672 | } |
| 1673 | |
Victor Stinner | 7907f8c | 2020-06-08 01:22:36 +0200 | [diff] [blame] | 1674 | if (is_main_interp) { |
| 1675 | _Py_HashRandomization_Fini(); |
| 1676 | _PyArg_Fini(); |
| 1677 | _Py_ClearFileSystemEncoding(); |
| 1678 | } |
| 1679 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1680 | finalize_interp_types(tstate->interp); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | static void |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1685 | finalize_interp_delete(PyInterpreterState *interp) |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1686 | { |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1687 | if (_Py_IsMainInterpreter(interp)) { |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1688 | /* Cleanup auto-thread-state */ |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1689 | _PyGILState_Fini(interp); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 1692 | /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can |
| 1693 | fail when it is being awaited by another running daemon thread (see |
| 1694 | bpo-9901). Instead pycore_create_interpreter() destroys the previously |
| 1695 | created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be |
| 1696 | called multiple times. */ |
| 1697 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1698 | PyInterpreterState_Delete(interp); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1702 | int |
| 1703 | Py_FinalizeEx(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1704 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1705 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1706 | |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1707 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1708 | if (!runtime->initialized) { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1709 | return status; |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1710 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1711 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1712 | /* Get current thread state and interpreter pointer */ |
| 1713 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1714 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1715 | // Wrap up existing "threading"-module-created, non-daemon threads. |
| 1716 | wait_for_thread_shutdown(tstate); |
| 1717 | |
| 1718 | // Make any remaining pending calls. |
Victor Stinner | 2b1df45 | 2020-01-13 18:46:59 +0100 | [diff] [blame] | 1719 | _Py_FinishPendingCalls(tstate); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1720 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1721 | /* The interpreter is still entirely intact at this point, and the |
| 1722 | * exit funcs may be relying on that. In particular, if some thread |
| 1723 | * or exit func is still waiting to do an import, the import machinery |
| 1724 | * expects Py_IsInitialized() to return true. So don't say the |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 1725 | * runtime is uninitialized until after the exit funcs have run. |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1726 | * Note that Threading.py uses an exit func to do a join on all the |
| 1727 | * threads created thru it, so this also protects pending imports in |
| 1728 | * the threads created via Threading. |
| 1729 | */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1730 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1731 | _PyAtExit_Call(tstate->interp); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1732 | |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1733 | /* Copy the core config, PyInterpreterState_Delete() free |
| 1734 | the core config memory */ |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1735 | #ifdef Py_REF_DEBUG |
Christian Heimes | 07f2ade | 2020-11-18 16:38:53 +0100 | [diff] [blame] | 1736 | int show_ref_count = tstate->interp->config.show_ref_count; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1737 | #endif |
| 1738 | #ifdef Py_TRACE_REFS |
Christian Heimes | 07f2ade | 2020-11-18 16:38:53 +0100 | [diff] [blame] | 1739 | int dump_refs = tstate->interp->config.dump_refs; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1740 | #endif |
| 1741 | #ifdef WITH_PYMALLOC |
Christian Heimes | 07f2ade | 2020-11-18 16:38:53 +0100 | [diff] [blame] | 1742 | int malloc_stats = tstate->interp->config.malloc_stats; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1743 | #endif |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1744 | |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 1745 | /* Remaining daemon threads will automatically exit |
| 1746 | when they attempt to take the GIL (ex: PyEval_RestoreThread()). */ |
Victor Stinner | 7b3c252 | 2020-03-07 00:24:23 +0100 | [diff] [blame] | 1747 | _PyRuntimeState_SetFinalizing(runtime, tstate); |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1748 | runtime->initialized = 0; |
| 1749 | runtime->core_initialized = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1750 | |
Victor Stinner | 9ad58ac | 2020-03-09 23:37:49 +0100 | [diff] [blame] | 1751 | /* Destroy the state of all threads of the interpreter, except of the |
| 1752 | current thread. In practice, only daemon threads should still be alive, |
| 1753 | except if wait_for_thread_shutdown() has been cancelled by CTRL+C. |
| 1754 | Clear frames of other threads to call objects destructors. Destructors |
| 1755 | will be called in the current Python thread. Since |
| 1756 | _PyRuntimeState_SetFinalizing() has been called, no other Python thread |
| 1757 | can take the GIL at this point: if they try, they will exit |
| 1758 | immediately. */ |
| 1759 | _PyThreadState_DeleteExcept(runtime, tstate); |
| 1760 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1761 | /* Flush sys.stdout and sys.stderr */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1762 | if (flush_std_files() < 0) { |
| 1763 | status = -1; |
| 1764 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1765 | |
| 1766 | /* Disable signal handling */ |
Victor Stinner | 296a796 | 2020-11-17 16:22:23 +0100 | [diff] [blame] | 1767 | _PySignal_Fini(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1768 | |
| 1769 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 1770 | * before all modules are destroyed. |
| 1771 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 1772 | * XXX import a module, bad things can happen, because Python no |
| 1773 | * XXX longer believes it's initialized. |
| 1774 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 1775 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 1776 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 1777 | * XXX in <function callback at 0x008F5718> ignored |
| 1778 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 1779 | * XXX I haven't seen a real-life report of either of these. |
| 1780 | */ |
Victor Stinner | 8b34148 | 2020-10-30 17:00:00 +0100 | [diff] [blame] | 1781 | PyGC_Collect(); |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1782 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1783 | /* Destroy all modules */ |
Victor Stinner | dff1ad5 | 2020-10-30 18:03:28 +0100 | [diff] [blame] | 1784 | finalize_modules(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1785 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1786 | /* Print debug stats if any */ |
| 1787 | _PyEval_Fini(); |
| 1788 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1789 | /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1790 | if (flush_std_files() < 0) { |
| 1791 | status = -1; |
| 1792 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1793 | |
| 1794 | /* Collect final garbage. This disposes of cycles created by |
| 1795 | * class definitions, for example. |
| 1796 | * XXX This is disabled because it caused too many problems. If |
| 1797 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 1798 | * XXX a hard time running, because even the sys module has been |
| 1799 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 1800 | * XXX One symptom is a sequence of information-free messages |
| 1801 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 1802 | * XXX other threads can execute too, and any exception they encounter |
| 1803 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 1804 | * XXX fails to find what it *expects* to find in sys to help report |
| 1805 | * XXX the exception and consequent unexpected failures). I've also |
| 1806 | * XXX seen segfaults then, after adding print statements to the |
| 1807 | * XXX Python code getting called. |
| 1808 | */ |
| 1809 | #if 0 |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1810 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1811 | #endif |
| 1812 | |
| 1813 | /* Disable tracemalloc after all Python objects have been destroyed, |
| 1814 | so it is possible to use tracemalloc in objects destructor. */ |
| 1815 | _PyTraceMalloc_Fini(); |
| 1816 | |
| 1817 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 1818 | _PyImport_Fini(); |
| 1819 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1820 | /* unload faulthandler module */ |
| 1821 | _PyFaulthandler_Fini(); |
| 1822 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1823 | /* dump hash stats */ |
| 1824 | _PyHash_Fini(); |
| 1825 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1826 | #ifdef Py_REF_DEBUG |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1827 | if (show_ref_count) { |
Victor Stinner | 25420fe | 2017-11-20 18:12:22 -0800 | [diff] [blame] | 1828 | _PyDebug_PrintTotalRefs(); |
| 1829 | } |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1830 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1831 | |
| 1832 | #ifdef Py_TRACE_REFS |
| 1833 | /* Display all objects still alive -- this can invoke arbitrary |
| 1834 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 1835 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 1836 | * up later. |
| 1837 | */ |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1838 | if (dump_refs) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1839 | _Py_PrintReferences(stderr); |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1840 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1841 | #endif /* Py_TRACE_REFS */ |
| 1842 | |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1843 | finalize_interp_clear(tstate); |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1844 | finalize_interp_delete(tstate->interp); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1845 | |
| 1846 | #ifdef Py_TRACE_REFS |
| 1847 | /* Display addresses (& refcnts) of all objects still alive. |
| 1848 | * An address can be used to find the repr of the object, printed |
| 1849 | * above by _Py_PrintReferences. |
| 1850 | */ |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1851 | if (dump_refs) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1852 | _Py_PrintReferenceAddresses(stderr); |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1853 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1854 | #endif /* Py_TRACE_REFS */ |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1855 | #ifdef WITH_PYMALLOC |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1856 | if (malloc_stats) { |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1857 | _PyObject_DebugMallocStats(stderr); |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1858 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1859 | #endif |
| 1860 | |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1861 | call_ll_exitfuncs(runtime); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1862 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1863 | _PyRuntime_Finalize(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1864 | return status; |
| 1865 | } |
| 1866 | |
| 1867 | void |
| 1868 | Py_Finalize(void) |
| 1869 | { |
| 1870 | Py_FinalizeEx(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1871 | } |
| 1872 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1873 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1874 | /* Create and initialize a new interpreter and thread, and return the |
| 1875 | new thread. This requires that Py_Initialize() has been called |
| 1876 | first. |
| 1877 | |
| 1878 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 1879 | exception information is available even in this case -- the |
| 1880 | exception information is held in the thread, and there is no |
| 1881 | thread. |
| 1882 | |
| 1883 | Locking: as above. |
| 1884 | |
| 1885 | */ |
| 1886 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1887 | static PyStatus |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1888 | new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1889 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1890 | PyStatus status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1891 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1892 | status = _PyRuntime_Initialize(); |
| 1893 | if (_PyStatus_EXCEPTION(status)) { |
| 1894 | return status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1895 | } |
| 1896 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1897 | |
| 1898 | if (!runtime->initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1899 | return _PyStatus_ERR("Py_Initialize must be called first"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1900 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1901 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1902 | /* Issue #10915, #15751: The GIL API doesn't work with multiple |
| 1903 | interpreters: disable PyGILState_Check(). */ |
Victor Stinner | 1c4cbdf | 2020-04-13 11:45:21 +0200 | [diff] [blame] | 1904 | runtime->gilstate.check_enabled = 0; |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1905 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1906 | PyInterpreterState *interp = PyInterpreterState_New(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1907 | if (interp == NULL) { |
| 1908 | *tstate_p = NULL; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1909 | return _PyStatus_OK(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1910 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1911 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1912 | PyThreadState *tstate = PyThreadState_New(interp); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1913 | if (tstate == NULL) { |
| 1914 | PyInterpreterState_Delete(interp); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1915 | *tstate_p = NULL; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1916 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1917 | } |
| 1918 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1919 | PyThreadState *save_tstate = PyThreadState_Swap(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1920 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1921 | /* Copy the current interpreter config into the new interpreter */ |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 1922 | const PyConfig *config; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 1923 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1924 | if (save_tstate != NULL) { |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 1925 | config = _PyInterpreterState_GetConfig(save_tstate->interp); |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 1926 | } |
| 1927 | else |
| 1928 | #endif |
| 1929 | { |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1930 | /* No current thread state, copy from the main interpreter */ |
| 1931 | PyInterpreterState *main_interp = PyInterpreterState_Main(); |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 1932 | config = _PyInterpreterState_GetConfig(main_interp); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 1935 | |
| 1936 | status = _PyConfig_Copy(&interp->config, config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1937 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1938 | goto error; |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1939 | } |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1940 | interp->config._isolated_interpreter = isolated_subinterpreter; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1941 | |
Victor Stinner | 0dd5e7a | 2020-05-05 20:16:37 +0200 | [diff] [blame] | 1942 | status = init_interp_create_gil(tstate); |
| 1943 | if (_PyStatus_EXCEPTION(status)) { |
| 1944 | goto error; |
| 1945 | } |
| 1946 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 1947 | status = pycore_interp_init(tstate); |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1948 | if (_PyStatus_EXCEPTION(status)) { |
| 1949 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1950 | } |
| 1951 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1952 | status = init_interp_main(tstate); |
| 1953 | if (_PyStatus_EXCEPTION(status)) { |
| 1954 | goto error; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1955 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1956 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1957 | *tstate_p = tstate; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1958 | return _PyStatus_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1959 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1960 | error: |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1961 | *tstate_p = NULL; |
| 1962 | |
| 1963 | /* Oops, it didn't work. Undo it all. */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1964 | PyErr_PrintEx(0); |
| 1965 | PyThreadState_Clear(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1966 | PyThreadState_Delete(tstate); |
| 1967 | PyInterpreterState_Delete(interp); |
Victor Stinner | 9da7430 | 2019-11-20 11:17:17 +0100 | [diff] [blame] | 1968 | PyThreadState_Swap(save_tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1969 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1970 | return status; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | PyThreadState * |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1974 | _Py_NewInterpreter(int isolated_subinterpreter) |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1975 | { |
Stéphane Wirtel | 9e06d2b | 2019-03-18 17:10:29 +0100 | [diff] [blame] | 1976 | PyThreadState *tstate = NULL; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1977 | PyStatus status = new_interpreter(&tstate, isolated_subinterpreter); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1978 | if (_PyStatus_EXCEPTION(status)) { |
| 1979 | Py_ExitStatusException(status); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1980 | } |
| 1981 | return tstate; |
| 1982 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1983 | } |
| 1984 | |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 1985 | PyThreadState * |
| 1986 | Py_NewInterpreter(void) |
| 1987 | { |
| 1988 | return _Py_NewInterpreter(0); |
| 1989 | } |
| 1990 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1991 | /* Delete an interpreter and its last thread. This requires that the |
| 1992 | given thread state is current, that the thread has no remaining |
| 1993 | frames, and that it is its interpreter's only remaining thread. |
| 1994 | It is a fatal error to violate these constraints. |
| 1995 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1996 | (Py_FinalizeEx() doesn't have these constraints -- it zaps |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1997 | everything, regardless.) |
| 1998 | |
| 1999 | Locking: as above. |
| 2000 | |
| 2001 | */ |
| 2002 | |
| 2003 | void |
| 2004 | Py_EndInterpreter(PyThreadState *tstate) |
| 2005 | { |
| 2006 | PyInterpreterState *interp = tstate->interp; |
| 2007 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2008 | if (tstate != _PyThreadState_GET()) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2009 | Py_FatalError("thread is not current"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2010 | } |
| 2011 | if (tstate->frame != NULL) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2012 | Py_FatalError("thread still has a frame"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2013 | } |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 2014 | interp->finalizing = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2015 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 2016 | // Wrap up existing "threading"-module-created, non-daemon threads. |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2017 | wait_for_thread_shutdown(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2018 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 2019 | _PyAtExit_Call(tstate->interp); |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2020 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2021 | if (tstate != interp->tstate_head || tstate->next != NULL) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2022 | Py_FatalError("not the last thread"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2023 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2024 | |
Victor Stinner | dff1ad5 | 2020-10-30 18:03:28 +0100 | [diff] [blame] | 2025 | finalize_modules(tstate); |
| 2026 | |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 2027 | finalize_interp_clear(tstate); |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 2028 | finalize_interp_delete(tstate->interp); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2029 | } |
| 2030 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2031 | /* Add the __main__ module */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2032 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2033 | static PyStatus |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2034 | add_main_module(PyInterpreterState *interp) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2035 | { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2036 | PyObject *m, *d, *loader, *ann_dict; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2037 | m = PyImport_AddModule("__main__"); |
| 2038 | if (m == NULL) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2039 | return _PyStatus_ERR("can't create __main__ module"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2040 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2041 | d = PyModule_GetDict(m); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2042 | ann_dict = PyDict_New(); |
| 2043 | if ((ann_dict == NULL) || |
| 2044 | (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2045 | return _PyStatus_ERR("Failed to initialize __main__.__annotations__"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2046 | } |
| 2047 | Py_DECREF(ann_dict); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2048 | |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 2049 | if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) { |
| 2050 | if (PyErr_Occurred()) { |
| 2051 | return _PyStatus_ERR("Failed to test __main__.__builtins__"); |
| 2052 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2053 | PyObject *bimod = PyImport_ImportModule("builtins"); |
| 2054 | if (bimod == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2055 | return _PyStatus_ERR("Failed to retrieve builtins module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2056 | } |
| 2057 | if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2058 | return _PyStatus_ERR("Failed to initialize __main__.__builtins__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2059 | } |
| 2060 | Py_DECREF(bimod); |
| 2061 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2062 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2063 | /* Main is a little special - imp.is_builtin("__main__") will return |
| 2064 | * False, but BuiltinImporter is still the most appropriate initial |
| 2065 | * setting for its __loader__ attribute. A more suitable value will |
| 2066 | * be set if __main__ gets further initialized later in the startup |
| 2067 | * process. |
| 2068 | */ |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 2069 | loader = _PyDict_GetItemStringWithError(d, "__loader__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2070 | if (loader == NULL || loader == Py_None) { |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 2071 | if (PyErr_Occurred()) { |
| 2072 | return _PyStatus_ERR("Failed to test __main__.__loader__"); |
| 2073 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2074 | PyObject *loader = PyObject_GetAttrString(interp->importlib, |
| 2075 | "BuiltinImporter"); |
| 2076 | if (loader == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2077 | return _PyStatus_ERR("Failed to retrieve BuiltinImporter"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2078 | } |
| 2079 | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2080 | return _PyStatus_ERR("Failed to initialize __main__.__loader__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2081 | } |
| 2082 | Py_DECREF(loader); |
| 2083 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2084 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2085 | } |
| 2086 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2087 | /* Import the site module (not into __main__ though) */ |
| 2088 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2089 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2090 | init_import_site(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2091 | { |
| 2092 | PyObject *m; |
| 2093 | m = PyImport_ImportModule("site"); |
| 2094 | if (m == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2095 | return _PyStatus_ERR("Failed to import the site module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2096 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2097 | Py_DECREF(m); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2098 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2099 | } |
| 2100 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2101 | /* Check if a file descriptor is valid or not. |
| 2102 | Return 0 if the file descriptor is invalid, return non-zero otherwise. */ |
| 2103 | static int |
| 2104 | is_valid_fd(int fd) |
| 2105 | { |
Victor Stinner | 3092d6b | 2019-04-17 18:09:12 +0200 | [diff] [blame] | 2106 | /* dup() is faster than fstat(): fstat() can require input/output operations, |
| 2107 | whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python |
| 2108 | startup. Problem: dup() doesn't check if the file descriptor is valid on |
| 2109 | some platforms. |
| 2110 | |
| 2111 | bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other |
| 2112 | side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with |
| 2113 | EBADF. FreeBSD has similar issue (bpo-32849). |
| 2114 | |
| 2115 | Only use dup() on platforms where dup() is enough to detect invalid FD in |
| 2116 | corner cases: on Linux and Windows (bpo-32849). */ |
| 2117 | #if defined(__linux__) || defined(MS_WINDOWS) |
| 2118 | if (fd < 0) { |
| 2119 | return 0; |
| 2120 | } |
| 2121 | int fd2; |
| 2122 | |
| 2123 | _Py_BEGIN_SUPPRESS_IPH |
| 2124 | fd2 = dup(fd); |
| 2125 | if (fd2 >= 0) { |
| 2126 | close(fd2); |
| 2127 | } |
| 2128 | _Py_END_SUPPRESS_IPH |
| 2129 | |
| 2130 | return (fd2 >= 0); |
| 2131 | #else |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 2132 | struct stat st; |
| 2133 | return (fstat(fd, &st) == 0); |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 2134 | #endif |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | /* returns Py_None if the fd is not valid */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2138 | static PyObject* |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2139 | create_stdio(const PyConfig *config, PyObject* io, |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2140 | int fd, int write_mode, const char* name, |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 2141 | const wchar_t* encoding, const wchar_t* errors) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2142 | { |
| 2143 | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
| 2144 | const char* mode; |
| 2145 | const char* newline; |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 2146 | PyObject *line_buffering, *write_through; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2147 | int buffering, isatty; |
| 2148 | _Py_IDENTIFIER(open); |
| 2149 | _Py_IDENTIFIER(isatty); |
| 2150 | _Py_IDENTIFIER(TextIOWrapper); |
| 2151 | _Py_IDENTIFIER(mode); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2152 | const int buffered_stdio = config->buffered_stdio; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2153 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2154 | if (!is_valid_fd(fd)) |
| 2155 | Py_RETURN_NONE; |
| 2156 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2157 | /* stdin is always opened in buffered mode, first because it shouldn't |
| 2158 | make a difference in common use cases, second because TextIOWrapper |
| 2159 | depends on the presence of a read1() method which only exists on |
| 2160 | buffered streams. |
| 2161 | */ |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2162 | if (!buffered_stdio && write_mode) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2163 | buffering = 0; |
| 2164 | else |
| 2165 | buffering = -1; |
| 2166 | if (write_mode) |
| 2167 | mode = "wb"; |
| 2168 | else |
| 2169 | mode = "rb"; |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 2170 | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO", |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2171 | fd, mode, buffering, |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 2172 | Py_None, Py_None, /* encoding, errors */ |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 2173 | Py_None, Py_False); /* newline, closefd */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2174 | if (buf == NULL) |
| 2175 | goto error; |
| 2176 | |
| 2177 | if (buffering) { |
| 2178 | _Py_IDENTIFIER(raw); |
| 2179 | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
| 2180 | if (raw == NULL) |
| 2181 | goto error; |
| 2182 | } |
| 2183 | else { |
| 2184 | raw = buf; |
| 2185 | Py_INCREF(raw); |
| 2186 | } |
| 2187 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 2188 | #ifdef MS_WINDOWS |
| 2189 | /* Windows console IO is always UTF-8 encoded */ |
| 2190 | if (PyWindowsConsoleIO_Check(raw)) |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 2191 | encoding = L"utf-8"; |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 2192 | #endif |
| 2193 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2194 | text = PyUnicode_FromString(name); |
| 2195 | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
| 2196 | goto error; |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 2197 | res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2198 | if (res == NULL) |
| 2199 | goto error; |
| 2200 | isatty = PyObject_IsTrue(res); |
| 2201 | Py_DECREF(res); |
| 2202 | if (isatty == -1) |
| 2203 | goto error; |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2204 | if (!buffered_stdio) |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 2205 | write_through = Py_True; |
| 2206 | else |
| 2207 | write_through = Py_False; |
Jendrik Seipp | 5b90771 | 2020-01-01 23:21:43 +0100 | [diff] [blame] | 2208 | if (buffered_stdio && (isatty || fd == fileno(stderr))) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2209 | line_buffering = Py_True; |
| 2210 | else |
| 2211 | line_buffering = Py_False; |
| 2212 | |
| 2213 | Py_CLEAR(raw); |
| 2214 | Py_CLEAR(text); |
| 2215 | |
| 2216 | #ifdef MS_WINDOWS |
| 2217 | /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" |
| 2218 | newlines to "\n". |
| 2219 | sys.stdout and sys.stderr: translate "\n" to "\r\n". */ |
| 2220 | newline = NULL; |
| 2221 | #else |
| 2222 | /* sys.stdin: split lines at "\n". |
| 2223 | sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ |
| 2224 | newline = "\n"; |
| 2225 | #endif |
| 2226 | |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 2227 | PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1); |
| 2228 | if (encoding_str == NULL) { |
| 2229 | Py_CLEAR(buf); |
| 2230 | goto error; |
| 2231 | } |
| 2232 | |
| 2233 | PyObject *errors_str = PyUnicode_FromWideChar(errors, -1); |
| 2234 | if (errors_str == NULL) { |
| 2235 | Py_CLEAR(buf); |
| 2236 | Py_CLEAR(encoding_str); |
| 2237 | goto error; |
| 2238 | } |
| 2239 | |
| 2240 | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO", |
| 2241 | buf, encoding_str, errors_str, |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 2242 | newline, line_buffering, write_through); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2243 | Py_CLEAR(buf); |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 2244 | Py_CLEAR(encoding_str); |
| 2245 | Py_CLEAR(errors_str); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2246 | if (stream == NULL) |
| 2247 | goto error; |
| 2248 | |
| 2249 | if (write_mode) |
| 2250 | mode = "w"; |
| 2251 | else |
| 2252 | mode = "r"; |
| 2253 | text = PyUnicode_FromString(mode); |
| 2254 | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
| 2255 | goto error; |
| 2256 | Py_CLEAR(text); |
| 2257 | return stream; |
| 2258 | |
| 2259 | error: |
| 2260 | Py_XDECREF(buf); |
| 2261 | Py_XDECREF(stream); |
| 2262 | Py_XDECREF(text); |
| 2263 | Py_XDECREF(raw); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2264 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2265 | if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { |
| 2266 | /* Issue #24891: the file descriptor was closed after the first |
| 2267 | is_valid_fd() check was called. Ignore the OSError and set the |
| 2268 | stream to None. */ |
| 2269 | PyErr_Clear(); |
| 2270 | Py_RETURN_NONE; |
| 2271 | } |
| 2272 | return NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2273 | } |
| 2274 | |
Victor Stinner | 77d668b | 2021-04-12 10:44:53 +0200 | [diff] [blame] | 2275 | /* Set builtins.open to io.open */ |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 2276 | static PyStatus |
Andy Lester | 75cd5bf | 2020-03-12 02:49:05 -0500 | [diff] [blame] | 2277 | init_set_builtins_open(void) |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 2278 | { |
| 2279 | PyObject *iomod = NULL, *wrapper; |
| 2280 | PyObject *bimod = NULL; |
| 2281 | PyStatus res = _PyStatus_OK(); |
| 2282 | |
| 2283 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 2284 | goto error; |
| 2285 | } |
| 2286 | |
| 2287 | if (!(bimod = PyImport_ImportModule("builtins"))) { |
| 2288 | goto error; |
| 2289 | } |
| 2290 | |
Victor Stinner | 77d668b | 2021-04-12 10:44:53 +0200 | [diff] [blame] | 2291 | if (!(wrapper = PyObject_GetAttrString(iomod, "open"))) { |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 2292 | goto error; |
| 2293 | } |
| 2294 | |
| 2295 | /* Set builtins.open */ |
| 2296 | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
| 2297 | Py_DECREF(wrapper); |
| 2298 | goto error; |
| 2299 | } |
| 2300 | Py_DECREF(wrapper); |
| 2301 | goto done; |
| 2302 | |
| 2303 | error: |
| 2304 | res = _PyStatus_ERR("can't initialize io.open"); |
| 2305 | |
| 2306 | done: |
| 2307 | Py_XDECREF(bimod); |
| 2308 | Py_XDECREF(iomod); |
| 2309 | return res; |
| 2310 | } |
| 2311 | |
| 2312 | |
Victor Stinner | 77d668b | 2021-04-12 10:44:53 +0200 | [diff] [blame] | 2313 | /* Create sys.stdin, sys.stdout and sys.stderr */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2314 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2315 | init_sys_streams(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2316 | { |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 2317 | PyObject *iomod = NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2318 | PyObject *std = NULL; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2319 | int fd; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2320 | PyObject * encoding_attr; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2321 | PyStatus res = _PyStatus_OK(); |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 2322 | const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 2323 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2324 | /* Check that stdin is not a directory |
| 2325 | Using shell redirection, you can redirect stdin to a directory, |
| 2326 | crashing the Python interpreter. Catch this common mistake here |
| 2327 | and output a useful error message. Note that under MS Windows, |
| 2328 | the shell already prevents that. */ |
| 2329 | #ifndef MS_WINDOWS |
| 2330 | struct _Py_stat_struct sb; |
| 2331 | if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && |
| 2332 | S_ISDIR(sb.st_mode)) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2333 | return _PyStatus_ERR("<stdin> is a directory, cannot continue"); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2334 | } |
| 2335 | #endif |
| 2336 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2337 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 2338 | goto error; |
| 2339 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2340 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2341 | /* Set sys.stdin */ |
| 2342 | fd = fileno(stdin); |
| 2343 | /* Under some conditions stdin, stdout and stderr may not be connected |
| 2344 | * and fileno() may point to an invalid file descriptor. For example |
| 2345 | * GUI apps don't have valid standard streams by default. |
| 2346 | */ |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2347 | std = create_stdio(config, iomod, fd, 0, "<stdin>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 2348 | config->stdio_encoding, |
| 2349 | config->stdio_errors); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2350 | if (std == NULL) |
| 2351 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2352 | PySys_SetObject("__stdin__", std); |
| 2353 | _PySys_SetObjectId(&PyId_stdin, std); |
| 2354 | Py_DECREF(std); |
| 2355 | |
| 2356 | /* Set sys.stdout */ |
| 2357 | fd = fileno(stdout); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2358 | std = create_stdio(config, iomod, fd, 1, "<stdout>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 2359 | config->stdio_encoding, |
| 2360 | config->stdio_errors); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2361 | if (std == NULL) |
| 2362 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2363 | PySys_SetObject("__stdout__", std); |
| 2364 | _PySys_SetObjectId(&PyId_stdout, std); |
| 2365 | Py_DECREF(std); |
| 2366 | |
| 2367 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 2368 | /* Set sys.stderr, replaces the preliminary stderr */ |
| 2369 | fd = fileno(stderr); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2370 | std = create_stdio(config, iomod, fd, 1, "<stderr>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 2371 | config->stdio_encoding, |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 2372 | L"backslashreplace"); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 2373 | if (std == NULL) |
| 2374 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2375 | |
| 2376 | /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 2377 | when import.c tries to write to stderr in verbose mode. */ |
| 2378 | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 2379 | if (encoding_attr != NULL) { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2380 | const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2381 | if (std_encoding != NULL) { |
| 2382 | PyObject *codec_info = _PyCodec_Lookup(std_encoding); |
| 2383 | Py_XDECREF(codec_info); |
| 2384 | } |
| 2385 | Py_DECREF(encoding_attr); |
| 2386 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2387 | _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2388 | |
| 2389 | if (PySys_SetObject("__stderr__", std) < 0) { |
| 2390 | Py_DECREF(std); |
| 2391 | goto error; |
| 2392 | } |
| 2393 | if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { |
| 2394 | Py_DECREF(std); |
| 2395 | goto error; |
| 2396 | } |
| 2397 | Py_DECREF(std); |
| 2398 | #endif |
| 2399 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2400 | goto done; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2401 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2402 | error: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2403 | res = _PyStatus_ERR("can't initialize sys standard streams"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2404 | |
| 2405 | done: |
Victor Stinner | 124b9eb | 2018-08-29 01:29:06 +0200 | [diff] [blame] | 2406 | _Py_ClearStandardStreamEncoding(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2407 | Py_XDECREF(iomod); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2408 | return res; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2412 | static void |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2413 | _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp, |
| 2414 | PyThreadState *tstate) |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2415 | { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2416 | PUTS(fd, "\n"); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2417 | |
| 2418 | /* display the current Python stack */ |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2419 | _Py_DumpTracebackThreads(fd, interp, tstate); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2420 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2421 | |
| 2422 | /* Print the current exception (if an exception is set) with its traceback, |
| 2423 | or display the current Python stack. |
| 2424 | |
| 2425 | Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is |
| 2426 | called on catastrophic cases. |
| 2427 | |
| 2428 | Return 1 if the traceback was displayed, 0 otherwise. */ |
| 2429 | |
| 2430 | static int |
Andy Lester | 75cd5bf | 2020-03-12 02:49:05 -0500 | [diff] [blame] | 2431 | _Py_FatalError_PrintExc(PyThreadState *tstate) |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2432 | { |
| 2433 | PyObject *ferr, *res; |
| 2434 | PyObject *exception, *v, *tb; |
| 2435 | int has_tb; |
| 2436 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2437 | _PyErr_Fetch(tstate, &exception, &v, &tb); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2438 | if (exception == NULL) { |
| 2439 | /* No current exception */ |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | ferr = _PySys_GetObjectId(&PyId_stderr); |
| 2444 | if (ferr == NULL || ferr == Py_None) { |
| 2445 | /* sys.stderr is not set yet or set to None, |
| 2446 | no need to try to display the exception */ |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2450 | _PyErr_NormalizeException(tstate, &exception, &v, &tb); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2451 | if (tb == NULL) { |
| 2452 | tb = Py_None; |
| 2453 | Py_INCREF(tb); |
| 2454 | } |
| 2455 | PyException_SetTraceback(v, tb); |
| 2456 | if (exception == NULL) { |
| 2457 | /* PyErr_NormalizeException() failed */ |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | has_tb = (tb != Py_None); |
| 2462 | PyErr_Display(exception, v, tb); |
| 2463 | Py_XDECREF(exception); |
| 2464 | Py_XDECREF(v); |
| 2465 | Py_XDECREF(tb); |
| 2466 | |
| 2467 | /* sys.stderr may be buffered: call sys.stderr.flush() */ |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 2468 | res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2469 | if (res == NULL) { |
| 2470 | _PyErr_Clear(tstate); |
| 2471 | } |
| 2472 | else { |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2473 | Py_DECREF(res); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2474 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2475 | |
| 2476 | return has_tb; |
| 2477 | } |
| 2478 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2479 | /* Print fatal error message and abort */ |
| 2480 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2481 | #ifdef MS_WINDOWS |
| 2482 | static void |
| 2483 | fatal_output_debug(const char *msg) |
| 2484 | { |
| 2485 | /* buffer of 256 bytes allocated on the stack */ |
| 2486 | WCHAR buffer[256 / sizeof(WCHAR)]; |
| 2487 | size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; |
| 2488 | size_t msglen; |
| 2489 | |
| 2490 | OutputDebugStringW(L"Fatal Python error: "); |
| 2491 | |
| 2492 | msglen = strlen(msg); |
| 2493 | while (msglen) { |
| 2494 | size_t i; |
| 2495 | |
| 2496 | if (buflen > msglen) { |
| 2497 | buflen = msglen; |
| 2498 | } |
| 2499 | |
| 2500 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 2501 | conversion, assuming that the this error message actually uses |
| 2502 | ASCII only. If this ceases to be true, we will have to convert. */ |
| 2503 | for (i=0; i < buflen; ++i) { |
| 2504 | buffer[i] = msg[i]; |
| 2505 | } |
| 2506 | buffer[i] = L'\0'; |
| 2507 | OutputDebugStringW(buffer); |
| 2508 | |
| 2509 | msg += buflen; |
| 2510 | msglen -= buflen; |
| 2511 | } |
| 2512 | OutputDebugStringW(L"\n"); |
| 2513 | } |
| 2514 | #endif |
| 2515 | |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2516 | |
| 2517 | static void |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2518 | fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime) |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2519 | { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2520 | PUTS(fd, "Python runtime state: "); |
Victor Stinner | 7b3c252 | 2020-03-07 00:24:23 +0100 | [diff] [blame] | 2521 | PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); |
| 2522 | if (finalizing) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2523 | PUTS(fd, "finalizing (tstate=0x"); |
| 2524 | _Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2); |
| 2525 | PUTS(fd, ")"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2526 | } |
| 2527 | else if (runtime->initialized) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2528 | PUTS(fd, "initialized"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2529 | } |
| 2530 | else if (runtime->core_initialized) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2531 | PUTS(fd, "core initialized"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2532 | } |
| 2533 | else if (runtime->preinitialized) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2534 | PUTS(fd, "preinitialized"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2535 | } |
| 2536 | else if (runtime->preinitializing) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2537 | PUTS(fd, "preinitializing"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2538 | } |
| 2539 | else { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2540 | PUTS(fd, "unknown"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2541 | } |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2542 | PUTS(fd, "\n"); |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2546 | static inline void _Py_NO_RETURN |
| 2547 | fatal_error_exit(int status) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2548 | { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2549 | if (status < 0) { |
| 2550 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
| 2551 | DebugBreak(); |
| 2552 | #endif |
| 2553 | abort(); |
| 2554 | } |
| 2555 | else { |
| 2556 | exit(status); |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2561 | // Dump the list of extension modules of sys.modules, excluding stdlib modules |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2562 | // (sys.stdlib_module_names), into fd file descriptor. |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2563 | // |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2564 | // This function is called by a signal handler in faulthandler: avoid memory |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2565 | // allocations and keep the implementation simple. For example, the list is not |
| 2566 | // sorted on purpose. |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2567 | void |
| 2568 | _Py_DumpExtensionModules(int fd, PyInterpreterState *interp) |
| 2569 | { |
| 2570 | if (interp == NULL) { |
| 2571 | return; |
| 2572 | } |
| 2573 | PyObject *modules = interp->modules; |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2574 | if (modules == NULL || !PyDict_Check(modules)) { |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2575 | return; |
| 2576 | } |
| 2577 | |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2578 | Py_ssize_t pos; |
| 2579 | PyObject *key, *value; |
| 2580 | |
| 2581 | // Avoid PyDict_GetItemString() which calls PyUnicode_FromString(), |
| 2582 | // memory cannot be allocated on the heap in a signal handler. |
| 2583 | // Iterate on the dict instead. |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2584 | PyObject *stdlib_module_names = NULL; |
Victor Stinner | 3d55aa9 | 2021-04-07 23:12:45 +0200 | [diff] [blame] | 2585 | if (interp->sysdict != NULL) { |
| 2586 | pos = 0; |
| 2587 | while (PyDict_Next(interp->sysdict, &pos, &key, &value)) { |
| 2588 | if (PyUnicode_Check(key) |
| 2589 | && PyUnicode_CompareWithASCIIString(key, "stdlib_module_names") == 0) { |
| 2590 | stdlib_module_names = value; |
| 2591 | break; |
| 2592 | } |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2593 | } |
| 2594 | } |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2595 | // If we failed to get sys.stdlib_module_names or it's not a frozenset, |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2596 | // don't exclude stdlib modules. |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2597 | if (stdlib_module_names != NULL && !PyFrozenSet_Check(stdlib_module_names)) { |
| 2598 | stdlib_module_names = NULL; |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | // List extensions |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2602 | int header = 1; |
| 2603 | Py_ssize_t count = 0; |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2604 | pos = 0; |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2605 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 2606 | if (!PyUnicode_Check(key)) { |
| 2607 | continue; |
| 2608 | } |
| 2609 | if (!_PyModule_IsExtension(value)) { |
| 2610 | continue; |
| 2611 | } |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2612 | // Use the module name from the sys.modules key, |
| 2613 | // don't attempt to get the module object name. |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2614 | if (stdlib_module_names != NULL) { |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2615 | int is_stdlib_ext = 0; |
| 2616 | |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2617 | Py_ssize_t i = 0; |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2618 | PyObject *item; |
| 2619 | Py_hash_t hash; |
Victor Stinner | 9852cb3 | 2021-01-25 23:12:50 +0100 | [diff] [blame] | 2620 | while (_PySet_NextEntry(stdlib_module_names, &i, &item, &hash)) { |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2621 | if (PyUnicode_Check(item) |
| 2622 | && PyUnicode_Compare(key, item) == 0) |
| 2623 | { |
| 2624 | is_stdlib_ext = 1; |
| 2625 | break; |
| 2626 | } |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2627 | } |
Victor Stinner | db584bd | 2021-01-25 13:24:42 +0100 | [diff] [blame] | 2628 | if (is_stdlib_ext) { |
| 2629 | // Ignore stdlib extension |
| 2630 | continue; |
| 2631 | } |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | if (header) { |
| 2635 | PUTS(fd, "\nExtension modules: "); |
| 2636 | header = 0; |
| 2637 | } |
| 2638 | else { |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2639 | PUTS(fd, ", "); |
| 2640 | } |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2641 | |
| 2642 | _Py_DumpASCII(fd, key); |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2643 | count++; |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2644 | } |
Victor Stinner | 66f77ca | 2021-01-19 23:35:27 +0100 | [diff] [blame] | 2645 | |
| 2646 | if (count) { |
| 2647 | PUTS(fd, " (total: "); |
| 2648 | _Py_DumpDecimal(fd, count); |
| 2649 | PUTS(fd, ")"); |
| 2650 | PUTS(fd, "\n"); |
| 2651 | } |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2655 | static void _Py_NO_RETURN |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2656 | fatal_error(int fd, int header, const char *prefix, const char *msg, |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2657 | int status) |
| 2658 | { |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2659 | static int reentrant = 0; |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2660 | |
| 2661 | if (reentrant) { |
| 2662 | /* Py_FatalError() caused a second fatal error. |
| 2663 | Example: flush_std_files() raises a recursion error. */ |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2664 | fatal_error_exit(status); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2665 | } |
| 2666 | reentrant = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2667 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2668 | if (header) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2669 | PUTS(fd, "Fatal Python error: "); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2670 | if (prefix) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2671 | PUTS(fd, prefix); |
| 2672 | PUTS(fd, ": "); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2673 | } |
| 2674 | if (msg) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2675 | PUTS(fd, msg); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2676 | } |
| 2677 | else { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2678 | PUTS(fd, "<message not set>"); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2679 | } |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2680 | PUTS(fd, "\n"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2681 | } |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2682 | |
| 2683 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2684 | fatal_error_dump_runtime(fd, runtime); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2685 | |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2686 | /* Check if the current thread has a Python thread state |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2687 | and holds the GIL. |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2688 | |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2689 | tss_tstate is NULL if Py_FatalError() is called from a C thread which |
| 2690 | has no Python thread state. |
| 2691 | |
| 2692 | tss_tstate != tstate if the current Python thread does not hold the GIL. |
| 2693 | */ |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2694 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 2695 | PyInterpreterState *interp = NULL; |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2696 | PyThreadState *tss_tstate = PyGILState_GetThisThreadState(); |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2697 | if (tstate != NULL) { |
| 2698 | interp = tstate->interp; |
| 2699 | } |
| 2700 | else if (tss_tstate != NULL) { |
| 2701 | interp = tss_tstate->interp; |
| 2702 | } |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2703 | int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate); |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2704 | |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2705 | if (has_tstate_and_gil) { |
| 2706 | /* If an exception is set, print the exception with its traceback */ |
Andy Lester | 75cd5bf | 2020-03-12 02:49:05 -0500 | [diff] [blame] | 2707 | if (!_Py_FatalError_PrintExc(tss_tstate)) { |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2708 | /* No exception is set, or an exception is set without traceback */ |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2709 | _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | else { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2713 | _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2714 | } |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2715 | |
Victor Stinner | 250035d | 2021-01-18 20:47:13 +0100 | [diff] [blame] | 2716 | _Py_DumpExtensionModules(fd, interp); |
| 2717 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2718 | /* The main purpose of faulthandler is to display the traceback. |
| 2719 | This function already did its best to display a traceback. |
| 2720 | Disable faulthandler to prevent writing a second traceback |
| 2721 | on abort(). */ |
Victor Stinner | 2025d78 | 2016-03-16 23:19:15 +0100 | [diff] [blame] | 2722 | _PyFaulthandler_Fini(); |
| 2723 | |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2724 | /* Check if the current Python thread hold the GIL */ |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2725 | if (has_tstate_and_gil) { |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2726 | /* Flush sys.stdout and sys.stderr */ |
| 2727 | flush_std_files(); |
| 2728 | } |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 2729 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2730 | #ifdef MS_WINDOWS |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2731 | fatal_output_debug(msg); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2732 | #endif /* MS_WINDOWS */ |
| 2733 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2734 | fatal_error_exit(status); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2737 | |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2738 | #undef Py_FatalError |
| 2739 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2740 | void _Py_NO_RETURN |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2741 | Py_FatalError(const char *msg) |
| 2742 | { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2743 | fatal_error(fileno(stderr), 1, NULL, msg, -1); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2746 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2747 | void _Py_NO_RETURN |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2748 | _Py_FatalErrorFunc(const char *func, const char *msg) |
| 2749 | { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2750 | fatal_error(fileno(stderr), 1, func, msg, -1); |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2753 | |
| 2754 | void _Py_NO_RETURN |
| 2755 | _Py_FatalErrorFormat(const char *func, const char *format, ...) |
| 2756 | { |
| 2757 | static int reentrant = 0; |
| 2758 | if (reentrant) { |
| 2759 | /* _Py_FatalErrorFormat() caused a second fatal error */ |
| 2760 | fatal_error_exit(-1); |
| 2761 | } |
| 2762 | reentrant = 1; |
| 2763 | |
| 2764 | FILE *stream = stderr; |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2765 | const int fd = fileno(stream); |
| 2766 | PUTS(fd, "Fatal Python error: "); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2767 | if (func) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2768 | PUTS(fd, func); |
| 2769 | PUTS(fd, ": "); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2770 | } |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2771 | |
| 2772 | va_list vargs; |
| 2773 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2774 | va_start(vargs, format); |
| 2775 | #else |
| 2776 | va_start(vargs); |
| 2777 | #endif |
| 2778 | vfprintf(stream, format, vargs); |
| 2779 | va_end(vargs); |
| 2780 | |
| 2781 | fputs("\n", stream); |
| 2782 | fflush(stream); |
| 2783 | |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2784 | fatal_error(fd, 0, NULL, NULL, -1); |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 2788 | void _Py_NO_RETURN |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2789 | Py_ExitStatusException(PyStatus status) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2790 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2791 | if (_PyStatus_IS_EXIT(status)) { |
| 2792 | exit(status.exitcode); |
Victor Stinner | dbacfc2 | 2019-05-16 16:39:26 +0200 | [diff] [blame] | 2793 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2794 | else if (_PyStatus_IS_ERROR(status)) { |
Victor Stinner | 314b878 | 2021-01-18 18:34:56 +0100 | [diff] [blame] | 2795 | fatal_error(fileno(stderr), 1, status.func, status.err_msg, 1); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 2796 | } |
| 2797 | else { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2798 | Py_FatalError("Py_ExitStatusException() must not be called on success"); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 2799 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2800 | } |
| 2801 | |
Victor Stinner | 357704c | 2020-12-14 23:07:54 +0100 | [diff] [blame] | 2802 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2803 | /* Wait until threading._shutdown completes, provided |
| 2804 | the threading module was imported in the first place. |
| 2805 | The shutdown routine will wait until all non-daemon |
| 2806 | "threading" threads have completed. */ |
| 2807 | static void |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2808 | wait_for_thread_shutdown(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2809 | { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2810 | _Py_IDENTIFIER(_shutdown); |
| 2811 | PyObject *result; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 2812 | PyObject *threading = _PyImport_GetModuleId(&PyId_threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2813 | if (threading == NULL) { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2814 | if (_PyErr_Occurred(tstate)) { |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 2815 | PyErr_WriteUnraisable(NULL); |
| 2816 | } |
| 2817 | /* else: threading not imported */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2818 | return; |
| 2819 | } |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 2820 | result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2821 | if (result == NULL) { |
| 2822 | PyErr_WriteUnraisable(threading); |
| 2823 | } |
| 2824 | else { |
| 2825 | Py_DECREF(result); |
| 2826 | } |
| 2827 | Py_DECREF(threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | #define NEXITFUNCS 32 |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2831 | int Py_AtExit(void (*func)(void)) |
| 2832 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2833 | if (_PyRuntime.nexitfuncs >= NEXITFUNCS) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2834 | return -1; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2835 | _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
| 2839 | static void |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2840 | call_ll_exitfuncs(_PyRuntimeState *runtime) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2841 | { |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2842 | while (runtime->nexitfuncs > 0) { |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 2843 | /* pop last function from the list */ |
| 2844 | runtime->nexitfuncs--; |
| 2845 | void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs]; |
| 2846 | runtime->exitfuncs[runtime->nexitfuncs] = NULL; |
| 2847 | |
| 2848 | exitfunc(); |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2849 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2850 | |
| 2851 | fflush(stdout); |
| 2852 | fflush(stderr); |
| 2853 | } |
| 2854 | |
Victor Stinner | cfc8831 | 2018-08-01 16:41:25 +0200 | [diff] [blame] | 2855 | void _Py_NO_RETURN |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2856 | Py_Exit(int sts) |
| 2857 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 2858 | if (Py_FinalizeEx() < 0) { |
| 2859 | sts = 120; |
| 2860 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2861 | |
| 2862 | exit(sts); |
| 2863 | } |
| 2864 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2865 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2866 | /* |
| 2867 | * The file descriptor fd is considered ``interactive'' if either |
| 2868 | * a) isatty(fd) is TRUE, or |
| 2869 | * b) the -i flag was given, and the filename associated with |
| 2870 | * the descriptor is NULL or "<stdin>" or "???". |
| 2871 | */ |
| 2872 | int |
| 2873 | Py_FdIsInteractive(FILE *fp, const char *filename) |
| 2874 | { |
| 2875 | if (isatty((int)fileno(fp))) |
| 2876 | return 1; |
| 2877 | if (!Py_InteractiveFlag) |
| 2878 | return 0; |
| 2879 | return (filename == NULL) || |
| 2880 | (strcmp(filename, "<stdin>") == 0) || |
| 2881 | (strcmp(filename, "???") == 0); |
| 2882 | } |
| 2883 | |
| 2884 | |
Victor Stinner | a82f63f | 2020-12-09 22:37:27 +0100 | [diff] [blame] | 2885 | int |
| 2886 | _Py_FdIsInteractive(FILE *fp, PyObject *filename) |
| 2887 | { |
| 2888 | if (isatty((int)fileno(fp))) { |
| 2889 | return 1; |
| 2890 | } |
| 2891 | if (!Py_InteractiveFlag) { |
| 2892 | return 0; |
| 2893 | } |
| 2894 | return (filename == NULL) || |
| 2895 | (PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) || |
| 2896 | (PyUnicode_CompareWithASCIIString(filename, "???") == 0); |
| 2897 | } |
| 2898 | |
| 2899 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2900 | /* Wrappers around sigaction() or signal(). */ |
| 2901 | |
| 2902 | PyOS_sighandler_t |
| 2903 | PyOS_getsig(int sig) |
| 2904 | { |
| 2905 | #ifdef HAVE_SIGACTION |
| 2906 | struct sigaction context; |
| 2907 | if (sigaction(sig, NULL, &context) == -1) |
| 2908 | return SIG_ERR; |
| 2909 | return context.sa_handler; |
| 2910 | #else |
| 2911 | PyOS_sighandler_t handler; |
| 2912 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 2913 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 2914 | switch (sig) { |
| 2915 | /* Only these signals are valid */ |
| 2916 | case SIGINT: |
| 2917 | case SIGILL: |
| 2918 | case SIGFPE: |
| 2919 | case SIGSEGV: |
| 2920 | case SIGTERM: |
| 2921 | case SIGBREAK: |
| 2922 | case SIGABRT: |
| 2923 | break; |
| 2924 | /* Don't call signal() with other values or it will assert */ |
| 2925 | default: |
| 2926 | return SIG_ERR; |
| 2927 | } |
| 2928 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
| 2929 | handler = signal(sig, SIG_IGN); |
| 2930 | if (handler != SIG_ERR) |
| 2931 | signal(sig, handler); |
| 2932 | return handler; |
| 2933 | #endif |
| 2934 | } |
| 2935 | |
| 2936 | /* |
| 2937 | * All of the code in this function must only use async-signal-safe functions, |
| 2938 | * listed at `man 7 signal` or |
| 2939 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2940 | */ |
| 2941 | PyOS_sighandler_t |
| 2942 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 2943 | { |
| 2944 | #ifdef HAVE_SIGACTION |
| 2945 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 2946 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 2947 | * changes to invalidate that assumption. |
| 2948 | */ |
| 2949 | struct sigaction context, ocontext; |
| 2950 | context.sa_handler = handler; |
| 2951 | sigemptyset(&context.sa_mask); |
Gregory P. Smith | 02ac6f4 | 2021-03-04 21:49:30 -0800 | [diff] [blame] | 2952 | /* Using SA_ONSTACK is friendlier to other C/C++/Golang-VM code that |
| 2953 | * extension module or embedding code may use where tiny thread stacks |
| 2954 | * are used. https://bugs.python.org/issue43390 */ |
| 2955 | context.sa_flags = SA_ONSTACK; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2956 | if (sigaction(sig, &context, &ocontext) == -1) |
| 2957 | return SIG_ERR; |
| 2958 | return ocontext.sa_handler; |
| 2959 | #else |
| 2960 | PyOS_sighandler_t oldhandler; |
| 2961 | oldhandler = signal(sig, handler); |
| 2962 | #ifdef HAVE_SIGINTERRUPT |
| 2963 | siginterrupt(sig, 1); |
| 2964 | #endif |
| 2965 | return oldhandler; |
| 2966 | #endif |
| 2967 | } |
| 2968 | |
| 2969 | #ifdef __cplusplus |
| 2970 | } |
| 2971 | #endif |