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