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