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