blob: 03601ead4db096b79c4190f9eebb4e8c772a499c [file] [log] [blame]
Nick Coghland6009512014-11-20 21:39:37 +10001/* 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 Dower39294992016-08-30 21:22:36 -070034
35extern PyTypeObject PyWindowsConsoleIO_Type;
36#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
Nick Coghland6009512014-11-20 21:39:37 +100037#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
46extern "C" {
47#endif
48
49extern wchar_t *Py_GetPath(void);
50
51extern grammar _PyParser_Grammar; /* From graminit.c */
52
53/* Forward */
54static void initmain(PyInterpreterState *interp);
55static int initfsencoding(PyInterpreterState *interp);
56static void initsite(void);
57static int initstdio(void);
58static void initsigs(void);
59static void call_py_exitfuncs(void);
60static void wait_for_thread_shutdown(void);
61static void call_ll_exitfuncs(void);
62extern int _PyUnicode_Init(void);
63extern int _PyStructSequence_Init(void);
64extern void _PyUnicode_Fini(void);
65extern int _PyLong_Init(void);
66extern void PyLong_Fini(void);
67extern int _PyFaulthandler_Init(void);
68extern void _PyFaulthandler_Fini(void);
69extern void _PyHash_Fini(void);
70extern int _PyTraceMalloc_Init(void);
71extern int _PyTraceMalloc_Fini(void);
72
73#ifdef WITH_THREAD
74extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
75extern 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? */
80int Py_DebugFlag; /* Needed by parser.c */
81int Py_VerboseFlag; /* Needed by import.c */
82int Py_QuietFlag; /* Needed by sysmodule.c */
83int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
84int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
85int Py_OptimizeFlag = 0; /* Needed by compile.c */
86int Py_NoSiteFlag; /* Suppress 'import site' */
87int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
88int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
89int Py_FrozenFlag; /* Needed by getpath.c */
90int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Xiang Zhang0710d752017-03-11 13:02:52 +080091int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.pyc) */
Nick Coghland6009512014-11-20 21:39:37 +100092int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
93int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
94int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
95int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
Steve Dowercc16be82016-09-08 10:35:16 -070096#ifdef MS_WINDOWS
97int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */
Steve Dower39294992016-08-30 21:22:36 -070098int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
Steve Dowercc16be82016-09-08 10:35:16 -070099#endif
Nick Coghland6009512014-11-20 21:39:37 +1000100
101PyThreadState *_Py_Finalizing = NULL;
102
103/* Hack to force loading of object files */
104int (*_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
108since _warnings is builtin. This API should not be used. */
109PyObject *
110PyModule_GetWarningsModule(void)
111{
112 return PyImport_ImportModule("warnings");
113}
114
115static int initialized = 0;
116
117/* API to access the initialized flag -- useful for esoteric use */
118
119int
120Py_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
129static char *_Py_StandardStreamEncoding = NULL;
130static char *_Py_StandardStreamErrors = NULL;
131
132int
133Py_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 Dower39294992016-08-30 21:22:36 -0700161#ifdef MS_WINDOWS
162 if (_Py_StandardStreamEncoding) {
163 /* Overriding the stream encoding implies legacy streams */
164 Py_LegacyWindowsStdioFlag = 1;
165 }
166#endif
Nick Coghland6009512014-11-20 21:39:37 +1000167 return 0;
168}
169
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000170/* Global initializations. Can be undone by Py_FinalizeEx(). Don't
171 call this twice without an intervening Py_FinalizeEx() call. When
Nick Coghland6009512014-11-20 21:39:37 +1000172 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
182static int
183add_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
193static char*
194get_codec_name(const char *encoding)
195{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200196 const char *name_utf8;
197 char *name_str;
Nick Coghland6009512014-11-20 21:39:37 +1000198 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 Storchaka06515832016-11-20 09:13:07 +0200209 name_utf8 = PyUnicode_AsUTF8(name);
Nick Coghland6009512014-11-20 21:39:37 +1000210 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
220error:
221 Py_XDECREF(codec);
222 Py_XDECREF(name);
223 return NULL;
224}
225
226static char*
227get_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 Krah144da4e2016-04-26 01:56:50 +0200240#elif defined(__ANDROID__)
241 return get_codec_name("UTF-8");
Nick Coghland6009512014-11-20 21:39:37 +1000242#else
243 PyErr_SetNone(PyExc_NotImplementedError);
244 return NULL;
245#endif
246}
247
248static void
249import_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 Storchaka133138a2016-08-02 22:51:21 +0300271 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 Stinnercd6e6942015-09-18 09:11:57 +0200276 /* Import the _imp module */
Nick Coghland6009512014-11-20 21:39:37 +1000277 impmod = PyInit_imp();
278 if (impmod == NULL) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200279 Py_FatalError("Py_Initialize: can't import _imp");
Nick Coghland6009512014-11-20 21:39:37 +1000280 }
281 else if (Py_VerboseFlag) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200282 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000283 }
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 Stinnercd6e6942015-09-18 09:11:57 +0200292 /* Install importlib as the implementation of import */
Nick Coghland6009512014-11-20 21:39:37 +1000293 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
Eric Snow6b4be192017-05-22 21:36:03 -0700294 if (value != NULL)
295 value = PyObject_CallMethod(importlib,
296 "_install_external_importers", "");
Nick Coghland6009512014-11-20 21:39:37 +1000297 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
308void
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 Gayeb445ad72016-11-16 07:24:20 +0100322#ifdef HAVE_SETLOCALE
Nick Coghland6009512014-11-20 21:39:37 +1000323 /* 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 Snow6b4be192017-05-22 21:36:03 -0700337 /* The variable is only tested for existence here;
338 _Py_HashRandomization_Init will check its value further. */
Nick Coghland6009512014-11-20 21:39:37 +1000339 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
340 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
Steve Dowercc16be82016-09-08 10:35:16 -0700341#ifdef MS_WINDOWS
342 if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
343 Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);
Steve Dower39294992016-08-30 21:22:36 -0700344 if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0')
345 Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p);
Steve Dowercc16be82016-09-08 10:35:16 -0700346#endif
Nick Coghland6009512014-11-20 21:39:37 +1000347
Eric Snow6b4be192017-05-22 21:36:03 -0700348 _Py_HashRandomization_Init();
Nick Coghland6009512014-11-20 21:39:37 +1000349
Eric Snowe3774162017-05-22 19:46:40 -0700350 _PyInterpreterState_Init();
Nick Coghland6009512014-11-20 21:39:37 +1000351 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 Panterb4ce1fc2015-11-30 03:18:29 +0000361 /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
Nick Coghland6009512014-11-20 21:39:37 +1000362 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 Panterb4ce1fc2015-11-30 03:18:29 +0000365 that we can call Py_Initialize / Py_FinalizeEx multiple times. */
Nick Coghland6009512014-11-20 21:39:37 +1000366 _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 Snow6b4be192017-05-22 21:36:03 -0700408 sysmod = _PySys_BeginInit();
Nick Coghland6009512014-11-20 21:39:37 +1000409 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 Snow6b4be192017-05-22 21:36:03 -0700415 if (_PySys_EndInit(interp->sysdict) < 0)
416 Py_FatalError("Py_Initialize: can't initialize sys");
Nick Coghland6009512014-11-20 21:39:37 +1000417 _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 Stinner13019fd2015-04-03 13:10:54 +0200441 if (_PyTime_Init() < 0)
442 Py_FatalError("Py_Initialize: can't initialize time");
443
Nick Coghland6009512014-11-20 21:39:37 +1000444 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 Coghland6009512014-11-20 21:39:37 +1000450 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
478void
479Py_InitializeEx(int install_sigs)
480{
481 _Py_InitializeEx_Private(install_sigs, 1);
482}
483
484void
485Py_Initialize(void)
486{
487 Py_InitializeEx(1);
488}
489
490
491#ifdef COUNT_ALLOCS
492extern void dump_counts(FILE*);
493#endif
494
495/* Flush stdout and stderr */
496
497static int
498file_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 Panterb4ce1fc2015-11-30 03:18:29 +0000513static int
Nick Coghland6009512014-11-20 21:39:37 +1000514flush_std_files(void)
515{
516 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
517 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
518 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000519 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +1000520
521 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Victor Stinner3466bde2016-09-05 18:16:01 -0700522 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000523 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +1000524 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000525 status = -1;
526 }
Nick Coghland6009512014-11-20 21:39:37 +1000527 else
528 Py_DECREF(tmp);
529 }
530
531 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Victor Stinner3466bde2016-09-05 18:16:01 -0700532 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000533 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +1000534 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000535 status = -1;
536 }
Nick Coghland6009512014-11-20 21:39:37 +1000537 else
538 Py_DECREF(tmp);
539 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000540
541 return status;
Nick Coghland6009512014-11-20 21:39:37 +1000542}
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 Panterb4ce1fc2015-11-30 03:18:29 +0000558int
559Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +1000560{
561 PyInterpreterState *interp;
562 PyThreadState *tstate;
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000563 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +1000564
565 if (!initialized)
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000566 return status;
Nick Coghland6009512014-11-20 21:39:37 +1000567
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 Stinnere0deff32015-03-24 13:46:18 +0100590 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000591 if (flush_std_files() < 0) {
592 status = -1;
593 }
Nick Coghland6009512014-11-20 21:39:37 +1000594
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 Langafef7e942016-09-09 21:47:46 -0700610 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +1000611#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 Langafef7e942016-09-09 21:47:46 -0700615 while (_PyGC_CollectIfEnabled() > 0)
Nick Coghland6009512014-11-20 21:39:37 +1000616 /* nothing */;
617#endif
618 /* Destroy all modules */
619 PyImport_Cleanup();
620
Victor Stinnere0deff32015-03-24 13:46:18 +0100621 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000622 if (flush_std_files() < 0) {
623 status = -1;
624 }
Nick Coghland6009512014-11-20 21:39:37 +1000625
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 Langafef7e942016-09-09 21:47:46 -0700642 _PyGC_CollectIfEnabled();
Nick Coghland6009512014-11-20 21:39:37 +1000643#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 Storchaka7e160ce2016-07-03 21:03:53 +0300660 dump_counts(stderr);
Nick Coghland6009512014-11-20 21:39:37 +1000661#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 Snow6b4be192017-05-22 21:36:03 -0700702 _Py_HashRandomization_Fini();
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300703 _PyArg_Fini();
Yury Selivanoveb636452016-09-08 22:01:51 -0700704 PyAsyncGen_Fini();
Nick Coghland6009512014-11-20 21:39:37 +1000705
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 Stinner8a1be612016-03-14 22:07:55 +0100730
Nick Coghland6009512014-11-20 21:39:37 +1000731 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 Stinner34be8072016-03-14 12:04:26 +0100741#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 Coghland6009512014-11-20 21:39:37 +1000747#endif
748
749 call_ll_exitfuncs();
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000750 return status;
751}
752
753void
754Py_Finalize(void)
755{
756 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +1000757}
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
772PyThreadState *
773Py_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 Stinnerd7292b52016-06-17 12:29:00 +0200782#ifdef WITH_THREAD
Victor Stinner8a1be612016-03-14 22:07:55 +0100783 /* Issue #10915, #15751: The GIL API doesn't work with multiple
784 interpreters: disable PyGILState_Check(). */
785 _PyGILState_check_enabled = 0;
Berker Peksag531396c2016-06-17 13:25:01 +0300786#endif
Victor Stinner8a1be612016-03-14 22:07:55 +0100787
Nick Coghland6009512014-11-20 21:39:37 +1000788 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 Brandl4b5b0622016-01-18 08:00:15 +0100844 "Py_Initialize: can't initialize sys standard streams");
Nick Coghland6009512014-11-20 21:39:37 +1000845 initmain(interp);
846 if (!Py_NoSiteFlag)
847 initsite();
848 }
849
850 if (!PyErr_Occurred())
851 return tstate;
852
853handle_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 Panterb4ce1fc2015-11-30 03:18:29 +0000870 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +1000871 everything, regardless.)
872
873 Locking: as above.
874
875*/
876
877void
878Py_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
899static wchar_t *progname = L"python";
900#else
901static wchar_t *progname = L"python3";
902#endif
903
904void
905Py_SetProgramName(wchar_t *pn)
906{
907 if (pn && *pn)
908 progname = pn;
909}
910
911wchar_t *
912Py_GetProgramName(void)
913{
914 return progname;
915}
916
917static wchar_t *default_home = NULL;
918static wchar_t env_home[MAXPATHLEN+1];
919
920void
921Py_SetPythonHome(wchar_t *home)
922{
923 default_home = home;
924}
925
926wchar_t *
927Py_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
945static void
946initmain(PyInterpreterState *interp)
947{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700948 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +1000949 m = PyImport_AddModule("__main__");
950 if (m == NULL)
951 Py_FatalError("can't create __main__ module");
952 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700953 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 Coghland6009512014-11-20 21:39:37 +1000959 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
989static int
990initfsencoding(PyInterpreterState *interp)
991{
992 PyObject *codec;
993
Steve Dowercc16be82016-09-08 10:35:16 -0700994#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 Coghland6009512014-11-20 21:39:37 +10001006 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 Dowercc16be82016-09-08 10:35:16 -07001016#endif
Nick Coghland6009512014-11-20 21:39:37 +10001017
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
1033static void
1034initsite(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 Stinner874dbe82015-09-04 17:29:57 +02001049/* Check if a file descriptor is valid or not.
1050 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
1051static int
1052is_valid_fd(int fd)
1053{
Victor Stinner1c4670e2017-05-04 00:45:56 +02001054#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 Stinner874dbe82015-09-04 17:29:57 +02001062 int fd2;
Steve Dower940f33a2016-09-08 11:21:54 -07001063 if (fd < 0)
Victor Stinner874dbe82015-09-04 17:29:57 +02001064 return 0;
1065 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner449b2712015-09-29 13:59:50 +02001066 /* 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 Stinner874dbe82015-09-04 17:29:57 +02001069 fd2 = dup(fd);
1070 if (fd2 >= 0)
1071 close(fd2);
1072 _Py_END_SUPPRESS_IPH
1073 return fd2 >= 0;
Victor Stinner1c4670e2017-05-04 00:45:56 +02001074#endif
Victor Stinner874dbe82015-09-04 17:29:57 +02001075}
1076
1077/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10001078static PyObject*
1079create_stdio(PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001080 int fd, int write_mode, const char* name,
1081 const char* encoding, const char* errors)
Nick Coghland6009512014-11-20 21:39:37 +10001082{
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 Stinner874dbe82015-09-04 17:29:57 +02001093 if (!is_valid_fd(fd))
1094 Py_RETURN_NONE;
1095
Nick Coghland6009512014-11-20 21:39:37 +10001096 /* 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 Panterb4ce1fc2015-11-30 03:18:29 +00001111 Py_None, Py_None, /* encoding, errors */
1112 Py_None, 0); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10001113 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 Dower39294992016-08-30 21:22:36 -07001127#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 Coghland6009512014-11-20 21:39:37 +10001133 text = PyUnicode_FromString(name);
1134 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
1135 goto error;
Victor Stinner3466bde2016-09-05 18:16:01 -07001136 res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001137 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
1179error:
1180 Py_XDECREF(buf);
1181 Py_XDECREF(stream);
1182 Py_XDECREF(text);
1183 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10001184
Victor Stinner874dbe82015-09-04 17:29:57 +02001185 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 Coghland6009512014-11-20 21:39:37 +10001193}
1194
1195/* Initialize sys.stdin, stdout, stderr and builtins.open */
1196static int
1197initstdio(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 Coghland6009512014-11-20 21:39:37 +10001240 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 Storchakafc435112016-04-10 14:34:13 +03001252 if (*err && !errors) {
Nick Coghland6009512014-11-20 21:39:37 +10001253 errors = err;
1254 }
1255 }
1256 if (*pythonioencoding && !encoding) {
1257 encoding = pythonioencoding;
1258 }
1259 }
Serhiy Storchakafc435112016-04-10 14:34:13 +03001260 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 Coghland6009512014-11-20 21:39:37 +10001268 }
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 Stinner874dbe82015-09-04 17:29:57 +02001276 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1277 if (std == NULL)
1278 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001279 PySys_SetObject("__stdin__", std);
1280 _PySys_SetObjectId(&PyId_stdin, std);
1281 Py_DECREF(std);
1282
1283 /* Set sys.stdout */
1284 fd = fileno(stdout);
Victor Stinner874dbe82015-09-04 17:29:57 +02001285 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1286 if (std == NULL)
1287 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001288 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 Stinner874dbe82015-09-04 17:29:57 +02001295 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1296 if (std == NULL)
1297 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001298
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 Storchaka85b0f5b2016-11-20 10:16:47 +02001303 const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
Nick Coghland6009512014-11-20 21:39:37 +10001304 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 Stinner10dc4842015-03-24 12:01:30 +01001344static void
Victor Stinner791da1c2016-03-14 16:53:12 +01001345_Py_FatalError_DumpTracebacks(int fd)
Victor Stinner10dc4842015-03-24 12:01:30 +01001346{
Victor Stinner10dc4842015-03-24 12:01:30 +01001347 fputc('\n', stderr);
1348 fflush(stderr);
1349
1350 /* display the current Python stack */
Victor Stinner861d9ab2016-03-16 22:45:24 +01001351 _Py_DumpTracebackThreads(fd, NULL, NULL);
Victor Stinner10dc4842015-03-24 12:01:30 +01001352}
Victor Stinner791da1c2016-03-14 16:53:12 +01001353
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
1362static 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 Stinner3466bde2016-09-05 18:16:01 -07001406 res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Victor Stinner791da1c2016-03-14 16:53:12 +01001407 if (res == NULL)
1408 PyErr_Clear();
1409 else
1410 Py_DECREF(res);
1411
1412 return has_tb;
1413}
1414
Nick Coghland6009512014-11-20 21:39:37 +10001415/* Print fatal error message and abort */
1416
1417void
1418Py_FatalError(const char *msg)
1419{
1420 const int fd = fileno(stderr);
Victor Stinner53345a42015-03-25 01:55:14 +01001421 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 Coghland6009512014-11-20 21:39:37 +10001434
1435 fprintf(stderr, "Fatal Python error: %s\n", msg);
1436 fflush(stderr); /* it helps in Windows debug build */
Victor Stinner10dc4842015-03-24 12:01:30 +01001437
Victor Stinnere0deff32015-03-24 13:46:18 +01001438 /* Print the exception (if an exception is set) with its traceback,
1439 * or display the current Python stack. */
Victor Stinner791da1c2016-03-14 16:53:12 +01001440 if (!_Py_FatalError_PrintExc(fd))
1441 _Py_FatalError_DumpTracebacks(fd);
Victor Stinner10dc4842015-03-24 12:01:30 +01001442
Victor Stinner2025d782016-03-16 23:19:15 +01001443 /* 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 Stinner791da1c2016-03-14 16:53:12 +01001448 /* 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 Stinnere0deff32015-03-24 13:46:18 +01001453
Nick Coghland6009512014-11-20 21:39:37 +10001454#ifdef MS_WINDOWS
Victor Stinner53345a42015-03-25 01:55:14 +01001455 len = strlen(msg);
Nick Coghland6009512014-11-20 21:39:37 +10001456
Victor Stinner53345a42015-03-25 01:55:14 +01001457 /* 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
1468exit:
1469#if defined(MS_WINDOWS) && defined(_DEBUG)
Nick Coghland6009512014-11-20 21:39:37 +10001470 DebugBreak();
1471#endif
Nick Coghland6009512014-11-20 21:39:37 +10001472 abort();
1473}
1474
1475/* Clean up and exit */
1476
1477#ifdef WITH_THREAD
Victor Stinnerd7292b52016-06-17 12:29:00 +02001478# include "pythread.h"
Nick Coghland6009512014-11-20 21:39:37 +10001479#endif
1480
1481static void (*pyexitfunc)(void) = NULL;
1482/* For the atexit module. */
1483void _Py_PyAtExit(void (*func)(void))
1484{
1485 pyexitfunc = func;
1486}
1487
1488static void
1489call_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. */
1502static void
1503wait_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 Stinner3466bde2016-09-05 18:16:01 -07001516 result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001517 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
1528static void (*exitfuncs[NEXITFUNCS])(void);
1529static int nexitfuncs = 0;
1530
1531int Py_AtExit(void (*func)(void))
1532{
1533 if (nexitfuncs >= NEXITFUNCS)
1534 return -1;
1535 exitfuncs[nexitfuncs++] = func;
1536 return 0;
1537}
1538
1539static void
1540call_ll_exitfuncs(void)
1541{
1542 while (nexitfuncs > 0)
1543 (*exitfuncs[--nexitfuncs])();
1544
1545 fflush(stdout);
1546 fflush(stderr);
1547}
1548
1549void
1550Py_Exit(int sts)
1551{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001552 if (Py_FinalizeEx() < 0) {
1553 sts = 120;
1554 }
Nick Coghland6009512014-11-20 21:39:37 +10001555
1556 exit(sts);
1557}
1558
1559static void
1560initsigs(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 */
1584void
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 */
1605int
1606Py_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 Coghland6009512014-11-20 21:39:37 +10001618/* Wrappers around sigaction() or signal(). */
1619
1620PyOS_sighandler_t
1621PyOS_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 */
1659PyOS_sighandler_t
1660PyOS_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