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 | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 57 | static _PyInitError init_sys_streams(void); |
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 | |
| 417 | static char * |
| 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 |
| 443 | static const char *_C_LOCALE_COERCION_WARNING = |
| 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 | |
Stéphane Wirtel | ccfdb60 | 2017-07-25 14:32:08 +0200 | [diff] [blame] | 609 | /* TODO: Progressively move functionality from Py_BeginInitialization to |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 610 | * Py_ReadConfig and Py_EndInitialization |
| 611 | */ |
| 612 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 613 | _PyInitError |
| 614 | _Py_InitializeCore(const _PyCoreConfig *config) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 615 | { |
| 616 | PyInterpreterState *interp; |
| 617 | PyThreadState *tstate; |
| 618 | PyObject *bimod, *sysmod, *pstderr; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 619 | _PyCoreConfig core_config = _PyCoreConfig_INIT; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 620 | _PyMainInterpreterConfig preinit_config = _PyMainInterpreterConfig_INIT; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 621 | _PyInitError err; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 622 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 623 | err = _PyRuntime_Initialize(); |
| 624 | if (_Py_INIT_FAILED(err)) { |
| 625 | return err; |
| 626 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 627 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 628 | if (config != NULL) { |
| 629 | core_config = *config; |
| 630 | } |
| 631 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 632 | if (_PyMem_SetupAllocators(core_config.allocator) < 0) { |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame^] | 633 | return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 636 | if (_PyRuntime.initialized) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 637 | return _Py_INIT_ERR("main interpreter already initialized"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 638 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 639 | if (_PyRuntime.core_initialized) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 640 | return _Py_INIT_ERR("runtime core already initialized"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Py_Finalize leaves _Py_Finalizing set in order to help daemon |
| 644 | * threads behave a little more gracefully at interpreter shutdown. |
| 645 | * We clobber it here so the new interpreter can start with a clean |
| 646 | * slate. |
| 647 | * |
| 648 | * However, this may still lead to misbehaviour if there are daemon |
| 649 | * threads still hanging around from a previous Py_Initialize/Finalize |
| 650 | * pair :( |
| 651 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 652 | _PyRuntime.finalizing = NULL; |
| 653 | |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 654 | #ifndef MS_WINDOWS |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 655 | /* Set up the LC_CTYPE locale, so we can obtain |
| 656 | the locale's charset without having to switch |
| 657 | locales. */ |
xdegaye | 1588be6 | 2017-11-12 12:45:59 +0100 | [diff] [blame] | 658 | _Py_SetLocaleFromEnv(LC_CTYPE); |
Nick Coghlan | eb81795 | 2017-06-18 12:29:42 +1000 | [diff] [blame] | 659 | _emit_stderr_warning_for_legacy_locale(); |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 660 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 661 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 662 | err = _Py_HashRandomization_Init(&core_config); |
| 663 | if (_Py_INIT_FAILED(err)) { |
| 664 | return err; |
| 665 | } |
| 666 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 667 | if (!core_config.use_hash_seed || core_config.hash_seed) { |
| 668 | /* Random or non-zero hash seed */ |
| 669 | Py_HashRandomizationFlag = 1; |
| 670 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 671 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 672 | err = _PyInterpreterState_Enable(&_PyRuntime); |
| 673 | if (_Py_INIT_FAILED(err)) { |
| 674 | return err; |
| 675 | } |
| 676 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 677 | interp = PyInterpreterState_New(); |
| 678 | if (interp == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 679 | return _Py_INIT_ERR("can't make main interpreter"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 680 | interp->core_config = core_config; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 681 | interp->config = preinit_config; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 682 | |
| 683 | tstate = PyThreadState_New(interp); |
| 684 | if (tstate == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 685 | return _Py_INIT_ERR("can't make first thread"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 686 | (void) PyThreadState_Swap(tstate); |
| 687 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 688 | /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 689 | destroying the GIL might fail when it is being referenced from |
| 690 | another running thread (see issue #9901). |
| 691 | Instead we destroy the previously created GIL here, which ensures |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 692 | that we can call Py_Initialize / Py_FinalizeEx multiple times. */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 693 | _PyEval_FiniThreads(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 694 | /* Auto-thread-state API */ |
| 695 | _PyGILState_Init(interp, tstate); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 696 | |
| 697 | _Py_ReadyTypes(); |
| 698 | |
| 699 | if (!_PyFrame_Init()) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 700 | return _Py_INIT_ERR("can't init frames"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 701 | |
| 702 | if (!_PyLong_Init()) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 703 | return _Py_INIT_ERR("can't init longs"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 704 | |
| 705 | if (!PyByteArray_Init()) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 706 | return _Py_INIT_ERR("can't init bytearray"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 707 | |
| 708 | if (!_PyFloat_Init()) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 709 | return _Py_INIT_ERR("can't init float"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 710 | |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 711 | PyObject *modules = PyDict_New(); |
| 712 | if (modules == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 713 | return _Py_INIT_ERR("can't make modules dictionary"); |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 714 | interp->modules = modules; |
| 715 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 716 | err = _PySys_BeginInit(&sysmod); |
| 717 | if (_Py_INIT_FAILED(err)) { |
| 718 | return err; |
| 719 | } |
| 720 | |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 721 | interp->sysdict = PyModule_GetDict(sysmod); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 722 | if (interp->sysdict == NULL) { |
| 723 | return _Py_INIT_ERR("can't initialize sys dict"); |
| 724 | } |
| 725 | |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 726 | Py_INCREF(interp->sysdict); |
| 727 | PyDict_SetItemString(interp->sysdict, "modules", modules); |
| 728 | _PyImport_FixupBuiltin(sysmod, "sys", modules); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 729 | |
| 730 | /* Init Unicode implementation; relies on the codec registry */ |
| 731 | if (_PyUnicode_Init() < 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 732 | return _Py_INIT_ERR("can't initialize unicode"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 733 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 734 | if (_PyStructSequence_Init() < 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 735 | return _Py_INIT_ERR("can't initialize structseq"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 736 | |
| 737 | bimod = _PyBuiltin_Init(); |
| 738 | if (bimod == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 739 | return _Py_INIT_ERR("can't initialize builtins modules"); |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 740 | _PyImport_FixupBuiltin(bimod, "builtins", modules); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 741 | interp->builtins = PyModule_GetDict(bimod); |
| 742 | if (interp->builtins == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 743 | return _Py_INIT_ERR("can't initialize builtins dict"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 744 | Py_INCREF(interp->builtins); |
| 745 | |
| 746 | /* initialize builtin exceptions */ |
| 747 | _PyExc_Init(bimod); |
| 748 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 749 | /* Set up a preliminary stderr printer until we have enough |
| 750 | infrastructure for the io module in place. */ |
| 751 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 752 | if (pstderr == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 753 | return _Py_INIT_ERR("can't set preliminary stderr"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 754 | _PySys_SetObjectId(&PyId_stderr, pstderr); |
| 755 | PySys_SetObject("__stderr__", pstderr); |
| 756 | Py_DECREF(pstderr); |
| 757 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 758 | err = _PyImport_Init(); |
| 759 | if (_Py_INIT_FAILED(err)) { |
| 760 | return err; |
| 761 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 762 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 763 | err = _PyImportHooks_Init(); |
| 764 | if (_Py_INIT_FAILED(err)) { |
| 765 | return err; |
| 766 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 767 | |
| 768 | /* Initialize _warnings. */ |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 769 | if (_PyWarnings_InitWithConfig(&interp->core_config) == NULL) { |
| 770 | return _Py_INIT_ERR("can't initialize warnings"); |
| 771 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 772 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 773 | /* This call sets up builtin and frozen import support */ |
| 774 | if (!interp->core_config._disable_importlib) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 775 | err = initimport(interp, sysmod); |
| 776 | if (_Py_INIT_FAILED(err)) { |
| 777 | return err; |
| 778 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /* Only when we get here is the runtime core fully initialized */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 782 | _PyRuntime.core_initialized = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 783 | return _Py_INIT_OK(); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 786 | /* Read configuration settings from standard locations |
| 787 | * |
| 788 | * This function doesn't make any changes to the interpreter state - it |
| 789 | * merely populates any missing configuration settings. This allows an |
| 790 | * embedding application to completely override a config option by |
| 791 | * setting it before calling this function, or else modify the default |
| 792 | * setting before passing the fully populated config to Py_EndInitialization. |
| 793 | * |
| 794 | * More advanced selective initialization tricks are possible by calling |
| 795 | * this function multiple times with various preconfigured settings. |
| 796 | */ |
| 797 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 798 | _PyInitError |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 799 | _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config) |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 800 | { |
| 801 | /* Signal handlers are installed by default */ |
| 802 | if (config->install_signal_handlers < 0) { |
| 803 | config->install_signal_handlers = 1; |
| 804 | } |
Victor Stinner | f04ebe2 | 2017-11-25 00:01:23 +0100 | [diff] [blame] | 805 | |
| 806 | if (config->program_name == NULL) { |
| 807 | config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); |
| 808 | if (config->program_name == NULL) { |
| 809 | return _Py_INIT_NO_MEMORY(); |
| 810 | } |
| 811 | } |
| 812 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 813 | return _Py_INIT_OK(); |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 816 | |
| 817 | void |
| 818 | _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config) |
| 819 | { |
Victor Stinner | f04ebe2 | 2017-11-25 00:01:23 +0100 | [diff] [blame] | 820 | #define CLEAR(ATTR) \ |
| 821 | do { \ |
| 822 | PyMem_RawFree(ATTR); \ |
| 823 | ATTR = NULL; \ |
| 824 | } while (0) |
| 825 | |
| 826 | CLEAR(config->module_search_path_env); |
| 827 | CLEAR(config->home); |
| 828 | CLEAR(config->program_name); |
| 829 | #undef CLEAR |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 833 | /* Update interpreter state based on supplied configuration settings |
| 834 | * |
| 835 | * After calling this function, most of the restrictions on the interpreter |
| 836 | * are lifted. The only remaining incomplete settings are those related |
| 837 | * to the main module (sys.argv[0], __main__ metadata) |
| 838 | * |
| 839 | * Calling this when the interpreter is not initializing, is already |
| 840 | * initialized or without a valid current thread state is a fatal error. |
| 841 | * Other errors should be reported as normal Python exceptions with a |
| 842 | * non-zero return code. |
| 843 | */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 844 | _PyInitError |
| 845 | _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 846 | { |
| 847 | PyInterpreterState *interp; |
| 848 | PyThreadState *tstate; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 849 | _PyInitError err; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 850 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 851 | if (!_PyRuntime.core_initialized) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 852 | return _Py_INIT_ERR("runtime core not initialized"); |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 853 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 854 | if (_PyRuntime.initialized) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 855 | return _Py_INIT_ERR("main interpreter already initialized"); |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 858 | /* Get current thread state and interpreter pointer */ |
| 859 | tstate = PyThreadState_GET(); |
| 860 | if (!tstate) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 861 | return _Py_INIT_ERR("failed to read thread state"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 862 | interp = tstate->interp; |
| 863 | if (!interp) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 864 | return _Py_INIT_ERR("failed to get interpreter"); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 865 | |
| 866 | /* Now finish configuring the main interpreter */ |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 867 | interp->config = *config; |
| 868 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 869 | if (interp->core_config._disable_importlib) { |
| 870 | /* Special mode for freeze_importlib: run with no import system |
| 871 | * |
| 872 | * This means anything which needs support from extension modules |
| 873 | * or pure Python code in the standard library won't work. |
| 874 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 875 | _PyRuntime.initialized = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 876 | return _Py_INIT_OK(); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 877 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 878 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 879 | /* TODO: Report exceptions rather than fatal errors below here */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 880 | |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 881 | if (_PyTime_Init() < 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 882 | return _Py_INIT_ERR("can't initialize time"); |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 883 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 884 | /* GetPath may initialize state that _PySys_EndInit locks |
| 885 | in, and so has to be called first. */ |
| 886 | err = _PyPathConfig_Init(&interp->config); |
| 887 | if (_Py_INIT_FAILED(err)) { |
| 888 | return err; |
| 889 | } |
| 890 | wchar_t *sys_path = Py_GetPath(); |
| 891 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 892 | /* Finish setting up the sys module and import system */ |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 893 | PySys_SetPath(sys_path); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 894 | if (_PySys_EndInit(interp->sysdict) < 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 895 | return _Py_INIT_ERR("can't finish initializing sys"); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 896 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 897 | err = initexternalimport(interp); |
| 898 | if (_Py_INIT_FAILED(err)) { |
| 899 | return err; |
| 900 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 901 | |
| 902 | /* initialize the faulthandler module */ |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 903 | err = _PyFaulthandler_Init(interp->core_config.faulthandler); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 904 | if (_Py_INIT_FAILED(err)) { |
| 905 | return err; |
| 906 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 907 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 908 | err = initfsencoding(interp); |
| 909 | if (_Py_INIT_FAILED(err)) { |
| 910 | return err; |
| 911 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 912 | |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 913 | if (interp->config.install_signal_handlers) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 914 | err = initsigs(); /* Signal handling stuff, including initintr() */ |
| 915 | if (_Py_INIT_FAILED(err)) { |
| 916 | return err; |
| 917 | } |
| 918 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 919 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 920 | if (_PyTraceMalloc_Init(interp->core_config.tracemalloc) < 0) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 921 | return _Py_INIT_ERR("can't initialize tracemalloc"); |
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 = add_main_module(interp); |
| 924 | if (_Py_INIT_FAILED(err)) { |
| 925 | return err; |
| 926 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 927 | |
| 928 | err = init_sys_streams(); |
| 929 | if (_Py_INIT_FAILED(err)) { |
| 930 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 931 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 932 | |
| 933 | /* Initialize warnings. */ |
| 934 | if (PySys_HasWarnOptions()) { |
| 935 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 936 | if (warnings_module == NULL) { |
| 937 | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
| 938 | PyErr_Print(); |
| 939 | } |
| 940 | Py_XDECREF(warnings_module); |
| 941 | } |
| 942 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 943 | _PyRuntime.initialized = 1; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 944 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 945 | if (!Py_NoSiteFlag) { |
| 946 | err = initsite(); /* Module site */ |
| 947 | if (_Py_INIT_FAILED(err)) { |
| 948 | return err; |
| 949 | } |
| 950 | } |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 951 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 952 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 953 | } |
| 954 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 955 | #undef _INIT_DEBUG_PRINT |
| 956 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 957 | _PyInitError |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 958 | _Py_InitializeEx_Private(int install_sigs, int install_importlib) |
| 959 | { |
| 960 | _PyCoreConfig core_config = _PyCoreConfig_INIT; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 961 | _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 962 | _PyInitError err; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 963 | |
| 964 | /* TODO: Moar config options! */ |
| 965 | core_config.ignore_environment = Py_IgnoreEnvironmentFlag; |
| 966 | core_config._disable_importlib = !install_importlib; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 967 | config.install_signal_handlers = install_sigs; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 968 | |
| 969 | err = _Py_InitializeCore(&core_config); |
| 970 | if (_Py_INIT_FAILED(err)) { |
| 971 | return err; |
| 972 | } |
| 973 | |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 974 | /* TODO: Print any exceptions raised by these operations */ |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 975 | err = _PyMainInterpreterConfig_Read(&config); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 976 | if (_Py_INIT_FAILED(err)) { |
| 977 | return err; |
| 978 | } |
| 979 | |
| 980 | err = _Py_InitializeMainInterpreter(&config); |
| 981 | if (_Py_INIT_FAILED(err)) { |
| 982 | return err; |
| 983 | } |
| 984 | |
| 985 | return _Py_INIT_OK(); |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
| 989 | void |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 990 | Py_InitializeEx(int install_sigs) |
| 991 | { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 992 | _PyInitError err = _Py_InitializeEx_Private(install_sigs, 1); |
| 993 | if (_Py_INIT_FAILED(err)) { |
| 994 | _Py_FatalInitError(err); |
| 995 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | void |
| 999 | Py_Initialize(void) |
| 1000 | { |
| 1001 | Py_InitializeEx(1); |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | #ifdef COUNT_ALLOCS |
| 1006 | extern void dump_counts(FILE*); |
| 1007 | #endif |
| 1008 | |
| 1009 | /* Flush stdout and stderr */ |
| 1010 | |
| 1011 | static int |
| 1012 | file_is_closed(PyObject *fobj) |
| 1013 | { |
| 1014 | int r; |
| 1015 | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
| 1016 | if (tmp == NULL) { |
| 1017 | PyErr_Clear(); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | r = PyObject_IsTrue(tmp); |
| 1021 | Py_DECREF(tmp); |
| 1022 | if (r < 0) |
| 1023 | PyErr_Clear(); |
| 1024 | return r > 0; |
| 1025 | } |
| 1026 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1027 | static int |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1028 | flush_std_files(void) |
| 1029 | { |
| 1030 | PyObject *fout = _PySys_GetObjectId(&PyId_stdout); |
| 1031 | PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); |
| 1032 | PyObject *tmp; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1033 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1034 | |
| 1035 | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1036 | tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1037 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1038 | PyErr_WriteUnraisable(fout); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1039 | status = -1; |
| 1040 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1041 | else |
| 1042 | Py_DECREF(tmp); |
| 1043 | } |
| 1044 | |
| 1045 | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1046 | tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1047 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1048 | PyErr_Clear(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1049 | status = -1; |
| 1050 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1051 | else |
| 1052 | Py_DECREF(tmp); |
| 1053 | } |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1054 | |
| 1055 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* Undo the effect of Py_Initialize(). |
| 1059 | |
| 1060 | Beware: if multiple interpreter and/or thread states exist, these |
| 1061 | are not wiped out; only the current thread and interpreter state |
| 1062 | are deleted. But since everything else is deleted, those other |
| 1063 | interpreter and thread states should no longer be used. |
| 1064 | |
| 1065 | (XXX We should do better, e.g. wipe out all interpreters and |
| 1066 | threads.) |
| 1067 | |
| 1068 | Locking: as above. |
| 1069 | |
| 1070 | */ |
| 1071 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1072 | int |
| 1073 | Py_FinalizeEx(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1074 | { |
| 1075 | PyInterpreterState *interp; |
| 1076 | PyThreadState *tstate; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1077 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1078 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1079 | if (!_PyRuntime.initialized) |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1080 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1081 | |
| 1082 | wait_for_thread_shutdown(); |
| 1083 | |
| 1084 | /* The interpreter is still entirely intact at this point, and the |
| 1085 | * exit funcs may be relying on that. In particular, if some thread |
| 1086 | * or exit func is still waiting to do an import, the import machinery |
| 1087 | * expects Py_IsInitialized() to return true. So don't say the |
| 1088 | * interpreter is uninitialized until after the exit funcs have run. |
| 1089 | * Note that Threading.py uses an exit func to do a join on all the |
| 1090 | * threads created thru it, so this also protects pending imports in |
| 1091 | * the threads created via Threading. |
| 1092 | */ |
| 1093 | call_py_exitfuncs(); |
| 1094 | |
| 1095 | /* Get current thread state and interpreter pointer */ |
| 1096 | tstate = PyThreadState_GET(); |
| 1097 | interp = tstate->interp; |
| 1098 | |
| 1099 | /* Remaining threads (e.g. daemon threads) will automatically exit |
| 1100 | after taking the GIL (in PyEval_RestoreThread()). */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1101 | _PyRuntime.finalizing = tstate; |
| 1102 | _PyRuntime.initialized = 0; |
| 1103 | _PyRuntime.core_initialized = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1104 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1105 | /* Flush sys.stdout and sys.stderr */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1106 | if (flush_std_files() < 0) { |
| 1107 | status = -1; |
| 1108 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1109 | |
| 1110 | /* Disable signal handling */ |
| 1111 | PyOS_FiniInterrupts(); |
| 1112 | |
| 1113 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 1114 | * before all modules are destroyed. |
| 1115 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 1116 | * XXX import a module, bad things can happen, because Python no |
| 1117 | * XXX longer believes it's initialized. |
| 1118 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 1119 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 1120 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 1121 | * XXX in <function callback at 0x008F5718> ignored |
| 1122 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 1123 | * XXX I haven't seen a real-life report of either of these. |
| 1124 | */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1125 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1126 | #ifdef COUNT_ALLOCS |
| 1127 | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
| 1128 | each collection might release some types from the type |
| 1129 | list, so they become garbage. */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1130 | while (_PyGC_CollectIfEnabled() > 0) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1131 | /* nothing */; |
| 1132 | #endif |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1133 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1134 | /* Destroy all modules */ |
| 1135 | PyImport_Cleanup(); |
| 1136 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1137 | /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1138 | if (flush_std_files() < 0) { |
| 1139 | status = -1; |
| 1140 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1141 | |
| 1142 | /* Collect final garbage. This disposes of cycles created by |
| 1143 | * class definitions, for example. |
| 1144 | * XXX This is disabled because it caused too many problems. If |
| 1145 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 1146 | * XXX a hard time running, because even the sys module has been |
| 1147 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 1148 | * XXX One symptom is a sequence of information-free messages |
| 1149 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 1150 | * XXX other threads can execute too, and any exception they encounter |
| 1151 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 1152 | * XXX fails to find what it *expects* to find in sys to help report |
| 1153 | * XXX the exception and consequent unexpected failures). I've also |
| 1154 | * XXX seen segfaults then, after adding print statements to the |
| 1155 | * XXX Python code getting called. |
| 1156 | */ |
| 1157 | #if 0 |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 1158 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1159 | #endif |
| 1160 | |
| 1161 | /* Disable tracemalloc after all Python objects have been destroyed, |
| 1162 | so it is possible to use tracemalloc in objects destructor. */ |
| 1163 | _PyTraceMalloc_Fini(); |
| 1164 | |
| 1165 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 1166 | _PyImport_Fini(); |
| 1167 | |
| 1168 | /* Cleanup typeobject.c's internal caches. */ |
| 1169 | _PyType_Fini(); |
| 1170 | |
| 1171 | /* unload faulthandler module */ |
| 1172 | _PyFaulthandler_Fini(); |
| 1173 | |
| 1174 | /* Debugging stuff */ |
| 1175 | #ifdef COUNT_ALLOCS |
Serhiy Storchaka | 7e160ce | 2016-07-03 21:03:53 +0300 | [diff] [blame] | 1176 | dump_counts(stderr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1177 | #endif |
| 1178 | /* dump hash stats */ |
| 1179 | _PyHash_Fini(); |
| 1180 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1181 | #ifdef Py_REF_DEBUG |
Victor Stinner | 25420fe | 2017-11-20 18:12:22 -0800 | [diff] [blame] | 1182 | if (interp->core_config.show_ref_count) { |
| 1183 | _PyDebug_PrintTotalRefs(); |
| 1184 | } |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1185 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1186 | |
| 1187 | #ifdef Py_TRACE_REFS |
| 1188 | /* Display all objects still alive -- this can invoke arbitrary |
| 1189 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 1190 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 1191 | * up later. |
| 1192 | */ |
| 1193 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 1194 | _Py_PrintReferences(stderr); |
| 1195 | #endif /* Py_TRACE_REFS */ |
| 1196 | |
| 1197 | /* Clear interpreter state and all thread states. */ |
| 1198 | PyInterpreterState_Clear(interp); |
| 1199 | |
| 1200 | /* Now we decref the exception classes. After this point nothing |
| 1201 | can raise an exception. That's okay, because each Fini() method |
| 1202 | below has been checked to make sure no exceptions are ever |
| 1203 | raised. |
| 1204 | */ |
| 1205 | |
| 1206 | _PyExc_Fini(); |
| 1207 | |
| 1208 | /* Sundry finalizers */ |
| 1209 | PyMethod_Fini(); |
| 1210 | PyFrame_Fini(); |
| 1211 | PyCFunction_Fini(); |
| 1212 | PyTuple_Fini(); |
| 1213 | PyList_Fini(); |
| 1214 | PySet_Fini(); |
| 1215 | PyBytes_Fini(); |
| 1216 | PyByteArray_Fini(); |
| 1217 | PyLong_Fini(); |
| 1218 | PyFloat_Fini(); |
| 1219 | PyDict_Fini(); |
| 1220 | PySlice_Fini(); |
| 1221 | _PyGC_Fini(); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 1222 | _Py_HashRandomization_Fini(); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1223 | _PyArg_Fini(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1224 | PyAsyncGen_Fini(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1225 | |
| 1226 | /* Cleanup Unicode implementation */ |
| 1227 | _PyUnicode_Fini(); |
| 1228 | |
| 1229 | /* reset file system default encoding */ |
| 1230 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
| 1231 | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
| 1232 | Py_FileSystemDefaultEncoding = NULL; |
| 1233 | } |
| 1234 | |
| 1235 | /* XXX Still allocated: |
| 1236 | - various static ad-hoc pointers to interned strings |
| 1237 | - int and float free list blocks |
| 1238 | - whatever various modules and libraries allocate |
| 1239 | */ |
| 1240 | |
| 1241 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
| 1242 | |
| 1243 | /* Cleanup auto-thread-state */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1244 | _PyGILState_Fini(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1245 | |
| 1246 | /* Delete current thread. After this, many C API calls become crashy. */ |
| 1247 | PyThreadState_Swap(NULL); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1248 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1249 | PyInterpreterState_Delete(interp); |
| 1250 | |
| 1251 | #ifdef Py_TRACE_REFS |
| 1252 | /* Display addresses (& refcnts) of all objects still alive. |
| 1253 | * An address can be used to find the repr of the object, printed |
| 1254 | * above by _Py_PrintReferences. |
| 1255 | */ |
| 1256 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 1257 | _Py_PrintReferenceAddresses(stderr); |
| 1258 | #endif /* Py_TRACE_REFS */ |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1259 | #ifdef WITH_PYMALLOC |
| 1260 | if (_PyMem_PymallocEnabled()) { |
| 1261 | char *opt = Py_GETENV("PYTHONMALLOCSTATS"); |
| 1262 | if (opt != NULL && *opt != '\0') |
| 1263 | _PyObject_DebugMallocStats(stderr); |
| 1264 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
| 1267 | call_ll_exitfuncs(); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1268 | |
| 1269 | _PyPathConfig_Fini(); |
| 1270 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1271 | _PyRuntime_Finalize(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1272 | return status; |
| 1273 | } |
| 1274 | |
| 1275 | void |
| 1276 | Py_Finalize(void) |
| 1277 | { |
| 1278 | Py_FinalizeEx(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* Create and initialize a new interpreter and thread, and return the |
| 1282 | new thread. This requires that Py_Initialize() has been called |
| 1283 | first. |
| 1284 | |
| 1285 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 1286 | exception information is available even in this case -- the |
| 1287 | exception information is held in the thread, and there is no |
| 1288 | thread. |
| 1289 | |
| 1290 | Locking: as above. |
| 1291 | |
| 1292 | */ |
| 1293 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1294 | static _PyInitError |
| 1295 | new_interpreter(PyThreadState **tstate_p) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1296 | { |
| 1297 | PyInterpreterState *interp; |
| 1298 | PyThreadState *tstate, *save_tstate; |
| 1299 | PyObject *bimod, *sysmod; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1300 | _PyInitError err; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1301 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1302 | if (!_PyRuntime.initialized) { |
| 1303 | return _Py_INIT_ERR("Py_Initialize must be called first"); |
| 1304 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1305 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1306 | /* Issue #10915, #15751: The GIL API doesn't work with multiple |
| 1307 | interpreters: disable PyGILState_Check(). */ |
| 1308 | _PyGILState_check_enabled = 0; |
| 1309 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1310 | interp = PyInterpreterState_New(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1311 | if (interp == NULL) { |
| 1312 | *tstate_p = NULL; |
| 1313 | return _Py_INIT_OK(); |
| 1314 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1315 | |
| 1316 | tstate = PyThreadState_New(interp); |
| 1317 | if (tstate == NULL) { |
| 1318 | PyInterpreterState_Delete(interp); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1319 | *tstate_p = NULL; |
| 1320 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | save_tstate = PyThreadState_Swap(tstate); |
| 1324 | |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1325 | /* Copy the current interpreter config into the new interpreter */ |
| 1326 | if (save_tstate != NULL) { |
| 1327 | interp->core_config = save_tstate->interp->core_config; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1328 | interp->config = save_tstate->interp->config; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1329 | } else { |
| 1330 | /* No current thread state, copy from the main interpreter */ |
| 1331 | PyInterpreterState *main_interp = PyInterpreterState_Main(); |
| 1332 | interp->core_config = main_interp->core_config; |
Eric Snow | c7ec998 | 2017-05-23 23:00:52 -0700 | [diff] [blame] | 1333 | interp->config = main_interp->config; |
Eric Snow | 1abcf67 | 2017-05-23 21:46:51 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1336 | err = _PyPathConfig_Init(&interp->config); |
| 1337 | if (_Py_INIT_FAILED(err)) { |
| 1338 | return err; |
| 1339 | } |
| 1340 | wchar_t *sys_path = Py_GetPath(); |
| 1341 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1342 | /* XXX The following is lax in error checking */ |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1343 | PyObject *modules = PyDict_New(); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1344 | if (modules == NULL) { |
| 1345 | return _Py_INIT_ERR("can't make modules dictionary"); |
| 1346 | } |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1347 | interp->modules = modules; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1348 | |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1349 | sysmod = _PyImport_FindBuiltin("sys", modules); |
| 1350 | if (sysmod != NULL) { |
| 1351 | interp->sysdict = PyModule_GetDict(sysmod); |
| 1352 | if (interp->sysdict == NULL) |
| 1353 | goto handle_error; |
| 1354 | Py_INCREF(interp->sysdict); |
| 1355 | PyDict_SetItemString(interp->sysdict, "modules", modules); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 1356 | PySys_SetPath(sys_path); |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 1357 | _PySys_EndInit(interp->sysdict); |
| 1358 | } |
| 1359 | |
| 1360 | bimod = _PyImport_FindBuiltin("builtins", modules); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1361 | if (bimod != NULL) { |
| 1362 | interp->builtins = PyModule_GetDict(bimod); |
| 1363 | if (interp->builtins == NULL) |
| 1364 | goto handle_error; |
| 1365 | Py_INCREF(interp->builtins); |
| 1366 | } |
| 1367 | |
| 1368 | /* initialize builtin exceptions */ |
| 1369 | _PyExc_Init(bimod); |
| 1370 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1371 | if (bimod != NULL && sysmod != NULL) { |
| 1372 | PyObject *pstderr; |
| 1373 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1374 | /* Set up a preliminary stderr printer until we have enough |
| 1375 | infrastructure for the io module in place. */ |
| 1376 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1377 | if (pstderr == NULL) { |
| 1378 | return _Py_INIT_ERR("can't set preliminary stderr"); |
| 1379 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1380 | _PySys_SetObjectId(&PyId_stderr, pstderr); |
| 1381 | PySys_SetObject("__stderr__", pstderr); |
| 1382 | Py_DECREF(pstderr); |
| 1383 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1384 | err = _PyImportHooks_Init(); |
| 1385 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1386 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1387 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1388 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1389 | err = initimport(interp, sysmod); |
| 1390 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1391 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1392 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1393 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1394 | err = initexternalimport(interp); |
| 1395 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1396 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1397 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1398 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1399 | err = initfsencoding(interp); |
| 1400 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1401 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1404 | err = init_sys_streams(); |
| 1405 | if (_Py_INIT_FAILED(err)) { |
| 1406 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | err = add_main_module(interp); |
| 1410 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1411 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | if (!Py_NoSiteFlag) { |
| 1415 | err = initsite(); |
| 1416 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1417 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1418 | } |
| 1419 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1420 | } |
| 1421 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1422 | if (PyErr_Occurred()) { |
| 1423 | goto handle_error; |
| 1424 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1425 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1426 | *tstate_p = tstate; |
| 1427 | return _Py_INIT_OK(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1428 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1429 | handle_error: |
| 1430 | /* Oops, it didn't work. Undo it all. */ |
| 1431 | |
| 1432 | PyErr_PrintEx(0); |
| 1433 | PyThreadState_Clear(tstate); |
| 1434 | PyThreadState_Swap(save_tstate); |
| 1435 | PyThreadState_Delete(tstate); |
| 1436 | PyInterpreterState_Delete(interp); |
| 1437 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1438 | *tstate_p = NULL; |
| 1439 | return _Py_INIT_OK(); |
| 1440 | } |
| 1441 | |
| 1442 | PyThreadState * |
| 1443 | Py_NewInterpreter(void) |
| 1444 | { |
| 1445 | PyThreadState *tstate; |
| 1446 | _PyInitError err = new_interpreter(&tstate); |
| 1447 | if (_Py_INIT_FAILED(err)) { |
| 1448 | _Py_FatalInitError(err); |
| 1449 | } |
| 1450 | return tstate; |
| 1451 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /* Delete an interpreter and its last thread. This requires that the |
| 1455 | given thread state is current, that the thread has no remaining |
| 1456 | frames, and that it is its interpreter's only remaining thread. |
| 1457 | It is a fatal error to violate these constraints. |
| 1458 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1459 | (Py_FinalizeEx() doesn't have these constraints -- it zaps |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1460 | everything, regardless.) |
| 1461 | |
| 1462 | Locking: as above. |
| 1463 | |
| 1464 | */ |
| 1465 | |
| 1466 | void |
| 1467 | Py_EndInterpreter(PyThreadState *tstate) |
| 1468 | { |
| 1469 | PyInterpreterState *interp = tstate->interp; |
| 1470 | |
| 1471 | if (tstate != PyThreadState_GET()) |
| 1472 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
| 1473 | if (tstate->frame != NULL) |
| 1474 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
| 1475 | |
| 1476 | wait_for_thread_shutdown(); |
| 1477 | |
| 1478 | if (tstate != interp->tstate_head || tstate->next != NULL) |
| 1479 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
| 1480 | |
| 1481 | PyImport_Cleanup(); |
| 1482 | PyInterpreterState_Clear(interp); |
| 1483 | PyThreadState_Swap(NULL); |
| 1484 | PyInterpreterState_Delete(interp); |
| 1485 | } |
| 1486 | |
| 1487 | #ifdef MS_WINDOWS |
| 1488 | static wchar_t *progname = L"python"; |
| 1489 | #else |
| 1490 | static wchar_t *progname = L"python3"; |
| 1491 | #endif |
| 1492 | |
| 1493 | void |
| 1494 | Py_SetProgramName(wchar_t *pn) |
| 1495 | { |
| 1496 | if (pn && *pn) |
| 1497 | progname = pn; |
| 1498 | } |
| 1499 | |
| 1500 | wchar_t * |
| 1501 | Py_GetProgramName(void) |
| 1502 | { |
| 1503 | return progname; |
| 1504 | } |
| 1505 | |
| 1506 | static wchar_t *default_home = NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1507 | |
| 1508 | void |
| 1509 | Py_SetPythonHome(wchar_t *home) |
| 1510 | { |
| 1511 | default_home = home; |
| 1512 | } |
| 1513 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1514 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1515 | wchar_t* |
| 1516 | Py_GetPythonHome(void) |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1517 | { |
| 1518 | /* Use a static buffer to avoid heap memory allocation failure. |
| 1519 | Py_GetPythonHome() doesn't allow to report error, and the caller |
| 1520 | doesn't release memory. */ |
| 1521 | static wchar_t buffer[MAXPATHLEN+1]; |
| 1522 | |
| 1523 | if (default_home) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1524 | return default_home; |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | char *home = Py_GETENV("PYTHONHOME"); |
| 1528 | if (!home) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1529 | return NULL; |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | size_t size = Py_ARRAY_LENGTH(buffer); |
| 1533 | size_t r = mbstowcs(buffer, home, size); |
| 1534 | if (r == (size_t)-1 || r >= size) { |
| 1535 | /* conversion failed or the static buffer is too small */ |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1536 | return NULL; |
Victor Stinner | 1f15111 | 2017-11-23 10:43:14 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1539 | return buffer; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1540 | } |
| 1541 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1542 | /* Add the __main__ module */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1543 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1544 | static _PyInitError |
| 1545 | add_main_module(PyInterpreterState *interp) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1546 | { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1547 | PyObject *m, *d, *loader, *ann_dict; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1548 | m = PyImport_AddModule("__main__"); |
| 1549 | if (m == NULL) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1550 | return _Py_INIT_ERR("can't create __main__ module"); |
| 1551 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1552 | d = PyModule_GetDict(m); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1553 | ann_dict = PyDict_New(); |
| 1554 | if ((ann_dict == NULL) || |
| 1555 | (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1556 | return _Py_INIT_ERR("Failed to initialize __main__.__annotations__"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1557 | } |
| 1558 | Py_DECREF(ann_dict); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1559 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1560 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 1561 | PyObject *bimod = PyImport_ImportModule("builtins"); |
| 1562 | if (bimod == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1563 | return _Py_INIT_ERR("Failed to retrieve builtins module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1564 | } |
| 1565 | if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1566 | return _Py_INIT_ERR("Failed to initialize __main__.__builtins__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1567 | } |
| 1568 | Py_DECREF(bimod); |
| 1569 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1570 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1571 | /* Main is a little special - imp.is_builtin("__main__") will return |
| 1572 | * False, but BuiltinImporter is still the most appropriate initial |
| 1573 | * setting for its __loader__ attribute. A more suitable value will |
| 1574 | * be set if __main__ gets further initialized later in the startup |
| 1575 | * process. |
| 1576 | */ |
| 1577 | loader = PyDict_GetItemString(d, "__loader__"); |
| 1578 | if (loader == NULL || loader == Py_None) { |
| 1579 | PyObject *loader = PyObject_GetAttrString(interp->importlib, |
| 1580 | "BuiltinImporter"); |
| 1581 | if (loader == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1582 | return _Py_INIT_ERR("Failed to retrieve BuiltinImporter"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1583 | } |
| 1584 | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1585 | return _Py_INIT_ERR("Failed to initialize __main__.__loader__"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1586 | } |
| 1587 | Py_DECREF(loader); |
| 1588 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1589 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1590 | } |
| 1591 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1592 | static _PyInitError |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1593 | initfsencoding(PyInterpreterState *interp) |
| 1594 | { |
| 1595 | PyObject *codec; |
| 1596 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1597 | #ifdef MS_WINDOWS |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1598 | if (Py_LegacyWindowsFSEncodingFlag) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1599 | Py_FileSystemDefaultEncoding = "mbcs"; |
| 1600 | Py_FileSystemDefaultEncodeErrors = "replace"; |
| 1601 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1602 | else { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1603 | Py_FileSystemDefaultEncoding = "utf-8"; |
| 1604 | Py_FileSystemDefaultEncodeErrors = "surrogatepass"; |
| 1605 | } |
| 1606 | #else |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1607 | if (Py_FileSystemDefaultEncoding == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1608 | Py_FileSystemDefaultEncoding = get_locale_encoding(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1609 | if (Py_FileSystemDefaultEncoding == NULL) { |
| 1610 | return _Py_INIT_ERR("Unable to get the locale encoding"); |
| 1611 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1612 | |
| 1613 | Py_HasFileSystemDefaultEncoding = 0; |
| 1614 | interp->fscodec_initialized = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1615 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1616 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1617 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1618 | |
| 1619 | /* the encoding is mbcs, utf-8 or ascii */ |
| 1620 | codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); |
| 1621 | if (!codec) { |
| 1622 | /* Such error can only occurs in critical situations: no more |
| 1623 | * memory, import a module of the standard library failed, |
| 1624 | * etc. */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1625 | return _Py_INIT_ERR("unable to load the file system codec"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1626 | } |
| 1627 | Py_DECREF(codec); |
| 1628 | interp->fscodec_initialized = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1629 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /* Import the site module (not into __main__ though) */ |
| 1633 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1634 | static _PyInitError |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1635 | initsite(void) |
| 1636 | { |
| 1637 | PyObject *m; |
| 1638 | m = PyImport_ImportModule("site"); |
| 1639 | if (m == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1640 | return _Py_INIT_USER_ERR("Failed to import the site module"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1641 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1642 | Py_DECREF(m); |
| 1643 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1644 | } |
| 1645 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1646 | /* Check if a file descriptor is valid or not. |
| 1647 | Return 0 if the file descriptor is invalid, return non-zero otherwise. */ |
| 1648 | static int |
| 1649 | is_valid_fd(int fd) |
| 1650 | { |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1651 | #ifdef __APPLE__ |
| 1652 | /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe |
| 1653 | and the other side of the pipe is closed, dup(1) succeed, whereas |
| 1654 | fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect |
| 1655 | such error. */ |
| 1656 | struct stat st; |
| 1657 | return (fstat(fd, &st) == 0); |
| 1658 | #else |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1659 | int fd2; |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 1660 | if (fd < 0) |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1661 | return 0; |
| 1662 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 449b271 | 2015-09-29 13:59:50 +0200 | [diff] [blame] | 1663 | /* Prefer dup() over fstat(). fstat() can require input/output whereas |
| 1664 | dup() doesn't, there is a low risk of EMFILE/ENFILE at Python |
| 1665 | startup. */ |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1666 | fd2 = dup(fd); |
| 1667 | if (fd2 >= 0) |
| 1668 | close(fd2); |
| 1669 | _Py_END_SUPPRESS_IPH |
| 1670 | return fd2 >= 0; |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1671 | #endif |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* returns Py_None if the fd is not valid */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1675 | static PyObject* |
| 1676 | create_stdio(PyObject* io, |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1677 | int fd, int write_mode, const char* name, |
| 1678 | const char* encoding, const char* errors) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1679 | { |
| 1680 | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
| 1681 | const char* mode; |
| 1682 | const char* newline; |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1683 | PyObject *line_buffering, *write_through; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1684 | int buffering, isatty; |
| 1685 | _Py_IDENTIFIER(open); |
| 1686 | _Py_IDENTIFIER(isatty); |
| 1687 | _Py_IDENTIFIER(TextIOWrapper); |
| 1688 | _Py_IDENTIFIER(mode); |
| 1689 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1690 | if (!is_valid_fd(fd)) |
| 1691 | Py_RETURN_NONE; |
| 1692 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1693 | /* stdin is always opened in buffered mode, first because it shouldn't |
| 1694 | make a difference in common use cases, second because TextIOWrapper |
| 1695 | depends on the presence of a read1() method which only exists on |
| 1696 | buffered streams. |
| 1697 | */ |
| 1698 | if (Py_UnbufferedStdioFlag && write_mode) |
| 1699 | buffering = 0; |
| 1700 | else |
| 1701 | buffering = -1; |
| 1702 | if (write_mode) |
| 1703 | mode = "wb"; |
| 1704 | else |
| 1705 | mode = "rb"; |
| 1706 | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", |
| 1707 | fd, mode, buffering, |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1708 | Py_None, Py_None, /* encoding, errors */ |
| 1709 | Py_None, 0); /* newline, closefd */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1710 | if (buf == NULL) |
| 1711 | goto error; |
| 1712 | |
| 1713 | if (buffering) { |
| 1714 | _Py_IDENTIFIER(raw); |
| 1715 | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
| 1716 | if (raw == NULL) |
| 1717 | goto error; |
| 1718 | } |
| 1719 | else { |
| 1720 | raw = buf; |
| 1721 | Py_INCREF(raw); |
| 1722 | } |
| 1723 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 1724 | #ifdef MS_WINDOWS |
| 1725 | /* Windows console IO is always UTF-8 encoded */ |
| 1726 | if (PyWindowsConsoleIO_Check(raw)) |
| 1727 | encoding = "utf-8"; |
| 1728 | #endif |
| 1729 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1730 | text = PyUnicode_FromString(name); |
| 1731 | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
| 1732 | goto error; |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1733 | res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1734 | if (res == NULL) |
| 1735 | goto error; |
| 1736 | isatty = PyObject_IsTrue(res); |
| 1737 | Py_DECREF(res); |
| 1738 | if (isatty == -1) |
| 1739 | goto error; |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1740 | if (Py_UnbufferedStdioFlag) |
| 1741 | write_through = Py_True; |
| 1742 | else |
| 1743 | write_through = Py_False; |
| 1744 | if (isatty && !Py_UnbufferedStdioFlag) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1745 | line_buffering = Py_True; |
| 1746 | else |
| 1747 | line_buffering = Py_False; |
| 1748 | |
| 1749 | Py_CLEAR(raw); |
| 1750 | Py_CLEAR(text); |
| 1751 | |
| 1752 | #ifdef MS_WINDOWS |
| 1753 | /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" |
| 1754 | newlines to "\n". |
| 1755 | sys.stdout and sys.stderr: translate "\n" to "\r\n". */ |
| 1756 | newline = NULL; |
| 1757 | #else |
| 1758 | /* sys.stdin: split lines at "\n". |
| 1759 | sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ |
| 1760 | newline = "\n"; |
| 1761 | #endif |
| 1762 | |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1763 | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO", |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1764 | buf, encoding, errors, |
Serhiy Storchaka | 77732be | 2017-10-04 20:25:40 +0300 | [diff] [blame] | 1765 | newline, line_buffering, write_through); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1766 | Py_CLEAR(buf); |
| 1767 | if (stream == NULL) |
| 1768 | goto error; |
| 1769 | |
| 1770 | if (write_mode) |
| 1771 | mode = "w"; |
| 1772 | else |
| 1773 | mode = "r"; |
| 1774 | text = PyUnicode_FromString(mode); |
| 1775 | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
| 1776 | goto error; |
| 1777 | Py_CLEAR(text); |
| 1778 | return stream; |
| 1779 | |
| 1780 | error: |
| 1781 | Py_XDECREF(buf); |
| 1782 | Py_XDECREF(stream); |
| 1783 | Py_XDECREF(text); |
| 1784 | Py_XDECREF(raw); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1785 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1786 | if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { |
| 1787 | /* Issue #24891: the file descriptor was closed after the first |
| 1788 | is_valid_fd() check was called. Ignore the OSError and set the |
| 1789 | stream to None. */ |
| 1790 | PyErr_Clear(); |
| 1791 | Py_RETURN_NONE; |
| 1792 | } |
| 1793 | return NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1797 | static _PyInitError |
| 1798 | init_sys_streams(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1799 | { |
| 1800 | PyObject *iomod = NULL, *wrapper; |
| 1801 | PyObject *bimod = NULL; |
| 1802 | PyObject *m; |
| 1803 | PyObject *std = NULL; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1804 | int fd; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1805 | PyObject * encoding_attr; |
| 1806 | char *pythonioencoding = NULL, *encoding, *errors; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1807 | _PyInitError res = _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1808 | |
| 1809 | /* Hack to avoid a nasty recursion issue when Python is invoked |
| 1810 | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ |
| 1811 | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { |
| 1812 | goto error; |
| 1813 | } |
| 1814 | Py_DECREF(m); |
| 1815 | |
| 1816 | if (!(m = PyImport_ImportModule("encodings.latin_1"))) { |
| 1817 | goto error; |
| 1818 | } |
| 1819 | Py_DECREF(m); |
| 1820 | |
| 1821 | if (!(bimod = PyImport_ImportModule("builtins"))) { |
| 1822 | goto error; |
| 1823 | } |
| 1824 | |
| 1825 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 1826 | goto error; |
| 1827 | } |
| 1828 | if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
| 1829 | goto error; |
| 1830 | } |
| 1831 | |
| 1832 | /* Set builtins.open */ |
| 1833 | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
| 1834 | Py_DECREF(wrapper); |
| 1835 | goto error; |
| 1836 | } |
| 1837 | Py_DECREF(wrapper); |
| 1838 | |
| 1839 | encoding = _Py_StandardStreamEncoding; |
| 1840 | errors = _Py_StandardStreamErrors; |
| 1841 | if (!encoding || !errors) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1842 | pythonioencoding = Py_GETENV("PYTHONIOENCODING"); |
| 1843 | if (pythonioencoding) { |
| 1844 | char *err; |
| 1845 | pythonioencoding = _PyMem_Strdup(pythonioencoding); |
| 1846 | if (pythonioencoding == NULL) { |
| 1847 | PyErr_NoMemory(); |
| 1848 | goto error; |
| 1849 | } |
| 1850 | err = strchr(pythonioencoding, ':'); |
| 1851 | if (err) { |
| 1852 | *err = '\0'; |
| 1853 | err++; |
Serhiy Storchaka | fc43511 | 2016-04-10 14:34:13 +0300 | [diff] [blame] | 1854 | if (*err && !errors) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1855 | errors = err; |
| 1856 | } |
| 1857 | } |
| 1858 | if (*pythonioencoding && !encoding) { |
| 1859 | encoding = pythonioencoding; |
| 1860 | } |
| 1861 | } |
Serhiy Storchaka | fc43511 | 2016-04-10 14:34:13 +0300 | [diff] [blame] | 1862 | if (!errors && !(pythonioencoding && *pythonioencoding)) { |
Nick Coghlan | 6ea4186 | 2017-06-11 13:16:15 +1000 | [diff] [blame] | 1863 | /* Choose the default error handler based on the current locale */ |
| 1864 | errors = get_default_standard_stream_error_handler(); |
Serhiy Storchaka | fc43511 | 2016-04-10 14:34:13 +0300 | [diff] [blame] | 1865 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* Set sys.stdin */ |
| 1869 | fd = fileno(stdin); |
| 1870 | /* Under some conditions stdin, stdout and stderr may not be connected |
| 1871 | * and fileno() may point to an invalid file descriptor. For example |
| 1872 | * GUI apps don't have valid standard streams by default. |
| 1873 | */ |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1874 | std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
| 1875 | if (std == NULL) |
| 1876 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1877 | PySys_SetObject("__stdin__", std); |
| 1878 | _PySys_SetObjectId(&PyId_stdin, std); |
| 1879 | Py_DECREF(std); |
| 1880 | |
| 1881 | /* Set sys.stdout */ |
| 1882 | fd = fileno(stdout); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1883 | std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
| 1884 | if (std == NULL) |
| 1885 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1886 | PySys_SetObject("__stdout__", std); |
| 1887 | _PySys_SetObjectId(&PyId_stdout, std); |
| 1888 | Py_DECREF(std); |
| 1889 | |
| 1890 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 1891 | /* Set sys.stderr, replaces the preliminary stderr */ |
| 1892 | fd = fileno(stderr); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1893 | std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
| 1894 | if (std == NULL) |
| 1895 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1896 | |
| 1897 | /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 1898 | when import.c tries to write to stderr in verbose mode. */ |
| 1899 | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 1900 | if (encoding_attr != NULL) { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1901 | const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1902 | if (std_encoding != NULL) { |
| 1903 | PyObject *codec_info = _PyCodec_Lookup(std_encoding); |
| 1904 | Py_XDECREF(codec_info); |
| 1905 | } |
| 1906 | Py_DECREF(encoding_attr); |
| 1907 | } |
| 1908 | PyErr_Clear(); /* Not a fatal error if codec isn't available */ |
| 1909 | |
| 1910 | if (PySys_SetObject("__stderr__", std) < 0) { |
| 1911 | Py_DECREF(std); |
| 1912 | goto error; |
| 1913 | } |
| 1914 | if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { |
| 1915 | Py_DECREF(std); |
| 1916 | goto error; |
| 1917 | } |
| 1918 | Py_DECREF(std); |
| 1919 | #endif |
| 1920 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1921 | goto done; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1922 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1923 | error: |
| 1924 | res = _Py_INIT_ERR("can't initialize sys standard streams"); |
| 1925 | |
| 1926 | done: |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1927 | /* We won't need them anymore. */ |
| 1928 | if (_Py_StandardStreamEncoding) { |
| 1929 | PyMem_RawFree(_Py_StandardStreamEncoding); |
| 1930 | _Py_StandardStreamEncoding = NULL; |
| 1931 | } |
| 1932 | if (_Py_StandardStreamErrors) { |
| 1933 | PyMem_RawFree(_Py_StandardStreamErrors); |
| 1934 | _Py_StandardStreamErrors = NULL; |
| 1935 | } |
| 1936 | PyMem_Free(pythonioencoding); |
| 1937 | Py_XDECREF(bimod); |
| 1938 | Py_XDECREF(iomod); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 1939 | return res; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1943 | static void |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1944 | _Py_FatalError_DumpTracebacks(int fd) |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1945 | { |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1946 | fputc('\n', stderr); |
| 1947 | fflush(stderr); |
| 1948 | |
| 1949 | /* display the current Python stack */ |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 1950 | _Py_DumpTracebackThreads(fd, NULL, NULL); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1951 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1952 | |
| 1953 | /* Print the current exception (if an exception is set) with its traceback, |
| 1954 | or display the current Python stack. |
| 1955 | |
| 1956 | Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is |
| 1957 | called on catastrophic cases. |
| 1958 | |
| 1959 | Return 1 if the traceback was displayed, 0 otherwise. */ |
| 1960 | |
| 1961 | static int |
| 1962 | _Py_FatalError_PrintExc(int fd) |
| 1963 | { |
| 1964 | PyObject *ferr, *res; |
| 1965 | PyObject *exception, *v, *tb; |
| 1966 | int has_tb; |
| 1967 | |
| 1968 | if (PyThreadState_GET() == NULL) { |
| 1969 | /* The GIL is released: trying to acquire it is likely to deadlock, |
| 1970 | just give up. */ |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
| 1974 | PyErr_Fetch(&exception, &v, &tb); |
| 1975 | if (exception == NULL) { |
| 1976 | /* No current exception */ |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
| 1980 | ferr = _PySys_GetObjectId(&PyId_stderr); |
| 1981 | if (ferr == NULL || ferr == Py_None) { |
| 1982 | /* sys.stderr is not set yet or set to None, |
| 1983 | no need to try to display the exception */ |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | PyErr_NormalizeException(&exception, &v, &tb); |
| 1988 | if (tb == NULL) { |
| 1989 | tb = Py_None; |
| 1990 | Py_INCREF(tb); |
| 1991 | } |
| 1992 | PyException_SetTraceback(v, tb); |
| 1993 | if (exception == NULL) { |
| 1994 | /* PyErr_NormalizeException() failed */ |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | has_tb = (tb != Py_None); |
| 1999 | PyErr_Display(exception, v, tb); |
| 2000 | Py_XDECREF(exception); |
| 2001 | Py_XDECREF(v); |
| 2002 | Py_XDECREF(tb); |
| 2003 | |
| 2004 | /* sys.stderr may be buffered: call sys.stderr.flush() */ |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2005 | res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2006 | if (res == NULL) |
| 2007 | PyErr_Clear(); |
| 2008 | else |
| 2009 | Py_DECREF(res); |
| 2010 | |
| 2011 | return has_tb; |
| 2012 | } |
| 2013 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2014 | /* Print fatal error message and abort */ |
| 2015 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2016 | #ifdef MS_WINDOWS |
| 2017 | static void |
| 2018 | fatal_output_debug(const char *msg) |
| 2019 | { |
| 2020 | /* buffer of 256 bytes allocated on the stack */ |
| 2021 | WCHAR buffer[256 / sizeof(WCHAR)]; |
| 2022 | size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; |
| 2023 | size_t msglen; |
| 2024 | |
| 2025 | OutputDebugStringW(L"Fatal Python error: "); |
| 2026 | |
| 2027 | msglen = strlen(msg); |
| 2028 | while (msglen) { |
| 2029 | size_t i; |
| 2030 | |
| 2031 | if (buflen > msglen) { |
| 2032 | buflen = msglen; |
| 2033 | } |
| 2034 | |
| 2035 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 2036 | conversion, assuming that the this error message actually uses |
| 2037 | ASCII only. If this ceases to be true, we will have to convert. */ |
| 2038 | for (i=0; i < buflen; ++i) { |
| 2039 | buffer[i] = msg[i]; |
| 2040 | } |
| 2041 | buffer[i] = L'\0'; |
| 2042 | OutputDebugStringW(buffer); |
| 2043 | |
| 2044 | msg += buflen; |
| 2045 | msglen -= buflen; |
| 2046 | } |
| 2047 | OutputDebugStringW(L"\n"); |
| 2048 | } |
| 2049 | #endif |
| 2050 | |
Benjamin Peterson | cef88b9 | 2017-11-25 13:02:55 -0800 | [diff] [blame] | 2051 | static void _Py_NO_RETURN |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2052 | fatal_error(const char *prefix, const char *msg, int status) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2053 | { |
| 2054 | const int fd = fileno(stderr); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2055 | static int reentrant = 0; |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2056 | |
| 2057 | if (reentrant) { |
| 2058 | /* Py_FatalError() caused a second fatal error. |
| 2059 | Example: flush_std_files() raises a recursion error. */ |
| 2060 | goto exit; |
| 2061 | } |
| 2062 | reentrant = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2063 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2064 | fprintf(stderr, "Fatal Python error: "); |
| 2065 | if (prefix) { |
| 2066 | fputs(prefix, stderr); |
| 2067 | fputs(": ", stderr); |
| 2068 | } |
| 2069 | if (msg) { |
| 2070 | fputs(msg, stderr); |
| 2071 | } |
| 2072 | else { |
| 2073 | fprintf(stderr, "<message not set>"); |
| 2074 | } |
| 2075 | fputs("\n", stderr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2076 | fflush(stderr); /* it helps in Windows debug build */ |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2077 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 2078 | /* Print the exception (if an exception is set) with its traceback, |
| 2079 | * or display the current Python stack. */ |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2080 | if (!_Py_FatalError_PrintExc(fd)) { |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2081 | _Py_FatalError_DumpTracebacks(fd); |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2082 | } |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 2083 | |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2084 | /* The main purpose of faulthandler is to display the traceback. |
| 2085 | This function already did its best to display a traceback. |
| 2086 | Disable faulthandler to prevent writing a second traceback |
| 2087 | on abort(). */ |
Victor Stinner | 2025d78 | 2016-03-16 23:19:15 +0100 | [diff] [blame] | 2088 | _PyFaulthandler_Fini(); |
| 2089 | |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 2090 | /* Check if the current Python thread hold the GIL */ |
| 2091 | if (PyThreadState_GET() != NULL) { |
| 2092 | /* Flush sys.stdout and sys.stderr */ |
| 2093 | flush_std_files(); |
| 2094 | } |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 2095 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2096 | #ifdef MS_WINDOWS |
Victor Stinner | 8d5a3aa | 2017-10-04 09:50:12 -0700 | [diff] [blame] | 2097 | fatal_output_debug(msg); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2098 | #endif /* MS_WINDOWS */ |
| 2099 | |
| 2100 | exit: |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2101 | if (status < 0) { |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 2102 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2103 | DebugBreak(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2104 | #endif |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2105 | abort(); |
| 2106 | } |
| 2107 | else { |
| 2108 | exit(status); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | void |
| 2113 | Py_FatalError(const char *msg) |
| 2114 | { |
| 2115 | fatal_error(NULL, msg, -1); |
| 2116 | } |
| 2117 | |
| 2118 | void |
| 2119 | _Py_FatalInitError(_PyInitError err) |
| 2120 | { |
| 2121 | /* On "user" error: exit with status 1. |
| 2122 | For all other errors, call abort(). */ |
| 2123 | int status = err.user_err ? 1 : -1; |
| 2124 | fatal_error(err.prefix, err.msg, status); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* Clean up and exit */ |
| 2128 | |
Victor Stinner | d7292b5 | 2016-06-17 12:29:00 +0200 | [diff] [blame] | 2129 | # include "pythread.h" |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2130 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2131 | /* For the atexit module. */ |
| 2132 | void _Py_PyAtExit(void (*func)(void)) |
| 2133 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2134 | _PyRuntime.pyexitfunc = func; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | static void |
| 2138 | call_py_exitfuncs(void) |
| 2139 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2140 | if (_PyRuntime.pyexitfunc == NULL) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2141 | return; |
| 2142 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2143 | (*_PyRuntime.pyexitfunc)(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2144 | PyErr_Clear(); |
| 2145 | } |
| 2146 | |
| 2147 | /* Wait until threading._shutdown completes, provided |
| 2148 | the threading module was imported in the first place. |
| 2149 | The shutdown routine will wait until all non-daemon |
| 2150 | "threading" threads have completed. */ |
| 2151 | static void |
| 2152 | wait_for_thread_shutdown(void) |
| 2153 | { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2154 | _Py_IDENTIFIER(_shutdown); |
| 2155 | PyObject *result; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 2156 | PyObject *threading = _PyImport_GetModuleId(&PyId_threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2157 | if (threading == NULL) { |
| 2158 | /* threading not imported */ |
| 2159 | PyErr_Clear(); |
| 2160 | return; |
| 2161 | } |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2162 | result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2163 | if (result == NULL) { |
| 2164 | PyErr_WriteUnraisable(threading); |
| 2165 | } |
| 2166 | else { |
| 2167 | Py_DECREF(result); |
| 2168 | } |
| 2169 | Py_DECREF(threading); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | #define NEXITFUNCS 32 |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2173 | int Py_AtExit(void (*func)(void)) |
| 2174 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2175 | if (_PyRuntime.nexitfuncs >= NEXITFUNCS) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2176 | return -1; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2177 | _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2178 | return 0; |
| 2179 | } |
| 2180 | |
| 2181 | static void |
| 2182 | call_ll_exitfuncs(void) |
| 2183 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2184 | while (_PyRuntime.nexitfuncs > 0) |
| 2185 | (*_PyRuntime.exitfuncs[--_PyRuntime.nexitfuncs])(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2186 | |
| 2187 | fflush(stdout); |
| 2188 | fflush(stderr); |
| 2189 | } |
| 2190 | |
| 2191 | void |
| 2192 | Py_Exit(int sts) |
| 2193 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 2194 | if (Py_FinalizeEx() < 0) { |
| 2195 | sts = 120; |
| 2196 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2197 | |
| 2198 | exit(sts); |
| 2199 | } |
| 2200 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2201 | static _PyInitError |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2202 | initsigs(void) |
| 2203 | { |
| 2204 | #ifdef SIGPIPE |
| 2205 | PyOS_setsig(SIGPIPE, SIG_IGN); |
| 2206 | #endif |
| 2207 | #ifdef SIGXFZ |
| 2208 | PyOS_setsig(SIGXFZ, SIG_IGN); |
| 2209 | #endif |
| 2210 | #ifdef SIGXFSZ |
| 2211 | PyOS_setsig(SIGXFSZ, SIG_IGN); |
| 2212 | #endif |
| 2213 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
| 2214 | if (PyErr_Occurred()) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2215 | return _Py_INIT_ERR("can't import signal"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2216 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2217 | return _Py_INIT_OK(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
| 2222 | * |
| 2223 | * All of the code in this function must only use async-signal-safe functions, |
| 2224 | * listed at `man 7 signal` or |
| 2225 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2226 | */ |
| 2227 | void |
| 2228 | _Py_RestoreSignals(void) |
| 2229 | { |
| 2230 | #ifdef SIGPIPE |
| 2231 | PyOS_setsig(SIGPIPE, SIG_DFL); |
| 2232 | #endif |
| 2233 | #ifdef SIGXFZ |
| 2234 | PyOS_setsig(SIGXFZ, SIG_DFL); |
| 2235 | #endif |
| 2236 | #ifdef SIGXFSZ |
| 2237 | PyOS_setsig(SIGXFSZ, SIG_DFL); |
| 2238 | #endif |
| 2239 | } |
| 2240 | |
| 2241 | |
| 2242 | /* |
| 2243 | * The file descriptor fd is considered ``interactive'' if either |
| 2244 | * a) isatty(fd) is TRUE, or |
| 2245 | * b) the -i flag was given, and the filename associated with |
| 2246 | * the descriptor is NULL or "<stdin>" or "???". |
| 2247 | */ |
| 2248 | int |
| 2249 | Py_FdIsInteractive(FILE *fp, const char *filename) |
| 2250 | { |
| 2251 | if (isatty((int)fileno(fp))) |
| 2252 | return 1; |
| 2253 | if (!Py_InteractiveFlag) |
| 2254 | return 0; |
| 2255 | return (filename == NULL) || |
| 2256 | (strcmp(filename, "<stdin>") == 0) || |
| 2257 | (strcmp(filename, "???") == 0); |
| 2258 | } |
| 2259 | |
| 2260 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2261 | /* Wrappers around sigaction() or signal(). */ |
| 2262 | |
| 2263 | PyOS_sighandler_t |
| 2264 | PyOS_getsig(int sig) |
| 2265 | { |
| 2266 | #ifdef HAVE_SIGACTION |
| 2267 | struct sigaction context; |
| 2268 | if (sigaction(sig, NULL, &context) == -1) |
| 2269 | return SIG_ERR; |
| 2270 | return context.sa_handler; |
| 2271 | #else |
| 2272 | PyOS_sighandler_t handler; |
| 2273 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 2274 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 2275 | switch (sig) { |
| 2276 | /* Only these signals are valid */ |
| 2277 | case SIGINT: |
| 2278 | case SIGILL: |
| 2279 | case SIGFPE: |
| 2280 | case SIGSEGV: |
| 2281 | case SIGTERM: |
| 2282 | case SIGBREAK: |
| 2283 | case SIGABRT: |
| 2284 | break; |
| 2285 | /* Don't call signal() with other values or it will assert */ |
| 2286 | default: |
| 2287 | return SIG_ERR; |
| 2288 | } |
| 2289 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
| 2290 | handler = signal(sig, SIG_IGN); |
| 2291 | if (handler != SIG_ERR) |
| 2292 | signal(sig, handler); |
| 2293 | return handler; |
| 2294 | #endif |
| 2295 | } |
| 2296 | |
| 2297 | /* |
| 2298 | * All of the code in this function must only use async-signal-safe functions, |
| 2299 | * listed at `man 7 signal` or |
| 2300 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2301 | */ |
| 2302 | PyOS_sighandler_t |
| 2303 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 2304 | { |
| 2305 | #ifdef HAVE_SIGACTION |
| 2306 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 2307 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 2308 | * changes to invalidate that assumption. |
| 2309 | */ |
| 2310 | struct sigaction context, ocontext; |
| 2311 | context.sa_handler = handler; |
| 2312 | sigemptyset(&context.sa_mask); |
| 2313 | context.sa_flags = 0; |
| 2314 | if (sigaction(sig, &context, &ocontext) == -1) |
| 2315 | return SIG_ERR; |
| 2316 | return ocontext.sa_handler; |
| 2317 | #else |
| 2318 | PyOS_sighandler_t oldhandler; |
| 2319 | oldhandler = signal(sig, handler); |
| 2320 | #ifdef HAVE_SIGINTERRUPT |
| 2321 | siginterrupt(sig, 1); |
| 2322 | #endif |
| 2323 | return oldhandler; |
| 2324 | #endif |
| 2325 | } |
| 2326 | |
| 2327 | #ifdef __cplusplus |
| 2328 | } |
| 2329 | #endif |