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