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