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