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