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 */ |
| 7 | #include "grammar.h" |
| 8 | #include "node.h" |
| 9 | #include "token.h" |
| 10 | #include "parsetok.h" |
| 11 | #include "errcode.h" |
| 12 | #include "code.h" |
| 13 | #include "symtable.h" |
| 14 | #include "ast.h" |
| 15 | #include "marshal.h" |
| 16 | #include "osdefs.h" |
| 17 | #include <locale.h> |
| 18 | |
| 19 | #ifdef HAVE_SIGNAL_H |
| 20 | #include <signal.h> |
| 21 | #endif |
| 22 | |
| 23 | #ifdef MS_WINDOWS |
| 24 | #include "malloc.h" /* for alloca */ |
| 25 | #endif |
| 26 | |
| 27 | #ifdef HAVE_LANGINFO_H |
| 28 | #include <langinfo.h> |
| 29 | #endif |
| 30 | |
| 31 | #ifdef MS_WINDOWS |
| 32 | #undef BYTE |
| 33 | #include "windows.h" |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 34 | |
| 35 | extern PyTypeObject PyWindowsConsoleIO_Type; |
| 36 | #define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type)) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | _Py_IDENTIFIER(flush); |
| 40 | _Py_IDENTIFIER(name); |
| 41 | _Py_IDENTIFIER(stdin); |
| 42 | _Py_IDENTIFIER(stdout); |
| 43 | _Py_IDENTIFIER(stderr); |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | extern wchar_t *Py_GetPath(void); |
| 50 | |
| 51 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
| 52 | |
| 53 | /* Forward */ |
| 54 | static void initmain(PyInterpreterState *interp); |
| 55 | static int initfsencoding(PyInterpreterState *interp); |
| 56 | static void initsite(void); |
| 57 | static int initstdio(void); |
| 58 | static void initsigs(void); |
| 59 | static void call_py_exitfuncs(void); |
| 60 | static void wait_for_thread_shutdown(void); |
| 61 | static void call_ll_exitfuncs(void); |
| 62 | extern int _PyUnicode_Init(void); |
| 63 | extern int _PyStructSequence_Init(void); |
| 64 | extern void _PyUnicode_Fini(void); |
| 65 | extern int _PyLong_Init(void); |
| 66 | extern void PyLong_Fini(void); |
| 67 | extern int _PyFaulthandler_Init(void); |
| 68 | extern void _PyFaulthandler_Fini(void); |
| 69 | extern void _PyHash_Fini(void); |
| 70 | extern int _PyTraceMalloc_Init(void); |
| 71 | extern int _PyTraceMalloc_Fini(void); |
| 72 | |
| 73 | #ifdef WITH_THREAD |
| 74 | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); |
| 75 | extern void _PyGILState_Fini(void); |
| 76 | #endif /* WITH_THREAD */ |
| 77 | |
| 78 | /* Global configuration variable declarations are in pydebug.h */ |
| 79 | /* XXX (ncoghlan): move those declarations to pylifecycle.h? */ |
| 80 | int Py_DebugFlag; /* Needed by parser.c */ |
| 81 | int Py_VerboseFlag; /* Needed by import.c */ |
| 82 | int Py_QuietFlag; /* Needed by sysmodule.c */ |
| 83 | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ |
| 84 | int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */ |
| 85 | int Py_OptimizeFlag = 0; /* Needed by compile.c */ |
| 86 | int Py_NoSiteFlag; /* Suppress 'import site' */ |
| 87 | int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ |
| 88 | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ |
| 89 | int Py_FrozenFlag; /* Needed by getpath.c */ |
| 90 | int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ |
Xiang Zhang | 0710d75 | 2017-03-11 13:02:52 +0800 | [diff] [blame] | 91 | int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.pyc) */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 92 | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ |
| 93 | int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ |
| 94 | int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ |
| 95 | int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 96 | #ifdef MS_WINDOWS |
| 97 | int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */ |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 98 | int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 99 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 100 | |
| 101 | PyThreadState *_Py_Finalizing = NULL; |
| 102 | |
| 103 | /* Hack to force loading of object files */ |
| 104 | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ |
| 105 | PyOS_mystrnicmp; /* Python/pystrcmp.o */ |
| 106 | |
| 107 | /* PyModule_GetWarningsModule is no longer necessary as of 2.6 |
| 108 | since _warnings is builtin. This API should not be used. */ |
| 109 | PyObject * |
| 110 | PyModule_GetWarningsModule(void) |
| 111 | { |
| 112 | return PyImport_ImportModule("warnings"); |
| 113 | } |
| 114 | |
| 115 | static int initialized = 0; |
| 116 | |
| 117 | /* API to access the initialized flag -- useful for esoteric use */ |
| 118 | |
| 119 | int |
| 120 | Py_IsInitialized(void) |
| 121 | { |
| 122 | return initialized; |
| 123 | } |
| 124 | |
| 125 | /* Helper to allow an embedding application to override the normal |
| 126 | * mechanism that attempts to figure out an appropriate IO encoding |
| 127 | */ |
| 128 | |
| 129 | static char *_Py_StandardStreamEncoding = NULL; |
| 130 | static char *_Py_StandardStreamErrors = NULL; |
| 131 | |
| 132 | int |
| 133 | Py_SetStandardStreamEncoding(const char *encoding, const char *errors) |
| 134 | { |
| 135 | if (Py_IsInitialized()) { |
| 136 | /* This is too late to have any effect */ |
| 137 | return -1; |
| 138 | } |
| 139 | /* Can't call PyErr_NoMemory() on errors, as Python hasn't been |
| 140 | * initialised yet. |
| 141 | * |
| 142 | * However, the raw memory allocators are initialised appropriately |
| 143 | * as C static variables, so _PyMem_RawStrdup is OK even though |
| 144 | * Py_Initialize hasn't been called yet. |
| 145 | */ |
| 146 | if (encoding) { |
| 147 | _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding); |
| 148 | if (!_Py_StandardStreamEncoding) { |
| 149 | return -2; |
| 150 | } |
| 151 | } |
| 152 | if (errors) { |
| 153 | _Py_StandardStreamErrors = _PyMem_RawStrdup(errors); |
| 154 | if (!_Py_StandardStreamErrors) { |
| 155 | if (_Py_StandardStreamEncoding) { |
| 156 | PyMem_RawFree(_Py_StandardStreamEncoding); |
| 157 | } |
| 158 | return -3; |
| 159 | } |
| 160 | } |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 161 | #ifdef MS_WINDOWS |
| 162 | if (_Py_StandardStreamEncoding) { |
| 163 | /* Overriding the stream encoding implies legacy streams */ |
| 164 | Py_LegacyWindowsStdioFlag = 1; |
| 165 | } |
| 166 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 170 | /* Global initializations. Can be undone by Py_FinalizeEx(). Don't |
| 171 | call this twice without an intervening Py_FinalizeEx() call. When |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 172 | initializations fail, a fatal error is issued and the function does |
| 173 | not return. On return, the first thread and interpreter state have |
| 174 | been created. |
| 175 | |
| 176 | Locking: you must hold the interpreter lock while calling this. |
| 177 | (If the lock has not yet been initialized, that's equivalent to |
| 178 | having the lock, but you cannot use multiple threads.) |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | static int |
| 183 | add_flag(int flag, const char *envs) |
| 184 | { |
| 185 | int env = atoi(envs); |
| 186 | if (flag < env) |
| 187 | flag = env; |
| 188 | if (flag < 1) |
| 189 | flag = 1; |
| 190 | return flag; |
| 191 | } |
| 192 | |
| 193 | static char* |
| 194 | get_codec_name(const char *encoding) |
| 195 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 196 | const char *name_utf8; |
| 197 | char *name_str; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 198 | PyObject *codec, *name = NULL; |
| 199 | |
| 200 | codec = _PyCodec_Lookup(encoding); |
| 201 | if (!codec) |
| 202 | goto error; |
| 203 | |
| 204 | name = _PyObject_GetAttrId(codec, &PyId_name); |
| 205 | Py_CLEAR(codec); |
| 206 | if (!name) |
| 207 | goto error; |
| 208 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 209 | name_utf8 = PyUnicode_AsUTF8(name); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 210 | if (name_utf8 == NULL) |
| 211 | goto error; |
| 212 | name_str = _PyMem_RawStrdup(name_utf8); |
| 213 | Py_DECREF(name); |
| 214 | if (name_str == NULL) { |
| 215 | PyErr_NoMemory(); |
| 216 | return NULL; |
| 217 | } |
| 218 | return name_str; |
| 219 | |
| 220 | error: |
| 221 | Py_XDECREF(codec); |
| 222 | Py_XDECREF(name); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | static char* |
| 227 | get_locale_encoding(void) |
| 228 | { |
| 229 | #ifdef MS_WINDOWS |
| 230 | char codepage[100]; |
| 231 | PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP()); |
| 232 | return get_codec_name(codepage); |
| 233 | #elif defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 234 | char* codeset = nl_langinfo(CODESET); |
| 235 | if (!codeset || codeset[0] == '\0') { |
| 236 | PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); |
| 237 | return NULL; |
| 238 | } |
| 239 | return get_codec_name(codeset); |
Stefan Krah | 144da4e | 2016-04-26 01:56:50 +0200 | [diff] [blame] | 240 | #elif defined(__ANDROID__) |
| 241 | return get_codec_name("UTF-8"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 242 | #else |
| 243 | PyErr_SetNone(PyExc_NotImplementedError); |
| 244 | return NULL; |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | import_init(PyInterpreterState *interp, PyObject *sysmod) |
| 250 | { |
| 251 | PyObject *importlib; |
| 252 | PyObject *impmod; |
| 253 | PyObject *sys_modules; |
| 254 | PyObject *value; |
| 255 | |
| 256 | /* Import _importlib through its frozen version, _frozen_importlib. */ |
| 257 | if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { |
| 258 | Py_FatalError("Py_Initialize: can't import _frozen_importlib"); |
| 259 | } |
| 260 | else if (Py_VerboseFlag) { |
| 261 | PySys_FormatStderr("import _frozen_importlib # frozen\n"); |
| 262 | } |
| 263 | importlib = PyImport_AddModule("_frozen_importlib"); |
| 264 | if (importlib == NULL) { |
| 265 | Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " |
| 266 | "sys.modules"); |
| 267 | } |
| 268 | interp->importlib = importlib; |
| 269 | Py_INCREF(interp->importlib); |
| 270 | |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 271 | interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); |
| 272 | if (interp->import_func == NULL) |
| 273 | Py_FatalError("Py_Initialize: __import__ not found"); |
| 274 | Py_INCREF(interp->import_func); |
| 275 | |
Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 276 | /* Import the _imp module */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 277 | impmod = PyInit_imp(); |
| 278 | if (impmod == NULL) { |
Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 279 | Py_FatalError("Py_Initialize: can't import _imp"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 280 | } |
| 281 | else if (Py_VerboseFlag) { |
Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 282 | PySys_FormatStderr("import _imp # builtin\n"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 283 | } |
| 284 | sys_modules = PyImport_GetModuleDict(); |
| 285 | if (Py_VerboseFlag) { |
| 286 | PySys_FormatStderr("import sys # builtin\n"); |
| 287 | } |
| 288 | if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) { |
| 289 | Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); |
| 290 | } |
| 291 | |
Victor Stinner | cd6e694 | 2015-09-18 09:11:57 +0200 | [diff] [blame] | 292 | /* Install importlib as the implementation of import */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 293 | value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 294 | if (value != NULL) |
| 295 | value = PyObject_CallMethod(importlib, |
| 296 | "_install_external_importers", ""); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 297 | if (value == NULL) { |
| 298 | PyErr_Print(); |
| 299 | Py_FatalError("Py_Initialize: importlib install failed"); |
| 300 | } |
| 301 | Py_DECREF(value); |
| 302 | Py_DECREF(impmod); |
| 303 | |
| 304 | _PyImportZip_Init(); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | void |
| 309 | _Py_InitializeEx_Private(int install_sigs, int install_importlib) |
| 310 | { |
| 311 | PyInterpreterState *interp; |
| 312 | PyThreadState *tstate; |
| 313 | PyObject *bimod, *sysmod, *pstderr; |
| 314 | char *p; |
| 315 | extern void _Py_ReadyTypes(void); |
| 316 | |
| 317 | if (initialized) |
| 318 | return; |
| 319 | initialized = 1; |
| 320 | _Py_Finalizing = NULL; |
| 321 | |
Xavier de Gaye | b445ad7 | 2016-11-16 07:24:20 +0100 | [diff] [blame] | 322 | #ifdef HAVE_SETLOCALE |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 323 | /* Set up the LC_CTYPE locale, so we can obtain |
| 324 | the locale's charset without having to switch |
| 325 | locales. */ |
| 326 | setlocale(LC_CTYPE, ""); |
| 327 | #endif |
| 328 | |
| 329 | if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') |
| 330 | Py_DebugFlag = add_flag(Py_DebugFlag, p); |
| 331 | if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') |
| 332 | Py_VerboseFlag = add_flag(Py_VerboseFlag, p); |
| 333 | if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') |
| 334 | Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); |
| 335 | if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') |
| 336 | Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 337 | /* The variable is only tested for existence here; |
| 338 | _Py_HashRandomization_Init will check its value further. */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 339 | if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') |
| 340 | Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 341 | #ifdef MS_WINDOWS |
| 342 | if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0') |
| 343 | Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 344 | if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0') |
| 345 | Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 346 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 347 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 348 | _Py_HashRandomization_Init(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 349 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 350 | _PyInterpreterState_Init(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 351 | interp = PyInterpreterState_New(); |
| 352 | if (interp == NULL) |
| 353 | Py_FatalError("Py_Initialize: can't make first interpreter"); |
| 354 | |
| 355 | tstate = PyThreadState_New(interp); |
| 356 | if (tstate == NULL) |
| 357 | Py_FatalError("Py_Initialize: can't make first thread"); |
| 358 | (void) PyThreadState_Swap(tstate); |
| 359 | |
| 360 | #ifdef WITH_THREAD |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 361 | /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 362 | destroying the GIL might fail when it is being referenced from |
| 363 | another running thread (see issue #9901). |
| 364 | Instead we destroy the previously created GIL here, which ensures |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 365 | that we can call Py_Initialize / Py_FinalizeEx multiple times. */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 366 | _PyEval_FiniThreads(); |
| 367 | |
| 368 | /* Auto-thread-state API */ |
| 369 | _PyGILState_Init(interp, tstate); |
| 370 | #endif /* WITH_THREAD */ |
| 371 | |
| 372 | _Py_ReadyTypes(); |
| 373 | |
| 374 | if (!_PyFrame_Init()) |
| 375 | Py_FatalError("Py_Initialize: can't init frames"); |
| 376 | |
| 377 | if (!_PyLong_Init()) |
| 378 | Py_FatalError("Py_Initialize: can't init longs"); |
| 379 | |
| 380 | if (!PyByteArray_Init()) |
| 381 | Py_FatalError("Py_Initialize: can't init bytearray"); |
| 382 | |
| 383 | if (!_PyFloat_Init()) |
| 384 | Py_FatalError("Py_Initialize: can't init float"); |
| 385 | |
| 386 | interp->modules = PyDict_New(); |
| 387 | if (interp->modules == NULL) |
| 388 | Py_FatalError("Py_Initialize: can't make modules dictionary"); |
| 389 | |
| 390 | /* Init Unicode implementation; relies on the codec registry */ |
| 391 | if (_PyUnicode_Init() < 0) |
| 392 | Py_FatalError("Py_Initialize: can't initialize unicode"); |
| 393 | if (_PyStructSequence_Init() < 0) |
| 394 | Py_FatalError("Py_Initialize: can't initialize structseq"); |
| 395 | |
| 396 | bimod = _PyBuiltin_Init(); |
| 397 | if (bimod == NULL) |
| 398 | Py_FatalError("Py_Initialize: can't initialize builtins modules"); |
| 399 | _PyImport_FixupBuiltin(bimod, "builtins"); |
| 400 | interp->builtins = PyModule_GetDict(bimod); |
| 401 | if (interp->builtins == NULL) |
| 402 | Py_FatalError("Py_Initialize: can't initialize builtins dict"); |
| 403 | Py_INCREF(interp->builtins); |
| 404 | |
| 405 | /* initialize builtin exceptions */ |
| 406 | _PyExc_Init(bimod); |
| 407 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 408 | sysmod = _PySys_BeginInit(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 409 | if (sysmod == NULL) |
| 410 | Py_FatalError("Py_Initialize: can't initialize sys"); |
| 411 | interp->sysdict = PyModule_GetDict(sysmod); |
| 412 | if (interp->sysdict == NULL) |
| 413 | Py_FatalError("Py_Initialize: can't initialize sys dict"); |
| 414 | Py_INCREF(interp->sysdict); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 415 | if (_PySys_EndInit(interp->sysdict) < 0) |
| 416 | Py_FatalError("Py_Initialize: can't initialize sys"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 417 | _PyImport_FixupBuiltin(sysmod, "sys"); |
| 418 | PySys_SetPath(Py_GetPath()); |
| 419 | PyDict_SetItemString(interp->sysdict, "modules", |
| 420 | interp->modules); |
| 421 | |
| 422 | /* Set up a preliminary stderr printer until we have enough |
| 423 | infrastructure for the io module in place. */ |
| 424 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 425 | if (pstderr == NULL) |
| 426 | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
| 427 | _PySys_SetObjectId(&PyId_stderr, pstderr); |
| 428 | PySys_SetObject("__stderr__", pstderr); |
| 429 | Py_DECREF(pstderr); |
| 430 | |
| 431 | _PyImport_Init(); |
| 432 | |
| 433 | _PyImportHooks_Init(); |
| 434 | |
| 435 | /* Initialize _warnings. */ |
| 436 | _PyWarnings_Init(); |
| 437 | |
| 438 | if (!install_importlib) |
| 439 | return; |
| 440 | |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 441 | if (_PyTime_Init() < 0) |
| 442 | Py_FatalError("Py_Initialize: can't initialize time"); |
| 443 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 444 | import_init(interp, sysmod); |
| 445 | |
| 446 | /* initialize the faulthandler module */ |
| 447 | if (_PyFaulthandler_Init()) |
| 448 | Py_FatalError("Py_Initialize: can't initialize faulthandler"); |
| 449 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 450 | if (initfsencoding(interp) < 0) |
| 451 | Py_FatalError("Py_Initialize: unable to load the file system codec"); |
| 452 | |
| 453 | if (install_sigs) |
| 454 | initsigs(); /* Signal handling stuff, including initintr() */ |
| 455 | |
| 456 | if (_PyTraceMalloc_Init() < 0) |
| 457 | Py_FatalError("Py_Initialize: can't initialize tracemalloc"); |
| 458 | |
| 459 | initmain(interp); /* Module __main__ */ |
| 460 | if (initstdio() < 0) |
| 461 | Py_FatalError( |
| 462 | "Py_Initialize: can't initialize sys standard streams"); |
| 463 | |
| 464 | /* Initialize warnings. */ |
| 465 | if (PySys_HasWarnOptions()) { |
| 466 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 467 | if (warnings_module == NULL) { |
| 468 | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
| 469 | PyErr_Print(); |
| 470 | } |
| 471 | Py_XDECREF(warnings_module); |
| 472 | } |
| 473 | |
| 474 | if (!Py_NoSiteFlag) |
| 475 | initsite(); /* Module site */ |
| 476 | } |
| 477 | |
| 478 | void |
| 479 | Py_InitializeEx(int install_sigs) |
| 480 | { |
| 481 | _Py_InitializeEx_Private(install_sigs, 1); |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | Py_Initialize(void) |
| 486 | { |
| 487 | Py_InitializeEx(1); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | #ifdef COUNT_ALLOCS |
| 492 | extern void dump_counts(FILE*); |
| 493 | #endif |
| 494 | |
| 495 | /* Flush stdout and stderr */ |
| 496 | |
| 497 | static int |
| 498 | file_is_closed(PyObject *fobj) |
| 499 | { |
| 500 | int r; |
| 501 | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
| 502 | if (tmp == NULL) { |
| 503 | PyErr_Clear(); |
| 504 | return 0; |
| 505 | } |
| 506 | r = PyObject_IsTrue(tmp); |
| 507 | Py_DECREF(tmp); |
| 508 | if (r < 0) |
| 509 | PyErr_Clear(); |
| 510 | return r > 0; |
| 511 | } |
| 512 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 513 | static int |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 514 | flush_std_files(void) |
| 515 | { |
| 516 | PyObject *fout = _PySys_GetObjectId(&PyId_stdout); |
| 517 | PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); |
| 518 | PyObject *tmp; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 519 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 520 | |
| 521 | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 522 | tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 523 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 524 | PyErr_WriteUnraisable(fout); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 525 | status = -1; |
| 526 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 527 | else |
| 528 | Py_DECREF(tmp); |
| 529 | } |
| 530 | |
| 531 | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 532 | tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 533 | if (tmp == NULL) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 534 | PyErr_Clear(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 535 | status = -1; |
| 536 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 537 | else |
| 538 | Py_DECREF(tmp); |
| 539 | } |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 540 | |
| 541 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Undo the effect of Py_Initialize(). |
| 545 | |
| 546 | Beware: if multiple interpreter and/or thread states exist, these |
| 547 | are not wiped out; only the current thread and interpreter state |
| 548 | are deleted. But since everything else is deleted, those other |
| 549 | interpreter and thread states should no longer be used. |
| 550 | |
| 551 | (XXX We should do better, e.g. wipe out all interpreters and |
| 552 | threads.) |
| 553 | |
| 554 | Locking: as above. |
| 555 | |
| 556 | */ |
| 557 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 558 | int |
| 559 | Py_FinalizeEx(void) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 560 | { |
| 561 | PyInterpreterState *interp; |
| 562 | PyThreadState *tstate; |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 563 | int status = 0; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 564 | |
| 565 | if (!initialized) |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 566 | return status; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 567 | |
| 568 | wait_for_thread_shutdown(); |
| 569 | |
| 570 | /* The interpreter is still entirely intact at this point, and the |
| 571 | * exit funcs may be relying on that. In particular, if some thread |
| 572 | * or exit func is still waiting to do an import, the import machinery |
| 573 | * expects Py_IsInitialized() to return true. So don't say the |
| 574 | * interpreter is uninitialized until after the exit funcs have run. |
| 575 | * Note that Threading.py uses an exit func to do a join on all the |
| 576 | * threads created thru it, so this also protects pending imports in |
| 577 | * the threads created via Threading. |
| 578 | */ |
| 579 | call_py_exitfuncs(); |
| 580 | |
| 581 | /* Get current thread state and interpreter pointer */ |
| 582 | tstate = PyThreadState_GET(); |
| 583 | interp = tstate->interp; |
| 584 | |
| 585 | /* Remaining threads (e.g. daemon threads) will automatically exit |
| 586 | after taking the GIL (in PyEval_RestoreThread()). */ |
| 587 | _Py_Finalizing = tstate; |
| 588 | initialized = 0; |
| 589 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 590 | /* Flush sys.stdout and sys.stderr */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 591 | if (flush_std_files() < 0) { |
| 592 | status = -1; |
| 593 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 594 | |
| 595 | /* Disable signal handling */ |
| 596 | PyOS_FiniInterrupts(); |
| 597 | |
| 598 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 599 | * before all modules are destroyed. |
| 600 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 601 | * XXX import a module, bad things can happen, because Python no |
| 602 | * XXX longer believes it's initialized. |
| 603 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 604 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 605 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 606 | * XXX in <function callback at 0x008F5718> ignored |
| 607 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 608 | * XXX I haven't seen a real-life report of either of these. |
| 609 | */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 610 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 611 | #ifdef COUNT_ALLOCS |
| 612 | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
| 613 | each collection might release some types from the type |
| 614 | list, so they become garbage. */ |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 615 | while (_PyGC_CollectIfEnabled() > 0) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 616 | /* nothing */; |
| 617 | #endif |
| 618 | /* Destroy all modules */ |
| 619 | PyImport_Cleanup(); |
| 620 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 621 | /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 622 | if (flush_std_files() < 0) { |
| 623 | status = -1; |
| 624 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 625 | |
| 626 | /* Collect final garbage. This disposes of cycles created by |
| 627 | * class definitions, for example. |
| 628 | * XXX This is disabled because it caused too many problems. If |
| 629 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 630 | * XXX a hard time running, because even the sys module has been |
| 631 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 632 | * XXX One symptom is a sequence of information-free messages |
| 633 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 634 | * XXX other threads can execute too, and any exception they encounter |
| 635 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 636 | * XXX fails to find what it *expects* to find in sys to help report |
| 637 | * XXX the exception and consequent unexpected failures). I've also |
| 638 | * XXX seen segfaults then, after adding print statements to the |
| 639 | * XXX Python code getting called. |
| 640 | */ |
| 641 | #if 0 |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 642 | _PyGC_CollectIfEnabled(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 643 | #endif |
| 644 | |
| 645 | /* Disable tracemalloc after all Python objects have been destroyed, |
| 646 | so it is possible to use tracemalloc in objects destructor. */ |
| 647 | _PyTraceMalloc_Fini(); |
| 648 | |
| 649 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 650 | _PyImport_Fini(); |
| 651 | |
| 652 | /* Cleanup typeobject.c's internal caches. */ |
| 653 | _PyType_Fini(); |
| 654 | |
| 655 | /* unload faulthandler module */ |
| 656 | _PyFaulthandler_Fini(); |
| 657 | |
| 658 | /* Debugging stuff */ |
| 659 | #ifdef COUNT_ALLOCS |
Serhiy Storchaka | 7e160ce | 2016-07-03 21:03:53 +0300 | [diff] [blame] | 660 | dump_counts(stderr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 661 | #endif |
| 662 | /* dump hash stats */ |
| 663 | _PyHash_Fini(); |
| 664 | |
| 665 | _PY_DEBUG_PRINT_TOTAL_REFS(); |
| 666 | |
| 667 | #ifdef Py_TRACE_REFS |
| 668 | /* Display all objects still alive -- this can invoke arbitrary |
| 669 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 670 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 671 | * up later. |
| 672 | */ |
| 673 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 674 | _Py_PrintReferences(stderr); |
| 675 | #endif /* Py_TRACE_REFS */ |
| 676 | |
| 677 | /* Clear interpreter state and all thread states. */ |
| 678 | PyInterpreterState_Clear(interp); |
| 679 | |
| 680 | /* Now we decref the exception classes. After this point nothing |
| 681 | can raise an exception. That's okay, because each Fini() method |
| 682 | below has been checked to make sure no exceptions are ever |
| 683 | raised. |
| 684 | */ |
| 685 | |
| 686 | _PyExc_Fini(); |
| 687 | |
| 688 | /* Sundry finalizers */ |
| 689 | PyMethod_Fini(); |
| 690 | PyFrame_Fini(); |
| 691 | PyCFunction_Fini(); |
| 692 | PyTuple_Fini(); |
| 693 | PyList_Fini(); |
| 694 | PySet_Fini(); |
| 695 | PyBytes_Fini(); |
| 696 | PyByteArray_Fini(); |
| 697 | PyLong_Fini(); |
| 698 | PyFloat_Fini(); |
| 699 | PyDict_Fini(); |
| 700 | PySlice_Fini(); |
| 701 | _PyGC_Fini(); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 702 | _Py_HashRandomization_Fini(); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 703 | _PyArg_Fini(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 704 | PyAsyncGen_Fini(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 705 | |
| 706 | /* Cleanup Unicode implementation */ |
| 707 | _PyUnicode_Fini(); |
| 708 | |
| 709 | /* reset file system default encoding */ |
| 710 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
| 711 | PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); |
| 712 | Py_FileSystemDefaultEncoding = NULL; |
| 713 | } |
| 714 | |
| 715 | /* XXX Still allocated: |
| 716 | - various static ad-hoc pointers to interned strings |
| 717 | - int and float free list blocks |
| 718 | - whatever various modules and libraries allocate |
| 719 | */ |
| 720 | |
| 721 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
| 722 | |
| 723 | /* Cleanup auto-thread-state */ |
| 724 | #ifdef WITH_THREAD |
| 725 | _PyGILState_Fini(); |
| 726 | #endif /* WITH_THREAD */ |
| 727 | |
| 728 | /* Delete current thread. After this, many C API calls become crashy. */ |
| 729 | PyThreadState_Swap(NULL); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 730 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 731 | PyInterpreterState_Delete(interp); |
| 732 | |
| 733 | #ifdef Py_TRACE_REFS |
| 734 | /* Display addresses (& refcnts) of all objects still alive. |
| 735 | * An address can be used to find the repr of the object, printed |
| 736 | * above by _Py_PrintReferences. |
| 737 | */ |
| 738 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 739 | _Py_PrintReferenceAddresses(stderr); |
| 740 | #endif /* Py_TRACE_REFS */ |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 741 | #ifdef WITH_PYMALLOC |
| 742 | if (_PyMem_PymallocEnabled()) { |
| 743 | char *opt = Py_GETENV("PYTHONMALLOCSTATS"); |
| 744 | if (opt != NULL && *opt != '\0') |
| 745 | _PyObject_DebugMallocStats(stderr); |
| 746 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 747 | #endif |
| 748 | |
| 749 | call_ll_exitfuncs(); |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 750 | return status; |
| 751 | } |
| 752 | |
| 753 | void |
| 754 | Py_Finalize(void) |
| 755 | { |
| 756 | Py_FinalizeEx(); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* Create and initialize a new interpreter and thread, and return the |
| 760 | new thread. This requires that Py_Initialize() has been called |
| 761 | first. |
| 762 | |
| 763 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 764 | exception information is available even in this case -- the |
| 765 | exception information is held in the thread, and there is no |
| 766 | thread. |
| 767 | |
| 768 | Locking: as above. |
| 769 | |
| 770 | */ |
| 771 | |
| 772 | PyThreadState * |
| 773 | Py_NewInterpreter(void) |
| 774 | { |
| 775 | PyInterpreterState *interp; |
| 776 | PyThreadState *tstate, *save_tstate; |
| 777 | PyObject *bimod, *sysmod; |
| 778 | |
| 779 | if (!initialized) |
| 780 | Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); |
| 781 | |
Victor Stinner | d7292b5 | 2016-06-17 12:29:00 +0200 | [diff] [blame] | 782 | #ifdef WITH_THREAD |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 783 | /* Issue #10915, #15751: The GIL API doesn't work with multiple |
| 784 | interpreters: disable PyGILState_Check(). */ |
| 785 | _PyGILState_check_enabled = 0; |
Berker Peksag | 531396c | 2016-06-17 13:25:01 +0300 | [diff] [blame] | 786 | #endif |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 787 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 788 | interp = PyInterpreterState_New(); |
| 789 | if (interp == NULL) |
| 790 | return NULL; |
| 791 | |
| 792 | tstate = PyThreadState_New(interp); |
| 793 | if (tstate == NULL) { |
| 794 | PyInterpreterState_Delete(interp); |
| 795 | return NULL; |
| 796 | } |
| 797 | |
| 798 | save_tstate = PyThreadState_Swap(tstate); |
| 799 | |
| 800 | /* XXX The following is lax in error checking */ |
| 801 | |
| 802 | interp->modules = PyDict_New(); |
| 803 | |
| 804 | bimod = _PyImport_FindBuiltin("builtins"); |
| 805 | if (bimod != NULL) { |
| 806 | interp->builtins = PyModule_GetDict(bimod); |
| 807 | if (interp->builtins == NULL) |
| 808 | goto handle_error; |
| 809 | Py_INCREF(interp->builtins); |
| 810 | } |
| 811 | |
| 812 | /* initialize builtin exceptions */ |
| 813 | _PyExc_Init(bimod); |
| 814 | |
| 815 | sysmod = _PyImport_FindBuiltin("sys"); |
| 816 | if (bimod != NULL && sysmod != NULL) { |
| 817 | PyObject *pstderr; |
| 818 | |
| 819 | interp->sysdict = PyModule_GetDict(sysmod); |
| 820 | if (interp->sysdict == NULL) |
| 821 | goto handle_error; |
| 822 | Py_INCREF(interp->sysdict); |
| 823 | PySys_SetPath(Py_GetPath()); |
| 824 | PyDict_SetItemString(interp->sysdict, "modules", |
| 825 | interp->modules); |
| 826 | /* Set up a preliminary stderr printer until we have enough |
| 827 | infrastructure for the io module in place. */ |
| 828 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 829 | if (pstderr == NULL) |
| 830 | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
| 831 | _PySys_SetObjectId(&PyId_stderr, pstderr); |
| 832 | PySys_SetObject("__stderr__", pstderr); |
| 833 | Py_DECREF(pstderr); |
| 834 | |
| 835 | _PyImportHooks_Init(); |
| 836 | |
| 837 | import_init(interp, sysmod); |
| 838 | |
| 839 | if (initfsencoding(interp) < 0) |
| 840 | goto handle_error; |
| 841 | |
| 842 | if (initstdio() < 0) |
| 843 | Py_FatalError( |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 844 | "Py_Initialize: can't initialize sys standard streams"); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 845 | initmain(interp); |
| 846 | if (!Py_NoSiteFlag) |
| 847 | initsite(); |
| 848 | } |
| 849 | |
| 850 | if (!PyErr_Occurred()) |
| 851 | return tstate; |
| 852 | |
| 853 | handle_error: |
| 854 | /* Oops, it didn't work. Undo it all. */ |
| 855 | |
| 856 | PyErr_PrintEx(0); |
| 857 | PyThreadState_Clear(tstate); |
| 858 | PyThreadState_Swap(save_tstate); |
| 859 | PyThreadState_Delete(tstate); |
| 860 | PyInterpreterState_Delete(interp); |
| 861 | |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | /* Delete an interpreter and its last thread. This requires that the |
| 866 | given thread state is current, that the thread has no remaining |
| 867 | frames, and that it is its interpreter's only remaining thread. |
| 868 | It is a fatal error to violate these constraints. |
| 869 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 870 | (Py_FinalizeEx() doesn't have these constraints -- it zaps |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 871 | everything, regardless.) |
| 872 | |
| 873 | Locking: as above. |
| 874 | |
| 875 | */ |
| 876 | |
| 877 | void |
| 878 | Py_EndInterpreter(PyThreadState *tstate) |
| 879 | { |
| 880 | PyInterpreterState *interp = tstate->interp; |
| 881 | |
| 882 | if (tstate != PyThreadState_GET()) |
| 883 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
| 884 | if (tstate->frame != NULL) |
| 885 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
| 886 | |
| 887 | wait_for_thread_shutdown(); |
| 888 | |
| 889 | if (tstate != interp->tstate_head || tstate->next != NULL) |
| 890 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
| 891 | |
| 892 | PyImport_Cleanup(); |
| 893 | PyInterpreterState_Clear(interp); |
| 894 | PyThreadState_Swap(NULL); |
| 895 | PyInterpreterState_Delete(interp); |
| 896 | } |
| 897 | |
| 898 | #ifdef MS_WINDOWS |
| 899 | static wchar_t *progname = L"python"; |
| 900 | #else |
| 901 | static wchar_t *progname = L"python3"; |
| 902 | #endif |
| 903 | |
| 904 | void |
| 905 | Py_SetProgramName(wchar_t *pn) |
| 906 | { |
| 907 | if (pn && *pn) |
| 908 | progname = pn; |
| 909 | } |
| 910 | |
| 911 | wchar_t * |
| 912 | Py_GetProgramName(void) |
| 913 | { |
| 914 | return progname; |
| 915 | } |
| 916 | |
| 917 | static wchar_t *default_home = NULL; |
| 918 | static wchar_t env_home[MAXPATHLEN+1]; |
| 919 | |
| 920 | void |
| 921 | Py_SetPythonHome(wchar_t *home) |
| 922 | { |
| 923 | default_home = home; |
| 924 | } |
| 925 | |
| 926 | wchar_t * |
| 927 | Py_GetPythonHome(void) |
| 928 | { |
| 929 | wchar_t *home = default_home; |
| 930 | if (home == NULL && !Py_IgnoreEnvironmentFlag) { |
| 931 | char* chome = Py_GETENV("PYTHONHOME"); |
| 932 | if (chome) { |
| 933 | size_t size = Py_ARRAY_LENGTH(env_home); |
| 934 | size_t r = mbstowcs(env_home, chome, size); |
| 935 | if (r != (size_t)-1 && r < size) |
| 936 | home = env_home; |
| 937 | } |
| 938 | |
| 939 | } |
| 940 | return home; |
| 941 | } |
| 942 | |
| 943 | /* Create __main__ module */ |
| 944 | |
| 945 | static void |
| 946 | initmain(PyInterpreterState *interp) |
| 947 | { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 948 | PyObject *m, *d, *loader, *ann_dict; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 949 | m = PyImport_AddModule("__main__"); |
| 950 | if (m == NULL) |
| 951 | Py_FatalError("can't create __main__ module"); |
| 952 | d = PyModule_GetDict(m); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 953 | ann_dict = PyDict_New(); |
| 954 | if ((ann_dict == NULL) || |
| 955 | (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { |
| 956 | Py_FatalError("Failed to initialize __main__.__annotations__"); |
| 957 | } |
| 958 | Py_DECREF(ann_dict); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 959 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 960 | PyObject *bimod = PyImport_ImportModule("builtins"); |
| 961 | if (bimod == NULL) { |
| 962 | Py_FatalError("Failed to retrieve builtins module"); |
| 963 | } |
| 964 | if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { |
| 965 | Py_FatalError("Failed to initialize __main__.__builtins__"); |
| 966 | } |
| 967 | Py_DECREF(bimod); |
| 968 | } |
| 969 | /* Main is a little special - imp.is_builtin("__main__") will return |
| 970 | * False, but BuiltinImporter is still the most appropriate initial |
| 971 | * setting for its __loader__ attribute. A more suitable value will |
| 972 | * be set if __main__ gets further initialized later in the startup |
| 973 | * process. |
| 974 | */ |
| 975 | loader = PyDict_GetItemString(d, "__loader__"); |
| 976 | if (loader == NULL || loader == Py_None) { |
| 977 | PyObject *loader = PyObject_GetAttrString(interp->importlib, |
| 978 | "BuiltinImporter"); |
| 979 | if (loader == NULL) { |
| 980 | Py_FatalError("Failed to retrieve BuiltinImporter"); |
| 981 | } |
| 982 | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
| 983 | Py_FatalError("Failed to initialize __main__.__loader__"); |
| 984 | } |
| 985 | Py_DECREF(loader); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | static int |
| 990 | initfsencoding(PyInterpreterState *interp) |
| 991 | { |
| 992 | PyObject *codec; |
| 993 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 994 | #ifdef MS_WINDOWS |
| 995 | if (Py_LegacyWindowsFSEncodingFlag) |
| 996 | { |
| 997 | Py_FileSystemDefaultEncoding = "mbcs"; |
| 998 | Py_FileSystemDefaultEncodeErrors = "replace"; |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | Py_FileSystemDefaultEncoding = "utf-8"; |
| 1003 | Py_FileSystemDefaultEncodeErrors = "surrogatepass"; |
| 1004 | } |
| 1005 | #else |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1006 | if (Py_FileSystemDefaultEncoding == NULL) |
| 1007 | { |
| 1008 | Py_FileSystemDefaultEncoding = get_locale_encoding(); |
| 1009 | if (Py_FileSystemDefaultEncoding == NULL) |
| 1010 | Py_FatalError("Py_Initialize: Unable to get the locale encoding"); |
| 1011 | |
| 1012 | Py_HasFileSystemDefaultEncoding = 0; |
| 1013 | interp->fscodec_initialized = 1; |
| 1014 | return 0; |
| 1015 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1016 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1017 | |
| 1018 | /* the encoding is mbcs, utf-8 or ascii */ |
| 1019 | codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); |
| 1020 | if (!codec) { |
| 1021 | /* Such error can only occurs in critical situations: no more |
| 1022 | * memory, import a module of the standard library failed, |
| 1023 | * etc. */ |
| 1024 | return -1; |
| 1025 | } |
| 1026 | Py_DECREF(codec); |
| 1027 | interp->fscodec_initialized = 1; |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | /* Import the site module (not into __main__ though) */ |
| 1032 | |
| 1033 | static void |
| 1034 | initsite(void) |
| 1035 | { |
| 1036 | PyObject *m; |
| 1037 | m = PyImport_ImportModule("site"); |
| 1038 | if (m == NULL) { |
| 1039 | fprintf(stderr, "Failed to import the site module\n"); |
| 1040 | PyErr_Print(); |
| 1041 | Py_Finalize(); |
| 1042 | exit(1); |
| 1043 | } |
| 1044 | else { |
| 1045 | Py_DECREF(m); |
| 1046 | } |
| 1047 | } |
| 1048 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1049 | /* Check if a file descriptor is valid or not. |
| 1050 | Return 0 if the file descriptor is invalid, return non-zero otherwise. */ |
| 1051 | static int |
| 1052 | is_valid_fd(int fd) |
| 1053 | { |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1054 | #ifdef __APPLE__ |
| 1055 | /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe |
| 1056 | and the other side of the pipe is closed, dup(1) succeed, whereas |
| 1057 | fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect |
| 1058 | such error. */ |
| 1059 | struct stat st; |
| 1060 | return (fstat(fd, &st) == 0); |
| 1061 | #else |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1062 | int fd2; |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 1063 | if (fd < 0) |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1064 | return 0; |
| 1065 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 449b271 | 2015-09-29 13:59:50 +0200 | [diff] [blame] | 1066 | /* Prefer dup() over fstat(). fstat() can require input/output whereas |
| 1067 | dup() doesn't, there is a low risk of EMFILE/ENFILE at Python |
| 1068 | startup. */ |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1069 | fd2 = dup(fd); |
| 1070 | if (fd2 >= 0) |
| 1071 | close(fd2); |
| 1072 | _Py_END_SUPPRESS_IPH |
| 1073 | return fd2 >= 0; |
Victor Stinner | 1c4670e | 2017-05-04 00:45:56 +0200 | [diff] [blame] | 1074 | #endif |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* returns Py_None if the fd is not valid */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1078 | static PyObject* |
| 1079 | create_stdio(PyObject* io, |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1080 | int fd, int write_mode, const char* name, |
| 1081 | const char* encoding, const char* errors) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1082 | { |
| 1083 | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
| 1084 | const char* mode; |
| 1085 | const char* newline; |
| 1086 | PyObject *line_buffering; |
| 1087 | int buffering, isatty; |
| 1088 | _Py_IDENTIFIER(open); |
| 1089 | _Py_IDENTIFIER(isatty); |
| 1090 | _Py_IDENTIFIER(TextIOWrapper); |
| 1091 | _Py_IDENTIFIER(mode); |
| 1092 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1093 | if (!is_valid_fd(fd)) |
| 1094 | Py_RETURN_NONE; |
| 1095 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1096 | /* stdin is always opened in buffered mode, first because it shouldn't |
| 1097 | make a difference in common use cases, second because TextIOWrapper |
| 1098 | depends on the presence of a read1() method which only exists on |
| 1099 | buffered streams. |
| 1100 | */ |
| 1101 | if (Py_UnbufferedStdioFlag && write_mode) |
| 1102 | buffering = 0; |
| 1103 | else |
| 1104 | buffering = -1; |
| 1105 | if (write_mode) |
| 1106 | mode = "wb"; |
| 1107 | else |
| 1108 | mode = "rb"; |
| 1109 | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", |
| 1110 | fd, mode, buffering, |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1111 | Py_None, Py_None, /* encoding, errors */ |
| 1112 | Py_None, 0); /* newline, closefd */ |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1113 | if (buf == NULL) |
| 1114 | goto error; |
| 1115 | |
| 1116 | if (buffering) { |
| 1117 | _Py_IDENTIFIER(raw); |
| 1118 | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
| 1119 | if (raw == NULL) |
| 1120 | goto error; |
| 1121 | } |
| 1122 | else { |
| 1123 | raw = buf; |
| 1124 | Py_INCREF(raw); |
| 1125 | } |
| 1126 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 1127 | #ifdef MS_WINDOWS |
| 1128 | /* Windows console IO is always UTF-8 encoded */ |
| 1129 | if (PyWindowsConsoleIO_Check(raw)) |
| 1130 | encoding = "utf-8"; |
| 1131 | #endif |
| 1132 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1133 | text = PyUnicode_FromString(name); |
| 1134 | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
| 1135 | goto error; |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1136 | res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1137 | if (res == NULL) |
| 1138 | goto error; |
| 1139 | isatty = PyObject_IsTrue(res); |
| 1140 | Py_DECREF(res); |
| 1141 | if (isatty == -1) |
| 1142 | goto error; |
| 1143 | if (isatty || Py_UnbufferedStdioFlag) |
| 1144 | line_buffering = Py_True; |
| 1145 | else |
| 1146 | line_buffering = Py_False; |
| 1147 | |
| 1148 | Py_CLEAR(raw); |
| 1149 | Py_CLEAR(text); |
| 1150 | |
| 1151 | #ifdef MS_WINDOWS |
| 1152 | /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" |
| 1153 | newlines to "\n". |
| 1154 | sys.stdout and sys.stderr: translate "\n" to "\r\n". */ |
| 1155 | newline = NULL; |
| 1156 | #else |
| 1157 | /* sys.stdin: split lines at "\n". |
| 1158 | sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ |
| 1159 | newline = "\n"; |
| 1160 | #endif |
| 1161 | |
| 1162 | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", |
| 1163 | buf, encoding, errors, |
| 1164 | newline, line_buffering); |
| 1165 | Py_CLEAR(buf); |
| 1166 | if (stream == NULL) |
| 1167 | goto error; |
| 1168 | |
| 1169 | if (write_mode) |
| 1170 | mode = "w"; |
| 1171 | else |
| 1172 | mode = "r"; |
| 1173 | text = PyUnicode_FromString(mode); |
| 1174 | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
| 1175 | goto error; |
| 1176 | Py_CLEAR(text); |
| 1177 | return stream; |
| 1178 | |
| 1179 | error: |
| 1180 | Py_XDECREF(buf); |
| 1181 | Py_XDECREF(stream); |
| 1182 | Py_XDECREF(text); |
| 1183 | Py_XDECREF(raw); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1184 | |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1185 | if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { |
| 1186 | /* Issue #24891: the file descriptor was closed after the first |
| 1187 | is_valid_fd() check was called. Ignore the OSError and set the |
| 1188 | stream to None. */ |
| 1189 | PyErr_Clear(); |
| 1190 | Py_RETURN_NONE; |
| 1191 | } |
| 1192 | return NULL; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
| 1196 | static int |
| 1197 | initstdio(void) |
| 1198 | { |
| 1199 | PyObject *iomod = NULL, *wrapper; |
| 1200 | PyObject *bimod = NULL; |
| 1201 | PyObject *m; |
| 1202 | PyObject *std = NULL; |
| 1203 | int status = 0, fd; |
| 1204 | PyObject * encoding_attr; |
| 1205 | char *pythonioencoding = NULL, *encoding, *errors; |
| 1206 | |
| 1207 | /* Hack to avoid a nasty recursion issue when Python is invoked |
| 1208 | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ |
| 1209 | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { |
| 1210 | goto error; |
| 1211 | } |
| 1212 | Py_DECREF(m); |
| 1213 | |
| 1214 | if (!(m = PyImport_ImportModule("encodings.latin_1"))) { |
| 1215 | goto error; |
| 1216 | } |
| 1217 | Py_DECREF(m); |
| 1218 | |
| 1219 | if (!(bimod = PyImport_ImportModule("builtins"))) { |
| 1220 | goto error; |
| 1221 | } |
| 1222 | |
| 1223 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 1224 | goto error; |
| 1225 | } |
| 1226 | if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
| 1227 | goto error; |
| 1228 | } |
| 1229 | |
| 1230 | /* Set builtins.open */ |
| 1231 | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
| 1232 | Py_DECREF(wrapper); |
| 1233 | goto error; |
| 1234 | } |
| 1235 | Py_DECREF(wrapper); |
| 1236 | |
| 1237 | encoding = _Py_StandardStreamEncoding; |
| 1238 | errors = _Py_StandardStreamErrors; |
| 1239 | if (!encoding || !errors) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1240 | pythonioencoding = Py_GETENV("PYTHONIOENCODING"); |
| 1241 | if (pythonioencoding) { |
| 1242 | char *err; |
| 1243 | pythonioencoding = _PyMem_Strdup(pythonioencoding); |
| 1244 | if (pythonioencoding == NULL) { |
| 1245 | PyErr_NoMemory(); |
| 1246 | goto error; |
| 1247 | } |
| 1248 | err = strchr(pythonioencoding, ':'); |
| 1249 | if (err) { |
| 1250 | *err = '\0'; |
| 1251 | err++; |
Serhiy Storchaka | fc43511 | 2016-04-10 14:34:13 +0300 | [diff] [blame] | 1252 | if (*err && !errors) { |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1253 | errors = err; |
| 1254 | } |
| 1255 | } |
| 1256 | if (*pythonioencoding && !encoding) { |
| 1257 | encoding = pythonioencoding; |
| 1258 | } |
| 1259 | } |
Serhiy Storchaka | fc43511 | 2016-04-10 14:34:13 +0300 | [diff] [blame] | 1260 | if (!errors && !(pythonioencoding && *pythonioencoding)) { |
| 1261 | /* When the LC_CTYPE locale is the POSIX locale ("C locale"), |
| 1262 | stdin and stdout use the surrogateescape error handler by |
| 1263 | default, instead of the strict error handler. */ |
| 1264 | char *loc = setlocale(LC_CTYPE, NULL); |
| 1265 | if (loc != NULL && strcmp(loc, "C") == 0) |
| 1266 | errors = "surrogateescape"; |
| 1267 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* Set sys.stdin */ |
| 1271 | fd = fileno(stdin); |
| 1272 | /* Under some conditions stdin, stdout and stderr may not be connected |
| 1273 | * and fileno() may point to an invalid file descriptor. For example |
| 1274 | * GUI apps don't have valid standard streams by default. |
| 1275 | */ |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1276 | std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
| 1277 | if (std == NULL) |
| 1278 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1279 | PySys_SetObject("__stdin__", std); |
| 1280 | _PySys_SetObjectId(&PyId_stdin, std); |
| 1281 | Py_DECREF(std); |
| 1282 | |
| 1283 | /* Set sys.stdout */ |
| 1284 | fd = fileno(stdout); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1285 | std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
| 1286 | if (std == NULL) |
| 1287 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1288 | PySys_SetObject("__stdout__", std); |
| 1289 | _PySys_SetObjectId(&PyId_stdout, std); |
| 1290 | Py_DECREF(std); |
| 1291 | |
| 1292 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 1293 | /* Set sys.stderr, replaces the preliminary stderr */ |
| 1294 | fd = fileno(stderr); |
Victor Stinner | 874dbe8 | 2015-09-04 17:29:57 +0200 | [diff] [blame] | 1295 | std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
| 1296 | if (std == NULL) |
| 1297 | goto error; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1298 | |
| 1299 | /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 1300 | when import.c tries to write to stderr in verbose mode. */ |
| 1301 | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 1302 | if (encoding_attr != NULL) { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1303 | const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1304 | if (std_encoding != NULL) { |
| 1305 | PyObject *codec_info = _PyCodec_Lookup(std_encoding); |
| 1306 | Py_XDECREF(codec_info); |
| 1307 | } |
| 1308 | Py_DECREF(encoding_attr); |
| 1309 | } |
| 1310 | PyErr_Clear(); /* Not a fatal error if codec isn't available */ |
| 1311 | |
| 1312 | if (PySys_SetObject("__stderr__", std) < 0) { |
| 1313 | Py_DECREF(std); |
| 1314 | goto error; |
| 1315 | } |
| 1316 | if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { |
| 1317 | Py_DECREF(std); |
| 1318 | goto error; |
| 1319 | } |
| 1320 | Py_DECREF(std); |
| 1321 | #endif |
| 1322 | |
| 1323 | if (0) { |
| 1324 | error: |
| 1325 | status = -1; |
| 1326 | } |
| 1327 | |
| 1328 | /* We won't need them anymore. */ |
| 1329 | if (_Py_StandardStreamEncoding) { |
| 1330 | PyMem_RawFree(_Py_StandardStreamEncoding); |
| 1331 | _Py_StandardStreamEncoding = NULL; |
| 1332 | } |
| 1333 | if (_Py_StandardStreamErrors) { |
| 1334 | PyMem_RawFree(_Py_StandardStreamErrors); |
| 1335 | _Py_StandardStreamErrors = NULL; |
| 1336 | } |
| 1337 | PyMem_Free(pythonioencoding); |
| 1338 | Py_XDECREF(bimod); |
| 1339 | Py_XDECREF(iomod); |
| 1340 | return status; |
| 1341 | } |
| 1342 | |
| 1343 | |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1344 | static void |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1345 | _Py_FatalError_DumpTracebacks(int fd) |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1346 | { |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1347 | fputc('\n', stderr); |
| 1348 | fflush(stderr); |
| 1349 | |
| 1350 | /* display the current Python stack */ |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 1351 | _Py_DumpTracebackThreads(fd, NULL, NULL); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1352 | } |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* Print the current exception (if an exception is set) with its traceback, |
| 1355 | or display the current Python stack. |
| 1356 | |
| 1357 | Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is |
| 1358 | called on catastrophic cases. |
| 1359 | |
| 1360 | Return 1 if the traceback was displayed, 0 otherwise. */ |
| 1361 | |
| 1362 | static int |
| 1363 | _Py_FatalError_PrintExc(int fd) |
| 1364 | { |
| 1365 | PyObject *ferr, *res; |
| 1366 | PyObject *exception, *v, *tb; |
| 1367 | int has_tb; |
| 1368 | |
| 1369 | if (PyThreadState_GET() == NULL) { |
| 1370 | /* The GIL is released: trying to acquire it is likely to deadlock, |
| 1371 | just give up. */ |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | PyErr_Fetch(&exception, &v, &tb); |
| 1376 | if (exception == NULL) { |
| 1377 | /* No current exception */ |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | ferr = _PySys_GetObjectId(&PyId_stderr); |
| 1382 | if (ferr == NULL || ferr == Py_None) { |
| 1383 | /* sys.stderr is not set yet or set to None, |
| 1384 | no need to try to display the exception */ |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | PyErr_NormalizeException(&exception, &v, &tb); |
| 1389 | if (tb == NULL) { |
| 1390 | tb = Py_None; |
| 1391 | Py_INCREF(tb); |
| 1392 | } |
| 1393 | PyException_SetTraceback(v, tb); |
| 1394 | if (exception == NULL) { |
| 1395 | /* PyErr_NormalizeException() failed */ |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | has_tb = (tb != Py_None); |
| 1400 | PyErr_Display(exception, v, tb); |
| 1401 | Py_XDECREF(exception); |
| 1402 | Py_XDECREF(v); |
| 1403 | Py_XDECREF(tb); |
| 1404 | |
| 1405 | /* sys.stderr may be buffered: call sys.stderr.flush() */ |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1406 | res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1407 | if (res == NULL) |
| 1408 | PyErr_Clear(); |
| 1409 | else |
| 1410 | Py_DECREF(res); |
| 1411 | |
| 1412 | return has_tb; |
| 1413 | } |
| 1414 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1415 | /* Print fatal error message and abort */ |
| 1416 | |
| 1417 | void |
| 1418 | Py_FatalError(const char *msg) |
| 1419 | { |
| 1420 | const int fd = fileno(stderr); |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1421 | static int reentrant = 0; |
| 1422 | #ifdef MS_WINDOWS |
| 1423 | size_t len; |
| 1424 | WCHAR* buffer; |
| 1425 | size_t i; |
| 1426 | #endif |
| 1427 | |
| 1428 | if (reentrant) { |
| 1429 | /* Py_FatalError() caused a second fatal error. |
| 1430 | Example: flush_std_files() raises a recursion error. */ |
| 1431 | goto exit; |
| 1432 | } |
| 1433 | reentrant = 1; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1434 | |
| 1435 | fprintf(stderr, "Fatal Python error: %s\n", msg); |
| 1436 | fflush(stderr); /* it helps in Windows debug build */ |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1437 | |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1438 | /* Print the exception (if an exception is set) with its traceback, |
| 1439 | * or display the current Python stack. */ |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1440 | if (!_Py_FatalError_PrintExc(fd)) |
| 1441 | _Py_FatalError_DumpTracebacks(fd); |
Victor Stinner | 10dc484 | 2015-03-24 12:01:30 +0100 | [diff] [blame] | 1442 | |
Victor Stinner | 2025d78 | 2016-03-16 23:19:15 +0100 | [diff] [blame] | 1443 | /* The main purpose of faulthandler is to display the traceback. We already |
| 1444 | * did our best to display it. So faulthandler can now be disabled. |
| 1445 | * (Don't trigger it on abort().) */ |
| 1446 | _PyFaulthandler_Fini(); |
| 1447 | |
Victor Stinner | 791da1c | 2016-03-14 16:53:12 +0100 | [diff] [blame] | 1448 | /* Check if the current Python thread hold the GIL */ |
| 1449 | if (PyThreadState_GET() != NULL) { |
| 1450 | /* Flush sys.stdout and sys.stderr */ |
| 1451 | flush_std_files(); |
| 1452 | } |
Victor Stinner | e0deff3 | 2015-03-24 13:46:18 +0100 | [diff] [blame] | 1453 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1454 | #ifdef MS_WINDOWS |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1455 | len = strlen(msg); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1456 | |
Victor Stinner | 53345a4 | 2015-03-25 01:55:14 +0100 | [diff] [blame] | 1457 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 1458 | conversion, assuming that the this error message actually uses ASCII |
| 1459 | only. If this ceases to be true, we will have to convert. */ |
| 1460 | buffer = alloca( (len+1) * (sizeof *buffer)); |
| 1461 | for( i=0; i<=len; ++i) |
| 1462 | buffer[i] = msg[i]; |
| 1463 | OutputDebugStringW(L"Fatal Python error: "); |
| 1464 | OutputDebugStringW(buffer); |
| 1465 | OutputDebugStringW(L"\n"); |
| 1466 | #endif /* MS_WINDOWS */ |
| 1467 | |
| 1468 | exit: |
| 1469 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1470 | DebugBreak(); |
| 1471 | #endif |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1472 | abort(); |
| 1473 | } |
| 1474 | |
| 1475 | /* Clean up and exit */ |
| 1476 | |
| 1477 | #ifdef WITH_THREAD |
Victor Stinner | d7292b5 | 2016-06-17 12:29:00 +0200 | [diff] [blame] | 1478 | # include "pythread.h" |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1479 | #endif |
| 1480 | |
| 1481 | static void (*pyexitfunc)(void) = NULL; |
| 1482 | /* For the atexit module. */ |
| 1483 | void _Py_PyAtExit(void (*func)(void)) |
| 1484 | { |
| 1485 | pyexitfunc = func; |
| 1486 | } |
| 1487 | |
| 1488 | static void |
| 1489 | call_py_exitfuncs(void) |
| 1490 | { |
| 1491 | if (pyexitfunc == NULL) |
| 1492 | return; |
| 1493 | |
| 1494 | (*pyexitfunc)(); |
| 1495 | PyErr_Clear(); |
| 1496 | } |
| 1497 | |
| 1498 | /* Wait until threading._shutdown completes, provided |
| 1499 | the threading module was imported in the first place. |
| 1500 | The shutdown routine will wait until all non-daemon |
| 1501 | "threading" threads have completed. */ |
| 1502 | static void |
| 1503 | wait_for_thread_shutdown(void) |
| 1504 | { |
| 1505 | #ifdef WITH_THREAD |
| 1506 | _Py_IDENTIFIER(_shutdown); |
| 1507 | PyObject *result; |
| 1508 | PyThreadState *tstate = PyThreadState_GET(); |
| 1509 | PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, |
| 1510 | "threading"); |
| 1511 | if (threading == NULL) { |
| 1512 | /* threading not imported */ |
| 1513 | PyErr_Clear(); |
| 1514 | return; |
| 1515 | } |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 1516 | result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1517 | if (result == NULL) { |
| 1518 | PyErr_WriteUnraisable(threading); |
| 1519 | } |
| 1520 | else { |
| 1521 | Py_DECREF(result); |
| 1522 | } |
| 1523 | Py_DECREF(threading); |
| 1524 | #endif |
| 1525 | } |
| 1526 | |
| 1527 | #define NEXITFUNCS 32 |
| 1528 | static void (*exitfuncs[NEXITFUNCS])(void); |
| 1529 | static int nexitfuncs = 0; |
| 1530 | |
| 1531 | int Py_AtExit(void (*func)(void)) |
| 1532 | { |
| 1533 | if (nexitfuncs >= NEXITFUNCS) |
| 1534 | return -1; |
| 1535 | exitfuncs[nexitfuncs++] = func; |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | static void |
| 1540 | call_ll_exitfuncs(void) |
| 1541 | { |
| 1542 | while (nexitfuncs > 0) |
| 1543 | (*exitfuncs[--nexitfuncs])(); |
| 1544 | |
| 1545 | fflush(stdout); |
| 1546 | fflush(stderr); |
| 1547 | } |
| 1548 | |
| 1549 | void |
| 1550 | Py_Exit(int sts) |
| 1551 | { |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1552 | if (Py_FinalizeEx() < 0) { |
| 1553 | sts = 120; |
| 1554 | } |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1555 | |
| 1556 | exit(sts); |
| 1557 | } |
| 1558 | |
| 1559 | static void |
| 1560 | initsigs(void) |
| 1561 | { |
| 1562 | #ifdef SIGPIPE |
| 1563 | PyOS_setsig(SIGPIPE, SIG_IGN); |
| 1564 | #endif |
| 1565 | #ifdef SIGXFZ |
| 1566 | PyOS_setsig(SIGXFZ, SIG_IGN); |
| 1567 | #endif |
| 1568 | #ifdef SIGXFSZ |
| 1569 | PyOS_setsig(SIGXFSZ, SIG_IGN); |
| 1570 | #endif |
| 1571 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
| 1572 | if (PyErr_Occurred()) { |
| 1573 | Py_FatalError("Py_Initialize: can't import signal"); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
| 1579 | * |
| 1580 | * All of the code in this function must only use async-signal-safe functions, |
| 1581 | * listed at `man 7 signal` or |
| 1582 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 1583 | */ |
| 1584 | void |
| 1585 | _Py_RestoreSignals(void) |
| 1586 | { |
| 1587 | #ifdef SIGPIPE |
| 1588 | PyOS_setsig(SIGPIPE, SIG_DFL); |
| 1589 | #endif |
| 1590 | #ifdef SIGXFZ |
| 1591 | PyOS_setsig(SIGXFZ, SIG_DFL); |
| 1592 | #endif |
| 1593 | #ifdef SIGXFSZ |
| 1594 | PyOS_setsig(SIGXFSZ, SIG_DFL); |
| 1595 | #endif |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | /* |
| 1600 | * The file descriptor fd is considered ``interactive'' if either |
| 1601 | * a) isatty(fd) is TRUE, or |
| 1602 | * b) the -i flag was given, and the filename associated with |
| 1603 | * the descriptor is NULL or "<stdin>" or "???". |
| 1604 | */ |
| 1605 | int |
| 1606 | Py_FdIsInteractive(FILE *fp, const char *filename) |
| 1607 | { |
| 1608 | if (isatty((int)fileno(fp))) |
| 1609 | return 1; |
| 1610 | if (!Py_InteractiveFlag) |
| 1611 | return 0; |
| 1612 | return (filename == NULL) || |
| 1613 | (strcmp(filename, "<stdin>") == 0) || |
| 1614 | (strcmp(filename, "???") == 0); |
| 1615 | } |
| 1616 | |
| 1617 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 1618 | /* Wrappers around sigaction() or signal(). */ |
| 1619 | |
| 1620 | PyOS_sighandler_t |
| 1621 | PyOS_getsig(int sig) |
| 1622 | { |
| 1623 | #ifdef HAVE_SIGACTION |
| 1624 | struct sigaction context; |
| 1625 | if (sigaction(sig, NULL, &context) == -1) |
| 1626 | return SIG_ERR; |
| 1627 | return context.sa_handler; |
| 1628 | #else |
| 1629 | PyOS_sighandler_t handler; |
| 1630 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 1631 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1632 | switch (sig) { |
| 1633 | /* Only these signals are valid */ |
| 1634 | case SIGINT: |
| 1635 | case SIGILL: |
| 1636 | case SIGFPE: |
| 1637 | case SIGSEGV: |
| 1638 | case SIGTERM: |
| 1639 | case SIGBREAK: |
| 1640 | case SIGABRT: |
| 1641 | break; |
| 1642 | /* Don't call signal() with other values or it will assert */ |
| 1643 | default: |
| 1644 | return SIG_ERR; |
| 1645 | } |
| 1646 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
| 1647 | handler = signal(sig, SIG_IGN); |
| 1648 | if (handler != SIG_ERR) |
| 1649 | signal(sig, handler); |
| 1650 | return handler; |
| 1651 | #endif |
| 1652 | } |
| 1653 | |
| 1654 | /* |
| 1655 | * All of the code in this function must only use async-signal-safe functions, |
| 1656 | * listed at `man 7 signal` or |
| 1657 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 1658 | */ |
| 1659 | PyOS_sighandler_t |
| 1660 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 1661 | { |
| 1662 | #ifdef HAVE_SIGACTION |
| 1663 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 1664 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 1665 | * changes to invalidate that assumption. |
| 1666 | */ |
| 1667 | struct sigaction context, ocontext; |
| 1668 | context.sa_handler = handler; |
| 1669 | sigemptyset(&context.sa_mask); |
| 1670 | context.sa_flags = 0; |
| 1671 | if (sigaction(sig, &context, &ocontext) == -1) |
| 1672 | return SIG_ERR; |
| 1673 | return ocontext.sa_handler; |
| 1674 | #else |
| 1675 | PyOS_sighandler_t oldhandler; |
| 1676 | oldhandler = signal(sig, handler); |
| 1677 | #ifdef HAVE_SIGINTERRUPT |
| 1678 | siginterrupt(sig, 1); |
| 1679 | #endif |
| 1680 | return oldhandler; |
| 1681 | #endif |
| 1682 | } |
| 1683 | |
| 1684 | #ifdef __cplusplus |
| 1685 | } |
| 1686 | #endif |