blob: 1a68255c93fe544f1403358b6b8ff42fbb131334 [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 */
91int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
92int 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{
196 char *name_utf8, *name_str;
197 PyObject *codec, *name = NULL;
198
199 codec = _PyCodec_Lookup(encoding);
200 if (!codec)
201 goto error;
202
203 name = _PyObject_GetAttrId(codec, &PyId_name);
204 Py_CLEAR(codec);
205 if (!name)
206 goto error;
207
208 name_utf8 = _PyUnicode_AsString(name);
209 if (name_utf8 == NULL)
210 goto error;
211 name_str = _PyMem_RawStrdup(name_utf8);
212 Py_DECREF(name);
213 if (name_str == NULL) {
214 PyErr_NoMemory();
215 return NULL;
216 }
217 return name_str;
218
219error:
220 Py_XDECREF(codec);
221 Py_XDECREF(name);
222 return NULL;
223}
224
225static char*
226get_locale_encoding(void)
227{
228#ifdef MS_WINDOWS
229 char codepage[100];
230 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
231 return get_codec_name(codepage);
232#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
233 char* codeset = nl_langinfo(CODESET);
234 if (!codeset || codeset[0] == '\0') {
235 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
236 return NULL;
237 }
238 return get_codec_name(codeset);
Stefan Krah144da4e2016-04-26 01:56:50 +0200239#elif defined(__ANDROID__)
240 return get_codec_name("UTF-8");
Nick Coghland6009512014-11-20 21:39:37 +1000241#else
242 PyErr_SetNone(PyExc_NotImplementedError);
243 return NULL;
244#endif
245}
246
247static void
248import_init(PyInterpreterState *interp, PyObject *sysmod)
249{
250 PyObject *importlib;
251 PyObject *impmod;
252 PyObject *sys_modules;
253 PyObject *value;
254
255 /* Import _importlib through its frozen version, _frozen_importlib. */
256 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
257 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
258 }
259 else if (Py_VerboseFlag) {
260 PySys_FormatStderr("import _frozen_importlib # frozen\n");
261 }
262 importlib = PyImport_AddModule("_frozen_importlib");
263 if (importlib == NULL) {
264 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
265 "sys.modules");
266 }
267 interp->importlib = importlib;
268 Py_INCREF(interp->importlib);
269
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300270 interp->import_func = PyDict_GetItemString(interp->builtins, "__import__");
271 if (interp->import_func == NULL)
272 Py_FatalError("Py_Initialize: __import__ not found");
273 Py_INCREF(interp->import_func);
274
Victor Stinnercd6e6942015-09-18 09:11:57 +0200275 /* Import the _imp module */
Nick Coghland6009512014-11-20 21:39:37 +1000276 impmod = PyInit_imp();
277 if (impmod == NULL) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200278 Py_FatalError("Py_Initialize: can't import _imp");
Nick Coghland6009512014-11-20 21:39:37 +1000279 }
280 else if (Py_VerboseFlag) {
Victor Stinnercd6e6942015-09-18 09:11:57 +0200281 PySys_FormatStderr("import _imp # builtin\n");
Nick Coghland6009512014-11-20 21:39:37 +1000282 }
283 sys_modules = PyImport_GetModuleDict();
284 if (Py_VerboseFlag) {
285 PySys_FormatStderr("import sys # builtin\n");
286 }
287 if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
288 Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
289 }
290
Victor Stinnercd6e6942015-09-18 09:11:57 +0200291 /* Install importlib as the implementation of import */
Nick Coghland6009512014-11-20 21:39:37 +1000292 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
293 if (value == NULL) {
294 PyErr_Print();
295 Py_FatalError("Py_Initialize: importlib install failed");
296 }
297 Py_DECREF(value);
298 Py_DECREF(impmod);
299
300 _PyImportZip_Init();
301}
302
303
304void
305_Py_InitializeEx_Private(int install_sigs, int install_importlib)
306{
307 PyInterpreterState *interp;
308 PyThreadState *tstate;
309 PyObject *bimod, *sysmod, *pstderr;
310 char *p;
311 extern void _Py_ReadyTypes(void);
312
313 if (initialized)
314 return;
315 initialized = 1;
316 _Py_Finalizing = NULL;
317
318#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
319 /* Set up the LC_CTYPE locale, so we can obtain
320 the locale's charset without having to switch
321 locales. */
322 setlocale(LC_CTYPE, "");
323#endif
324
325 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
326 Py_DebugFlag = add_flag(Py_DebugFlag, p);
327 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
328 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
329 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
330 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
331 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
332 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
333 /* The variable is only tested for existence here; _PyRandom_Init will
334 check its value further. */
335 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
336 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
Steve Dowercc16be82016-09-08 10:35:16 -0700337#ifdef MS_WINDOWS
338 if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
339 Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);
Steve Dower39294992016-08-30 21:22:36 -0700340 if ((p = Py_GETENV("PYTHONLEGACYWINDOWSSTDIO")) && *p != '\0')
341 Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p);
Steve Dowercc16be82016-09-08 10:35:16 -0700342#endif
Nick Coghland6009512014-11-20 21:39:37 +1000343
344 _PyRandom_Init();
345
346 interp = PyInterpreterState_New();
347 if (interp == NULL)
348 Py_FatalError("Py_Initialize: can't make first interpreter");
349
350 tstate = PyThreadState_New(interp);
351 if (tstate == NULL)
352 Py_FatalError("Py_Initialize: can't make first thread");
353 (void) PyThreadState_Swap(tstate);
354
355#ifdef WITH_THREAD
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000356 /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
Nick Coghland6009512014-11-20 21:39:37 +1000357 destroying the GIL might fail when it is being referenced from
358 another running thread (see issue #9901).
359 Instead we destroy the previously created GIL here, which ensures
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000360 that we can call Py_Initialize / Py_FinalizeEx multiple times. */
Nick Coghland6009512014-11-20 21:39:37 +1000361 _PyEval_FiniThreads();
362
363 /* Auto-thread-state API */
364 _PyGILState_Init(interp, tstate);
365#endif /* WITH_THREAD */
366
367 _Py_ReadyTypes();
368
369 if (!_PyFrame_Init())
370 Py_FatalError("Py_Initialize: can't init frames");
371
372 if (!_PyLong_Init())
373 Py_FatalError("Py_Initialize: can't init longs");
374
375 if (!PyByteArray_Init())
376 Py_FatalError("Py_Initialize: can't init bytearray");
377
378 if (!_PyFloat_Init())
379 Py_FatalError("Py_Initialize: can't init float");
380
381 interp->modules = PyDict_New();
382 if (interp->modules == NULL)
383 Py_FatalError("Py_Initialize: can't make modules dictionary");
384
385 /* Init Unicode implementation; relies on the codec registry */
386 if (_PyUnicode_Init() < 0)
387 Py_FatalError("Py_Initialize: can't initialize unicode");
388 if (_PyStructSequence_Init() < 0)
389 Py_FatalError("Py_Initialize: can't initialize structseq");
390
391 bimod = _PyBuiltin_Init();
392 if (bimod == NULL)
393 Py_FatalError("Py_Initialize: can't initialize builtins modules");
394 _PyImport_FixupBuiltin(bimod, "builtins");
395 interp->builtins = PyModule_GetDict(bimod);
396 if (interp->builtins == NULL)
397 Py_FatalError("Py_Initialize: can't initialize builtins dict");
398 Py_INCREF(interp->builtins);
399
400 /* initialize builtin exceptions */
401 _PyExc_Init(bimod);
402
403 sysmod = _PySys_Init();
404 if (sysmod == NULL)
405 Py_FatalError("Py_Initialize: can't initialize sys");
406 interp->sysdict = PyModule_GetDict(sysmod);
407 if (interp->sysdict == NULL)
408 Py_FatalError("Py_Initialize: can't initialize sys dict");
409 Py_INCREF(interp->sysdict);
410 _PyImport_FixupBuiltin(sysmod, "sys");
411 PySys_SetPath(Py_GetPath());
412 PyDict_SetItemString(interp->sysdict, "modules",
413 interp->modules);
414
415 /* Set up a preliminary stderr printer until we have enough
416 infrastructure for the io module in place. */
417 pstderr = PyFile_NewStdPrinter(fileno(stderr));
418 if (pstderr == NULL)
419 Py_FatalError("Py_Initialize: can't set preliminary stderr");
420 _PySys_SetObjectId(&PyId_stderr, pstderr);
421 PySys_SetObject("__stderr__", pstderr);
422 Py_DECREF(pstderr);
423
424 _PyImport_Init();
425
426 _PyImportHooks_Init();
427
428 /* Initialize _warnings. */
429 _PyWarnings_Init();
430
431 if (!install_importlib)
432 return;
433
Victor Stinner13019fd2015-04-03 13:10:54 +0200434 if (_PyTime_Init() < 0)
435 Py_FatalError("Py_Initialize: can't initialize time");
436
Nick Coghland6009512014-11-20 21:39:37 +1000437 import_init(interp, sysmod);
438
439 /* initialize the faulthandler module */
440 if (_PyFaulthandler_Init())
441 Py_FatalError("Py_Initialize: can't initialize faulthandler");
442
Nick Coghland6009512014-11-20 21:39:37 +1000443 if (initfsencoding(interp) < 0)
444 Py_FatalError("Py_Initialize: unable to load the file system codec");
445
446 if (install_sigs)
447 initsigs(); /* Signal handling stuff, including initintr() */
448
449 if (_PyTraceMalloc_Init() < 0)
450 Py_FatalError("Py_Initialize: can't initialize tracemalloc");
451
452 initmain(interp); /* Module __main__ */
453 if (initstdio() < 0)
454 Py_FatalError(
455 "Py_Initialize: can't initialize sys standard streams");
456
457 /* Initialize warnings. */
458 if (PySys_HasWarnOptions()) {
459 PyObject *warnings_module = PyImport_ImportModule("warnings");
460 if (warnings_module == NULL) {
461 fprintf(stderr, "'import warnings' failed; traceback:\n");
462 PyErr_Print();
463 }
464 Py_XDECREF(warnings_module);
465 }
466
467 if (!Py_NoSiteFlag)
468 initsite(); /* Module site */
469}
470
471void
472Py_InitializeEx(int install_sigs)
473{
474 _Py_InitializeEx_Private(install_sigs, 1);
475}
476
477void
478Py_Initialize(void)
479{
480 Py_InitializeEx(1);
481}
482
483
484#ifdef COUNT_ALLOCS
485extern void dump_counts(FILE*);
486#endif
487
488/* Flush stdout and stderr */
489
490static int
491file_is_closed(PyObject *fobj)
492{
493 int r;
494 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
495 if (tmp == NULL) {
496 PyErr_Clear();
497 return 0;
498 }
499 r = PyObject_IsTrue(tmp);
500 Py_DECREF(tmp);
501 if (r < 0)
502 PyErr_Clear();
503 return r > 0;
504}
505
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000506static int
Nick Coghland6009512014-11-20 21:39:37 +1000507flush_std_files(void)
508{
509 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
510 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
511 PyObject *tmp;
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000512 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +1000513
514 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Victor Stinner3466bde2016-09-05 18:16:01 -0700515 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000516 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +1000517 PyErr_WriteUnraisable(fout);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000518 status = -1;
519 }
Nick Coghland6009512014-11-20 21:39:37 +1000520 else
521 Py_DECREF(tmp);
522 }
523
524 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Victor Stinner3466bde2016-09-05 18:16:01 -0700525 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000526 if (tmp == NULL) {
Nick Coghland6009512014-11-20 21:39:37 +1000527 PyErr_Clear();
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000528 status = -1;
529 }
Nick Coghland6009512014-11-20 21:39:37 +1000530 else
531 Py_DECREF(tmp);
532 }
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000533
534 return status;
Nick Coghland6009512014-11-20 21:39:37 +1000535}
536
537/* Undo the effect of Py_Initialize().
538
539 Beware: if multiple interpreter and/or thread states exist, these
540 are not wiped out; only the current thread and interpreter state
541 are deleted. But since everything else is deleted, those other
542 interpreter and thread states should no longer be used.
543
544 (XXX We should do better, e.g. wipe out all interpreters and
545 threads.)
546
547 Locking: as above.
548
549*/
550
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000551int
552Py_FinalizeEx(void)
Nick Coghland6009512014-11-20 21:39:37 +1000553{
554 PyInterpreterState *interp;
555 PyThreadState *tstate;
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000556 int status = 0;
Nick Coghland6009512014-11-20 21:39:37 +1000557
558 if (!initialized)
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000559 return status;
Nick Coghland6009512014-11-20 21:39:37 +1000560
561 wait_for_thread_shutdown();
562
563 /* The interpreter is still entirely intact at this point, and the
564 * exit funcs may be relying on that. In particular, if some thread
565 * or exit func is still waiting to do an import, the import machinery
566 * expects Py_IsInitialized() to return true. So don't say the
567 * interpreter is uninitialized until after the exit funcs have run.
568 * Note that Threading.py uses an exit func to do a join on all the
569 * threads created thru it, so this also protects pending imports in
570 * the threads created via Threading.
571 */
572 call_py_exitfuncs();
573
574 /* Get current thread state and interpreter pointer */
575 tstate = PyThreadState_GET();
576 interp = tstate->interp;
577
578 /* Remaining threads (e.g. daemon threads) will automatically exit
579 after taking the GIL (in PyEval_RestoreThread()). */
580 _Py_Finalizing = tstate;
581 initialized = 0;
582
Victor Stinnere0deff32015-03-24 13:46:18 +0100583 /* Flush sys.stdout and sys.stderr */
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000584 if (flush_std_files() < 0) {
585 status = -1;
586 }
Nick Coghland6009512014-11-20 21:39:37 +1000587
588 /* Disable signal handling */
589 PyOS_FiniInterrupts();
590
591 /* Collect garbage. This may call finalizers; it's nice to call these
592 * before all modules are destroyed.
593 * XXX If a __del__ or weakref callback is triggered here, and tries to
594 * XXX import a module, bad things can happen, because Python no
595 * XXX longer believes it's initialized.
596 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
597 * XXX is easy to provoke that way. I've also seen, e.g.,
598 * XXX Exception exceptions.ImportError: 'No module named sha'
599 * XXX in <function callback at 0x008F5718> ignored
600 * XXX but I'm unclear on exactly how that one happens. In any case,
601 * XXX I haven't seen a real-life report of either of these.
602 */
603 PyGC_Collect();
604#ifdef COUNT_ALLOCS
605 /* With COUNT_ALLOCS, it helps to run GC multiple times:
606 each collection might release some types from the type
607 list, so they become garbage. */
608 while (PyGC_Collect() > 0)
609 /* nothing */;
610#endif
611 /* Destroy all modules */
612 PyImport_Cleanup();
613
Victor Stinnere0deff32015-03-24 13:46:18 +0100614 /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000615 if (flush_std_files() < 0) {
616 status = -1;
617 }
Nick Coghland6009512014-11-20 21:39:37 +1000618
619 /* Collect final garbage. This disposes of cycles created by
620 * class definitions, for example.
621 * XXX This is disabled because it caused too many problems. If
622 * XXX a __del__ or weakref callback triggers here, Python code has
623 * XXX a hard time running, because even the sys module has been
624 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
625 * XXX One symptom is a sequence of information-free messages
626 * XXX coming from threads (if a __del__ or callback is invoked,
627 * XXX other threads can execute too, and any exception they encounter
628 * XXX triggers a comedy of errors as subsystem after subsystem
629 * XXX fails to find what it *expects* to find in sys to help report
630 * XXX the exception and consequent unexpected failures). I've also
631 * XXX seen segfaults then, after adding print statements to the
632 * XXX Python code getting called.
633 */
634#if 0
635 PyGC_Collect();
636#endif
637
638 /* Disable tracemalloc after all Python objects have been destroyed,
639 so it is possible to use tracemalloc in objects destructor. */
640 _PyTraceMalloc_Fini();
641
642 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
643 _PyImport_Fini();
644
645 /* Cleanup typeobject.c's internal caches. */
646 _PyType_Fini();
647
648 /* unload faulthandler module */
649 _PyFaulthandler_Fini();
650
651 /* Debugging stuff */
652#ifdef COUNT_ALLOCS
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300653 dump_counts(stderr);
Nick Coghland6009512014-11-20 21:39:37 +1000654#endif
655 /* dump hash stats */
656 _PyHash_Fini();
657
658 _PY_DEBUG_PRINT_TOTAL_REFS();
659
660#ifdef Py_TRACE_REFS
661 /* Display all objects still alive -- this can invoke arbitrary
662 * __repr__ overrides, so requires a mostly-intact interpreter.
663 * Alas, a lot of stuff may still be alive now that will be cleaned
664 * up later.
665 */
666 if (Py_GETENV("PYTHONDUMPREFS"))
667 _Py_PrintReferences(stderr);
668#endif /* Py_TRACE_REFS */
669
670 /* Clear interpreter state and all thread states. */
671 PyInterpreterState_Clear(interp);
672
673 /* Now we decref the exception classes. After this point nothing
674 can raise an exception. That's okay, because each Fini() method
675 below has been checked to make sure no exceptions are ever
676 raised.
677 */
678
679 _PyExc_Fini();
680
681 /* Sundry finalizers */
682 PyMethod_Fini();
683 PyFrame_Fini();
684 PyCFunction_Fini();
685 PyTuple_Fini();
686 PyList_Fini();
687 PySet_Fini();
688 PyBytes_Fini();
689 PyByteArray_Fini();
690 PyLong_Fini();
691 PyFloat_Fini();
692 PyDict_Fini();
693 PySlice_Fini();
694 _PyGC_Fini();
695 _PyRandom_Fini();
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300696 _PyArg_Fini();
Nick Coghland6009512014-11-20 21:39:37 +1000697
698 /* Cleanup Unicode implementation */
699 _PyUnicode_Fini();
700
701 /* reset file system default encoding */
702 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
703 PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
704 Py_FileSystemDefaultEncoding = NULL;
705 }
706
707 /* XXX Still allocated:
708 - various static ad-hoc pointers to interned strings
709 - int and float free list blocks
710 - whatever various modules and libraries allocate
711 */
712
713 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
714
715 /* Cleanup auto-thread-state */
716#ifdef WITH_THREAD
717 _PyGILState_Fini();
718#endif /* WITH_THREAD */
719
720 /* Delete current thread. After this, many C API calls become crashy. */
721 PyThreadState_Swap(NULL);
Victor Stinner8a1be612016-03-14 22:07:55 +0100722
Nick Coghland6009512014-11-20 21:39:37 +1000723 PyInterpreterState_Delete(interp);
724
725#ifdef Py_TRACE_REFS
726 /* Display addresses (& refcnts) of all objects still alive.
727 * An address can be used to find the repr of the object, printed
728 * above by _Py_PrintReferences.
729 */
730 if (Py_GETENV("PYTHONDUMPREFS"))
731 _Py_PrintReferenceAddresses(stderr);
732#endif /* Py_TRACE_REFS */
Victor Stinner34be8072016-03-14 12:04:26 +0100733#ifdef WITH_PYMALLOC
734 if (_PyMem_PymallocEnabled()) {
735 char *opt = Py_GETENV("PYTHONMALLOCSTATS");
736 if (opt != NULL && *opt != '\0')
737 _PyObject_DebugMallocStats(stderr);
738 }
Nick Coghland6009512014-11-20 21:39:37 +1000739#endif
740
741 call_ll_exitfuncs();
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000742 return status;
743}
744
745void
746Py_Finalize(void)
747{
748 Py_FinalizeEx();
Nick Coghland6009512014-11-20 21:39:37 +1000749}
750
751/* Create and initialize a new interpreter and thread, and return the
752 new thread. This requires that Py_Initialize() has been called
753 first.
754
755 Unsuccessful initialization yields a NULL pointer. Note that *no*
756 exception information is available even in this case -- the
757 exception information is held in the thread, and there is no
758 thread.
759
760 Locking: as above.
761
762*/
763
764PyThreadState *
765Py_NewInterpreter(void)
766{
767 PyInterpreterState *interp;
768 PyThreadState *tstate, *save_tstate;
769 PyObject *bimod, *sysmod;
770
771 if (!initialized)
772 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
773
Victor Stinnerd7292b52016-06-17 12:29:00 +0200774#ifdef WITH_THREAD
Victor Stinner8a1be612016-03-14 22:07:55 +0100775 /* Issue #10915, #15751: The GIL API doesn't work with multiple
776 interpreters: disable PyGILState_Check(). */
777 _PyGILState_check_enabled = 0;
Berker Peksag531396c2016-06-17 13:25:01 +0300778#endif
Victor Stinner8a1be612016-03-14 22:07:55 +0100779
Nick Coghland6009512014-11-20 21:39:37 +1000780 interp = PyInterpreterState_New();
781 if (interp == NULL)
782 return NULL;
783
784 tstate = PyThreadState_New(interp);
785 if (tstate == NULL) {
786 PyInterpreterState_Delete(interp);
787 return NULL;
788 }
789
790 save_tstate = PyThreadState_Swap(tstate);
791
792 /* XXX The following is lax in error checking */
793
794 interp->modules = PyDict_New();
795
796 bimod = _PyImport_FindBuiltin("builtins");
797 if (bimod != NULL) {
798 interp->builtins = PyModule_GetDict(bimod);
799 if (interp->builtins == NULL)
800 goto handle_error;
801 Py_INCREF(interp->builtins);
802 }
803
804 /* initialize builtin exceptions */
805 _PyExc_Init(bimod);
806
807 sysmod = _PyImport_FindBuiltin("sys");
808 if (bimod != NULL && sysmod != NULL) {
809 PyObject *pstderr;
810
811 interp->sysdict = PyModule_GetDict(sysmod);
812 if (interp->sysdict == NULL)
813 goto handle_error;
814 Py_INCREF(interp->sysdict);
815 PySys_SetPath(Py_GetPath());
816 PyDict_SetItemString(interp->sysdict, "modules",
817 interp->modules);
818 /* Set up a preliminary stderr printer until we have enough
819 infrastructure for the io module in place. */
820 pstderr = PyFile_NewStdPrinter(fileno(stderr));
821 if (pstderr == NULL)
822 Py_FatalError("Py_Initialize: can't set preliminary stderr");
823 _PySys_SetObjectId(&PyId_stderr, pstderr);
824 PySys_SetObject("__stderr__", pstderr);
825 Py_DECREF(pstderr);
826
827 _PyImportHooks_Init();
828
829 import_init(interp, sysmod);
830
831 if (initfsencoding(interp) < 0)
832 goto handle_error;
833
834 if (initstdio() < 0)
835 Py_FatalError(
Georg Brandl4b5b0622016-01-18 08:00:15 +0100836 "Py_Initialize: can't initialize sys standard streams");
Nick Coghland6009512014-11-20 21:39:37 +1000837 initmain(interp);
838 if (!Py_NoSiteFlag)
839 initsite();
840 }
841
842 if (!PyErr_Occurred())
843 return tstate;
844
845handle_error:
846 /* Oops, it didn't work. Undo it all. */
847
848 PyErr_PrintEx(0);
849 PyThreadState_Clear(tstate);
850 PyThreadState_Swap(save_tstate);
851 PyThreadState_Delete(tstate);
852 PyInterpreterState_Delete(interp);
853
854 return NULL;
855}
856
857/* Delete an interpreter and its last thread. This requires that the
858 given thread state is current, that the thread has no remaining
859 frames, and that it is its interpreter's only remaining thread.
860 It is a fatal error to violate these constraints.
861
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000862 (Py_FinalizeEx() doesn't have these constraints -- it zaps
Nick Coghland6009512014-11-20 21:39:37 +1000863 everything, regardless.)
864
865 Locking: as above.
866
867*/
868
869void
870Py_EndInterpreter(PyThreadState *tstate)
871{
872 PyInterpreterState *interp = tstate->interp;
873
874 if (tstate != PyThreadState_GET())
875 Py_FatalError("Py_EndInterpreter: thread is not current");
876 if (tstate->frame != NULL)
877 Py_FatalError("Py_EndInterpreter: thread still has a frame");
878
879 wait_for_thread_shutdown();
880
881 if (tstate != interp->tstate_head || tstate->next != NULL)
882 Py_FatalError("Py_EndInterpreter: not the last thread");
883
884 PyImport_Cleanup();
885 PyInterpreterState_Clear(interp);
886 PyThreadState_Swap(NULL);
887 PyInterpreterState_Delete(interp);
888}
889
890#ifdef MS_WINDOWS
891static wchar_t *progname = L"python";
892#else
893static wchar_t *progname = L"python3";
894#endif
895
896void
897Py_SetProgramName(wchar_t *pn)
898{
899 if (pn && *pn)
900 progname = pn;
901}
902
903wchar_t *
904Py_GetProgramName(void)
905{
906 return progname;
907}
908
909static wchar_t *default_home = NULL;
910static wchar_t env_home[MAXPATHLEN+1];
911
912void
913Py_SetPythonHome(wchar_t *home)
914{
915 default_home = home;
916}
917
918wchar_t *
919Py_GetPythonHome(void)
920{
921 wchar_t *home = default_home;
922 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
923 char* chome = Py_GETENV("PYTHONHOME");
924 if (chome) {
925 size_t size = Py_ARRAY_LENGTH(env_home);
926 size_t r = mbstowcs(env_home, chome, size);
927 if (r != (size_t)-1 && r < size)
928 home = env_home;
929 }
930
931 }
932 return home;
933}
934
935/* Create __main__ module */
936
937static void
938initmain(PyInterpreterState *interp)
939{
940 PyObject *m, *d, *loader;
941 m = PyImport_AddModule("__main__");
942 if (m == NULL)
943 Py_FatalError("can't create __main__ module");
944 d = PyModule_GetDict(m);
945 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
946 PyObject *bimod = PyImport_ImportModule("builtins");
947 if (bimod == NULL) {
948 Py_FatalError("Failed to retrieve builtins module");
949 }
950 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
951 Py_FatalError("Failed to initialize __main__.__builtins__");
952 }
953 Py_DECREF(bimod);
954 }
955 /* Main is a little special - imp.is_builtin("__main__") will return
956 * False, but BuiltinImporter is still the most appropriate initial
957 * setting for its __loader__ attribute. A more suitable value will
958 * be set if __main__ gets further initialized later in the startup
959 * process.
960 */
961 loader = PyDict_GetItemString(d, "__loader__");
962 if (loader == NULL || loader == Py_None) {
963 PyObject *loader = PyObject_GetAttrString(interp->importlib,
964 "BuiltinImporter");
965 if (loader == NULL) {
966 Py_FatalError("Failed to retrieve BuiltinImporter");
967 }
968 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
969 Py_FatalError("Failed to initialize __main__.__loader__");
970 }
971 Py_DECREF(loader);
972 }
973}
974
975static int
976initfsencoding(PyInterpreterState *interp)
977{
978 PyObject *codec;
979
Steve Dowercc16be82016-09-08 10:35:16 -0700980#ifdef MS_WINDOWS
981 if (Py_LegacyWindowsFSEncodingFlag)
982 {
983 Py_FileSystemDefaultEncoding = "mbcs";
984 Py_FileSystemDefaultEncodeErrors = "replace";
985 }
986 else
987 {
988 Py_FileSystemDefaultEncoding = "utf-8";
989 Py_FileSystemDefaultEncodeErrors = "surrogatepass";
990 }
991#else
Nick Coghland6009512014-11-20 21:39:37 +1000992 if (Py_FileSystemDefaultEncoding == NULL)
993 {
994 Py_FileSystemDefaultEncoding = get_locale_encoding();
995 if (Py_FileSystemDefaultEncoding == NULL)
996 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
997
998 Py_HasFileSystemDefaultEncoding = 0;
999 interp->fscodec_initialized = 1;
1000 return 0;
1001 }
Steve Dowercc16be82016-09-08 10:35:16 -07001002#endif
Nick Coghland6009512014-11-20 21:39:37 +10001003
1004 /* the encoding is mbcs, utf-8 or ascii */
1005 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
1006 if (!codec) {
1007 /* Such error can only occurs in critical situations: no more
1008 * memory, import a module of the standard library failed,
1009 * etc. */
1010 return -1;
1011 }
1012 Py_DECREF(codec);
1013 interp->fscodec_initialized = 1;
1014 return 0;
1015}
1016
1017/* Import the site module (not into __main__ though) */
1018
1019static void
1020initsite(void)
1021{
1022 PyObject *m;
1023 m = PyImport_ImportModule("site");
1024 if (m == NULL) {
1025 fprintf(stderr, "Failed to import the site module\n");
1026 PyErr_Print();
1027 Py_Finalize();
1028 exit(1);
1029 }
1030 else {
1031 Py_DECREF(m);
1032 }
1033}
1034
Victor Stinner874dbe82015-09-04 17:29:57 +02001035/* Check if a file descriptor is valid or not.
1036 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
1037static int
1038is_valid_fd(int fd)
1039{
1040 int fd2;
Steve Dower940f33a2016-09-08 11:21:54 -07001041 if (fd < 0)
Victor Stinner874dbe82015-09-04 17:29:57 +02001042 return 0;
1043 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner449b2712015-09-29 13:59:50 +02001044 /* Prefer dup() over fstat(). fstat() can require input/output whereas
1045 dup() doesn't, there is a low risk of EMFILE/ENFILE at Python
1046 startup. */
Victor Stinner874dbe82015-09-04 17:29:57 +02001047 fd2 = dup(fd);
1048 if (fd2 >= 0)
1049 close(fd2);
1050 _Py_END_SUPPRESS_IPH
1051 return fd2 >= 0;
1052}
1053
1054/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10001055static PyObject*
1056create_stdio(PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001057 int fd, int write_mode, const char* name,
1058 const char* encoding, const char* errors)
Nick Coghland6009512014-11-20 21:39:37 +10001059{
1060 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
1061 const char* mode;
1062 const char* newline;
1063 PyObject *line_buffering;
1064 int buffering, isatty;
1065 _Py_IDENTIFIER(open);
1066 _Py_IDENTIFIER(isatty);
1067 _Py_IDENTIFIER(TextIOWrapper);
1068 _Py_IDENTIFIER(mode);
1069
Victor Stinner874dbe82015-09-04 17:29:57 +02001070 if (!is_valid_fd(fd))
1071 Py_RETURN_NONE;
1072
Nick Coghland6009512014-11-20 21:39:37 +10001073 /* stdin is always opened in buffered mode, first because it shouldn't
1074 make a difference in common use cases, second because TextIOWrapper
1075 depends on the presence of a read1() method which only exists on
1076 buffered streams.
1077 */
1078 if (Py_UnbufferedStdioFlag && write_mode)
1079 buffering = 0;
1080 else
1081 buffering = -1;
1082 if (write_mode)
1083 mode = "wb";
1084 else
1085 mode = "rb";
1086 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
1087 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001088 Py_None, Py_None, /* encoding, errors */
1089 Py_None, 0); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10001090 if (buf == NULL)
1091 goto error;
1092
1093 if (buffering) {
1094 _Py_IDENTIFIER(raw);
1095 raw = _PyObject_GetAttrId(buf, &PyId_raw);
1096 if (raw == NULL)
1097 goto error;
1098 }
1099 else {
1100 raw = buf;
1101 Py_INCREF(raw);
1102 }
1103
Steve Dower39294992016-08-30 21:22:36 -07001104#ifdef MS_WINDOWS
1105 /* Windows console IO is always UTF-8 encoded */
1106 if (PyWindowsConsoleIO_Check(raw))
1107 encoding = "utf-8";
1108#endif
1109
Nick Coghland6009512014-11-20 21:39:37 +10001110 text = PyUnicode_FromString(name);
1111 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
1112 goto error;
Victor Stinner3466bde2016-09-05 18:16:01 -07001113 res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001114 if (res == NULL)
1115 goto error;
1116 isatty = PyObject_IsTrue(res);
1117 Py_DECREF(res);
1118 if (isatty == -1)
1119 goto error;
1120 if (isatty || Py_UnbufferedStdioFlag)
1121 line_buffering = Py_True;
1122 else
1123 line_buffering = Py_False;
1124
1125 Py_CLEAR(raw);
1126 Py_CLEAR(text);
1127
1128#ifdef MS_WINDOWS
1129 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1130 newlines to "\n".
1131 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1132 newline = NULL;
1133#else
1134 /* sys.stdin: split lines at "\n".
1135 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1136 newline = "\n";
1137#endif
1138
1139 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
1140 buf, encoding, errors,
1141 newline, line_buffering);
1142 Py_CLEAR(buf);
1143 if (stream == NULL)
1144 goto error;
1145
1146 if (write_mode)
1147 mode = "w";
1148 else
1149 mode = "r";
1150 text = PyUnicode_FromString(mode);
1151 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
1152 goto error;
1153 Py_CLEAR(text);
1154 return stream;
1155
1156error:
1157 Py_XDECREF(buf);
1158 Py_XDECREF(stream);
1159 Py_XDECREF(text);
1160 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10001161
Victor Stinner874dbe82015-09-04 17:29:57 +02001162 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
1163 /* Issue #24891: the file descriptor was closed after the first
1164 is_valid_fd() check was called. Ignore the OSError and set the
1165 stream to None. */
1166 PyErr_Clear();
1167 Py_RETURN_NONE;
1168 }
1169 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001170}
1171
1172/* Initialize sys.stdin, stdout, stderr and builtins.open */
1173static int
1174initstdio(void)
1175{
1176 PyObject *iomod = NULL, *wrapper;
1177 PyObject *bimod = NULL;
1178 PyObject *m;
1179 PyObject *std = NULL;
1180 int status = 0, fd;
1181 PyObject * encoding_attr;
1182 char *pythonioencoding = NULL, *encoding, *errors;
1183
1184 /* Hack to avoid a nasty recursion issue when Python is invoked
1185 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1186 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1187 goto error;
1188 }
1189 Py_DECREF(m);
1190
1191 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1192 goto error;
1193 }
1194 Py_DECREF(m);
1195
1196 if (!(bimod = PyImport_ImportModule("builtins"))) {
1197 goto error;
1198 }
1199
1200 if (!(iomod = PyImport_ImportModule("io"))) {
1201 goto error;
1202 }
1203 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1204 goto error;
1205 }
1206
1207 /* Set builtins.open */
1208 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
1209 Py_DECREF(wrapper);
1210 goto error;
1211 }
1212 Py_DECREF(wrapper);
1213
1214 encoding = _Py_StandardStreamEncoding;
1215 errors = _Py_StandardStreamErrors;
1216 if (!encoding || !errors) {
Nick Coghland6009512014-11-20 21:39:37 +10001217 pythonioencoding = Py_GETENV("PYTHONIOENCODING");
1218 if (pythonioencoding) {
1219 char *err;
1220 pythonioencoding = _PyMem_Strdup(pythonioencoding);
1221 if (pythonioencoding == NULL) {
1222 PyErr_NoMemory();
1223 goto error;
1224 }
1225 err = strchr(pythonioencoding, ':');
1226 if (err) {
1227 *err = '\0';
1228 err++;
Serhiy Storchakafc435112016-04-10 14:34:13 +03001229 if (*err && !errors) {
Nick Coghland6009512014-11-20 21:39:37 +10001230 errors = err;
1231 }
1232 }
1233 if (*pythonioencoding && !encoding) {
1234 encoding = pythonioencoding;
1235 }
1236 }
Serhiy Storchakafc435112016-04-10 14:34:13 +03001237 if (!errors && !(pythonioencoding && *pythonioencoding)) {
1238 /* When the LC_CTYPE locale is the POSIX locale ("C locale"),
1239 stdin and stdout use the surrogateescape error handler by
1240 default, instead of the strict error handler. */
1241 char *loc = setlocale(LC_CTYPE, NULL);
1242 if (loc != NULL && strcmp(loc, "C") == 0)
1243 errors = "surrogateescape";
1244 }
Nick Coghland6009512014-11-20 21:39:37 +10001245 }
1246
1247 /* Set sys.stdin */
1248 fd = fileno(stdin);
1249 /* Under some conditions stdin, stdout and stderr may not be connected
1250 * and fileno() may point to an invalid file descriptor. For example
1251 * GUI apps don't have valid standard streams by default.
1252 */
Victor Stinner874dbe82015-09-04 17:29:57 +02001253 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1254 if (std == NULL)
1255 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001256 PySys_SetObject("__stdin__", std);
1257 _PySys_SetObjectId(&PyId_stdin, std);
1258 Py_DECREF(std);
1259
1260 /* Set sys.stdout */
1261 fd = fileno(stdout);
Victor Stinner874dbe82015-09-04 17:29:57 +02001262 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1263 if (std == NULL)
1264 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001265 PySys_SetObject("__stdout__", std);
1266 _PySys_SetObjectId(&PyId_stdout, std);
1267 Py_DECREF(std);
1268
1269#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
1270 /* Set sys.stderr, replaces the preliminary stderr */
1271 fd = fileno(stderr);
Victor Stinner874dbe82015-09-04 17:29:57 +02001272 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1273 if (std == NULL)
1274 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001275
1276 /* Same as hack above, pre-import stderr's codec to avoid recursion
1277 when import.c tries to write to stderr in verbose mode. */
1278 encoding_attr = PyObject_GetAttrString(std, "encoding");
1279 if (encoding_attr != NULL) {
1280 const char * std_encoding;
1281 std_encoding = _PyUnicode_AsString(encoding_attr);
1282 if (std_encoding != NULL) {
1283 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
1284 Py_XDECREF(codec_info);
1285 }
1286 Py_DECREF(encoding_attr);
1287 }
1288 PyErr_Clear(); /* Not a fatal error if codec isn't available */
1289
1290 if (PySys_SetObject("__stderr__", std) < 0) {
1291 Py_DECREF(std);
1292 goto error;
1293 }
1294 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
1295 Py_DECREF(std);
1296 goto error;
1297 }
1298 Py_DECREF(std);
1299#endif
1300
1301 if (0) {
1302 error:
1303 status = -1;
1304 }
1305
1306 /* We won't need them anymore. */
1307 if (_Py_StandardStreamEncoding) {
1308 PyMem_RawFree(_Py_StandardStreamEncoding);
1309 _Py_StandardStreamEncoding = NULL;
1310 }
1311 if (_Py_StandardStreamErrors) {
1312 PyMem_RawFree(_Py_StandardStreamErrors);
1313 _Py_StandardStreamErrors = NULL;
1314 }
1315 PyMem_Free(pythonioencoding);
1316 Py_XDECREF(bimod);
1317 Py_XDECREF(iomod);
1318 return status;
1319}
1320
1321
Victor Stinner10dc4842015-03-24 12:01:30 +01001322static void
Victor Stinner791da1c2016-03-14 16:53:12 +01001323_Py_FatalError_DumpTracebacks(int fd)
Victor Stinner10dc4842015-03-24 12:01:30 +01001324{
Victor Stinner10dc4842015-03-24 12:01:30 +01001325 fputc('\n', stderr);
1326 fflush(stderr);
1327
1328 /* display the current Python stack */
Victor Stinner861d9ab2016-03-16 22:45:24 +01001329 _Py_DumpTracebackThreads(fd, NULL, NULL);
Victor Stinner10dc4842015-03-24 12:01:30 +01001330}
Victor Stinner791da1c2016-03-14 16:53:12 +01001331
1332/* Print the current exception (if an exception is set) with its traceback,
1333 or display the current Python stack.
1334
1335 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
1336 called on catastrophic cases.
1337
1338 Return 1 if the traceback was displayed, 0 otherwise. */
1339
1340static int
1341_Py_FatalError_PrintExc(int fd)
1342{
1343 PyObject *ferr, *res;
1344 PyObject *exception, *v, *tb;
1345 int has_tb;
1346
1347 if (PyThreadState_GET() == NULL) {
1348 /* The GIL is released: trying to acquire it is likely to deadlock,
1349 just give up. */
1350 return 0;
1351 }
1352
1353 PyErr_Fetch(&exception, &v, &tb);
1354 if (exception == NULL) {
1355 /* No current exception */
1356 return 0;
1357 }
1358
1359 ferr = _PySys_GetObjectId(&PyId_stderr);
1360 if (ferr == NULL || ferr == Py_None) {
1361 /* sys.stderr is not set yet or set to None,
1362 no need to try to display the exception */
1363 return 0;
1364 }
1365
1366 PyErr_NormalizeException(&exception, &v, &tb);
1367 if (tb == NULL) {
1368 tb = Py_None;
1369 Py_INCREF(tb);
1370 }
1371 PyException_SetTraceback(v, tb);
1372 if (exception == NULL) {
1373 /* PyErr_NormalizeException() failed */
1374 return 0;
1375 }
1376
1377 has_tb = (tb != Py_None);
1378 PyErr_Display(exception, v, tb);
1379 Py_XDECREF(exception);
1380 Py_XDECREF(v);
1381 Py_XDECREF(tb);
1382
1383 /* sys.stderr may be buffered: call sys.stderr.flush() */
Victor Stinner3466bde2016-09-05 18:16:01 -07001384 res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Victor Stinner791da1c2016-03-14 16:53:12 +01001385 if (res == NULL)
1386 PyErr_Clear();
1387 else
1388 Py_DECREF(res);
1389
1390 return has_tb;
1391}
1392
Nick Coghland6009512014-11-20 21:39:37 +10001393/* Print fatal error message and abort */
1394
1395void
1396Py_FatalError(const char *msg)
1397{
1398 const int fd = fileno(stderr);
Victor Stinner53345a42015-03-25 01:55:14 +01001399 static int reentrant = 0;
1400#ifdef MS_WINDOWS
1401 size_t len;
1402 WCHAR* buffer;
1403 size_t i;
1404#endif
1405
1406 if (reentrant) {
1407 /* Py_FatalError() caused a second fatal error.
1408 Example: flush_std_files() raises a recursion error. */
1409 goto exit;
1410 }
1411 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001412
1413 fprintf(stderr, "Fatal Python error: %s\n", msg);
1414 fflush(stderr); /* it helps in Windows debug build */
Victor Stinner10dc4842015-03-24 12:01:30 +01001415
Victor Stinnere0deff32015-03-24 13:46:18 +01001416 /* Print the exception (if an exception is set) with its traceback,
1417 * or display the current Python stack. */
Victor Stinner791da1c2016-03-14 16:53:12 +01001418 if (!_Py_FatalError_PrintExc(fd))
1419 _Py_FatalError_DumpTracebacks(fd);
Victor Stinner10dc4842015-03-24 12:01:30 +01001420
Victor Stinner2025d782016-03-16 23:19:15 +01001421 /* The main purpose of faulthandler is to display the traceback. We already
1422 * did our best to display it. So faulthandler can now be disabled.
1423 * (Don't trigger it on abort().) */
1424 _PyFaulthandler_Fini();
1425
Victor Stinner791da1c2016-03-14 16:53:12 +01001426 /* Check if the current Python thread hold the GIL */
1427 if (PyThreadState_GET() != NULL) {
1428 /* Flush sys.stdout and sys.stderr */
1429 flush_std_files();
1430 }
Victor Stinnere0deff32015-03-24 13:46:18 +01001431
Nick Coghland6009512014-11-20 21:39:37 +10001432#ifdef MS_WINDOWS
Victor Stinner53345a42015-03-25 01:55:14 +01001433 len = strlen(msg);
Nick Coghland6009512014-11-20 21:39:37 +10001434
Victor Stinner53345a42015-03-25 01:55:14 +01001435 /* Convert the message to wchar_t. This uses a simple one-to-one
1436 conversion, assuming that the this error message actually uses ASCII
1437 only. If this ceases to be true, we will have to convert. */
1438 buffer = alloca( (len+1) * (sizeof *buffer));
1439 for( i=0; i<=len; ++i)
1440 buffer[i] = msg[i];
1441 OutputDebugStringW(L"Fatal Python error: ");
1442 OutputDebugStringW(buffer);
1443 OutputDebugStringW(L"\n");
1444#endif /* MS_WINDOWS */
1445
1446exit:
1447#if defined(MS_WINDOWS) && defined(_DEBUG)
Nick Coghland6009512014-11-20 21:39:37 +10001448 DebugBreak();
1449#endif
Nick Coghland6009512014-11-20 21:39:37 +10001450 abort();
1451}
1452
1453/* Clean up and exit */
1454
1455#ifdef WITH_THREAD
Victor Stinnerd7292b52016-06-17 12:29:00 +02001456# include "pythread.h"
Nick Coghland6009512014-11-20 21:39:37 +10001457#endif
1458
1459static void (*pyexitfunc)(void) = NULL;
1460/* For the atexit module. */
1461void _Py_PyAtExit(void (*func)(void))
1462{
1463 pyexitfunc = func;
1464}
1465
1466static void
1467call_py_exitfuncs(void)
1468{
1469 if (pyexitfunc == NULL)
1470 return;
1471
1472 (*pyexitfunc)();
1473 PyErr_Clear();
1474}
1475
1476/* Wait until threading._shutdown completes, provided
1477 the threading module was imported in the first place.
1478 The shutdown routine will wait until all non-daemon
1479 "threading" threads have completed. */
1480static void
1481wait_for_thread_shutdown(void)
1482{
1483#ifdef WITH_THREAD
1484 _Py_IDENTIFIER(_shutdown);
1485 PyObject *result;
1486 PyThreadState *tstate = PyThreadState_GET();
1487 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1488 "threading");
1489 if (threading == NULL) {
1490 /* threading not imported */
1491 PyErr_Clear();
1492 return;
1493 }
Victor Stinner3466bde2016-09-05 18:16:01 -07001494 result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001495 if (result == NULL) {
1496 PyErr_WriteUnraisable(threading);
1497 }
1498 else {
1499 Py_DECREF(result);
1500 }
1501 Py_DECREF(threading);
1502#endif
1503}
1504
1505#define NEXITFUNCS 32
1506static void (*exitfuncs[NEXITFUNCS])(void);
1507static int nexitfuncs = 0;
1508
1509int Py_AtExit(void (*func)(void))
1510{
1511 if (nexitfuncs >= NEXITFUNCS)
1512 return -1;
1513 exitfuncs[nexitfuncs++] = func;
1514 return 0;
1515}
1516
1517static void
1518call_ll_exitfuncs(void)
1519{
1520 while (nexitfuncs > 0)
1521 (*exitfuncs[--nexitfuncs])();
1522
1523 fflush(stdout);
1524 fflush(stderr);
1525}
1526
1527void
1528Py_Exit(int sts)
1529{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001530 if (Py_FinalizeEx() < 0) {
1531 sts = 120;
1532 }
Nick Coghland6009512014-11-20 21:39:37 +10001533
1534 exit(sts);
1535}
1536
1537static void
1538initsigs(void)
1539{
1540#ifdef SIGPIPE
1541 PyOS_setsig(SIGPIPE, SIG_IGN);
1542#endif
1543#ifdef SIGXFZ
1544 PyOS_setsig(SIGXFZ, SIG_IGN);
1545#endif
1546#ifdef SIGXFSZ
1547 PyOS_setsig(SIGXFSZ, SIG_IGN);
1548#endif
1549 PyOS_InitInterrupts(); /* May imply initsignal() */
1550 if (PyErr_Occurred()) {
1551 Py_FatalError("Py_Initialize: can't import signal");
1552 }
1553}
1554
1555
1556/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
1557 *
1558 * All of the code in this function must only use async-signal-safe functions,
1559 * listed at `man 7 signal` or
1560 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1561 */
1562void
1563_Py_RestoreSignals(void)
1564{
1565#ifdef SIGPIPE
1566 PyOS_setsig(SIGPIPE, SIG_DFL);
1567#endif
1568#ifdef SIGXFZ
1569 PyOS_setsig(SIGXFZ, SIG_DFL);
1570#endif
1571#ifdef SIGXFSZ
1572 PyOS_setsig(SIGXFSZ, SIG_DFL);
1573#endif
1574}
1575
1576
1577/*
1578 * The file descriptor fd is considered ``interactive'' if either
1579 * a) isatty(fd) is TRUE, or
1580 * b) the -i flag was given, and the filename associated with
1581 * the descriptor is NULL or "<stdin>" or "???".
1582 */
1583int
1584Py_FdIsInteractive(FILE *fp, const char *filename)
1585{
1586 if (isatty((int)fileno(fp)))
1587 return 1;
1588 if (!Py_InteractiveFlag)
1589 return 0;
1590 return (filename == NULL) ||
1591 (strcmp(filename, "<stdin>") == 0) ||
1592 (strcmp(filename, "???") == 0);
1593}
1594
1595
Nick Coghland6009512014-11-20 21:39:37 +10001596/* Wrappers around sigaction() or signal(). */
1597
1598PyOS_sighandler_t
1599PyOS_getsig(int sig)
1600{
1601#ifdef HAVE_SIGACTION
1602 struct sigaction context;
1603 if (sigaction(sig, NULL, &context) == -1)
1604 return SIG_ERR;
1605 return context.sa_handler;
1606#else
1607 PyOS_sighandler_t handler;
1608/* Special signal handling for the secure CRT in Visual Studio 2005 */
1609#if defined(_MSC_VER) && _MSC_VER >= 1400
1610 switch (sig) {
1611 /* Only these signals are valid */
1612 case SIGINT:
1613 case SIGILL:
1614 case SIGFPE:
1615 case SIGSEGV:
1616 case SIGTERM:
1617 case SIGBREAK:
1618 case SIGABRT:
1619 break;
1620 /* Don't call signal() with other values or it will assert */
1621 default:
1622 return SIG_ERR;
1623 }
1624#endif /* _MSC_VER && _MSC_VER >= 1400 */
1625 handler = signal(sig, SIG_IGN);
1626 if (handler != SIG_ERR)
1627 signal(sig, handler);
1628 return handler;
1629#endif
1630}
1631
1632/*
1633 * All of the code in this function must only use async-signal-safe functions,
1634 * listed at `man 7 signal` or
1635 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1636 */
1637PyOS_sighandler_t
1638PyOS_setsig(int sig, PyOS_sighandler_t handler)
1639{
1640#ifdef HAVE_SIGACTION
1641 /* Some code in Modules/signalmodule.c depends on sigaction() being
1642 * used here if HAVE_SIGACTION is defined. Fix that if this code
1643 * changes to invalidate that assumption.
1644 */
1645 struct sigaction context, ocontext;
1646 context.sa_handler = handler;
1647 sigemptyset(&context.sa_mask);
1648 context.sa_flags = 0;
1649 if (sigaction(sig, &context, &ocontext) == -1)
1650 return SIG_ERR;
1651 return ocontext.sa_handler;
1652#else
1653 PyOS_sighandler_t oldhandler;
1654 oldhandler = signal(sig, handler);
1655#ifdef HAVE_SIGINTERRUPT
1656 siginterrupt(sig, 1);
1657#endif
1658 return oldhandler;
1659#endif
1660}
1661
1662#ifdef __cplusplus
1663}
1664#endif