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