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