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 | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 579 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 580 | |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 581 | |
| 582 | if (!_PyLong_Init(tstate)) { |
| 583 | return _PyStatus_ERR("can't init longs"); |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 584 | } |
Victor Stinner | ef5aa9a | 2019-11-20 00:38:03 +0100 | [diff] [blame] | 585 | |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 586 | if (is_main_interp) { |
Victor Stinner | e7e699e | 2019-11-20 12:08:13 +0100 | [diff] [blame] | 587 | status = _PyUnicode_Init(); |
| 588 | if (_PyStatus_EXCEPTION(status)) { |
| 589 | return status; |
| 590 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 593 | status = _PyExc_Init(); |
| 594 | if (_PyStatus_EXCEPTION(status)) { |
| 595 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Victor Stinner | e7e699e | 2019-11-20 12:08:13 +0100 | [diff] [blame] | 598 | if (is_main_interp) { |
| 599 | if (!_PyFloat_Init()) { |
| 600 | return _PyStatus_ERR("can't init float"); |
| 601 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 602 | |
Victor Stinner | e7e699e | 2019-11-20 12:08:13 +0100 | [diff] [blame] | 603 | if (_PyStructSequence_Init() < 0) { |
| 604 | return _PyStatus_ERR("can't initialize structseq"); |
| 605 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 606 | } |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 607 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 608 | status = _PyErr_Init(); |
| 609 | if (_PyStatus_EXCEPTION(status)) { |
| 610 | return status; |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Victor Stinner | e7e699e | 2019-11-20 12:08:13 +0100 | [diff] [blame] | 613 | if (is_main_interp) { |
| 614 | if (!_PyContext_Init()) { |
| 615 | return _PyStatus_ERR("can't init context"); |
| 616 | } |
Victor Stinner | ef5aa9a | 2019-11-20 00:38:03 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 619 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 623 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 624 | pycore_init_builtins(PyThreadState *tstate) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 625 | { |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 626 | assert(!_PyErr_Occurred(tstate)); |
| 627 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 628 | PyObject *bimod = _PyBuiltin_Init(tstate); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 629 | if (bimod == NULL) { |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 630 | goto error; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 631 | } |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 632 | |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 633 | PyInterpreterState *interp = tstate->interp; |
| 634 | if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) { |
| 635 | goto error; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 636 | } |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 637 | |
| 638 | PyObject *builtins_dict = PyModule_GetDict(bimod); |
| 639 | if (builtins_dict == NULL) { |
| 640 | goto error; |
| 641 | } |
| 642 | Py_INCREF(builtins_dict); |
| 643 | interp->builtins = builtins_dict; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 644 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 645 | PyStatus status = _PyBuiltins_AddExceptions(bimod); |
| 646 | if (_PyStatus_EXCEPTION(status)) { |
| 647 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 648 | } |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 649 | |
| 650 | interp->builtins_copy = PyDict_Copy(interp->builtins); |
| 651 | if (interp->builtins_copy == NULL) { |
| 652 | goto error; |
| 653 | } |
Pablo Galindo | b96c6b0 | 2019-12-04 11:19:59 +0000 | [diff] [blame] | 654 | Py_DECREF(bimod); |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 655 | |
| 656 | assert(!_PyErr_Occurred(tstate)); |
| 657 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 658 | return _PyStatus_OK(); |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 659 | |
| 660 | error: |
Pablo Galindo | b96c6b0 | 2019-12-04 11:19:59 +0000 | [diff] [blame] | 661 | Py_XDECREF(bimod); |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 662 | return _PyStatus_ERR("can't initialize builtins module"); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 666 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 667 | pycore_init_import_warnings(PyThreadState *tstate, PyObject *sysmod) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 668 | { |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 669 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 670 | |
Victor Stinner | 2582d46 | 2019-11-22 19:24:49 +0100 | [diff] [blame] | 671 | PyStatus status = _PyImportHooks_Init(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 672 | if (_PyStatus_EXCEPTION(status)) { |
| 673 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 674 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 675 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 676 | const PyConfig *config = &tstate->interp->config; |
Victor Stinner | 2ec1a1b | 2019-11-22 21:54:33 +0100 | [diff] [blame] | 677 | if (_Py_IsMainInterpreter(tstate)) { |
| 678 | /* Initialize _warnings. */ |
| 679 | if (_PyWarnings_Init() == NULL) { |
| 680 | return _PyStatus_ERR("can't initialize warnings"); |
| 681 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 682 | |
Victor Stinner | 2ec1a1b | 2019-11-22 21:54:33 +0100 | [diff] [blame] | 683 | if (config->_install_importlib) { |
| 684 | status = _PyConfig_WritePathConfig(config); |
| 685 | if (_PyStatus_EXCEPTION(status)) { |
| 686 | return status; |
| 687 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 691 | /* This call sets up builtin and frozen import support */ |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 692 | if (config->_install_importlib) { |
| 693 | status = init_importlib(tstate, sysmod); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 694 | if (_PyStatus_EXCEPTION(status)) { |
| 695 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 696 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 697 | } |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 698 | |
| 699 | assert(!_PyErr_Occurred(tstate)); |
| 700 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 701 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 705 | static PyStatus |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 706 | pycore_interp_init(PyThreadState *tstate) |
| 707 | { |
| 708 | PyStatus status; |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 709 | PyObject *sysmod = NULL; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 710 | |
| 711 | status = pycore_init_types(tstate); |
| 712 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 713 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 716 | status = _PySys_Create(tstate, &sysmod); |
| 717 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 718 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | status = pycore_init_builtins(tstate); |
| 722 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 723 | goto done; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Victor Stinner | 080ee5a | 2019-12-08 21:55:58 +0100 | [diff] [blame] | 726 | status = pycore_init_import_warnings(tstate, sysmod); |
| 727 | |
| 728 | done: |
| 729 | /* sys.modules['sys'] contains a strong reference to the module */ |
| 730 | Py_XDECREF(sysmod); |
| 731 | return status; |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
| 735 | static PyStatus |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 736 | pyinit_config(_PyRuntimeState *runtime, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 737 | PyThreadState **tstate_p, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 738 | const PyConfig *config) |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 739 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 740 | _PyConfig_Write(config, runtime); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 741 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 742 | PyStatus status = pycore_init_runtime(runtime, config); |
| 743 | if (_PyStatus_EXCEPTION(status)) { |
| 744 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 747 | PyThreadState *tstate; |
| 748 | status = pycore_create_interpreter(runtime, config, &tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 749 | if (_PyStatus_EXCEPTION(status)) { |
| 750 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 751 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 752 | config = &tstate->interp->config; |
| 753 | *tstate_p = tstate; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 754 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 755 | status = pycore_interp_init(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 756 | if (_PyStatus_EXCEPTION(status)) { |
| 757 | return status; |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 758 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 759 | |
| 760 | /* Only when we get here is the runtime core fully initialized */ |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 761 | runtime->core_initialized = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 762 | return _PyStatus_OK(); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 765 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 766 | PyStatus |
| 767 | _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args) |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 768 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 769 | PyStatus status; |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 770 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 771 | if (src_config == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 772 | return _PyStatus_ERR("preinitialization config is NULL"); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 775 | status = _PyRuntime_Initialize(); |
| 776 | if (_PyStatus_EXCEPTION(status)) { |
| 777 | return status; |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 778 | } |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 779 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 780 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 781 | if (runtime->preinitialized) { |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 782 | /* If it's already configured: ignored the new configuration */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 783 | return _PyStatus_OK(); |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 786 | /* Note: preinitialized remains 1 on error, it is only set to 0 |
| 787 | at exit on success. */ |
| 788 | runtime->preinitializing = 1; |
| 789 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 790 | PyPreConfig config; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 791 | |
| 792 | status = _PyPreConfig_InitFromPreConfig(&config, src_config); |
| 793 | if (_PyStatus_EXCEPTION(status)) { |
| 794 | return status; |
| 795 | } |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 796 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 797 | status = _PyPreConfig_Read(&config, args); |
| 798 | if (_PyStatus_EXCEPTION(status)) { |
| 799 | return status; |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 802 | status = _PyPreConfig_Write(&config); |
| 803 | if (_PyStatus_EXCEPTION(status)) { |
| 804 | return status; |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 807 | runtime->preinitializing = 0; |
| 808 | runtime->preinitialized = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 809 | return _PyStatus_OK(); |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 812 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 813 | PyStatus |
| 814 | Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv) |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 815 | { |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 816 | _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv}; |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 817 | return _Py_PreInitializeFromPyArgv(src_config, &args); |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 821 | PyStatus |
| 822 | 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] | 823 | { |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 824 | _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv}; |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 825 | return _Py_PreInitializeFromPyArgv(src_config, &args); |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 829 | PyStatus |
| 830 | Py_PreInitialize(const PyPreConfig *src_config) |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 831 | { |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 832 | return _Py_PreInitializeFromPyArgv(src_config, NULL); |
Victor Stinner | a6fbc4e | 2019-03-25 18:37:10 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 836 | PyStatus |
| 837 | _Py_PreInitializeFromConfig(const PyConfig *config, |
| 838 | const _PyArgv *args) |
Victor Stinner | f29084d | 2019-03-20 02:20:13 +0100 | [diff] [blame] | 839 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 840 | assert(config != NULL); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 841 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 842 | PyStatus status = _PyRuntime_Initialize(); |
| 843 | if (_PyStatus_EXCEPTION(status)) { |
| 844 | return status; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 845 | } |
| 846 | _PyRuntimeState *runtime = &_PyRuntime; |
| 847 | |
Victor Stinner | d3b9041 | 2019-09-17 23:59:51 +0200 | [diff] [blame] | 848 | if (runtime->preinitialized) { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 849 | /* Already initialized: do nothing */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 850 | return _PyStatus_OK(); |
Victor Stinner | 70005ac | 2019-05-02 15:25:34 -0400 | [diff] [blame] | 851 | } |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 852 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 853 | PyPreConfig preconfig; |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 854 | |
Victor Stinner | 3c30a76 | 2019-10-01 10:56:37 +0200 | [diff] [blame] | 855 | _PyPreConfig_InitFromConfig(&preconfig, config); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 856 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 857 | if (!config->parse_argv) { |
| 858 | return Py_PreInitialize(&preconfig); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 859 | } |
| 860 | else if (args == NULL) { |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 861 | _PyArgv config_args = { |
| 862 | .use_bytes_argv = 0, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 863 | .argc = config->argv.length, |
| 864 | .wchar_argv = config->argv.items}; |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 865 | return _Py_PreInitializeFromPyArgv(&preconfig, &config_args); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 866 | } |
| 867 | else { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 868 | return _Py_PreInitializeFromPyArgv(&preconfig, args); |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 869 | } |
Victor Stinner | 7d2ef3e | 2019-03-06 00:36:56 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 873 | /* Begin interpreter initialization |
| 874 | * |
| 875 | * On return, the first thread and interpreter state have been created, |
| 876 | * but the compiler, signal handling, multithreading and |
| 877 | * multiple interpreter support, and codec infrastructure are not yet |
| 878 | * available. |
| 879 | * |
| 880 | * The import system will support builtin and frozen modules only. |
| 881 | * The only supported io is writing to sys.stderr |
| 882 | * |
| 883 | * If any operation invoked by this function fails, a fatal error is |
| 884 | * issued and the function does not return. |
| 885 | * |
| 886 | * Any code invoked from this function should *not* assume it has access |
| 887 | * to the Python C API (unless the API is explicitly listed as being |
| 888 | * safe to call without calling Py_Initialize first) |
| 889 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 890 | static PyStatus |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 891 | pyinit_core(_PyRuntimeState *runtime, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 892 | const PyConfig *src_config, |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 893 | PyThreadState **tstate_p) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 894 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 895 | PyStatus status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 896 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 897 | status = _Py_PreInitializeFromConfig(src_config, NULL); |
| 898 | if (_PyStatus_EXCEPTION(status)) { |
| 899 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 902 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 903 | _PyConfig_InitCompatConfig(&config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 904 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 905 | status = _PyConfig_Copy(&config, src_config); |
| 906 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 907 | goto done; |
| 908 | } |
| 909 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 910 | status = PyConfig_Read(&config); |
| 911 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 912 | goto done; |
| 913 | } |
| 914 | |
| 915 | if (!runtime->core_initialized) { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 916 | status = pyinit_config(runtime, tstate_p, &config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 917 | } |
| 918 | else { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 919 | status = pyinit_core_reconfigure(runtime, tstate_p, &config); |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 920 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 921 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 922 | goto done; |
| 923 | } |
| 924 | |
| 925 | done: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 926 | PyConfig_Clear(&config); |
| 927 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 930 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 931 | /* Py_Initialize() has already been called: update the main interpreter |
| 932 | configuration. Example of bpo-34008: Py_Main() called after |
| 933 | Py_Initialize(). */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 934 | static PyStatus |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 935 | _Py_ReconfigureMainInterpreter(PyThreadState *tstate) |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 936 | { |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 937 | PyConfig *config = &tstate->interp->config; |
Victor Stinner | 8b9dbc0 | 2019-03-27 01:36:16 +0100 | [diff] [blame] | 938 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 939 | PyObject *argv = _PyWideStringList_AsList(&config->argv); |
Victor Stinner | 8b9dbc0 | 2019-03-27 01:36:16 +0100 | [diff] [blame] | 940 | if (argv == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 941 | return _PyStatus_NO_MEMORY(); \ |
Victor Stinner | 8b9dbc0 | 2019-03-27 01:36:16 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 944 | int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv); |
Victor Stinner | 8b9dbc0 | 2019-03-27 01:36:16 +0100 | [diff] [blame] | 945 | Py_DECREF(argv); |
| 946 | if (res < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 947 | return _PyStatus_ERR("fail to set sys.argv"); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 948 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 949 | return _PyStatus_OK(); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 952 | |
| 953 | static PyStatus |
| 954 | init_interp_main(PyThreadState *tstate) |
| 955 | { |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 956 | assert(!_PyErr_Occurred(tstate)); |
| 957 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 958 | PyStatus status; |
| 959 | int is_main_interp = _Py_IsMainInterpreter(tstate); |
| 960 | PyInterpreterState *interp = tstate->interp; |
| 961 | PyConfig *config = &interp->config; |
| 962 | |
| 963 | if (!config->_install_importlib) { |
| 964 | /* Special mode for freeze_importlib: run with no import system |
| 965 | * |
| 966 | * This means anything which needs support from extension modules |
| 967 | * or pure Python code in the standard library won't work. |
| 968 | */ |
| 969 | if (is_main_interp) { |
| 970 | interp->runtime->initialized = 1; |
| 971 | } |
| 972 | return _PyStatus_OK(); |
| 973 | } |
| 974 | |
| 975 | if (is_main_interp) { |
| 976 | if (_PyTime_Init() < 0) { |
| 977 | return _PyStatus_ERR("can't initialize time"); |
| 978 | } |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 979 | } |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 980 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 981 | if (_PySys_InitMain(tstate) < 0) { |
| 982 | return _PyStatus_ERR("can't finish initializing sys"); |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | status = init_importlib_external(tstate); |
| 986 | if (_PyStatus_EXCEPTION(status)) { |
| 987 | return status; |
| 988 | } |
| 989 | |
| 990 | if (is_main_interp) { |
| 991 | /* initialize the faulthandler module */ |
| 992 | status = _PyFaulthandler_Init(config->faulthandler); |
| 993 | if (_PyStatus_EXCEPTION(status)) { |
| 994 | return status; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | status = _PyUnicode_InitEncodings(tstate); |
| 999 | if (_PyStatus_EXCEPTION(status)) { |
| 1000 | return status; |
| 1001 | } |
| 1002 | |
| 1003 | if (is_main_interp) { |
| 1004 | if (config->install_signal_handlers) { |
| 1005 | status = init_signals(tstate); |
| 1006 | if (_PyStatus_EXCEPTION(status)) { |
| 1007 | return status; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | if (_PyTraceMalloc_Init(config->tracemalloc) < 0) { |
| 1012 | return _PyStatus_ERR("can't initialize tracemalloc"); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | status = init_sys_streams(tstate); |
| 1017 | if (_PyStatus_EXCEPTION(status)) { |
| 1018 | return status; |
| 1019 | } |
| 1020 | |
| 1021 | status = init_set_builtins_open(tstate); |
| 1022 | if (_PyStatus_EXCEPTION(status)) { |
| 1023 | return status; |
| 1024 | } |
| 1025 | |
| 1026 | status = add_main_module(interp); |
| 1027 | if (_PyStatus_EXCEPTION(status)) { |
| 1028 | return status; |
| 1029 | } |
| 1030 | |
| 1031 | if (is_main_interp) { |
| 1032 | /* Initialize warnings. */ |
| 1033 | PyObject *warnoptions = PySys_GetObject("warnoptions"); |
| 1034 | if (warnoptions != NULL && PyList_Size(warnoptions) > 0) |
| 1035 | { |
| 1036 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 1037 | if (warnings_module == NULL) { |
| 1038 | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
| 1039 | _PyErr_Print(tstate); |
| 1040 | } |
| 1041 | Py_XDECREF(warnings_module); |
| 1042 | } |
| 1043 | |
| 1044 | interp->runtime->initialized = 1; |
| 1045 | } |
| 1046 | |
| 1047 | if (config->site_import) { |
| 1048 | status = init_import_site(); |
| 1049 | if (_PyStatus_EXCEPTION(status)) { |
| 1050 | return status; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | if (is_main_interp) { |
| 1055 | #ifndef MS_WINDOWS |
| 1056 | emit_stderr_warning_for_legacy_locale(interp->runtime); |
| 1057 | #endif |
| 1058 | } |
| 1059 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1060 | assert(!_PyErr_Occurred(tstate)); |
| 1061 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1062 | return _PyStatus_OK(); |
| 1063 | } |
| 1064 | |
| 1065 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1066 | /* Update interpreter state based on supplied configuration settings |
| 1067 | * |
| 1068 | * After calling this function, most of the restrictions on the interpreter |
| 1069 | * are lifted. The only remaining incomplete settings are those related |
| 1070 | * to the main module (sys.argv[0], __main__ metadata) |
| 1071 | * |
| 1072 | * Calling this when the interpreter is not initializing, is already |
| 1073 | * initialized or without a valid current thread state is a fatal error. |
| 1074 | * Other errors should be reported as normal Python exceptions with a |
| 1075 | * non-zero return code. |
| 1076 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1077 | static PyStatus |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 1078 | pyinit_main(PyThreadState *tstate) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1079 | { |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1080 | PyInterpreterState *interp = tstate->interp; |
| 1081 | if (!interp->runtime->core_initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1082 | return _PyStatus_ERR("runtime core not initialized"); |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1083 | } |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1084 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1085 | if (interp->runtime->initialized) { |
| 1086 | return _Py_ReconfigureMainInterpreter(tstate); |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1089 | PyStatus status = init_interp_main(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1090 | if (_PyStatus_EXCEPTION(status)) { |
| 1091 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1092 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1093 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1094 | } |
| 1095 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1096 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1097 | PyStatus |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1098 | _Py_InitializeMain(void) |
| 1099 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1100 | PyStatus status = _PyRuntime_Initialize(); |
| 1101 | if (_PyStatus_EXCEPTION(status)) { |
| 1102 | return status; |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1103 | } |
| 1104 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1105 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 1106 | return pyinit_main(tstate); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1110 | PyStatus |
| 1111 | Py_InitializeFromConfig(const PyConfig *config) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1112 | { |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1113 | if (config == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1114 | return _PyStatus_ERR("initialization config is NULL"); |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1117 | PyStatus status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1118 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1119 | status = _PyRuntime_Initialize(); |
| 1120 | if (_PyStatus_EXCEPTION(status)) { |
| 1121 | return status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1122 | } |
| 1123 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1124 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1125 | PyThreadState *tstate = NULL; |
| 1126 | status = pyinit_core(runtime, config, &tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1127 | if (_PyStatus_EXCEPTION(status)) { |
| 1128 | return status; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1129 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1130 | config = &tstate->interp->config; |
Victor Stinner | bc8ac6b | 2017-11-30 18:03:55 +0100 | [diff] [blame] | 1131 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 1132 | if (config->_init_main) { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 1133 | status = pyinit_main(tstate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1134 | if (_PyStatus_EXCEPTION(status)) { |
| 1135 | return status; |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 1136 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1137 | } |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 1138 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1139 | return _PyStatus_OK(); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1143 | void |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1144 | Py_InitializeEx(int install_sigs) |
| 1145 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1146 | PyStatus status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1147 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1148 | status = _PyRuntime_Initialize(); |
| 1149 | if (_PyStatus_EXCEPTION(status)) { |
| 1150 | Py_ExitStatusException(status); |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1151 | } |
| 1152 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1153 | |
| 1154 | if (runtime->initialized) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1155 | /* bpo-33932: Calling Py_Initialize() twice does nothing. */ |
| 1156 | return; |
| 1157 | } |
| 1158 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1159 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 1160 | _PyConfig_InitCompatConfig(&config); |
Victor Stinner | 441b10c | 2019-09-28 04:28:35 +0200 | [diff] [blame] | 1161 | |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 1162 | config.install_signal_handlers = install_sigs; |
| 1163 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1164 | status = Py_InitializeFromConfig(&config); |
| 1165 | if (_PyStatus_EXCEPTION(status)) { |
| 1166 | Py_ExitStatusException(status); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1167 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | void |
| 1171 | Py_Initialize(void) |
| 1172 | { |
| 1173 | Py_InitializeEx(1); |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 1178 | extern void _Py_dump_counts(FILE*); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1179 | #endif |
| 1180 | |
| 1181 | /* Flush stdout and stderr */ |
| 1182 | |
| 1183 | static int |
| 1184 | file_is_closed(PyObject *fobj) |
| 1185 | { |
| 1186 | int r; |
| 1187 | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
| 1188 | if (tmp == NULL) { |
| 1189 | PyErr_Clear(); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | r = PyObject_IsTrue(tmp); |
| 1193 | Py_DECREF(tmp); |
| 1194 | if (r < 0) |
| 1195 | PyErr_Clear(); |
| 1196 | return r > 0; |
| 1197 | } |
| 1198 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1199 | static int |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1200 | flush_std_files(void) |
| 1201 | { |
| 1202 | PyObject *fout = _PySys_GetObjectId(&PyId_stdout); |
| 1203 | PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); |
| 1204 | PyObject *tmp; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1205 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1206 | |
| 1207 | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1208 | tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1209 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1210 | PyErr_WriteUnraisable(fout); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1211 | status = -1; |
| 1212 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1213 | else |
| 1214 | Py_DECREF(tmp); |
| 1215 | } |
| 1216 | |
| 1217 | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1218 | tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1219 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1220 | PyErr_Clear(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1221 | status = -1; |
| 1222 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1223 | else |
| 1224 | Py_DECREF(tmp); |
| 1225 | } |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1226 | |
| 1227 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /* Undo the effect of Py_Initialize(). |
| 1231 | |
| 1232 | Beware: if multiple interpreter and/or thread states exist, these |
| 1233 | are not wiped out; only the current thread and interpreter state |
| 1234 | are deleted. But since everything else is deleted, those other |
| 1235 | interpreter and thread states should no longer be used. |
| 1236 | |
| 1237 | (XXX We should do better, e.g. wipe out all interpreters and |
| 1238 | threads.) |
| 1239 | |
| 1240 | Locking: as above. |
| 1241 | |
| 1242 | */ |
| 1243 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1244 | |
| 1245 | static void |
| 1246 | finalize_interp_types(PyThreadState *tstate, int is_main_interp) |
| 1247 | { |
| 1248 | if (is_main_interp) { |
| 1249 | /* Sundry finalizers */ |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1250 | _PyFrame_Fini(); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1251 | _PyTuple_Fini(); |
| 1252 | _PyList_Fini(); |
| 1253 | _PySet_Fini(); |
| 1254 | _PyBytes_Fini(); |
Victor Stinner | 630c8df | 2019-12-17 13:02:18 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | _PyLong_Fini(tstate); |
| 1258 | |
| 1259 | if (is_main_interp) { |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1260 | _PyFloat_Fini(); |
| 1261 | _PyDict_Fini(); |
| 1262 | _PySlice_Fini(); |
| 1263 | } |
| 1264 | |
| 1265 | _PyWarnings_Fini(tstate->interp); |
| 1266 | |
| 1267 | if (is_main_interp) { |
| 1268 | _Py_HashRandomization_Fini(); |
| 1269 | _PyArg_Fini(); |
| 1270 | _PyAsyncGen_Fini(); |
| 1271 | _PyContext_Fini(); |
Victor Stinner | 3d48334 | 2019-11-22 12:27:50 +0100 | [diff] [blame] | 1272 | } |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1273 | |
Victor Stinner | 3d48334 | 2019-11-22 12:27:50 +0100 | [diff] [blame] | 1274 | /* Cleanup Unicode implementation */ |
| 1275 | _PyUnicode_Fini(tstate); |
| 1276 | |
| 1277 | if (is_main_interp) { |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1278 | _Py_ClearFileSystemEncoding(); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | static void |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1284 | finalize_interp_clear(PyThreadState *tstate) |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1285 | { |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1286 | int is_main_interp = _Py_IsMainInterpreter(tstate); |
| 1287 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1288 | /* Clear interpreter state and all thread states */ |
| 1289 | PyInterpreterState_Clear(tstate->interp); |
| 1290 | |
Pablo Galindo | ac0e1c2 | 2019-12-04 11:51:03 +0000 | [diff] [blame] | 1291 | /* Trigger a GC collection on subinterpreters*/ |
| 1292 | if (!is_main_interp) { |
| 1293 | _PyGC_CollectNoFail(); |
| 1294 | } |
| 1295 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1296 | finalize_interp_types(tstate, is_main_interp); |
| 1297 | |
| 1298 | if (is_main_interp) { |
| 1299 | /* XXX Still allocated: |
| 1300 | - various static ad-hoc pointers to interned strings |
| 1301 | - int and float free list blocks |
| 1302 | - whatever various modules and libraries allocate |
| 1303 | */ |
| 1304 | |
| 1305 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
| 1306 | |
| 1307 | _PyExc_Fini(); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1308 | } |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1309 | |
| 1310 | _PyGC_Fini(tstate); |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | static void |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1315 | finalize_interp_delete(PyThreadState *tstate) |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1316 | { |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1317 | if (_Py_IsMainInterpreter(tstate)) { |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1318 | /* Cleanup auto-thread-state */ |
| 1319 | _PyGILState_Fini(tstate); |
| 1320 | } |
| 1321 | |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 1322 | PyInterpreterState_Delete(tstate->interp); |
| 1323 | } |
| 1324 | |
| 1325 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1326 | int |
| 1327 | Py_FinalizeEx(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1328 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1329 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1330 | |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1331 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1332 | if (!runtime->initialized) { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1333 | return status; |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1334 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1335 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1336 | /* Get current thread state and interpreter pointer */ |
| 1337 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 1338 | PyInterpreterState *interp = tstate->interp; |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1339 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1340 | // Wrap up existing "threading"-module-created, non-daemon threads. |
| 1341 | wait_for_thread_shutdown(tstate); |
| 1342 | |
| 1343 | // Make any remaining pending calls. |
| 1344 | _Py_FinishPendingCalls(runtime); |
| 1345 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1346 | /* The interpreter is still entirely intact at this point, and the |
| 1347 | * exit funcs may be relying on that. In particular, if some thread |
| 1348 | * or exit func is still waiting to do an import, the import machinery |
| 1349 | * expects Py_IsInitialized() to return true. So don't say the |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 1350 | * runtime is uninitialized until after the exit funcs have run. |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1351 | * Note that Threading.py uses an exit func to do a join on all the |
| 1352 | * threads created thru it, so this also protects pending imports in |
| 1353 | * the threads created via Threading. |
| 1354 | */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1355 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1356 | call_py_exitfuncs(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1357 | |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1358 | /* Copy the core config, PyInterpreterState_Delete() free |
| 1359 | the core config memory */ |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1360 | #ifdef Py_REF_DEBUG |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1361 | int show_ref_count = interp->config.show_ref_count; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1362 | #endif |
| 1363 | #ifdef Py_TRACE_REFS |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1364 | int dump_refs = interp->config.dump_refs; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1365 | #endif |
| 1366 | #ifdef WITH_PYMALLOC |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1367 | int malloc_stats = interp->config.malloc_stats; |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1368 | #endif |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1369 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1370 | /* Remaining threads (e.g. daemon threads) will automatically exit |
| 1371 | after taking the GIL (in PyEval_RestoreThread()). */ |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1372 | runtime->finalizing = tstate; |
| 1373 | runtime->initialized = 0; |
| 1374 | runtime->core_initialized = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1375 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1376 | /* Flush sys.stdout and sys.stderr */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1377 | if (flush_std_files() < 0) { |
| 1378 | status = -1; |
| 1379 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1380 | |
| 1381 | /* Disable signal handling */ |
| 1382 | PyOS_FiniInterrupts(); |
| 1383 | |
| 1384 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 1385 | * before all modules are destroyed. |
| 1386 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 1387 | * XXX import a module, bad things can happen, because Python no |
| 1388 | * XXX longer believes it's initialized. |
| 1389 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 1390 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 1391 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 1392 | * XXX in <function callback at 0x008F5718> ignored |
| 1393 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 1394 | * XXX I haven't seen a real-life report of either of these. |
| 1395 | */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1396 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1397 | #ifdef COUNT_ALLOCS |
| 1398 | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
| 1399 | each collection might release some types from the type |
| 1400 | list, so they become garbage. */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1401 | while (_PyGC_CollectIfEnabled() > 0) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1402 | /* nothing */; |
| 1403 | #endif |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1404 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1405 | /* Clear all loghooks */ |
| 1406 | /* We want minimal exposure of this function, so define the extern |
| 1407 | * here. The linker should discover the correct function without |
| 1408 | * exporting a symbol. */ |
| 1409 | extern void _PySys_ClearAuditHooks(void); |
| 1410 | _PySys_ClearAuditHooks(); |
| 1411 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1412 | /* Destroy all modules */ |
Victor Stinner | 987a0dc | 2019-06-19 10:36:10 +0200 | [diff] [blame] | 1413 | _PyImport_Cleanup(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1414 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1415 | /* Print debug stats if any */ |
| 1416 | _PyEval_Fini(); |
| 1417 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1418 | /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1419 | if (flush_std_files() < 0) { |
| 1420 | status = -1; |
| 1421 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1422 | |
| 1423 | /* Collect final garbage. This disposes of cycles created by |
| 1424 | * class definitions, for example. |
| 1425 | * XXX This is disabled because it caused too many problems. If |
| 1426 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 1427 | * XXX a hard time running, because even the sys module has been |
| 1428 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 1429 | * XXX One symptom is a sequence of information-free messages |
| 1430 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 1431 | * XXX other threads can execute too, and any exception they encounter |
| 1432 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 1433 | * XXX fails to find what it *expects* to find in sys to help report |
| 1434 | * XXX the exception and consequent unexpected failures). I've also |
| 1435 | * XXX seen segfaults then, after adding print statements to the |
| 1436 | * XXX Python code getting called. |
| 1437 | */ |
| 1438 | #if 0 |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1439 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1440 | #endif |
| 1441 | |
| 1442 | /* Disable tracemalloc after all Python objects have been destroyed, |
| 1443 | so it is possible to use tracemalloc in objects destructor. */ |
| 1444 | _PyTraceMalloc_Fini(); |
| 1445 | |
| 1446 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 1447 | _PyImport_Fini(); |
| 1448 | |
| 1449 | /* Cleanup typeobject.c's internal caches. */ |
| 1450 | _PyType_Fini(); |
| 1451 | |
| 1452 | /* unload faulthandler module */ |
| 1453 | _PyFaulthandler_Fini(); |
| 1454 | |
| 1455 | /* Debugging stuff */ |
| 1456 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 1457 | _Py_dump_counts(stderr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1458 | #endif |
| 1459 | /* dump hash stats */ |
| 1460 | _PyHash_Fini(); |
| 1461 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1462 | #ifdef Py_REF_DEBUG |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1463 | if (show_ref_count) { |
Victor Stinner | 25420fe | 2017-11-20 18:12:22 -0800 | [diff] [blame] | 1464 | _PyDebug_PrintTotalRefs(); |
| 1465 | } |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1466 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1467 | |
| 1468 | #ifdef Py_TRACE_REFS |
| 1469 | /* Display all objects still alive -- this can invoke arbitrary |
| 1470 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 1471 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 1472 | * up later. |
| 1473 | */ |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1474 | if (dump_refs) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1475 | _Py_PrintReferences(stderr); |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1476 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1477 | #endif /* Py_TRACE_REFS */ |
| 1478 | |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1479 | finalize_interp_clear(tstate); |
| 1480 | finalize_interp_delete(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1481 | |
| 1482 | #ifdef Py_TRACE_REFS |
| 1483 | /* Display addresses (& refcnts) of all objects still alive. |
| 1484 | * An address can be used to find the repr of the object, printed |
| 1485 | * above by _Py_PrintReferences. |
| 1486 | */ |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1487 | if (dump_refs) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1488 | _Py_PrintReferenceAddresses(stderr); |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1489 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1490 | #endif /* Py_TRACE_REFS */ |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1491 | #ifdef WITH_PYMALLOC |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1492 | if (malloc_stats) { |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1493 | _PyObject_DebugMallocStats(stderr); |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1494 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1495 | #endif |
| 1496 | |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 1497 | call_ll_exitfuncs(runtime); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1498 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1499 | _PyRuntime_Finalize(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1500 | return status; |
| 1501 | } |
| 1502 | |
| 1503 | void |
| 1504 | Py_Finalize(void) |
| 1505 | { |
| 1506 | Py_FinalizeEx(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1507 | } |
| 1508 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1509 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1510 | /* Create and initialize a new interpreter and thread, and return the |
| 1511 | new thread. This requires that Py_Initialize() has been called |
| 1512 | first. |
| 1513 | |
| 1514 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 1515 | exception information is available even in this case -- the |
| 1516 | exception information is held in the thread, and there is no |
| 1517 | thread. |
| 1518 | |
| 1519 | Locking: as above. |
| 1520 | |
| 1521 | */ |
| 1522 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1523 | static PyStatus |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1524 | new_interpreter(PyThreadState **tstate_p) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1525 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1526 | PyStatus status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1527 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1528 | status = _PyRuntime_Initialize(); |
| 1529 | if (_PyStatus_EXCEPTION(status)) { |
| 1530 | return status; |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1531 | } |
| 1532 | _PyRuntimeState *runtime = &_PyRuntime; |
| 1533 | |
| 1534 | if (!runtime->initialized) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1535 | return _PyStatus_ERR("Py_Initialize must be called first"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1536 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1537 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1538 | /* Issue #10915, #15751: The GIL API doesn't work with multiple |
| 1539 | interpreters: disable PyGILState_Check(). */ |
| 1540 | _PyGILState_check_enabled = 0; |
| 1541 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1542 | PyInterpreterState *interp = PyInterpreterState_New(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1543 | if (interp == NULL) { |
| 1544 | *tstate_p = NULL; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1545 | return _PyStatus_OK(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1546 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1547 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1548 | PyThreadState *tstate = PyThreadState_New(interp); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1549 | if (tstate == NULL) { |
| 1550 | PyInterpreterState_Delete(interp); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1551 | *tstate_p = NULL; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1552 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1553 | } |
| 1554 | |
Victor Stinner | 4312522 | 2019-04-24 18:23:53 +0200 | [diff] [blame] | 1555 | PyThreadState *save_tstate = PyThreadState_Swap(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1556 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1557 | /* Copy the current interpreter config into the new interpreter */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1558 | PyConfig *config; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1559 | if (save_tstate != NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1560 | config = &save_tstate->interp->config; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1561 | } else { |
| 1562 | /* No current thread state, copy from the main interpreter */ |
| 1563 | PyInterpreterState *main_interp = PyInterpreterState_Main(); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1564 | config = &main_interp->config; |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1565 | } |
| 1566 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1567 | status = _PyConfig_Copy(&interp->config, config); |
| 1568 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1569 | goto error; |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 1570 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1571 | |
Victor Stinner | d863ade | 2019-12-06 03:37:07 +0100 | [diff] [blame] | 1572 | status = pycore_interp_init(tstate); |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1573 | if (_PyStatus_EXCEPTION(status)) { |
| 1574 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1575 | } |
| 1576 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1577 | status = init_interp_main(tstate); |
| 1578 | if (_PyStatus_EXCEPTION(status)) { |
| 1579 | goto error; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1580 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1581 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1582 | *tstate_p = tstate; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1583 | return _PyStatus_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1584 | |
Victor Stinner | 81fe5bd | 2019-12-06 02:43:30 +0100 | [diff] [blame] | 1585 | error: |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1586 | *tstate_p = NULL; |
| 1587 | |
| 1588 | /* Oops, it didn't work. Undo it all. */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1589 | PyErr_PrintEx(0); |
| 1590 | PyThreadState_Clear(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1591 | PyThreadState_Delete(tstate); |
| 1592 | PyInterpreterState_Delete(interp); |
Victor Stinner | 9da7430 | 2019-11-20 11:17:17 +0100 | [diff] [blame] | 1593 | PyThreadState_Swap(save_tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1594 | |
Victor Stinner | b005136 | 2019-11-22 17:52:42 +0100 | [diff] [blame] | 1595 | return status; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | PyThreadState * |
| 1599 | Py_NewInterpreter(void) |
| 1600 | { |
Stéphane Wirtel | 9e06d2b | 2019-03-18 17:10:29 +0100 | [diff] [blame] | 1601 | PyThreadState *tstate = NULL; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1602 | PyStatus status = new_interpreter(&tstate); |
| 1603 | if (_PyStatus_EXCEPTION(status)) { |
| 1604 | Py_ExitStatusException(status); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1605 | } |
| 1606 | return tstate; |
| 1607 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /* Delete an interpreter and its last thread. This requires that the |
| 1611 | given thread state is current, that the thread has no remaining |
| 1612 | frames, and that it is its interpreter's only remaining thread. |
| 1613 | It is a fatal error to violate these constraints. |
| 1614 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1615 | (Py_FinalizeEx() doesn't have these constraints -- it zaps |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1616 | everything, regardless.) |
| 1617 | |
| 1618 | Locking: as above. |
| 1619 | |
| 1620 | */ |
| 1621 | |
| 1622 | void |
| 1623 | Py_EndInterpreter(PyThreadState *tstate) |
| 1624 | { |
| 1625 | PyInterpreterState *interp = tstate->interp; |
| 1626 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1627 | if (tstate != _PyThreadState_GET()) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1628 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1629 | } |
| 1630 | if (tstate->frame != NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1631 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1632 | } |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 1633 | interp->finalizing = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1634 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 1635 | // Wrap up existing "threading"-module-created, non-daemon threads. |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1636 | wait_for_thread_shutdown(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1637 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1638 | call_py_exitfuncs(tstate); |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 1639 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1640 | if (tstate != interp->tstate_head || tstate->next != NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1641 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1642 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1643 | |
Victor Stinner | 987a0dc | 2019-06-19 10:36:10 +0200 | [diff] [blame] | 1644 | _PyImport_Cleanup(tstate); |
Victor Stinner | b93f31f | 2019-11-20 18:39:12 +0100 | [diff] [blame] | 1645 | finalize_interp_clear(tstate); |
| 1646 | finalize_interp_delete(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1647 | } |
| 1648 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1649 | /* Add the __main__ module */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1650 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1651 | static PyStatus |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1652 | add_main_module(PyInterpreterState *interp) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1653 | { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1654 | PyObject *m, *d, *loader, *ann_dict; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1655 | m = PyImport_AddModule("__main__"); |
| 1656 | if (m == NULL) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1657 | return _PyStatus_ERR("can't create __main__ module"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1658 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1659 | d = PyModule_GetDict(m); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1660 | ann_dict = PyDict_New(); |
| 1661 | if ((ann_dict == NULL) || |
| 1662 | (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1663 | return _PyStatus_ERR("Failed to initialize __main__.__annotations__"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1664 | } |
| 1665 | Py_DECREF(ann_dict); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1666 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1667 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 1668 | PyObject *bimod = PyImport_ImportModule("builtins"); |
| 1669 | if (bimod == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1670 | return _PyStatus_ERR("Failed to retrieve builtins module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1671 | } |
| 1672 | if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1673 | return _PyStatus_ERR("Failed to initialize __main__.__builtins__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1674 | } |
| 1675 | Py_DECREF(bimod); |
| 1676 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1677 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1678 | /* Main is a little special - imp.is_builtin("__main__") will return |
| 1679 | * False, but BuiltinImporter is still the most appropriate initial |
| 1680 | * setting for its __loader__ attribute. A more suitable value will |
| 1681 | * be set if __main__ gets further initialized later in the startup |
| 1682 | * process. |
| 1683 | */ |
| 1684 | loader = PyDict_GetItemString(d, "__loader__"); |
| 1685 | if (loader == NULL || loader == Py_None) { |
| 1686 | PyObject *loader = PyObject_GetAttrString(interp->importlib, |
| 1687 | "BuiltinImporter"); |
| 1688 | if (loader == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1689 | return _PyStatus_ERR("Failed to retrieve BuiltinImporter"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1690 | } |
| 1691 | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1692 | return _PyStatus_ERR("Failed to initialize __main__.__loader__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1693 | } |
| 1694 | Py_DECREF(loader); |
| 1695 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1696 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1697 | } |
| 1698 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1699 | /* Import the site module (not into __main__ though) */ |
| 1700 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1701 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1702 | init_import_site(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1703 | { |
| 1704 | PyObject *m; |
| 1705 | m = PyImport_ImportModule("site"); |
| 1706 | if (m == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1707 | return _PyStatus_ERR("Failed to import the site module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1708 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1709 | Py_DECREF(m); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1710 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1711 | } |
| 1712 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1713 | /* Check if a file descriptor is valid or not. |
| 1714 | Return 0 if the file descriptor is invalid, return non-zero otherwise. */ |
| 1715 | static int |
| 1716 | is_valid_fd(int fd) |
| 1717 | { |
Victor Stinner | 3092d6b | 2019-04-17 18:09:12 +0200 | [diff] [blame] | 1718 | /* dup() is faster than fstat(): fstat() can require input/output operations, |
| 1719 | whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python |
| 1720 | startup. Problem: dup() doesn't check if the file descriptor is valid on |
| 1721 | some platforms. |
| 1722 | |
| 1723 | bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other |
| 1724 | side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with |
| 1725 | EBADF. FreeBSD has similar issue (bpo-32849). |
| 1726 | |
| 1727 | Only use dup() on platforms where dup() is enough to detect invalid FD in |
| 1728 | corner cases: on Linux and Windows (bpo-32849). */ |
| 1729 | #if defined(__linux__) || defined(MS_WINDOWS) |
| 1730 | if (fd < 0) { |
| 1731 | return 0; |
| 1732 | } |
| 1733 | int fd2; |
| 1734 | |
| 1735 | _Py_BEGIN_SUPPRESS_IPH |
| 1736 | fd2 = dup(fd); |
| 1737 | if (fd2 >= 0) { |
| 1738 | close(fd2); |
| 1739 | } |
| 1740 | _Py_END_SUPPRESS_IPH |
| 1741 | |
| 1742 | return (fd2 >= 0); |
| 1743 | #else |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1744 | struct stat st; |
| 1745 | return (fstat(fd, &st) == 0); |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1746 | #endif |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | /* returns Py_None if the fd is not valid */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1750 | static PyObject* |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1751 | create_stdio(const PyConfig *config, PyObject* io, |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1752 | int fd, int write_mode, const char* name, |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 1753 | const wchar_t* encoding, const wchar_t* errors) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1754 | { |
| 1755 | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
| 1756 | const char* mode; |
| 1757 | const char* newline; |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1758 | PyObject *line_buffering, *write_through; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1759 | int buffering, isatty; |
| 1760 | _Py_IDENTIFIER(open); |
| 1761 | _Py_IDENTIFIER(isatty); |
| 1762 | _Py_IDENTIFIER(TextIOWrapper); |
| 1763 | _Py_IDENTIFIER(mode); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1764 | const int buffered_stdio = config->buffered_stdio; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1765 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1766 | if (!is_valid_fd(fd)) |
| 1767 | Py_RETURN_NONE; |
| 1768 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1769 | /* stdin is always opened in buffered mode, first because it shouldn't |
| 1770 | make a difference in common use cases, second because TextIOWrapper |
| 1771 | depends on the presence of a read1() method which only exists on |
| 1772 | buffered streams. |
| 1773 | */ |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1774 | if (!buffered_stdio && write_mode) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1775 | buffering = 0; |
| 1776 | else |
| 1777 | buffering = -1; |
| 1778 | if (write_mode) |
| 1779 | mode = "wb"; |
| 1780 | else |
| 1781 | mode = "rb"; |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 1782 | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO", |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1783 | fd, mode, buffering, |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1784 | Py_None, Py_None, /* encoding, errors */ |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 1785 | Py_None, Py_False); /* newline, closefd */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1786 | if (buf == NULL) |
| 1787 | goto error; |
| 1788 | |
| 1789 | if (buffering) { |
| 1790 | _Py_IDENTIFIER(raw); |
| 1791 | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
| 1792 | if (raw == NULL) |
| 1793 | goto error; |
| 1794 | } |
| 1795 | else { |
| 1796 | raw = buf; |
| 1797 | Py_INCREF(raw); |
| 1798 | } |
| 1799 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 1800 | #ifdef MS_WINDOWS |
| 1801 | /* Windows console IO is always UTF-8 encoded */ |
| 1802 | if (PyWindowsConsoleIO_Check(raw)) |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 1803 | encoding = L"utf-8"; |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 1804 | #endif |
| 1805 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1806 | text = PyUnicode_FromString(name); |
| 1807 | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
| 1808 | goto error; |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1809 | res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1810 | if (res == NULL) |
| 1811 | goto error; |
| 1812 | isatty = PyObject_IsTrue(res); |
| 1813 | Py_DECREF(res); |
| 1814 | if (isatty == -1) |
| 1815 | goto error; |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1816 | if (!buffered_stdio) |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1817 | write_through = Py_True; |
| 1818 | else |
| 1819 | write_through = Py_False; |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1820 | if (isatty && buffered_stdio) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1821 | line_buffering = Py_True; |
| 1822 | else |
| 1823 | line_buffering = Py_False; |
| 1824 | |
| 1825 | Py_CLEAR(raw); |
| 1826 | Py_CLEAR(text); |
| 1827 | |
| 1828 | #ifdef MS_WINDOWS |
| 1829 | /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" |
| 1830 | newlines to "\n". |
| 1831 | sys.stdout and sys.stderr: translate "\n" to "\r\n". */ |
| 1832 | newline = NULL; |
| 1833 | #else |
| 1834 | /* sys.stdin: split lines at "\n". |
| 1835 | sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ |
| 1836 | newline = "\n"; |
| 1837 | #endif |
| 1838 | |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 1839 | PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1); |
| 1840 | if (encoding_str == NULL) { |
| 1841 | Py_CLEAR(buf); |
| 1842 | goto error; |
| 1843 | } |
| 1844 | |
| 1845 | PyObject *errors_str = PyUnicode_FromWideChar(errors, -1); |
| 1846 | if (errors_str == NULL) { |
| 1847 | Py_CLEAR(buf); |
| 1848 | Py_CLEAR(encoding_str); |
| 1849 | goto error; |
| 1850 | } |
| 1851 | |
| 1852 | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO", |
| 1853 | buf, encoding_str, errors_str, |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1854 | newline, line_buffering, write_through); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1855 | Py_CLEAR(buf); |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 1856 | Py_CLEAR(encoding_str); |
| 1857 | Py_CLEAR(errors_str); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1858 | if (stream == NULL) |
| 1859 | goto error; |
| 1860 | |
| 1861 | if (write_mode) |
| 1862 | mode = "w"; |
| 1863 | else |
| 1864 | mode = "r"; |
| 1865 | text = PyUnicode_FromString(mode); |
| 1866 | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
| 1867 | goto error; |
| 1868 | Py_CLEAR(text); |
| 1869 | return stream; |
| 1870 | |
| 1871 | error: |
| 1872 | Py_XDECREF(buf); |
| 1873 | Py_XDECREF(stream); |
| 1874 | Py_XDECREF(text); |
| 1875 | Py_XDECREF(raw); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1876 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1877 | if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { |
| 1878 | /* Issue #24891: the file descriptor was closed after the first |
| 1879 | is_valid_fd() check was called. Ignore the OSError and set the |
| 1880 | stream to None. */ |
| 1881 | PyErr_Clear(); |
| 1882 | Py_RETURN_NONE; |
| 1883 | } |
| 1884 | return NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1885 | } |
| 1886 | |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 1887 | /* Set builtins.open to io.OpenWrapper */ |
| 1888 | static PyStatus |
| 1889 | init_set_builtins_open(PyThreadState *tstate) |
| 1890 | { |
| 1891 | PyObject *iomod = NULL, *wrapper; |
| 1892 | PyObject *bimod = NULL; |
| 1893 | PyStatus res = _PyStatus_OK(); |
| 1894 | |
| 1895 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 1896 | goto error; |
| 1897 | } |
| 1898 | |
| 1899 | if (!(bimod = PyImport_ImportModule("builtins"))) { |
| 1900 | goto error; |
| 1901 | } |
| 1902 | |
| 1903 | if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
| 1904 | goto error; |
| 1905 | } |
| 1906 | |
| 1907 | /* Set builtins.open */ |
| 1908 | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
| 1909 | Py_DECREF(wrapper); |
| 1910 | goto error; |
| 1911 | } |
| 1912 | Py_DECREF(wrapper); |
| 1913 | goto done; |
| 1914 | |
| 1915 | error: |
| 1916 | res = _PyStatus_ERR("can't initialize io.open"); |
| 1917 | |
| 1918 | done: |
| 1919 | Py_XDECREF(bimod); |
| 1920 | Py_XDECREF(iomod); |
| 1921 | return res; |
| 1922 | } |
| 1923 | |
| 1924 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1925 | /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1926 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1927 | init_sys_streams(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1928 | { |
Victor Stinner | e0c9ab8 | 2019-11-22 16:19:14 +0100 | [diff] [blame] | 1929 | PyObject *iomod = NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1930 | PyObject *m; |
| 1931 | PyObject *std = NULL; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1932 | int fd; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1933 | PyObject * encoding_attr; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1934 | PyStatus res = _PyStatus_OK(); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 1935 | const PyConfig *config = &tstate->interp->config; |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1936 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 1937 | /* Check that stdin is not a directory |
| 1938 | Using shell redirection, you can redirect stdin to a directory, |
| 1939 | crashing the Python interpreter. Catch this common mistake here |
| 1940 | and output a useful error message. Note that under MS Windows, |
| 1941 | the shell already prevents that. */ |
| 1942 | #ifndef MS_WINDOWS |
| 1943 | struct _Py_stat_struct sb; |
| 1944 | if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && |
| 1945 | S_ISDIR(sb.st_mode)) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1946 | return _PyStatus_ERR("<stdin> is a directory, cannot continue"); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 1947 | } |
| 1948 | #endif |
| 1949 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1950 | /* Hack to avoid a nasty recursion issue when Python is invoked |
| 1951 | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ |
| 1952 | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { |
| 1953 | goto error; |
| 1954 | } |
| 1955 | Py_DECREF(m); |
| 1956 | |
| 1957 | if (!(m = PyImport_ImportModule("encodings.latin_1"))) { |
| 1958 | goto error; |
| 1959 | } |
| 1960 | Py_DECREF(m); |
| 1961 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1962 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 1963 | goto error; |
| 1964 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1965 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1966 | /* Set sys.stdin */ |
| 1967 | fd = fileno(stdin); |
| 1968 | /* Under some conditions stdin, stdout and stderr may not be connected |
| 1969 | * and fileno() may point to an invalid file descriptor. For example |
| 1970 | * GUI apps don't have valid standard streams by default. |
| 1971 | */ |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1972 | std = create_stdio(config, iomod, fd, 0, "<stdin>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1973 | config->stdio_encoding, |
| 1974 | config->stdio_errors); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1975 | if (std == NULL) |
| 1976 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1977 | PySys_SetObject("__stdin__", std); |
| 1978 | _PySys_SetObjectId(&PyId_stdin, std); |
| 1979 | Py_DECREF(std); |
| 1980 | |
| 1981 | /* Set sys.stdout */ |
| 1982 | fd = fileno(stdout); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1983 | std = create_stdio(config, iomod, fd, 1, "<stdout>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1984 | config->stdio_encoding, |
| 1985 | config->stdio_errors); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1986 | if (std == NULL) |
| 1987 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1988 | PySys_SetObject("__stdout__", std); |
| 1989 | _PySys_SetObjectId(&PyId_stdout, std); |
| 1990 | Py_DECREF(std); |
| 1991 | |
| 1992 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 1993 | /* Set sys.stderr, replaces the preliminary stderr */ |
| 1994 | fd = fileno(stderr); |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 1995 | std = create_stdio(config, iomod, fd, 1, "<stderr>", |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 1996 | config->stdio_encoding, |
Victor Stinner | 709d23d | 2019-05-02 14:56:30 -0400 | [diff] [blame] | 1997 | L"backslashreplace"); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1998 | if (std == NULL) |
| 1999 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2000 | |
| 2001 | /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 2002 | when import.c tries to write to stderr in verbose mode. */ |
| 2003 | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 2004 | if (encoding_attr != NULL) { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2005 | const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2006 | if (std_encoding != NULL) { |
| 2007 | PyObject *codec_info = _PyCodec_Lookup(std_encoding); |
| 2008 | Py_XDECREF(codec_info); |
| 2009 | } |
| 2010 | Py_DECREF(encoding_attr); |
| 2011 | } |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2012 | _PyErr_Clear(tstate); /* Not a fatal error if codec isn't available */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2013 | |
| 2014 | if (PySys_SetObject("__stderr__", std) < 0) { |
| 2015 | Py_DECREF(std); |
| 2016 | goto error; |
| 2017 | } |
| 2018 | if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { |
| 2019 | Py_DECREF(std); |
| 2020 | goto error; |
| 2021 | } |
| 2022 | Py_DECREF(std); |
| 2023 | #endif |
| 2024 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2025 | goto done; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2026 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2027 | error: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2028 | res = _PyStatus_ERR("can't initialize sys standard streams"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2029 | |
| 2030 | done: |
Victor Stinner | 124b9eb | 2018-08-29 01:29:06 +0200 | [diff] [blame] | 2031 | _Py_ClearStandardStreamEncoding(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2032 | Py_XDECREF(iomod); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 2033 | return res; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2037 | static void |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2038 | _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp, |
| 2039 | PyThreadState *tstate) |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2040 | { |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2041 | fputc('\n', stderr); |
| 2042 | fflush(stderr); |
| 2043 | |
| 2044 | /* display the current Python stack */ |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2045 | _Py_DumpTracebackThreads(fd, interp, tstate); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2046 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2047 | |
| 2048 | /* Print the current exception (if an exception is set) with its traceback, |
| 2049 | or display the current Python stack. |
| 2050 | |
| 2051 | Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is |
| 2052 | called on catastrophic cases. |
| 2053 | |
| 2054 | Return 1 if the traceback was displayed, 0 otherwise. */ |
| 2055 | |
| 2056 | static int |
| 2057 | _Py_FatalError_PrintExc(int fd) |
| 2058 | { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2059 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2060 | PyObject *ferr, *res; |
| 2061 | PyObject *exception, *v, *tb; |
| 2062 | int has_tb; |
| 2063 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2064 | _PyErr_Fetch(tstate, &exception, &v, &tb); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2065 | if (exception == NULL) { |
| 2066 | /* No current exception */ |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | ferr = _PySys_GetObjectId(&PyId_stderr); |
| 2071 | if (ferr == NULL || ferr == Py_None) { |
| 2072 | /* sys.stderr is not set yet or set to None, |
| 2073 | no need to try to display the exception */ |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2077 | _PyErr_NormalizeException(tstate, &exception, &v, &tb); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2078 | if (tb == NULL) { |
| 2079 | tb = Py_None; |
| 2080 | Py_INCREF(tb); |
| 2081 | } |
| 2082 | PyException_SetTraceback(v, tb); |
| 2083 | if (exception == NULL) { |
| 2084 | /* PyErr_NormalizeException() failed */ |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | has_tb = (tb != Py_None); |
| 2089 | PyErr_Display(exception, v, tb); |
| 2090 | Py_XDECREF(exception); |
| 2091 | Py_XDECREF(v); |
| 2092 | Py_XDECREF(tb); |
| 2093 | |
| 2094 | /* sys.stderr may be buffered: call sys.stderr.flush() */ |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 2095 | res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2096 | if (res == NULL) { |
| 2097 | _PyErr_Clear(tstate); |
| 2098 | } |
| 2099 | else { |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2100 | Py_DECREF(res); |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2101 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2102 | |
| 2103 | return has_tb; |
| 2104 | } |
| 2105 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2106 | /* Print fatal error message and abort */ |
| 2107 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2108 | #ifdef MS_WINDOWS |
| 2109 | static void |
| 2110 | fatal_output_debug(const char *msg) |
| 2111 | { |
| 2112 | /* buffer of 256 bytes allocated on the stack */ |
| 2113 | WCHAR buffer[256 / sizeof(WCHAR)]; |
| 2114 | size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; |
| 2115 | size_t msglen; |
| 2116 | |
| 2117 | OutputDebugStringW(L"Fatal Python error: "); |
| 2118 | |
| 2119 | msglen = strlen(msg); |
| 2120 | while (msglen) { |
| 2121 | size_t i; |
| 2122 | |
| 2123 | if (buflen > msglen) { |
| 2124 | buflen = msglen; |
| 2125 | } |
| 2126 | |
| 2127 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 2128 | conversion, assuming that the this error message actually uses |
| 2129 | ASCII only. If this ceases to be true, we will have to convert. */ |
| 2130 | for (i=0; i < buflen; ++i) { |
| 2131 | buffer[i] = msg[i]; |
| 2132 | } |
| 2133 | buffer[i] = L'\0'; |
| 2134 | OutputDebugStringW(buffer); |
| 2135 | |
| 2136 | msg += buflen; |
| 2137 | msglen -= buflen; |
| 2138 | } |
| 2139 | OutputDebugStringW(L"\n"); |
| 2140 | } |
| 2141 | #endif |
| 2142 | |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2143 | |
| 2144 | static void |
| 2145 | fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime) |
| 2146 | { |
| 2147 | fprintf(stream, "Python runtime state: "); |
| 2148 | if (runtime->finalizing) { |
| 2149 | fprintf(stream, "finalizing (tstate=%p)", runtime->finalizing); |
| 2150 | } |
| 2151 | else if (runtime->initialized) { |
| 2152 | fprintf(stream, "initialized"); |
| 2153 | } |
| 2154 | else if (runtime->core_initialized) { |
| 2155 | fprintf(stream, "core initialized"); |
| 2156 | } |
| 2157 | else if (runtime->preinitialized) { |
| 2158 | fprintf(stream, "preinitialized"); |
| 2159 | } |
| 2160 | else if (runtime->preinitializing) { |
| 2161 | fprintf(stream, "preinitializing"); |
| 2162 | } |
| 2163 | else { |
| 2164 | fprintf(stream, "unknown"); |
| 2165 | } |
| 2166 | fprintf(stream, "\n"); |
| 2167 | fflush(stream); |
| 2168 | } |
| 2169 | |
| 2170 | |
Benjamin Peterson | cef88b9 | 2017-11-25 13:02:55 -0800 | [diff] [blame] | 2171 | static void _Py_NO_RETURN |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2172 | fatal_error(const char *prefix, const char *msg, int status) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2173 | { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2174 | FILE *stream = stderr; |
| 2175 | const int fd = fileno(stream); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2176 | static int reentrant = 0; |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2177 | |
| 2178 | if (reentrant) { |
| 2179 | /* Py_FatalError() caused a second fatal error. |
| 2180 | Example: flush_std_files() raises a recursion error. */ |
| 2181 | goto exit; |
| 2182 | } |
| 2183 | reentrant = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2184 | |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2185 | fprintf(stream, "Fatal Python error: "); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2186 | if (prefix) { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2187 | fputs(prefix, stream); |
| 2188 | fputs(": ", stream); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2189 | } |
| 2190 | if (msg) { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2191 | fputs(msg, stream); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2192 | } |
| 2193 | else { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2194 | fprintf(stream, "<message not set>"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2195 | } |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2196 | fputs("\n", stream); |
| 2197 | fflush(stream); /* it helps in Windows debug build */ |
| 2198 | |
| 2199 | _PyRuntimeState *runtime = &_PyRuntime; |
| 2200 | fatal_error_dump_runtime(stream, runtime); |
| 2201 | |
| 2202 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 2203 | PyInterpreterState *interp = NULL; |
| 2204 | if (tstate != NULL) { |
| 2205 | interp = tstate->interp; |
| 2206 | } |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2207 | |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2208 | /* Check if the current thread has a Python thread state |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2209 | and holds the GIL. |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2210 | |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2211 | tss_tstate is NULL if Py_FatalError() is called from a C thread which |
| 2212 | has no Python thread state. |
| 2213 | |
| 2214 | tss_tstate != tstate if the current Python thread does not hold the GIL. |
| 2215 | */ |
| 2216 | PyThreadState *tss_tstate = PyGILState_GetThisThreadState(); |
| 2217 | int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate); |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2218 | if (has_tstate_and_gil) { |
| 2219 | /* If an exception is set, print the exception with its traceback */ |
| 2220 | if (!_Py_FatalError_PrintExc(fd)) { |
| 2221 | /* No exception is set, or an exception is set without traceback */ |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2222 | _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | else { |
Victor Stinner | 1ce16fb | 2019-09-18 01:35:33 +0200 | [diff] [blame] | 2226 | _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate); |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2227 | } |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2228 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2229 | /* The main purpose of faulthandler is to display the traceback. |
| 2230 | This function already did its best to display a traceback. |
| 2231 | Disable faulthandler to prevent writing a second traceback |
| 2232 | on abort(). */ |
Victor Stinner | 2025d78 | 2016-03-16 23:19:15 +0100 | [diff] [blame] | 2233 | _PyFaulthandler_Fini(); |
| 2234 | |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2235 | /* Check if the current Python thread hold the GIL */ |
Victor Stinner | 3a228ab | 2018-11-01 00:26:41 +0100 | [diff] [blame] | 2236 | if (has_tstate_and_gil) { |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2237 | /* Flush sys.stdout and sys.stderr */ |
| 2238 | flush_std_files(); |
| 2239 | } |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 2240 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2241 | #ifdef MS_WINDOWS |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2242 | fatal_output_debug(msg); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2243 | #endif /* MS_WINDOWS */ |
| 2244 | |
| 2245 | exit: |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2246 | if (status < 0) { |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2247 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2248 | DebugBreak(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2249 | #endif |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2250 | abort(); |
| 2251 | } |
| 2252 | else { |
| 2253 | exit(status); |
| 2254 | } |
| 2255 | } |
| 2256 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2257 | void _Py_NO_RETURN |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2258 | Py_FatalError(const char *msg) |
| 2259 | { |
| 2260 | fatal_error(NULL, msg, -1); |
| 2261 | } |
| 2262 | |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 2263 | void _Py_NO_RETURN |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2264 | Py_ExitStatusException(PyStatus status) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2265 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2266 | if (_PyStatus_IS_EXIT(status)) { |
| 2267 | exit(status.exitcode); |
Victor Stinner | dbacfc2 | 2019-05-16 16:39:26 +0200 | [diff] [blame] | 2268 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2269 | else if (_PyStatus_IS_ERROR(status)) { |
| 2270 | fatal_error(status.func, status.err_msg, 1); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 2271 | } |
| 2272 | else { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2273 | Py_FatalError("Py_ExitStatusException() must not be called on success"); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 2274 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* Clean up and exit */ |
| 2278 | |
Victor Stinner | d7292b5 | 2016-06-17 12:29:00 +0200 | [diff] [blame] | 2279 | # include "pythread.h" |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2280 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2281 | /* For the atexit module. */ |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2282 | void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2283 | { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2284 | PyInterpreterState *is = _PyInterpreterState_GET_UNSAFE(); |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2285 | |
Antoine Pitrou | fc5db95 | 2017-12-13 02:29:07 +0100 | [diff] [blame] | 2286 | /* Guard against API misuse (see bpo-17852) */ |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 2287 | assert(is->pyexitfunc == NULL || is->pyexitfunc == func); |
| 2288 | |
| 2289 | is->pyexitfunc = func; |
| 2290 | is->pyexitmodule = module; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | static void |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2294 | call_py_exitfuncs(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2295 | { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2296 | PyInterpreterState *interp = tstate->interp; |
| 2297 | if (interp->pyexitfunc == NULL) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2298 | return; |
| 2299 | |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2300 | (*interp->pyexitfunc)(interp->pyexitmodule); |
| 2301 | _PyErr_Clear(tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | /* Wait until threading._shutdown completes, provided |
| 2305 | the threading module was imported in the first place. |
| 2306 | The shutdown routine will wait until all non-daemon |
| 2307 | "threading" threads have completed. */ |
| 2308 | static void |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2309 | wait_for_thread_shutdown(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2310 | { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2311 | _Py_IDENTIFIER(_shutdown); |
| 2312 | PyObject *result; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 2313 | PyObject *threading = _PyImport_GetModuleId(&PyId_threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2314 | if (threading == NULL) { |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2315 | if (_PyErr_Occurred(tstate)) { |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 2316 | PyErr_WriteUnraisable(NULL); |
| 2317 | } |
| 2318 | /* else: threading not imported */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2319 | return; |
| 2320 | } |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 2321 | result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2322 | if (result == NULL) { |
| 2323 | PyErr_WriteUnraisable(threading); |
| 2324 | } |
| 2325 | else { |
| 2326 | Py_DECREF(result); |
| 2327 | } |
| 2328 | Py_DECREF(threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | #define NEXITFUNCS 32 |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2332 | int Py_AtExit(void (*func)(void)) |
| 2333 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2334 | if (_PyRuntime.nexitfuncs >= NEXITFUNCS) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2335 | return -1; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2336 | _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2337 | return 0; |
| 2338 | } |
| 2339 | |
| 2340 | static void |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2341 | call_ll_exitfuncs(_PyRuntimeState *runtime) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2342 | { |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2343 | while (runtime->nexitfuncs > 0) { |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 2344 | /* pop last function from the list */ |
| 2345 | runtime->nexitfuncs--; |
| 2346 | void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs]; |
| 2347 | runtime->exitfuncs[runtime->nexitfuncs] = NULL; |
| 2348 | |
| 2349 | exitfunc(); |
Victor Stinner | 8e91c24 | 2019-04-24 17:24:01 +0200 | [diff] [blame] | 2350 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2351 | |
| 2352 | fflush(stdout); |
| 2353 | fflush(stderr); |
| 2354 | } |
| 2355 | |
Victor Stinner | cfc8831 | 2018-08-01 16:41:25 +0200 | [diff] [blame] | 2356 | void _Py_NO_RETURN |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2357 | Py_Exit(int sts) |
| 2358 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 2359 | if (Py_FinalizeEx() < 0) { |
| 2360 | sts = 120; |
| 2361 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2362 | |
| 2363 | exit(sts); |
| 2364 | } |
| 2365 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2366 | static PyStatus |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2367 | init_signals(PyThreadState *tstate) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2368 | { |
| 2369 | #ifdef SIGPIPE |
| 2370 | PyOS_setsig(SIGPIPE, SIG_IGN); |
| 2371 | #endif |
| 2372 | #ifdef SIGXFZ |
| 2373 | PyOS_setsig(SIGXFZ, SIG_IGN); |
| 2374 | #endif |
| 2375 | #ifdef SIGXFSZ |
| 2376 | PyOS_setsig(SIGXFSZ, SIG_IGN); |
| 2377 | #endif |
| 2378 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
Victor Stinner | b45d259 | 2019-06-20 00:05:23 +0200 | [diff] [blame] | 2379 | if (_PyErr_Occurred(tstate)) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2380 | return _PyStatus_ERR("can't import signal"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2381 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2382 | return _PyStatus_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | |
| 2386 | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
| 2387 | * |
| 2388 | * All of the code in this function must only use async-signal-safe functions, |
| 2389 | * listed at `man 7 signal` or |
| 2390 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2391 | */ |
| 2392 | void |
| 2393 | _Py_RestoreSignals(void) |
| 2394 | { |
| 2395 | #ifdef SIGPIPE |
| 2396 | PyOS_setsig(SIGPIPE, SIG_DFL); |
| 2397 | #endif |
| 2398 | #ifdef SIGXFZ |
| 2399 | PyOS_setsig(SIGXFZ, SIG_DFL); |
| 2400 | #endif |
| 2401 | #ifdef SIGXFSZ |
| 2402 | PyOS_setsig(SIGXFSZ, SIG_DFL); |
| 2403 | #endif |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | /* |
| 2408 | * The file descriptor fd is considered ``interactive'' if either |
| 2409 | * a) isatty(fd) is TRUE, or |
| 2410 | * b) the -i flag was given, and the filename associated with |
| 2411 | * the descriptor is NULL or "<stdin>" or "???". |
| 2412 | */ |
| 2413 | int |
| 2414 | Py_FdIsInteractive(FILE *fp, const char *filename) |
| 2415 | { |
| 2416 | if (isatty((int)fileno(fp))) |
| 2417 | return 1; |
| 2418 | if (!Py_InteractiveFlag) |
| 2419 | return 0; |
| 2420 | return (filename == NULL) || |
| 2421 | (strcmp(filename, "<stdin>") == 0) || |
| 2422 | (strcmp(filename, "???") == 0); |
| 2423 | } |
| 2424 | |
| 2425 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2426 | /* Wrappers around sigaction() or signal(). */ |
| 2427 | |
| 2428 | PyOS_sighandler_t |
| 2429 | PyOS_getsig(int sig) |
| 2430 | { |
| 2431 | #ifdef HAVE_SIGACTION |
| 2432 | struct sigaction context; |
| 2433 | if (sigaction(sig, NULL, &context) == -1) |
| 2434 | return SIG_ERR; |
| 2435 | return context.sa_handler; |
| 2436 | #else |
| 2437 | PyOS_sighandler_t handler; |
| 2438 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 2439 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 2440 | switch (sig) { |
| 2441 | /* Only these signals are valid */ |
| 2442 | case SIGINT: |
| 2443 | case SIGILL: |
| 2444 | case SIGFPE: |
| 2445 | case SIGSEGV: |
| 2446 | case SIGTERM: |
| 2447 | case SIGBREAK: |
| 2448 | case SIGABRT: |
| 2449 | break; |
| 2450 | /* Don't call signal() with other values or it will assert */ |
| 2451 | default: |
| 2452 | return SIG_ERR; |
| 2453 | } |
| 2454 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
| 2455 | handler = signal(sig, SIG_IGN); |
| 2456 | if (handler != SIG_ERR) |
| 2457 | signal(sig, handler); |
| 2458 | return handler; |
| 2459 | #endif |
| 2460 | } |
| 2461 | |
| 2462 | /* |
| 2463 | * All of the code in this function must only use async-signal-safe functions, |
| 2464 | * listed at `man 7 signal` or |
| 2465 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2466 | */ |
| 2467 | PyOS_sighandler_t |
| 2468 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 2469 | { |
| 2470 | #ifdef HAVE_SIGACTION |
| 2471 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 2472 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 2473 | * changes to invalidate that assumption. |
| 2474 | */ |
| 2475 | struct sigaction context, ocontext; |
| 2476 | context.sa_handler = handler; |
| 2477 | sigemptyset(&context.sa_mask); |
| 2478 | context.sa_flags = 0; |
| 2479 | if (sigaction(sig, &context, &ocontext) == -1) |
| 2480 | return SIG_ERR; |
| 2481 | return ocontext.sa_handler; |
| 2482 | #else |
| 2483 | PyOS_sighandler_t oldhandler; |
| 2484 | oldhandler = signal(sig, handler); |
| 2485 | #ifdef HAVE_SIGINTERRUPT |
| 2486 | siginterrupt(sig, 1); |
| 2487 | #endif |
| 2488 | return oldhandler; |
| 2489 | #endif |
| 2490 | } |
| 2491 | |
| 2492 | #ifdef __cplusplus |
| 2493 | } |
| 2494 | #endif |