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