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