ยปCore Development>Code coverage>Python/pylifecycle.c

Python code coverage for Python/pylifecycle.c

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