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