blob: a2399ed576f4df42d0d3ba8c1e2c9c74fc8acacf [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 Stinner34be807c2016-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{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700940 PyObject *m, *d, *loader, *ann_dict;
Nick Coghland6009512014-11-20 21:39:37 +1000941 m = PyImport_AddModule("__main__");
942 if (m == NULL)
943 Py_FatalError("can't create __main__ module");
944 d = PyModule_GetDict(m);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700945 ann_dict = PyDict_New();
946 if ((ann_dict == NULL) ||
947 (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
948 Py_FatalError("Failed to initialize __main__.__annotations__");
949 }
950 Py_DECREF(ann_dict);
Nick Coghland6009512014-11-20 21:39:37 +1000951 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
952 PyObject *bimod = PyImport_ImportModule("builtins");
953 if (bimod == NULL) {
954 Py_FatalError("Failed to retrieve builtins module");
955 }
956 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
957 Py_FatalError("Failed to initialize __main__.__builtins__");
958 }
959 Py_DECREF(bimod);
960 }
961 /* Main is a little special - imp.is_builtin("__main__") will return
962 * False, but BuiltinImporter is still the most appropriate initial
963 * setting for its __loader__ attribute. A more suitable value will
964 * be set if __main__ gets further initialized later in the startup
965 * process.
966 */
967 loader = PyDict_GetItemString(d, "__loader__");
968 if (loader == NULL || loader == Py_None) {
969 PyObject *loader = PyObject_GetAttrString(interp->importlib,
970 "BuiltinImporter");
971 if (loader == NULL) {
972 Py_FatalError("Failed to retrieve BuiltinImporter");
973 }
974 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
975 Py_FatalError("Failed to initialize __main__.__loader__");
976 }
977 Py_DECREF(loader);
978 }
979}
980
981static int
982initfsencoding(PyInterpreterState *interp)
983{
984 PyObject *codec;
985
Steve Dowercc16be82016-09-08 10:35:16 -0700986#ifdef MS_WINDOWS
987 if (Py_LegacyWindowsFSEncodingFlag)
988 {
989 Py_FileSystemDefaultEncoding = "mbcs";
990 Py_FileSystemDefaultEncodeErrors = "replace";
991 }
992 else
993 {
994 Py_FileSystemDefaultEncoding = "utf-8";
995 Py_FileSystemDefaultEncodeErrors = "surrogatepass";
996 }
997#else
Nick Coghland6009512014-11-20 21:39:37 +1000998 if (Py_FileSystemDefaultEncoding == NULL)
999 {
1000 Py_FileSystemDefaultEncoding = get_locale_encoding();
1001 if (Py_FileSystemDefaultEncoding == NULL)
1002 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
1003
1004 Py_HasFileSystemDefaultEncoding = 0;
1005 interp->fscodec_initialized = 1;
1006 return 0;
1007 }
Steve Dowercc16be82016-09-08 10:35:16 -07001008#endif
Nick Coghland6009512014-11-20 21:39:37 +10001009
1010 /* the encoding is mbcs, utf-8 or ascii */
1011 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
1012 if (!codec) {
1013 /* Such error can only occurs in critical situations: no more
1014 * memory, import a module of the standard library failed,
1015 * etc. */
1016 return -1;
1017 }
1018 Py_DECREF(codec);
1019 interp->fscodec_initialized = 1;
1020 return 0;
1021}
1022
1023/* Import the site module (not into __main__ though) */
1024
1025static void
1026initsite(void)
1027{
1028 PyObject *m;
1029 m = PyImport_ImportModule("site");
1030 if (m == NULL) {
1031 fprintf(stderr, "Failed to import the site module\n");
1032 PyErr_Print();
1033 Py_Finalize();
1034 exit(1);
1035 }
1036 else {
1037 Py_DECREF(m);
1038 }
1039}
1040
Victor Stinner874dbe82015-09-04 17:29:57 +02001041/* Check if a file descriptor is valid or not.
1042 Return 0 if the file descriptor is invalid, return non-zero otherwise. */
1043static int
1044is_valid_fd(int fd)
1045{
1046 int fd2;
Steve Dower940f33a2016-09-08 11:21:54 -07001047 if (fd < 0)
Victor Stinner874dbe82015-09-04 17:29:57 +02001048 return 0;
1049 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner449b2712015-09-29 13:59:50 +02001050 /* Prefer dup() over fstat(). fstat() can require input/output whereas
1051 dup() doesn't, there is a low risk of EMFILE/ENFILE at Python
1052 startup. */
Victor Stinner874dbe82015-09-04 17:29:57 +02001053 fd2 = dup(fd);
1054 if (fd2 >= 0)
1055 close(fd2);
1056 _Py_END_SUPPRESS_IPH
1057 return fd2 >= 0;
1058}
1059
1060/* returns Py_None if the fd is not valid */
Nick Coghland6009512014-11-20 21:39:37 +10001061static PyObject*
1062create_stdio(PyObject* io,
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001063 int fd, int write_mode, const char* name,
1064 const char* encoding, const char* errors)
Nick Coghland6009512014-11-20 21:39:37 +10001065{
1066 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
1067 const char* mode;
1068 const char* newline;
1069 PyObject *line_buffering;
1070 int buffering, isatty;
1071 _Py_IDENTIFIER(open);
1072 _Py_IDENTIFIER(isatty);
1073 _Py_IDENTIFIER(TextIOWrapper);
1074 _Py_IDENTIFIER(mode);
1075
Victor Stinner874dbe82015-09-04 17:29:57 +02001076 if (!is_valid_fd(fd))
1077 Py_RETURN_NONE;
1078
Nick Coghland6009512014-11-20 21:39:37 +10001079 /* stdin is always opened in buffered mode, first because it shouldn't
1080 make a difference in common use cases, second because TextIOWrapper
1081 depends on the presence of a read1() method which only exists on
1082 buffered streams.
1083 */
1084 if (Py_UnbufferedStdioFlag && write_mode)
1085 buffering = 0;
1086 else
1087 buffering = -1;
1088 if (write_mode)
1089 mode = "wb";
1090 else
1091 mode = "rb";
1092 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
1093 fd, mode, buffering,
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001094 Py_None, Py_None, /* encoding, errors */
1095 Py_None, 0); /* newline, closefd */
Nick Coghland6009512014-11-20 21:39:37 +10001096 if (buf == NULL)
1097 goto error;
1098
1099 if (buffering) {
1100 _Py_IDENTIFIER(raw);
1101 raw = _PyObject_GetAttrId(buf, &PyId_raw);
1102 if (raw == NULL)
1103 goto error;
1104 }
1105 else {
1106 raw = buf;
1107 Py_INCREF(raw);
1108 }
1109
Steve Dower39294992016-08-30 21:22:36 -07001110#ifdef MS_WINDOWS
1111 /* Windows console IO is always UTF-8 encoded */
1112 if (PyWindowsConsoleIO_Check(raw))
1113 encoding = "utf-8";
1114#endif
1115
Nick Coghland6009512014-11-20 21:39:37 +10001116 text = PyUnicode_FromString(name);
1117 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
1118 goto error;
Victor Stinner3466bde2016-09-05 18:16:01 -07001119 res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001120 if (res == NULL)
1121 goto error;
1122 isatty = PyObject_IsTrue(res);
1123 Py_DECREF(res);
1124 if (isatty == -1)
1125 goto error;
1126 if (isatty || Py_UnbufferedStdioFlag)
1127 line_buffering = Py_True;
1128 else
1129 line_buffering = Py_False;
1130
1131 Py_CLEAR(raw);
1132 Py_CLEAR(text);
1133
1134#ifdef MS_WINDOWS
1135 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1136 newlines to "\n".
1137 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1138 newline = NULL;
1139#else
1140 /* sys.stdin: split lines at "\n".
1141 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1142 newline = "\n";
1143#endif
1144
1145 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
1146 buf, encoding, errors,
1147 newline, line_buffering);
1148 Py_CLEAR(buf);
1149 if (stream == NULL)
1150 goto error;
1151
1152 if (write_mode)
1153 mode = "w";
1154 else
1155 mode = "r";
1156 text = PyUnicode_FromString(mode);
1157 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
1158 goto error;
1159 Py_CLEAR(text);
1160 return stream;
1161
1162error:
1163 Py_XDECREF(buf);
1164 Py_XDECREF(stream);
1165 Py_XDECREF(text);
1166 Py_XDECREF(raw);
Nick Coghland6009512014-11-20 21:39:37 +10001167
Victor Stinner874dbe82015-09-04 17:29:57 +02001168 if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
1169 /* Issue #24891: the file descriptor was closed after the first
1170 is_valid_fd() check was called. Ignore the OSError and set the
1171 stream to None. */
1172 PyErr_Clear();
1173 Py_RETURN_NONE;
1174 }
1175 return NULL;
Nick Coghland6009512014-11-20 21:39:37 +10001176}
1177
1178/* Initialize sys.stdin, stdout, stderr and builtins.open */
1179static int
1180initstdio(void)
1181{
1182 PyObject *iomod = NULL, *wrapper;
1183 PyObject *bimod = NULL;
1184 PyObject *m;
1185 PyObject *std = NULL;
1186 int status = 0, fd;
1187 PyObject * encoding_attr;
1188 char *pythonioencoding = NULL, *encoding, *errors;
1189
1190 /* Hack to avoid a nasty recursion issue when Python is invoked
1191 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1192 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1193 goto error;
1194 }
1195 Py_DECREF(m);
1196
1197 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1198 goto error;
1199 }
1200 Py_DECREF(m);
1201
1202 if (!(bimod = PyImport_ImportModule("builtins"))) {
1203 goto error;
1204 }
1205
1206 if (!(iomod = PyImport_ImportModule("io"))) {
1207 goto error;
1208 }
1209 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1210 goto error;
1211 }
1212
1213 /* Set builtins.open */
1214 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
1215 Py_DECREF(wrapper);
1216 goto error;
1217 }
1218 Py_DECREF(wrapper);
1219
1220 encoding = _Py_StandardStreamEncoding;
1221 errors = _Py_StandardStreamErrors;
1222 if (!encoding || !errors) {
Nick Coghland6009512014-11-20 21:39:37 +10001223 pythonioencoding = Py_GETENV("PYTHONIOENCODING");
1224 if (pythonioencoding) {
1225 char *err;
1226 pythonioencoding = _PyMem_Strdup(pythonioencoding);
1227 if (pythonioencoding == NULL) {
1228 PyErr_NoMemory();
1229 goto error;
1230 }
1231 err = strchr(pythonioencoding, ':');
1232 if (err) {
1233 *err = '\0';
1234 err++;
Serhiy Storchakafc435112016-04-10 14:34:13 +03001235 if (*err && !errors) {
Nick Coghland6009512014-11-20 21:39:37 +10001236 errors = err;
1237 }
1238 }
1239 if (*pythonioencoding && !encoding) {
1240 encoding = pythonioencoding;
1241 }
1242 }
Serhiy Storchakafc435112016-04-10 14:34:13 +03001243 if (!errors && !(pythonioencoding && *pythonioencoding)) {
1244 /* When the LC_CTYPE locale is the POSIX locale ("C locale"),
1245 stdin and stdout use the surrogateescape error handler by
1246 default, instead of the strict error handler. */
1247 char *loc = setlocale(LC_CTYPE, NULL);
1248 if (loc != NULL && strcmp(loc, "C") == 0)
1249 errors = "surrogateescape";
1250 }
Nick Coghland6009512014-11-20 21:39:37 +10001251 }
1252
1253 /* Set sys.stdin */
1254 fd = fileno(stdin);
1255 /* Under some conditions stdin, stdout and stderr may not be connected
1256 * and fileno() may point to an invalid file descriptor. For example
1257 * GUI apps don't have valid standard streams by default.
1258 */
Victor Stinner874dbe82015-09-04 17:29:57 +02001259 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1260 if (std == NULL)
1261 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001262 PySys_SetObject("__stdin__", std);
1263 _PySys_SetObjectId(&PyId_stdin, std);
1264 Py_DECREF(std);
1265
1266 /* Set sys.stdout */
1267 fd = fileno(stdout);
Victor Stinner874dbe82015-09-04 17:29:57 +02001268 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1269 if (std == NULL)
1270 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001271 PySys_SetObject("__stdout__", std);
1272 _PySys_SetObjectId(&PyId_stdout, std);
1273 Py_DECREF(std);
1274
1275#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
1276 /* Set sys.stderr, replaces the preliminary stderr */
1277 fd = fileno(stderr);
Victor Stinner874dbe82015-09-04 17:29:57 +02001278 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1279 if (std == NULL)
1280 goto error;
Nick Coghland6009512014-11-20 21:39:37 +10001281
1282 /* Same as hack above, pre-import stderr's codec to avoid recursion
1283 when import.c tries to write to stderr in verbose mode. */
1284 encoding_attr = PyObject_GetAttrString(std, "encoding");
1285 if (encoding_attr != NULL) {
1286 const char * std_encoding;
1287 std_encoding = _PyUnicode_AsString(encoding_attr);
1288 if (std_encoding != NULL) {
1289 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
1290 Py_XDECREF(codec_info);
1291 }
1292 Py_DECREF(encoding_attr);
1293 }
1294 PyErr_Clear(); /* Not a fatal error if codec isn't available */
1295
1296 if (PySys_SetObject("__stderr__", std) < 0) {
1297 Py_DECREF(std);
1298 goto error;
1299 }
1300 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
1301 Py_DECREF(std);
1302 goto error;
1303 }
1304 Py_DECREF(std);
1305#endif
1306
1307 if (0) {
1308 error:
1309 status = -1;
1310 }
1311
1312 /* We won't need them anymore. */
1313 if (_Py_StandardStreamEncoding) {
1314 PyMem_RawFree(_Py_StandardStreamEncoding);
1315 _Py_StandardStreamEncoding = NULL;
1316 }
1317 if (_Py_StandardStreamErrors) {
1318 PyMem_RawFree(_Py_StandardStreamErrors);
1319 _Py_StandardStreamErrors = NULL;
1320 }
1321 PyMem_Free(pythonioencoding);
1322 Py_XDECREF(bimod);
1323 Py_XDECREF(iomod);
1324 return status;
1325}
1326
1327
Victor Stinner10dc4842015-03-24 12:01:30 +01001328static void
Victor Stinner791da1c2016-03-14 16:53:12 +01001329_Py_FatalError_DumpTracebacks(int fd)
Victor Stinner10dc4842015-03-24 12:01:30 +01001330{
Victor Stinner10dc4842015-03-24 12:01:30 +01001331 fputc('\n', stderr);
1332 fflush(stderr);
1333
1334 /* display the current Python stack */
Victor Stinner861d9ab2016-03-16 22:45:24 +01001335 _Py_DumpTracebackThreads(fd, NULL, NULL);
Victor Stinner10dc4842015-03-24 12:01:30 +01001336}
Victor Stinner791da1c2016-03-14 16:53:12 +01001337
1338/* Print the current exception (if an exception is set) with its traceback,
1339 or display the current Python stack.
1340
1341 Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
1342 called on catastrophic cases.
1343
1344 Return 1 if the traceback was displayed, 0 otherwise. */
1345
1346static int
1347_Py_FatalError_PrintExc(int fd)
1348{
1349 PyObject *ferr, *res;
1350 PyObject *exception, *v, *tb;
1351 int has_tb;
1352
1353 if (PyThreadState_GET() == NULL) {
1354 /* The GIL is released: trying to acquire it is likely to deadlock,
1355 just give up. */
1356 return 0;
1357 }
1358
1359 PyErr_Fetch(&exception, &v, &tb);
1360 if (exception == NULL) {
1361 /* No current exception */
1362 return 0;
1363 }
1364
1365 ferr = _PySys_GetObjectId(&PyId_stderr);
1366 if (ferr == NULL || ferr == Py_None) {
1367 /* sys.stderr is not set yet or set to None,
1368 no need to try to display the exception */
1369 return 0;
1370 }
1371
1372 PyErr_NormalizeException(&exception, &v, &tb);
1373 if (tb == NULL) {
1374 tb = Py_None;
1375 Py_INCREF(tb);
1376 }
1377 PyException_SetTraceback(v, tb);
1378 if (exception == NULL) {
1379 /* PyErr_NormalizeException() failed */
1380 return 0;
1381 }
1382
1383 has_tb = (tb != Py_None);
1384 PyErr_Display(exception, v, tb);
1385 Py_XDECREF(exception);
1386 Py_XDECREF(v);
1387 Py_XDECREF(tb);
1388
1389 /* sys.stderr may be buffered: call sys.stderr.flush() */
Victor Stinner3466bde2016-09-05 18:16:01 -07001390 res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Victor Stinner791da1c2016-03-14 16:53:12 +01001391 if (res == NULL)
1392 PyErr_Clear();
1393 else
1394 Py_DECREF(res);
1395
1396 return has_tb;
1397}
1398
Nick Coghland6009512014-11-20 21:39:37 +10001399/* Print fatal error message and abort */
1400
1401void
1402Py_FatalError(const char *msg)
1403{
1404 const int fd = fileno(stderr);
Victor Stinner53345a42015-03-25 01:55:14 +01001405 static int reentrant = 0;
1406#ifdef MS_WINDOWS
1407 size_t len;
1408 WCHAR* buffer;
1409 size_t i;
1410#endif
1411
1412 if (reentrant) {
1413 /* Py_FatalError() caused a second fatal error.
1414 Example: flush_std_files() raises a recursion error. */
1415 goto exit;
1416 }
1417 reentrant = 1;
Nick Coghland6009512014-11-20 21:39:37 +10001418
1419 fprintf(stderr, "Fatal Python error: %s\n", msg);
1420 fflush(stderr); /* it helps in Windows debug build */
Victor Stinner10dc4842015-03-24 12:01:30 +01001421
Victor Stinnere0deff32015-03-24 13:46:18 +01001422 /* Print the exception (if an exception is set) with its traceback,
1423 * or display the current Python stack. */
Victor Stinner791da1c2016-03-14 16:53:12 +01001424 if (!_Py_FatalError_PrintExc(fd))
1425 _Py_FatalError_DumpTracebacks(fd);
Victor Stinner10dc4842015-03-24 12:01:30 +01001426
Victor Stinner2025d782016-03-16 23:19:15 +01001427 /* The main purpose of faulthandler is to display the traceback. We already
1428 * did our best to display it. So faulthandler can now be disabled.
1429 * (Don't trigger it on abort().) */
1430 _PyFaulthandler_Fini();
1431
Victor Stinner791da1c2016-03-14 16:53:12 +01001432 /* Check if the current Python thread hold the GIL */
1433 if (PyThreadState_GET() != NULL) {
1434 /* Flush sys.stdout and sys.stderr */
1435 flush_std_files();
1436 }
Victor Stinnere0deff32015-03-24 13:46:18 +01001437
Nick Coghland6009512014-11-20 21:39:37 +10001438#ifdef MS_WINDOWS
Victor Stinner53345a42015-03-25 01:55:14 +01001439 len = strlen(msg);
Nick Coghland6009512014-11-20 21:39:37 +10001440
Victor Stinner53345a42015-03-25 01:55:14 +01001441 /* Convert the message to wchar_t. This uses a simple one-to-one
1442 conversion, assuming that the this error message actually uses ASCII
1443 only. If this ceases to be true, we will have to convert. */
1444 buffer = alloca( (len+1) * (sizeof *buffer));
1445 for( i=0; i<=len; ++i)
1446 buffer[i] = msg[i];
1447 OutputDebugStringW(L"Fatal Python error: ");
1448 OutputDebugStringW(buffer);
1449 OutputDebugStringW(L"\n");
1450#endif /* MS_WINDOWS */
1451
1452exit:
1453#if defined(MS_WINDOWS) && defined(_DEBUG)
Nick Coghland6009512014-11-20 21:39:37 +10001454 DebugBreak();
1455#endif
Nick Coghland6009512014-11-20 21:39:37 +10001456 abort();
1457}
1458
1459/* Clean up and exit */
1460
1461#ifdef WITH_THREAD
Victor Stinnerd7292b52016-06-17 12:29:00 +02001462# include "pythread.h"
Nick Coghland6009512014-11-20 21:39:37 +10001463#endif
1464
1465static void (*pyexitfunc)(void) = NULL;
1466/* For the atexit module. */
1467void _Py_PyAtExit(void (*func)(void))
1468{
1469 pyexitfunc = func;
1470}
1471
1472static void
1473call_py_exitfuncs(void)
1474{
1475 if (pyexitfunc == NULL)
1476 return;
1477
1478 (*pyexitfunc)();
1479 PyErr_Clear();
1480}
1481
1482/* Wait until threading._shutdown completes, provided
1483 the threading module was imported in the first place.
1484 The shutdown routine will wait until all non-daemon
1485 "threading" threads have completed. */
1486static void
1487wait_for_thread_shutdown(void)
1488{
1489#ifdef WITH_THREAD
1490 _Py_IDENTIFIER(_shutdown);
1491 PyObject *result;
1492 PyThreadState *tstate = PyThreadState_GET();
1493 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1494 "threading");
1495 if (threading == NULL) {
1496 /* threading not imported */
1497 PyErr_Clear();
1498 return;
1499 }
Victor Stinner3466bde2016-09-05 18:16:01 -07001500 result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
Nick Coghland6009512014-11-20 21:39:37 +10001501 if (result == NULL) {
1502 PyErr_WriteUnraisable(threading);
1503 }
1504 else {
1505 Py_DECREF(result);
1506 }
1507 Py_DECREF(threading);
1508#endif
1509}
1510
1511#define NEXITFUNCS 32
1512static void (*exitfuncs[NEXITFUNCS])(void);
1513static int nexitfuncs = 0;
1514
1515int Py_AtExit(void (*func)(void))
1516{
1517 if (nexitfuncs >= NEXITFUNCS)
1518 return -1;
1519 exitfuncs[nexitfuncs++] = func;
1520 return 0;
1521}
1522
1523static void
1524call_ll_exitfuncs(void)
1525{
1526 while (nexitfuncs > 0)
1527 (*exitfuncs[--nexitfuncs])();
1528
1529 fflush(stdout);
1530 fflush(stderr);
1531}
1532
1533void
1534Py_Exit(int sts)
1535{
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001536 if (Py_FinalizeEx() < 0) {
1537 sts = 120;
1538 }
Nick Coghland6009512014-11-20 21:39:37 +10001539
1540 exit(sts);
1541}
1542
1543static void
1544initsigs(void)
1545{
1546#ifdef SIGPIPE
1547 PyOS_setsig(SIGPIPE, SIG_IGN);
1548#endif
1549#ifdef SIGXFZ
1550 PyOS_setsig(SIGXFZ, SIG_IGN);
1551#endif
1552#ifdef SIGXFSZ
1553 PyOS_setsig(SIGXFSZ, SIG_IGN);
1554#endif
1555 PyOS_InitInterrupts(); /* May imply initsignal() */
1556 if (PyErr_Occurred()) {
1557 Py_FatalError("Py_Initialize: can't import signal");
1558 }
1559}
1560
1561
1562/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
1563 *
1564 * All of the code in this function must only use async-signal-safe functions,
1565 * listed at `man 7 signal` or
1566 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1567 */
1568void
1569_Py_RestoreSignals(void)
1570{
1571#ifdef SIGPIPE
1572 PyOS_setsig(SIGPIPE, SIG_DFL);
1573#endif
1574#ifdef SIGXFZ
1575 PyOS_setsig(SIGXFZ, SIG_DFL);
1576#endif
1577#ifdef SIGXFSZ
1578 PyOS_setsig(SIGXFSZ, SIG_DFL);
1579#endif
1580}
1581
1582
1583/*
1584 * The file descriptor fd is considered ``interactive'' if either
1585 * a) isatty(fd) is TRUE, or
1586 * b) the -i flag was given, and the filename associated with
1587 * the descriptor is NULL or "<stdin>" or "???".
1588 */
1589int
1590Py_FdIsInteractive(FILE *fp, const char *filename)
1591{
1592 if (isatty((int)fileno(fp)))
1593 return 1;
1594 if (!Py_InteractiveFlag)
1595 return 0;
1596 return (filename == NULL) ||
1597 (strcmp(filename, "<stdin>") == 0) ||
1598 (strcmp(filename, "???") == 0);
1599}
1600
1601
Nick Coghland6009512014-11-20 21:39:37 +10001602/* Wrappers around sigaction() or signal(). */
1603
1604PyOS_sighandler_t
1605PyOS_getsig(int sig)
1606{
1607#ifdef HAVE_SIGACTION
1608 struct sigaction context;
1609 if (sigaction(sig, NULL, &context) == -1)
1610 return SIG_ERR;
1611 return context.sa_handler;
1612#else
1613 PyOS_sighandler_t handler;
1614/* Special signal handling for the secure CRT in Visual Studio 2005 */
1615#if defined(_MSC_VER) && _MSC_VER >= 1400
1616 switch (sig) {
1617 /* Only these signals are valid */
1618 case SIGINT:
1619 case SIGILL:
1620 case SIGFPE:
1621 case SIGSEGV:
1622 case SIGTERM:
1623 case SIGBREAK:
1624 case SIGABRT:
1625 break;
1626 /* Don't call signal() with other values or it will assert */
1627 default:
1628 return SIG_ERR;
1629 }
1630#endif /* _MSC_VER && _MSC_VER >= 1400 */
1631 handler = signal(sig, SIG_IGN);
1632 if (handler != SIG_ERR)
1633 signal(sig, handler);
1634 return handler;
1635#endif
1636}
1637
1638/*
1639 * All of the code in this function must only use async-signal-safe functions,
1640 * listed at `man 7 signal` or
1641 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1642 */
1643PyOS_sighandler_t
1644PyOS_setsig(int sig, PyOS_sighandler_t handler)
1645{
1646#ifdef HAVE_SIGACTION
1647 /* Some code in Modules/signalmodule.c depends on sigaction() being
1648 * used here if HAVE_SIGACTION is defined. Fix that if this code
1649 * changes to invalidate that assumption.
1650 */
1651 struct sigaction context, ocontext;
1652 context.sa_handler = handler;
1653 sigemptyset(&context.sa_mask);
1654 context.sa_flags = 0;
1655 if (sigaction(sig, &context, &ocontext) == -1)
1656 return SIG_ERR;
1657 return ocontext.sa_handler;
1658#else
1659 PyOS_sighandler_t oldhandler;
1660 oldhandler = signal(sig, handler);
1661#ifdef HAVE_SIGINTERRUPT
1662 siginterrupt(sig, 1);
1663#endif
1664 return oldhandler;
1665#endif
1666}
1667
1668#ifdef __cplusplus
1669}
1670#endif