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