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