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