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