• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Python interpreter top-level routines, including init/exit */
2 
3 #include "Python.h"
4 
5 #include "pycore_ceval.h"         // _PyEval_FiniGIL()
6 #include "pycore_context.h"       // _PyContext_Init()
7 #include "pycore_fileutils.h"     // _Py_ResetForceASCII()
8 #include "pycore_import.h"        // _PyImport_BootstrapImp()
9 #include "pycore_initconfig.h"    // _PyStatus_OK()
10 #include "pycore_object.h"        // _PyDebug_PrintTotalRefs()
11 #include "pycore_pathconfig.h"    // _PyConfig_WritePathConfig()
12 #include "pycore_pyerrors.h"      // _PyErr_Occurred()
13 #include "pycore_pylifecycle.h"   // _PyErr_Print()
14 #include "pycore_pystate.h"       // _PyThreadState_GET()
15 #include "pycore_sysmodule.h"     // _PySys_ClearAuditHooks()
16 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads()
17 
18 #include <locale.h>               // setlocale()
19 
20 #if defined(__APPLE__)
21 #include <mach-o/loader.h>
22 #endif
23 
24 #ifdef HAVE_SIGNAL_H
25 #  include <signal.h>             // SIG_IGN
26 #endif
27 
28 #ifdef HAVE_LANGINFO_H
29 #  include <langinfo.h>           // nl_langinfo(CODESET)
30 #endif
31 
32 #ifdef MS_WINDOWS
33 #  undef BYTE
34 #  include "windows.h"
35 
36    extern PyTypeObject PyWindowsConsoleIO_Type;
37 #  define PyWindowsConsoleIO_Check(op) \
38        (PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
39 #endif
40 
41 #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
42 
43 
44 _Py_IDENTIFIER(flush);
45 _Py_IDENTIFIER(name);
46 _Py_IDENTIFIER(stdin);
47 _Py_IDENTIFIER(stdout);
48 _Py_IDENTIFIER(stderr);
49 _Py_IDENTIFIER(threading);
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 
56 /* Forward declarations */
57 static PyStatus add_main_module(PyInterpreterState *interp);
58 static PyStatus init_import_site(void);
59 static PyStatus init_set_builtins_open(void);
60 static PyStatus init_sys_streams(PyThreadState *tstate);
61 static void wait_for_thread_shutdown(PyThreadState *tstate);
62 static void call_ll_exitfuncs(_PyRuntimeState *runtime);
63 
64 int _Py_UnhandledKeyboardInterrupt = 0;
65 
66 /* The following places the `_PyRuntime` structure in a location that can be
67  * found without any external information. This is meant to ease access to the
68  * interpreter state for various runtime debugging tools, but is *not* an
69  * officially supported feature */
70 
71 #if defined(MS_WINDOWS)
72 
73 #pragma section("PyRuntime", read, write)
74 __declspec(allocate("PyRuntime"))
75 
76 #elif defined(__APPLE__)
77 
78 __attribute__((
79     section(SEG_DATA ",PyRuntime")
80 ))
81 
82 #endif
83 
84 _PyRuntimeState _PyRuntime
85 #if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
86 __attribute__ ((section (".PyRuntime")))
87 #endif
88 = _PyRuntimeState_INIT;
89 static int runtime_initialized = 0;
90 
91 PyStatus
_PyRuntime_Initialize(void)92 _PyRuntime_Initialize(void)
93 {
94     /* XXX We only initialize once in the process, which aligns with
95        the static initialization of the former globals now found in
96        _PyRuntime.  However, _PyRuntime *should* be initialized with
97        every Py_Initialize() call, but doing so breaks the runtime.
98        This is because the runtime state is not properly finalized
99        currently. */
100     if (runtime_initialized) {
101         return _PyStatus_OK();
102     }
103     runtime_initialized = 1;
104 
105     return _PyRuntimeState_Init(&_PyRuntime);
106 }
107 
108 void
_PyRuntime_Finalize(void)109 _PyRuntime_Finalize(void)
110 {
111     _PyRuntimeState_Fini(&_PyRuntime);
112     runtime_initialized = 0;
113 }
114 
115 int
_Py_IsFinalizing(void)116 _Py_IsFinalizing(void)
117 {
118     return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
119 }
120 
121 /* Hack to force loading of object files */
122 int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
123     PyOS_mystrnicmp; /* Python/pystrcmp.o */
124 
125 
126 /* APIs to access the initialization flags
127  *
128  * Can be called prior to Py_Initialize.
129  */
130 
131 int
_Py_IsCoreInitialized(void)132 _Py_IsCoreInitialized(void)
133 {
134     return _PyRuntime.core_initialized;
135 }
136 
137 int
Py_IsInitialized(void)138 Py_IsInitialized(void)
139 {
140     return _PyRuntime.initialized;
141 }
142 
143 
144 /* Global initializations.  Can be undone by Py_FinalizeEx().  Don't
145    call this twice without an intervening Py_FinalizeEx() call.  When
146    initializations fail, a fatal error is issued and the function does
147    not return.  On return, the first thread and interpreter state have
148    been created.
149 
150    Locking: you must hold the interpreter lock while calling this.
151    (If the lock has not yet been initialized, that's equivalent to
152    having the lock, but you cannot use multiple threads.)
153 
154 */
155 static int
init_importlib(PyThreadState * tstate,PyObject * sysmod)156 init_importlib(PyThreadState *tstate, PyObject *sysmod)
157 {
158     assert(!_PyErr_Occurred(tstate));
159 
160     PyInterpreterState *interp = tstate->interp;
161     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
162 
163     // Import _importlib through its frozen version, _frozen_importlib.
164     if (verbose) {
165         PySys_FormatStderr("import _frozen_importlib # frozen\n");
166     }
167     if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
168         return -1;
169     }
170     PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed
171     if (importlib == NULL) {
172         return -1;
173     }
174     interp->importlib = Py_NewRef(importlib);
175 
176     // Import the _imp module
177     if (verbose) {
178         PySys_FormatStderr("import _imp # builtin\n");
179     }
180     PyObject *imp_mod = _PyImport_BootstrapImp(tstate);
181     if (imp_mod == NULL) {
182         return -1;
183     }
184     if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
185         Py_DECREF(imp_mod);
186         return -1;
187     }
188 
189     // Install importlib as the implementation of import
190     PyObject *value = PyObject_CallMethod(importlib, "_install",
191                                           "OO", sysmod, imp_mod);
192     Py_DECREF(imp_mod);
193     if (value == NULL) {
194         return -1;
195     }
196     Py_DECREF(value);
197 
198     assert(!_PyErr_Occurred(tstate));
199     return 0;
200 }
201 
202 
203 static PyStatus
init_importlib_external(PyThreadState * tstate)204 init_importlib_external(PyThreadState *tstate)
205 {
206     PyObject *value;
207     value = PyObject_CallMethod(tstate->interp->importlib,
208                                 "_install_external_importers", "");
209     if (value == NULL) {
210         _PyErr_Print(tstate);
211         return _PyStatus_ERR("external importer setup failed");
212     }
213     Py_DECREF(value);
214     return _PyImportZip_Init(tstate);
215 }
216 
217 /* Helper functions to better handle the legacy C locale
218  *
219  * The legacy C locale assumes ASCII as the default text encoding, which
220  * causes problems not only for the CPython runtime, but also other
221  * components like GNU readline.
222  *
223  * Accordingly, when the CLI detects it, it attempts to coerce it to a
224  * more capable UTF-8 based alternative as follows:
225  *
226  *     if (_Py_LegacyLocaleDetected()) {
227  *         _Py_CoerceLegacyLocale();
228  *     }
229  *
230  * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
231  *
232  * Locale coercion also impacts the default error handler for the standard
233  * streams: while the usual default is "strict", the default for the legacy
234  * C locale and for any of the coercion target locales is "surrogateescape".
235  */
236 
237 int
_Py_LegacyLocaleDetected(int warn)238 _Py_LegacyLocaleDetected(int warn)
239 {
240 #ifndef MS_WINDOWS
241     if (!warn) {
242         const char *locale_override = getenv("LC_ALL");
243         if (locale_override != NULL && *locale_override != '\0') {
244             /* Don't coerce C locale if the LC_ALL environment variable
245                is set */
246             return 0;
247         }
248     }
249 
250     /* On non-Windows systems, the C locale is considered a legacy locale */
251     /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
252      *                 the POSIX locale as a simple alias for the C locale, so
253      *                 we may also want to check for that explicitly.
254      */
255     const char *ctype_loc = setlocale(LC_CTYPE, NULL);
256     return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
257 #else
258     /* Windows uses code pages instead of locales, so no locale is legacy */
259     return 0;
260 #endif
261 }
262 
263 #ifndef MS_WINDOWS
264 static const char *_C_LOCALE_WARNING =
265     "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
266     "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
267     "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
268     "locales is recommended.\n";
269 
270 static void
emit_stderr_warning_for_legacy_locale(_PyRuntimeState * runtime)271 emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
272 {
273     const PyPreConfig *preconfig = &runtime->preconfig;
274     if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
275         PySys_FormatStderr("%s", _C_LOCALE_WARNING);
276     }
277 }
278 #endif   /* !defined(MS_WINDOWS) */
279 
280 typedef struct _CandidateLocale {
281     const char *locale_name; /* The locale to try as a coercion target */
282 } _LocaleCoercionTarget;
283 
284 static _LocaleCoercionTarget _TARGET_LOCALES[] = {
285     {"C.UTF-8"},
286     {"C.utf8"},
287     {"UTF-8"},
288     {NULL}
289 };
290 
291 
292 int
_Py_IsLocaleCoercionTarget(const char * ctype_loc)293 _Py_IsLocaleCoercionTarget(const char *ctype_loc)
294 {
295     const _LocaleCoercionTarget *target = NULL;
296     for (target = _TARGET_LOCALES; target->locale_name; target++) {
297         if (strcmp(ctype_loc, target->locale_name) == 0) {
298             return 1;
299         }
300     }
301     return 0;
302 }
303 
304 
305 #ifdef PY_COERCE_C_LOCALE
306 static const char C_LOCALE_COERCION_WARNING[] =
307     "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
308     "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
309 
310 static int
_coerce_default_locale_settings(int warn,const _LocaleCoercionTarget * target)311 _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
312 {
313     const char *newloc = target->locale_name;
314 
315     /* Reset locale back to currently configured defaults */
316     _Py_SetLocaleFromEnv(LC_ALL);
317 
318     /* Set the relevant locale environment variable */
319     if (setenv("LC_CTYPE", newloc, 1)) {
320         fprintf(stderr,
321                 "Error setting LC_CTYPE, skipping C locale coercion\n");
322         return 0;
323     }
324     if (warn) {
325         fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
326     }
327 
328     /* Reconfigure with the overridden environment variables */
329     _Py_SetLocaleFromEnv(LC_ALL);
330     return 1;
331 }
332 #endif
333 
334 int
_Py_CoerceLegacyLocale(int warn)335 _Py_CoerceLegacyLocale(int warn)
336 {
337     int coerced = 0;
338 #ifdef PY_COERCE_C_LOCALE
339     char *oldloc = NULL;
340 
341     oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
342     if (oldloc == NULL) {
343         return coerced;
344     }
345 
346     const char *locale_override = getenv("LC_ALL");
347     if (locale_override == NULL || *locale_override == '\0') {
348         /* LC_ALL is also not set (or is set to an empty string) */
349         const _LocaleCoercionTarget *target = NULL;
350         for (target = _TARGET_LOCALES; target->locale_name; target++) {
351             const char *new_locale = setlocale(LC_CTYPE,
352                                                target->locale_name);
353             if (new_locale != NULL) {
354 #if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
355                 /* Also ensure that nl_langinfo works in this locale */
356                 char *codeset = nl_langinfo(CODESET);
357                 if (!codeset || *codeset == '\0') {
358                     /* CODESET is not set or empty, so skip coercion */
359                     new_locale = NULL;
360                     _Py_SetLocaleFromEnv(LC_CTYPE);
361                     continue;
362                 }
363 #endif
364                 /* Successfully configured locale, so make it the default */
365                 coerced = _coerce_default_locale_settings(warn, target);
366                 goto done;
367             }
368         }
369     }
370     /* No C locale warning here, as Py_Initialize will emit one later */
371 
372     setlocale(LC_CTYPE, oldloc);
373 
374 done:
375     PyMem_RawFree(oldloc);
376 #endif
377     return coerced;
378 }
379 
380 /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
381  * isolate the idiosyncrasies of different libc implementations. It reads the
382  * appropriate environment variable and uses its value to select the locale for
383  * 'category'. */
384 char *
_Py_SetLocaleFromEnv(int category)385 _Py_SetLocaleFromEnv(int category)
386 {
387     char *res;
388 #ifdef __ANDROID__
389     const char *locale;
390     const char **pvar;
391 #ifdef PY_COERCE_C_LOCALE
392     const char *coerce_c_locale;
393 #endif
394     const char *utf8_locale = "C.UTF-8";
395     const char *env_var_set[] = {
396         "LC_ALL",
397         "LC_CTYPE",
398         "LANG",
399         NULL,
400     };
401 
402     /* Android setlocale(category, "") doesn't check the environment variables
403      * and incorrectly sets the "C" locale at API 24 and older APIs. We only
404      * check the environment variables listed in env_var_set. */
405     for (pvar=env_var_set; *pvar; pvar++) {
406         locale = getenv(*pvar);
407         if (locale != NULL && *locale != '\0') {
408             if (strcmp(locale, utf8_locale) == 0 ||
409                     strcmp(locale, "en_US.UTF-8") == 0) {
410                 return setlocale(category, utf8_locale);
411             }
412             return setlocale(category, "C");
413         }
414     }
415 
416     /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
417      * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
418      * Quote from POSIX section "8.2 Internationalization Variables":
419      * "4. If the LANG environment variable is not set or is set to the empty
420      * string, the implementation-defined default locale shall be used." */
421 
422 #ifdef PY_COERCE_C_LOCALE
423     coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
424     if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
425         /* Some other ported code may check the environment variables (e.g. in
426          * extension modules), so we make sure that they match the locale
427          * configuration */
428         if (setenv("LC_CTYPE", utf8_locale, 1)) {
429             fprintf(stderr, "Warning: failed setting the LC_CTYPE "
430                             "environment variable to %s\n", utf8_locale);
431         }
432     }
433 #endif
434     res = setlocale(category, utf8_locale);
435 #else /* !defined(__ANDROID__) */
436     res = setlocale(category, "");
437 #endif
438     _Py_ResetForceASCII();
439     return res;
440 }
441 
442 
443 static int
interpreter_update_config(PyThreadState * tstate,int only_update_path_config)444 interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
445 {
446     const PyConfig *config = &tstate->interp->config;
447 
448     if (!only_update_path_config) {
449         PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
450         if (_PyStatus_EXCEPTION(status)) {
451             _PyErr_SetFromPyStatus(status);
452             return -1;
453         }
454     }
455 
456     if (_Py_IsMainInterpreter(tstate->interp)) {
457         PyStatus status = _PyConfig_WritePathConfig(config);
458         if (_PyStatus_EXCEPTION(status)) {
459             _PyErr_SetFromPyStatus(status);
460             return -1;
461         }
462     }
463 
464     // Update the sys module for the new configuration
465     if (_PySys_UpdateConfig(tstate) < 0) {
466         return -1;
467     }
468     return 0;
469 }
470 
471 
472 int
_PyInterpreterState_SetConfig(const PyConfig * src_config)473 _PyInterpreterState_SetConfig(const PyConfig *src_config)
474 {
475     PyThreadState *tstate = PyThreadState_Get();
476     int res = -1;
477 
478     PyConfig config;
479     PyConfig_InitPythonConfig(&config);
480     PyStatus status = _PyConfig_Copy(&config, src_config);
481     if (_PyStatus_EXCEPTION(status)) {
482         _PyErr_SetFromPyStatus(status);
483         goto done;
484     }
485 
486     status = PyConfig_Read(&config);
487     if (_PyStatus_EXCEPTION(status)) {
488         _PyErr_SetFromPyStatus(status);
489         goto done;
490     }
491 
492     status = _PyConfig_Copy(&tstate->interp->config, &config);
493     if (_PyStatus_EXCEPTION(status)) {
494         _PyErr_SetFromPyStatus(status);
495         goto done;
496     }
497 
498     res = interpreter_update_config(tstate, 0);
499 
500 done:
501     PyConfig_Clear(&config);
502     return res;
503 }
504 
505 
506 /* Global initializations.  Can be undone by Py_Finalize().  Don't
507    call this twice without an intervening Py_Finalize() call.
508 
509    Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
510    must have a corresponding call to Py_Finalize.
511 
512    Locking: you must hold the interpreter lock while calling these APIs.
513    (If the lock has not yet been initialized, that's equivalent to
514    having the lock, but you cannot use multiple threads.)
515 
516 */
517 
518 static PyStatus
pyinit_core_reconfigure(_PyRuntimeState * runtime,PyThreadState ** tstate_p,const PyConfig * config)519 pyinit_core_reconfigure(_PyRuntimeState *runtime,
520                         PyThreadState **tstate_p,
521                         const PyConfig *config)
522 {
523     PyStatus status;
524     PyThreadState *tstate = _PyThreadState_GET();
525     if (!tstate) {
526         return _PyStatus_ERR("failed to read thread state");
527     }
528     *tstate_p = tstate;
529 
530     PyInterpreterState *interp = tstate->interp;
531     if (interp == NULL) {
532         return _PyStatus_ERR("can't make main interpreter");
533     }
534 
535     status = _PyConfig_Write(config, runtime);
536     if (_PyStatus_EXCEPTION(status)) {
537         return status;
538     }
539 
540     status = _PyConfig_Copy(&interp->config, config);
541     if (_PyStatus_EXCEPTION(status)) {
542         return status;
543     }
544     config = _PyInterpreterState_GetConfig(interp);
545 
546     if (config->_install_importlib) {
547         status = _PyConfig_WritePathConfig(config);
548         if (_PyStatus_EXCEPTION(status)) {
549             return status;
550         }
551     }
552     return _PyStatus_OK();
553 }
554 
555 
556 static PyStatus
pycore_init_runtime(_PyRuntimeState * runtime,const PyConfig * config)557 pycore_init_runtime(_PyRuntimeState *runtime,
558                     const PyConfig *config)
559 {
560     if (runtime->initialized) {
561         return _PyStatus_ERR("main interpreter already initialized");
562     }
563 
564     PyStatus status = _PyConfig_Write(config, runtime);
565     if (_PyStatus_EXCEPTION(status)) {
566         return status;
567     }
568 
569     /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
570      * threads behave a little more gracefully at interpreter shutdown.
571      * We clobber it here so the new interpreter can start with a clean
572      * slate.
573      *
574      * However, this may still lead to misbehaviour if there are daemon
575      * threads still hanging around from a previous Py_Initialize/Finalize
576      * pair :(
577      */
578     _PyRuntimeState_SetFinalizing(runtime, NULL);
579 
580     status = _Py_HashRandomization_Init(config);
581     if (_PyStatus_EXCEPTION(status)) {
582         return status;
583     }
584 
585     status = _PyInterpreterState_Enable(runtime);
586     if (_PyStatus_EXCEPTION(status)) {
587         return status;
588     }
589     return _PyStatus_OK();
590 }
591 
592 
593 static PyStatus
init_interp_create_gil(PyThreadState * tstate)594 init_interp_create_gil(PyThreadState *tstate)
595 {
596     PyStatus status;
597 
598     /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
599        only called here. */
600     _PyEval_FiniGIL(tstate->interp);
601 
602     /* Auto-thread-state API */
603     status = _PyGILState_SetTstate(tstate);
604     if (_PyStatus_EXCEPTION(status)) {
605         return status;
606     }
607 
608     /* Create the GIL and take it */
609     status = _PyEval_InitGIL(tstate);
610     if (_PyStatus_EXCEPTION(status)) {
611         return status;
612     }
613 
614     return _PyStatus_OK();
615 }
616 
617 
618 static PyStatus
pycore_create_interpreter(_PyRuntimeState * runtime,const PyConfig * config,PyThreadState ** tstate_p)619 pycore_create_interpreter(_PyRuntimeState *runtime,
620                           const PyConfig *config,
621                           PyThreadState **tstate_p)
622 {
623     /* Auto-thread-state API */
624     PyStatus status = _PyGILState_Init(runtime);
625     if (_PyStatus_EXCEPTION(status)) {
626         return status;
627     }
628 
629     PyInterpreterState *interp = PyInterpreterState_New();
630     if (interp == NULL) {
631         return _PyStatus_ERR("can't make main interpreter");
632     }
633     assert(_Py_IsMainInterpreter(interp));
634 
635     status = _PyConfig_Copy(&interp->config, config);
636     if (_PyStatus_EXCEPTION(status)) {
637         return status;
638     }
639 
640     PyThreadState *tstate = PyThreadState_New(interp);
641     if (tstate == NULL) {
642         return _PyStatus_ERR("can't make first thread");
643     }
644     (void) PyThreadState_Swap(tstate);
645 
646     status = init_interp_create_gil(tstate);
647     if (_PyStatus_EXCEPTION(status)) {
648         return status;
649     }
650 
651     *tstate_p = tstate;
652     return _PyStatus_OK();
653 }
654 
655 
656 static PyStatus
pycore_init_singletons(PyInterpreterState * interp)657 pycore_init_singletons(PyInterpreterState *interp)
658 {
659     PyStatus status;
660 
661     if (_PyLong_Init(interp) < 0) {
662         return _PyStatus_ERR("can't init longs");
663     }
664 
665     if (_Py_IsMainInterpreter(interp)) {
666         _PyFloat_Init();
667     }
668 
669     status = _PyBytes_Init(interp);
670     if (_PyStatus_EXCEPTION(status)) {
671         return status;
672     }
673 
674     status = _PyUnicode_Init(interp);
675     if (_PyStatus_EXCEPTION(status)) {
676         return status;
677     }
678 
679     status = _PyTuple_Init(interp);
680     if (_PyStatus_EXCEPTION(status)) {
681         return status;
682     }
683 
684     return _PyStatus_OK();
685 }
686 
687 
688 static PyStatus
pycore_init_types(PyInterpreterState * interp)689 pycore_init_types(PyInterpreterState *interp)
690 {
691     PyStatus status;
692     int is_main_interp = _Py_IsMainInterpreter(interp);
693 
694     if (is_main_interp) {
695         if (_PyStructSequence_Init() < 0) {
696             return _PyStatus_ERR("can't initialize structseq");
697         }
698 
699         status = _PyTypes_Init();
700         if (_PyStatus_EXCEPTION(status)) {
701             return status;
702         }
703 
704         if (_PyLong_InitTypes() < 0) {
705             return _PyStatus_ERR("can't init int type");
706         }
707 
708         status = _PyUnicode_InitTypes();
709         if (_PyStatus_EXCEPTION(status)) {
710             return status;
711         }
712     }
713 
714     if (is_main_interp) {
715         if (_PyFloat_InitTypes() < 0) {
716             return _PyStatus_ERR("can't init float");
717         }
718     }
719 
720     status = _PyExc_Init(interp);
721     if (_PyStatus_EXCEPTION(status)) {
722         return status;
723     }
724 
725     status = _PyErr_InitTypes();
726     if (_PyStatus_EXCEPTION(status)) {
727         return status;
728     }
729 
730     if (is_main_interp) {
731         if (!_PyContext_Init()) {
732             return _PyStatus_ERR("can't init context");
733         }
734     }
735 
736     return _PyStatus_OK();
737 }
738 
739 
740 static PyStatus
pycore_init_builtins(PyThreadState * tstate)741 pycore_init_builtins(PyThreadState *tstate)
742 {
743     PyInterpreterState *interp = tstate->interp;
744 
745     PyObject *bimod = _PyBuiltin_Init(interp);
746     if (bimod == NULL) {
747         goto error;
748     }
749 
750     if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
751         goto error;
752     }
753 
754     PyObject *builtins_dict = PyModule_GetDict(bimod);
755     if (builtins_dict == NULL) {
756         goto error;
757     }
758     Py_INCREF(builtins_dict);
759     interp->builtins = builtins_dict;
760 
761     PyStatus status = _PyBuiltins_AddExceptions(bimod);
762     if (_PyStatus_EXCEPTION(status)) {
763         return status;
764     }
765 
766     interp->builtins_copy = PyDict_Copy(interp->builtins);
767     if (interp->builtins_copy == NULL) {
768         goto error;
769     }
770     Py_DECREF(bimod);
771 
772     // Get the __import__ function
773     PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
774                                                            "__import__");
775     if (import_func == NULL) {
776         goto error;
777     }
778     interp->import_func = Py_NewRef(import_func);
779 
780     assert(!_PyErr_Occurred(tstate));
781     return _PyStatus_OK();
782 
783 error:
784     Py_XDECREF(bimod);
785     return _PyStatus_ERR("can't initialize builtins module");
786 }
787 
788 
789 static PyStatus
pycore_interp_init(PyThreadState * tstate)790 pycore_interp_init(PyThreadState *tstate)
791 {
792     PyInterpreterState *interp = tstate->interp;
793     PyStatus status;
794     PyObject *sysmod = NULL;
795 
796     // Create singletons before the first PyType_Ready() call, since
797     // PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
798     // and the empty tuple singletons (tp_bases).
799     status = pycore_init_singletons(interp);
800     if (_PyStatus_EXCEPTION(status)) {
801         return status;
802     }
803 
804     // The GC must be initialized before the first GC collection.
805     status = _PyGC_Init(interp);
806     if (_PyStatus_EXCEPTION(status)) {
807         return status;
808     }
809 
810     status = pycore_init_types(interp);
811     if (_PyStatus_EXCEPTION(status)) {
812         goto done;
813     }
814 
815     if (_PyWarnings_InitState(interp) < 0) {
816         return _PyStatus_ERR("can't initialize warnings");
817     }
818 
819     status = _PyAtExit_Init(interp);
820     if (_PyStatus_EXCEPTION(status)) {
821         return status;
822     }
823 
824     status = _PySys_Create(tstate, &sysmod);
825     if (_PyStatus_EXCEPTION(status)) {
826         goto done;
827     }
828 
829     status = pycore_init_builtins(tstate);
830     if (_PyStatus_EXCEPTION(status)) {
831         goto done;
832     }
833 
834     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
835     if (config->_install_importlib) {
836         /* This call sets up builtin and frozen import support */
837         if (init_importlib(tstate, sysmod) < 0) {
838             return _PyStatus_ERR("failed to initialize importlib");
839         }
840     }
841 
842 done:
843     /* sys.modules['sys'] contains a strong reference to the module */
844     Py_XDECREF(sysmod);
845     return status;
846 }
847 
848 
849 static PyStatus
pyinit_config(_PyRuntimeState * runtime,PyThreadState ** tstate_p,const PyConfig * config)850 pyinit_config(_PyRuntimeState *runtime,
851               PyThreadState **tstate_p,
852               const PyConfig *config)
853 {
854     PyStatus status = pycore_init_runtime(runtime, config);
855     if (_PyStatus_EXCEPTION(status)) {
856         return status;
857     }
858 
859     PyThreadState *tstate;
860     status = pycore_create_interpreter(runtime, config, &tstate);
861     if (_PyStatus_EXCEPTION(status)) {
862         return status;
863     }
864     *tstate_p = tstate;
865 
866     status = pycore_interp_init(tstate);
867     if (_PyStatus_EXCEPTION(status)) {
868         return status;
869     }
870 
871     /* Only when we get here is the runtime core fully initialized */
872     runtime->core_initialized = 1;
873     return _PyStatus_OK();
874 }
875 
876 
877 PyStatus
_Py_PreInitializeFromPyArgv(const PyPreConfig * src_config,const _PyArgv * args)878 _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
879 {
880     PyStatus status;
881 
882     if (src_config == NULL) {
883         return _PyStatus_ERR("preinitialization config is NULL");
884     }
885 
886     status = _PyRuntime_Initialize();
887     if (_PyStatus_EXCEPTION(status)) {
888         return status;
889     }
890     _PyRuntimeState *runtime = &_PyRuntime;
891 
892     if (runtime->preinitialized) {
893         /* If it's already configured: ignored the new configuration */
894         return _PyStatus_OK();
895     }
896 
897     /* Note: preinitialized remains 1 on error, it is only set to 0
898        at exit on success. */
899     runtime->preinitializing = 1;
900 
901     PyPreConfig config;
902 
903     status = _PyPreConfig_InitFromPreConfig(&config, src_config);
904     if (_PyStatus_EXCEPTION(status)) {
905         return status;
906     }
907 
908     status = _PyPreConfig_Read(&config, args);
909     if (_PyStatus_EXCEPTION(status)) {
910         return status;
911     }
912 
913     status = _PyPreConfig_Write(&config);
914     if (_PyStatus_EXCEPTION(status)) {
915         return status;
916     }
917 
918     runtime->preinitializing = 0;
919     runtime->preinitialized = 1;
920     return _PyStatus_OK();
921 }
922 
923 
924 PyStatus
Py_PreInitializeFromBytesArgs(const PyPreConfig * src_config,Py_ssize_t argc,char ** argv)925 Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
926 {
927     _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
928     return _Py_PreInitializeFromPyArgv(src_config, &args);
929 }
930 
931 
932 PyStatus
Py_PreInitializeFromArgs(const PyPreConfig * src_config,Py_ssize_t argc,wchar_t ** argv)933 Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
934 {
935     _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
936     return _Py_PreInitializeFromPyArgv(src_config, &args);
937 }
938 
939 
940 PyStatus
Py_PreInitialize(const PyPreConfig * src_config)941 Py_PreInitialize(const PyPreConfig *src_config)
942 {
943     return _Py_PreInitializeFromPyArgv(src_config, NULL);
944 }
945 
946 
947 PyStatus
_Py_PreInitializeFromConfig(const PyConfig * config,const _PyArgv * args)948 _Py_PreInitializeFromConfig(const PyConfig *config,
949                             const _PyArgv *args)
950 {
951     assert(config != NULL);
952 
953     PyStatus status = _PyRuntime_Initialize();
954     if (_PyStatus_EXCEPTION(status)) {
955         return status;
956     }
957     _PyRuntimeState *runtime = &_PyRuntime;
958 
959     if (runtime->preinitialized) {
960         /* Already initialized: do nothing */
961         return _PyStatus_OK();
962     }
963 
964     PyPreConfig preconfig;
965 
966     _PyPreConfig_InitFromConfig(&preconfig, config);
967 
968     if (!config->parse_argv) {
969         return Py_PreInitialize(&preconfig);
970     }
971     else if (args == NULL) {
972         _PyArgv config_args = {
973             .use_bytes_argv = 0,
974             .argc = config->argv.length,
975             .wchar_argv = config->argv.items};
976         return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
977     }
978     else {
979         return _Py_PreInitializeFromPyArgv(&preconfig, args);
980     }
981 }
982 
983 
984 /* Begin interpreter initialization
985  *
986  * On return, the first thread and interpreter state have been created,
987  * but the compiler, signal handling, multithreading and
988  * multiple interpreter support, and codec infrastructure are not yet
989  * available.
990  *
991  * The import system will support builtin and frozen modules only.
992  * The only supported io is writing to sys.stderr
993  *
994  * If any operation invoked by this function fails, a fatal error is
995  * issued and the function does not return.
996  *
997  * Any code invoked from this function should *not* assume it has access
998  * to the Python C API (unless the API is explicitly listed as being
999  * safe to call without calling Py_Initialize first)
1000  */
1001 static PyStatus
pyinit_core(_PyRuntimeState * runtime,const PyConfig * src_config,PyThreadState ** tstate_p)1002 pyinit_core(_PyRuntimeState *runtime,
1003             const PyConfig *src_config,
1004             PyThreadState **tstate_p)
1005 {
1006     PyStatus status;
1007 
1008     status = _Py_PreInitializeFromConfig(src_config, NULL);
1009     if (_PyStatus_EXCEPTION(status)) {
1010         return status;
1011     }
1012 
1013     PyConfig config;
1014     PyConfig_InitPythonConfig(&config);
1015 
1016     status = _PyConfig_Copy(&config, src_config);
1017     if (_PyStatus_EXCEPTION(status)) {
1018         goto done;
1019     }
1020 
1021     // Read the configuration, but don't compute the path configuration
1022     // (it is computed in the main init).
1023     status = _PyConfig_Read(&config, 0);
1024     if (_PyStatus_EXCEPTION(status)) {
1025         goto done;
1026     }
1027 
1028     if (!runtime->core_initialized) {
1029         status = pyinit_config(runtime, tstate_p, &config);
1030     }
1031     else {
1032         status = pyinit_core_reconfigure(runtime, tstate_p, &config);
1033     }
1034     if (_PyStatus_EXCEPTION(status)) {
1035         goto done;
1036     }
1037 
1038 done:
1039     PyConfig_Clear(&config);
1040     return status;
1041 }
1042 
1043 
1044 /* Py_Initialize() has already been called: update the main interpreter
1045    configuration. Example of bpo-34008: Py_Main() called after
1046    Py_Initialize(). */
1047 static PyStatus
pyinit_main_reconfigure(PyThreadState * tstate)1048 pyinit_main_reconfigure(PyThreadState *tstate)
1049 {
1050     if (interpreter_update_config(tstate, 0) < 0) {
1051         return _PyStatus_ERR("fail to reconfigure Python");
1052     }
1053     return _PyStatus_OK();
1054 }
1055 
1056 
1057 static PyStatus
init_interp_main(PyThreadState * tstate)1058 init_interp_main(PyThreadState *tstate)
1059 {
1060     extern void _PyThread_debug_deprecation(void);
1061 
1062     assert(!_PyErr_Occurred(tstate));
1063 
1064     PyStatus status;
1065     int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
1066     PyInterpreterState *interp = tstate->interp;
1067     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
1068 
1069     if (!config->_install_importlib) {
1070         /* Special mode for freeze_importlib: run with no import system
1071          *
1072          * This means anything which needs support from extension modules
1073          * or pure Python code in the standard library won't work.
1074          */
1075         if (is_main_interp) {
1076             interp->runtime->initialized = 1;
1077         }
1078         return _PyStatus_OK();
1079     }
1080 
1081     // Compute the path configuration
1082     status = _PyConfig_InitPathConfig(&interp->config, 1);
1083     if (_PyStatus_EXCEPTION(status)) {
1084         return status;
1085     }
1086 
1087     if (interpreter_update_config(tstate, 1) < 0) {
1088         return _PyStatus_ERR("failed to update the Python config");
1089     }
1090 
1091     status = init_importlib_external(tstate);
1092     if (_PyStatus_EXCEPTION(status)) {
1093         return status;
1094     }
1095 
1096     if (is_main_interp) {
1097         /* initialize the faulthandler module */
1098         status = _PyFaulthandler_Init(config->faulthandler);
1099         if (_PyStatus_EXCEPTION(status)) {
1100             return status;
1101         }
1102     }
1103 
1104     status = _PyUnicode_InitEncodings(tstate);
1105     if (_PyStatus_EXCEPTION(status)) {
1106         return status;
1107     }
1108 
1109     if (is_main_interp) {
1110         if (_PySignal_Init(config->install_signal_handlers) < 0) {
1111             return _PyStatus_ERR("can't initialize signals");
1112         }
1113 
1114         if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
1115             return _PyStatus_ERR("can't initialize tracemalloc");
1116         }
1117     }
1118 
1119     status = init_sys_streams(tstate);
1120     if (_PyStatus_EXCEPTION(status)) {
1121         return status;
1122     }
1123 
1124     status = init_set_builtins_open();
1125     if (_PyStatus_EXCEPTION(status)) {
1126         return status;
1127     }
1128 
1129     status = add_main_module(interp);
1130     if (_PyStatus_EXCEPTION(status)) {
1131         return status;
1132     }
1133 
1134     if (is_main_interp) {
1135         /* Initialize warnings. */
1136         PyObject *warnoptions = PySys_GetObject("warnoptions");
1137         if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1138         {
1139             PyObject *warnings_module = PyImport_ImportModule("warnings");
1140             if (warnings_module == NULL) {
1141                 fprintf(stderr, "'import warnings' failed; traceback:\n");
1142                 _PyErr_Print(tstate);
1143             }
1144             Py_XDECREF(warnings_module);
1145         }
1146 
1147         interp->runtime->initialized = 1;
1148     }
1149 
1150     if (config->site_import) {
1151         status = init_import_site();
1152         if (_PyStatus_EXCEPTION(status)) {
1153             return status;
1154         }
1155     }
1156 
1157     if (is_main_interp) {
1158 #ifndef MS_WINDOWS
1159         emit_stderr_warning_for_legacy_locale(interp->runtime);
1160 #endif
1161     }
1162 
1163     // Warn about PYTHONTHREADDEBUG deprecation
1164     _PyThread_debug_deprecation();
1165 
1166     assert(!_PyErr_Occurred(tstate));
1167 
1168     return _PyStatus_OK();
1169 }
1170 
1171 
1172 /* Update interpreter state based on supplied configuration settings
1173  *
1174  * After calling this function, most of the restrictions on the interpreter
1175  * are lifted. The only remaining incomplete settings are those related
1176  * to the main module (sys.argv[0], __main__ metadata)
1177  *
1178  * Calling this when the interpreter is not initializing, is already
1179  * initialized or without a valid current thread state is a fatal error.
1180  * Other errors should be reported as normal Python exceptions with a
1181  * non-zero return code.
1182  */
1183 static PyStatus
pyinit_main(PyThreadState * tstate)1184 pyinit_main(PyThreadState *tstate)
1185 {
1186     PyInterpreterState *interp = tstate->interp;
1187     if (!interp->runtime->core_initialized) {
1188         return _PyStatus_ERR("runtime core not initialized");
1189     }
1190 
1191     if (interp->runtime->initialized) {
1192         return pyinit_main_reconfigure(tstate);
1193     }
1194 
1195     PyStatus status = init_interp_main(tstate);
1196     if (_PyStatus_EXCEPTION(status)) {
1197         return status;
1198     }
1199     return _PyStatus_OK();
1200 }
1201 
1202 
1203 PyStatus
Py_InitializeFromConfig(const PyConfig * config)1204 Py_InitializeFromConfig(const PyConfig *config)
1205 {
1206     if (config == NULL) {
1207         return _PyStatus_ERR("initialization config is NULL");
1208     }
1209 
1210     PyStatus status;
1211 
1212     status = _PyRuntime_Initialize();
1213     if (_PyStatus_EXCEPTION(status)) {
1214         return status;
1215     }
1216     _PyRuntimeState *runtime = &_PyRuntime;
1217 
1218     PyThreadState *tstate = NULL;
1219     status = pyinit_core(runtime, config, &tstate);
1220     if (_PyStatus_EXCEPTION(status)) {
1221         return status;
1222     }
1223     config = _PyInterpreterState_GetConfig(tstate->interp);
1224 
1225     if (config->_init_main) {
1226         status = pyinit_main(tstate);
1227         if (_PyStatus_EXCEPTION(status)) {
1228             return status;
1229         }
1230     }
1231 
1232     return _PyStatus_OK();
1233 }
1234 
1235 
1236 void
Py_InitializeEx(int install_sigs)1237 Py_InitializeEx(int install_sigs)
1238 {
1239     PyStatus status;
1240 
1241     status = _PyRuntime_Initialize();
1242     if (_PyStatus_EXCEPTION(status)) {
1243         Py_ExitStatusException(status);
1244     }
1245     _PyRuntimeState *runtime = &_PyRuntime;
1246 
1247     if (runtime->initialized) {
1248         /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1249         return;
1250     }
1251 
1252     PyConfig config;
1253     _PyConfig_InitCompatConfig(&config);
1254 
1255     config.install_signal_handlers = install_sigs;
1256 
1257     status = Py_InitializeFromConfig(&config);
1258     if (_PyStatus_EXCEPTION(status)) {
1259         Py_ExitStatusException(status);
1260     }
1261 }
1262 
1263 void
Py_Initialize(void)1264 Py_Initialize(void)
1265 {
1266     Py_InitializeEx(1);
1267 }
1268 
1269 
1270 PyStatus
_Py_InitializeMain(void)1271 _Py_InitializeMain(void)
1272 {
1273     PyStatus status = _PyRuntime_Initialize();
1274     if (_PyStatus_EXCEPTION(status)) {
1275         return status;
1276     }
1277     _PyRuntimeState *runtime = &_PyRuntime;
1278     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1279     return pyinit_main(tstate);
1280 }
1281 
1282 
1283 static void
finalize_modules_delete_special(PyThreadState * tstate,int verbose)1284 finalize_modules_delete_special(PyThreadState *tstate, int verbose)
1285 {
1286     // List of names to clear in sys
1287     static const char * const sys_deletes[] = {
1288         "path", "argv", "ps1", "ps2",
1289         "last_type", "last_value", "last_traceback",
1290         "path_hooks", "path_importer_cache", "meta_path",
1291         "__interactivehook__",
1292         NULL
1293     };
1294 
1295     static const char * const sys_files[] = {
1296         "stdin", "__stdin__",
1297         "stdout", "__stdout__",
1298         "stderr", "__stderr__",
1299         NULL
1300     };
1301 
1302     PyInterpreterState *interp = tstate->interp;
1303     if (verbose) {
1304         PySys_WriteStderr("# clear builtins._\n");
1305     }
1306     if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
1307         PyErr_WriteUnraisable(NULL);
1308     }
1309 
1310     const char * const *p;
1311     for (p = sys_deletes; *p != NULL; p++) {
1312         if (verbose) {
1313             PySys_WriteStderr("# clear sys.%s\n", *p);
1314         }
1315         if (PyDict_SetItemString(interp->sysdict, *p, Py_None) < 0) {
1316             PyErr_WriteUnraisable(NULL);
1317         }
1318     }
1319     for (p = sys_files; *p != NULL; p+=2) {
1320         const char *name = p[0];
1321         const char *orig_name = p[1];
1322         if (verbose) {
1323             PySys_WriteStderr("# restore sys.%s\n", name);
1324         }
1325         PyObject *value = _PyDict_GetItemStringWithError(interp->sysdict,
1326                                                          orig_name);
1327         if (value == NULL) {
1328             if (_PyErr_Occurred(tstate)) {
1329                 PyErr_WriteUnraisable(NULL);
1330             }
1331             value = Py_None;
1332         }
1333         if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
1334             PyErr_WriteUnraisable(NULL);
1335         }
1336     }
1337 }
1338 
1339 
1340 static PyObject*
finalize_remove_modules(PyObject * modules,int verbose)1341 finalize_remove_modules(PyObject *modules, int verbose)
1342 {
1343     PyObject *weaklist = PyList_New(0);
1344     if (weaklist == NULL) {
1345         PyErr_WriteUnraisable(NULL);
1346     }
1347 
1348 #define STORE_MODULE_WEAKREF(name, mod) \
1349         if (weaklist != NULL) { \
1350             PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1351             if (wr) { \
1352                 PyObject *tup = PyTuple_Pack(2, name, wr); \
1353                 if (!tup || PyList_Append(weaklist, tup) < 0) { \
1354                     PyErr_WriteUnraisable(NULL); \
1355                 } \
1356                 Py_XDECREF(tup); \
1357                 Py_DECREF(wr); \
1358             } \
1359             else { \
1360                 PyErr_WriteUnraisable(NULL); \
1361             } \
1362         }
1363 
1364 #define CLEAR_MODULE(name, mod) \
1365         if (PyModule_Check(mod)) { \
1366             if (verbose && PyUnicode_Check(name)) { \
1367                 PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
1368             } \
1369             STORE_MODULE_WEAKREF(name, mod); \
1370             if (PyObject_SetItem(modules, name, Py_None) < 0) { \
1371                 PyErr_WriteUnraisable(NULL); \
1372             } \
1373         }
1374 
1375     if (PyDict_CheckExact(modules)) {
1376         Py_ssize_t pos = 0;
1377         PyObject *key, *value;
1378         while (PyDict_Next(modules, &pos, &key, &value)) {
1379             CLEAR_MODULE(key, value);
1380         }
1381     }
1382     else {
1383         PyObject *iterator = PyObject_GetIter(modules);
1384         if (iterator == NULL) {
1385             PyErr_WriteUnraisable(NULL);
1386         }
1387         else {
1388             PyObject *key;
1389             while ((key = PyIter_Next(iterator))) {
1390                 PyObject *value = PyObject_GetItem(modules, key);
1391                 if (value == NULL) {
1392                     PyErr_WriteUnraisable(NULL);
1393                     continue;
1394                 }
1395                 CLEAR_MODULE(key, value);
1396                 Py_DECREF(value);
1397                 Py_DECREF(key);
1398             }
1399             if (PyErr_Occurred()) {
1400                 PyErr_WriteUnraisable(NULL);
1401             }
1402             Py_DECREF(iterator);
1403         }
1404     }
1405 #undef CLEAR_MODULE
1406 #undef STORE_MODULE_WEAKREF
1407 
1408     return weaklist;
1409 }
1410 
1411 
1412 static void
finalize_clear_modules_dict(PyObject * modules)1413 finalize_clear_modules_dict(PyObject *modules)
1414 {
1415     if (PyDict_CheckExact(modules)) {
1416         PyDict_Clear(modules);
1417     }
1418     else {
1419         _Py_IDENTIFIER(clear);
1420         if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) {
1421             PyErr_WriteUnraisable(NULL);
1422         }
1423     }
1424 }
1425 
1426 
1427 static void
finalize_restore_builtins(PyThreadState * tstate)1428 finalize_restore_builtins(PyThreadState *tstate)
1429 {
1430     PyInterpreterState *interp = tstate->interp;
1431     PyObject *dict = PyDict_Copy(interp->builtins);
1432     if (dict == NULL) {
1433         PyErr_WriteUnraisable(NULL);
1434     }
1435     PyDict_Clear(interp->builtins);
1436     if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
1437         _PyErr_Clear(tstate);
1438     }
1439     Py_XDECREF(dict);
1440 }
1441 
1442 
1443 static void
finalize_modules_clear_weaklist(PyInterpreterState * interp,PyObject * weaklist,int verbose)1444 finalize_modules_clear_weaklist(PyInterpreterState *interp,
1445                                 PyObject *weaklist, int verbose)
1446 {
1447     // First clear modules imported later
1448     for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
1449         PyObject *tup = PyList_GET_ITEM(weaklist, i);
1450         PyObject *name = PyTuple_GET_ITEM(tup, 0);
1451         PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
1452         if (mod == Py_None) {
1453             continue;
1454         }
1455         assert(PyModule_Check(mod));
1456         PyObject *dict = PyModule_GetDict(mod);
1457         if (dict == interp->builtins || dict == interp->sysdict) {
1458             continue;
1459         }
1460         Py_INCREF(mod);
1461         if (verbose && PyUnicode_Check(name)) {
1462             PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
1463         }
1464         _PyModule_Clear(mod);
1465         Py_DECREF(mod);
1466     }
1467 }
1468 
1469 
1470 static void
finalize_clear_sys_builtins_dict(PyInterpreterState * interp,int verbose)1471 finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
1472 {
1473     // Clear sys dict
1474     if (verbose) {
1475         PySys_FormatStderr("# cleanup[3] wiping sys\n");
1476     }
1477     _PyModule_ClearDict(interp->sysdict);
1478 
1479     // Clear builtins dict
1480     if (verbose) {
1481         PySys_FormatStderr("# cleanup[3] wiping builtins\n");
1482     }
1483     _PyModule_ClearDict(interp->builtins);
1484 }
1485 
1486 
1487 /* Clear modules, as good as we can */
1488 static void
finalize_modules(PyThreadState * tstate)1489 finalize_modules(PyThreadState *tstate)
1490 {
1491     PyInterpreterState *interp = tstate->interp;
1492     PyObject *modules = interp->modules;
1493     if (modules == NULL) {
1494         // Already done
1495         return;
1496     }
1497     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
1498 
1499     // Delete some special builtins._ and sys attributes first.  These are
1500     // common places where user values hide and people complain when their
1501     // destructors fail.  Since the modules containing them are
1502     // deleted *last* of all, they would come too late in the normal
1503     // destruction order.  Sigh.
1504     //
1505     // XXX Perhaps these precautions are obsolete. Who knows?
1506     finalize_modules_delete_special(tstate, verbose);
1507 
1508     // Remove all modules from sys.modules, hoping that garbage collection
1509     // can reclaim most of them: set all sys.modules values to None.
1510     //
1511     // We prepare a list which will receive (name, weakref) tuples of
1512     // modules when they are removed from sys.modules.  The name is used
1513     // for diagnosis messages (in verbose mode), while the weakref helps
1514     // detect those modules which have been held alive.
1515     PyObject *weaklist = finalize_remove_modules(modules, verbose);
1516 
1517     // Clear the modules dict
1518     finalize_clear_modules_dict(modules);
1519 
1520     // Restore the original builtins dict, to ensure that any
1521     // user data gets cleared.
1522     finalize_restore_builtins(tstate);
1523 
1524     // Collect garbage
1525     _PyGC_CollectNoFail(tstate);
1526 
1527     // Dump GC stats before it's too late, since it uses the warnings
1528     // machinery.
1529     _PyGC_DumpShutdownStats(interp);
1530 
1531     if (weaklist != NULL) {
1532         // Now, if there are any modules left alive, clear their globals to
1533         // minimize potential leaks.  All C extension modules actually end
1534         // up here, since they are kept alive in the interpreter state.
1535         //
1536         // The special treatment of "builtins" here is because even
1537         // when it's not referenced as a module, its dictionary is
1538         // referenced by almost every module's __builtins__.  Since
1539         // deleting a module clears its dictionary (even if there are
1540         // references left to it), we need to delete the "builtins"
1541         // module last.  Likewise, we don't delete sys until the very
1542         // end because it is implicitly referenced (e.g. by print).
1543         //
1544         // Since dict is ordered in CPython 3.6+, modules are saved in
1545         // importing order.  First clear modules imported later.
1546         finalize_modules_clear_weaklist(interp, weaklist, verbose);
1547         Py_DECREF(weaklist);
1548     }
1549 
1550     // Clear sys and builtins modules dict
1551     finalize_clear_sys_builtins_dict(interp, verbose);
1552 
1553     // Clear module dict copies stored in the interpreter state:
1554     // clear PyInterpreterState.modules_by_index and
1555     // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
1556     // initialization API)
1557     _PyInterpreterState_ClearModules(interp);
1558 
1559     // Clear and delete the modules directory.  Actual modules will
1560     // still be there only if imported during the execution of some
1561     // destructor.
1562     Py_SETREF(interp->modules, NULL);
1563 
1564     // Collect garbage once more
1565     _PyGC_CollectNoFail(tstate);
1566 }
1567 
1568 
1569 /* Flush stdout and stderr */
1570 
1571 static int
file_is_closed(PyObject * fobj)1572 file_is_closed(PyObject *fobj)
1573 {
1574     int r;
1575     PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1576     if (tmp == NULL) {
1577         PyErr_Clear();
1578         return 0;
1579     }
1580     r = PyObject_IsTrue(tmp);
1581     Py_DECREF(tmp);
1582     if (r < 0)
1583         PyErr_Clear();
1584     return r > 0;
1585 }
1586 
1587 
1588 static int
flush_std_files(void)1589 flush_std_files(void)
1590 {
1591     PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1592     PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
1593     PyObject *tmp;
1594     int status = 0;
1595 
1596     if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
1597         tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
1598         if (tmp == NULL) {
1599             PyErr_WriteUnraisable(fout);
1600             status = -1;
1601         }
1602         else
1603             Py_DECREF(tmp);
1604     }
1605 
1606     if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
1607         tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
1608         if (tmp == NULL) {
1609             PyErr_Clear();
1610             status = -1;
1611         }
1612         else
1613             Py_DECREF(tmp);
1614     }
1615 
1616     return status;
1617 }
1618 
1619 /* Undo the effect of Py_Initialize().
1620 
1621    Beware: if multiple interpreter and/or thread states exist, these
1622    are not wiped out; only the current thread and interpreter state
1623    are deleted.  But since everything else is deleted, those other
1624    interpreter and thread states should no longer be used.
1625 
1626    (XXX We should do better, e.g. wipe out all interpreters and
1627    threads.)
1628 
1629    Locking: as above.
1630 
1631 */
1632 
1633 
1634 static void
finalize_interp_types(PyInterpreterState * interp)1635 finalize_interp_types(PyInterpreterState *interp)
1636 {
1637     _PyExc_Fini(interp);
1638     _PyFrame_Fini(interp);
1639     _PyAsyncGen_Fini(interp);
1640     _PyContext_Fini(interp);
1641     _PyType_Fini(interp);
1642     // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
1643     // a dict internally.
1644     _PyUnicode_ClearInterned(interp);
1645 
1646     _PyDict_Fini(interp);
1647     _PyList_Fini(interp);
1648     _PyTuple_Fini(interp);
1649 
1650     _PySlice_Fini(interp);
1651 
1652     _PyBytes_Fini(interp);
1653     _PyUnicode_Fini(interp);
1654     _PyFloat_Fini(interp);
1655     _PyLong_Fini(interp);
1656 }
1657 
1658 
1659 static void
finalize_interp_clear(PyThreadState * tstate)1660 finalize_interp_clear(PyThreadState *tstate)
1661 {
1662     int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
1663 
1664     /* Clear interpreter state and all thread states */
1665     _PyInterpreterState_Clear(tstate);
1666 
1667     /* Clear all loghooks */
1668     /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1669        Call _PySys_ClearAuditHooks when PyObject available. */
1670     if (is_main_interp) {
1671         _PySys_ClearAuditHooks(tstate);
1672     }
1673 
1674     if (is_main_interp) {
1675         _Py_HashRandomization_Fini();
1676         _PyArg_Fini();
1677         _Py_ClearFileSystemEncoding();
1678     }
1679 
1680     finalize_interp_types(tstate->interp);
1681 }
1682 
1683 
1684 static void
finalize_interp_delete(PyInterpreterState * interp)1685 finalize_interp_delete(PyInterpreterState *interp)
1686 {
1687     if (_Py_IsMainInterpreter(interp)) {
1688         /* Cleanup auto-thread-state */
1689         _PyGILState_Fini(interp);
1690     }
1691 
1692     /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1693        fail when it is being awaited by another running daemon thread (see
1694        bpo-9901). Instead pycore_create_interpreter() destroys the previously
1695        created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1696        called multiple times. */
1697 
1698     PyInterpreterState_Delete(interp);
1699 }
1700 
1701 
1702 int
Py_FinalizeEx(void)1703 Py_FinalizeEx(void)
1704 {
1705     int status = 0;
1706 
1707     _PyRuntimeState *runtime = &_PyRuntime;
1708     if (!runtime->initialized) {
1709         return status;
1710     }
1711 
1712     /* Get current thread state and interpreter pointer */
1713     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1714 
1715     // Wrap up existing "threading"-module-created, non-daemon threads.
1716     wait_for_thread_shutdown(tstate);
1717 
1718     // Make any remaining pending calls.
1719     _Py_FinishPendingCalls(tstate);
1720 
1721     /* The interpreter is still entirely intact at this point, and the
1722      * exit funcs may be relying on that.  In particular, if some thread
1723      * or exit func is still waiting to do an import, the import machinery
1724      * expects Py_IsInitialized() to return true.  So don't say the
1725      * runtime is uninitialized until after the exit funcs have run.
1726      * Note that Threading.py uses an exit func to do a join on all the
1727      * threads created thru it, so this also protects pending imports in
1728      * the threads created via Threading.
1729      */
1730 
1731     _PyAtExit_Call(tstate->interp);
1732 
1733     /* Copy the core config, PyInterpreterState_Delete() free
1734        the core config memory */
1735 #ifdef Py_REF_DEBUG
1736     int show_ref_count = tstate->interp->config.show_ref_count;
1737 #endif
1738 #ifdef Py_TRACE_REFS
1739     int dump_refs = tstate->interp->config.dump_refs;
1740 #endif
1741 #ifdef WITH_PYMALLOC
1742     int malloc_stats = tstate->interp->config.malloc_stats;
1743 #endif
1744 
1745     /* Remaining daemon threads will automatically exit
1746        when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
1747     _PyRuntimeState_SetFinalizing(runtime, tstate);
1748     runtime->initialized = 0;
1749     runtime->core_initialized = 0;
1750 
1751     /* Destroy the state of all threads of the interpreter, except of the
1752        current thread. In practice, only daemon threads should still be alive,
1753        except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
1754        Clear frames of other threads to call objects destructors. Destructors
1755        will be called in the current Python thread. Since
1756        _PyRuntimeState_SetFinalizing() has been called, no other Python thread
1757        can take the GIL at this point: if they try, they will exit
1758        immediately. */
1759     _PyThreadState_DeleteExcept(runtime, tstate);
1760 
1761     /* Flush sys.stdout and sys.stderr */
1762     if (flush_std_files() < 0) {
1763         status = -1;
1764     }
1765 
1766     /* Disable signal handling */
1767     _PySignal_Fini();
1768 
1769     /* Collect garbage.  This may call finalizers; it's nice to call these
1770      * before all modules are destroyed.
1771      * XXX If a __del__ or weakref callback is triggered here, and tries to
1772      * XXX import a module, bad things can happen, because Python no
1773      * XXX longer believes it's initialized.
1774      * XXX     Fatal Python error: Interpreter not initialized (version mismatch?)
1775      * XXX is easy to provoke that way.  I've also seen, e.g.,
1776      * XXX     Exception exceptions.ImportError: 'No module named sha'
1777      * XXX         in <function callback at 0x008F5718> ignored
1778      * XXX but I'm unclear on exactly how that one happens.  In any case,
1779      * XXX I haven't seen a real-life report of either of these.
1780      */
1781     PyGC_Collect();
1782 
1783     /* Destroy all modules */
1784     finalize_modules(tstate);
1785 
1786     /* Print debug stats if any */
1787     _PyEval_Fini();
1788 
1789     /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
1790     if (flush_std_files() < 0) {
1791         status = -1;
1792     }
1793 
1794     /* Collect final garbage.  This disposes of cycles created by
1795      * class definitions, for example.
1796      * XXX This is disabled because it caused too many problems.  If
1797      * XXX a __del__ or weakref callback triggers here, Python code has
1798      * XXX a hard time running, because even the sys module has been
1799      * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
1800      * XXX One symptom is a sequence of information-free messages
1801      * XXX coming from threads (if a __del__ or callback is invoked,
1802      * XXX other threads can execute too, and any exception they encounter
1803      * XXX triggers a comedy of errors as subsystem after subsystem
1804      * XXX fails to find what it *expects* to find in sys to help report
1805      * XXX the exception and consequent unexpected failures).  I've also
1806      * XXX seen segfaults then, after adding print statements to the
1807      * XXX Python code getting called.
1808      */
1809 #if 0
1810     _PyGC_CollectIfEnabled();
1811 #endif
1812 
1813     /* Disable tracemalloc after all Python objects have been destroyed,
1814        so it is possible to use tracemalloc in objects destructor. */
1815     _PyTraceMalloc_Fini();
1816 
1817     /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
1818     _PyImport_Fini();
1819 
1820     /* unload faulthandler module */
1821     _PyFaulthandler_Fini();
1822 
1823     /* dump hash stats */
1824     _PyHash_Fini();
1825 
1826 #ifdef Py_REF_DEBUG
1827     if (show_ref_count) {
1828         _PyDebug_PrintTotalRefs();
1829     }
1830 #endif
1831 
1832 #ifdef Py_TRACE_REFS
1833     /* Display all objects still alive -- this can invoke arbitrary
1834      * __repr__ overrides, so requires a mostly-intact interpreter.
1835      * Alas, a lot of stuff may still be alive now that will be cleaned
1836      * up later.
1837      */
1838     if (dump_refs) {
1839         _Py_PrintReferences(stderr);
1840     }
1841 #endif /* Py_TRACE_REFS */
1842 
1843     finalize_interp_clear(tstate);
1844     finalize_interp_delete(tstate->interp);
1845 
1846 #ifdef Py_TRACE_REFS
1847     /* Display addresses (& refcnts) of all objects still alive.
1848      * An address can be used to find the repr of the object, printed
1849      * above by _Py_PrintReferences.
1850      */
1851     if (dump_refs) {
1852         _Py_PrintReferenceAddresses(stderr);
1853     }
1854 #endif /* Py_TRACE_REFS */
1855 #ifdef WITH_PYMALLOC
1856     if (malloc_stats) {
1857         _PyObject_DebugMallocStats(stderr);
1858     }
1859 #endif
1860 
1861     call_ll_exitfuncs(runtime);
1862 
1863     _PyRuntime_Finalize();
1864     return status;
1865 }
1866 
1867 void
Py_Finalize(void)1868 Py_Finalize(void)
1869 {
1870     Py_FinalizeEx();
1871 }
1872 
1873 
1874 /* Create and initialize a new interpreter and thread, and return the
1875    new thread.  This requires that Py_Initialize() has been called
1876    first.
1877 
1878    Unsuccessful initialization yields a NULL pointer.  Note that *no*
1879    exception information is available even in this case -- the
1880    exception information is held in the thread, and there is no
1881    thread.
1882 
1883    Locking: as above.
1884 
1885 */
1886 
1887 static PyStatus
new_interpreter(PyThreadState ** tstate_p,int isolated_subinterpreter)1888 new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter)
1889 {
1890     PyStatus status;
1891 
1892     status = _PyRuntime_Initialize();
1893     if (_PyStatus_EXCEPTION(status)) {
1894         return status;
1895     }
1896     _PyRuntimeState *runtime = &_PyRuntime;
1897 
1898     if (!runtime->initialized) {
1899         return _PyStatus_ERR("Py_Initialize must be called first");
1900     }
1901 
1902     /* Issue #10915, #15751: The GIL API doesn't work with multiple
1903        interpreters: disable PyGILState_Check(). */
1904     runtime->gilstate.check_enabled = 0;
1905 
1906     PyInterpreterState *interp = PyInterpreterState_New();
1907     if (interp == NULL) {
1908         *tstate_p = NULL;
1909         return _PyStatus_OK();
1910     }
1911 
1912     PyThreadState *tstate = PyThreadState_New(interp);
1913     if (tstate == NULL) {
1914         PyInterpreterState_Delete(interp);
1915         *tstate_p = NULL;
1916         return _PyStatus_OK();
1917     }
1918 
1919     PyThreadState *save_tstate = PyThreadState_Swap(tstate);
1920 
1921     /* Copy the current interpreter config into the new interpreter */
1922     const PyConfig *config;
1923 #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1924     if (save_tstate != NULL) {
1925         config = _PyInterpreterState_GetConfig(save_tstate->interp);
1926     }
1927     else
1928 #endif
1929     {
1930         /* No current thread state, copy from the main interpreter */
1931         PyInterpreterState *main_interp = PyInterpreterState_Main();
1932         config = _PyInterpreterState_GetConfig(main_interp);
1933     }
1934 
1935 
1936     status = _PyConfig_Copy(&interp->config, config);
1937     if (_PyStatus_EXCEPTION(status)) {
1938         goto error;
1939     }
1940     interp->config._isolated_interpreter = isolated_subinterpreter;
1941 
1942     status = init_interp_create_gil(tstate);
1943     if (_PyStatus_EXCEPTION(status)) {
1944         goto error;
1945     }
1946 
1947     status = pycore_interp_init(tstate);
1948     if (_PyStatus_EXCEPTION(status)) {
1949         goto error;
1950     }
1951 
1952     status = init_interp_main(tstate);
1953     if (_PyStatus_EXCEPTION(status)) {
1954         goto error;
1955     }
1956 
1957     *tstate_p = tstate;
1958     return _PyStatus_OK();
1959 
1960 error:
1961     *tstate_p = NULL;
1962 
1963     /* Oops, it didn't work.  Undo it all. */
1964     PyErr_PrintEx(0);
1965     PyThreadState_Clear(tstate);
1966     PyThreadState_Delete(tstate);
1967     PyInterpreterState_Delete(interp);
1968     PyThreadState_Swap(save_tstate);
1969 
1970     return status;
1971 }
1972 
1973 PyThreadState *
_Py_NewInterpreter(int isolated_subinterpreter)1974 _Py_NewInterpreter(int isolated_subinterpreter)
1975 {
1976     PyThreadState *tstate = NULL;
1977     PyStatus status = new_interpreter(&tstate, isolated_subinterpreter);
1978     if (_PyStatus_EXCEPTION(status)) {
1979         Py_ExitStatusException(status);
1980     }
1981     return tstate;
1982 
1983 }
1984 
1985 PyThreadState *
Py_NewInterpreter(void)1986 Py_NewInterpreter(void)
1987 {
1988     return _Py_NewInterpreter(0);
1989 }
1990 
1991 /* Delete an interpreter and its last thread.  This requires that the
1992    given thread state is current, that the thread has no remaining
1993    frames, and that it is its interpreter's only remaining thread.
1994    It is a fatal error to violate these constraints.
1995 
1996    (Py_FinalizeEx() doesn't have these constraints -- it zaps
1997    everything, regardless.)
1998 
1999    Locking: as above.
2000 
2001 */
2002 
2003 void
Py_EndInterpreter(PyThreadState * tstate)2004 Py_EndInterpreter(PyThreadState *tstate)
2005 {
2006     PyInterpreterState *interp = tstate->interp;
2007 
2008     if (tstate != _PyThreadState_GET()) {
2009         Py_FatalError("thread is not current");
2010     }
2011     if (tstate->frame != NULL) {
2012         Py_FatalError("thread still has a frame");
2013     }
2014     interp->finalizing = 1;
2015 
2016     // Wrap up existing "threading"-module-created, non-daemon threads.
2017     wait_for_thread_shutdown(tstate);
2018 
2019     _PyAtExit_Call(tstate->interp);
2020 
2021     if (tstate != interp->tstate_head || tstate->next != NULL) {
2022         Py_FatalError("not the last thread");
2023     }
2024 
2025     finalize_modules(tstate);
2026 
2027     finalize_interp_clear(tstate);
2028     finalize_interp_delete(tstate->interp);
2029 }
2030 
2031 /* Add the __main__ module */
2032 
2033 static PyStatus
add_main_module(PyInterpreterState * interp)2034 add_main_module(PyInterpreterState *interp)
2035 {
2036     PyObject *m, *d, *loader, *ann_dict;
2037     m = PyImport_AddModule("__main__");
2038     if (m == NULL)
2039         return _PyStatus_ERR("can't create __main__ module");
2040 
2041     d = PyModule_GetDict(m);
2042     ann_dict = PyDict_New();
2043     if ((ann_dict == NULL) ||
2044         (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
2045         return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
2046     }
2047     Py_DECREF(ann_dict);
2048 
2049     if (_PyDict_GetItemStringWithError(d, "__builtins__") == NULL) {
2050         if (PyErr_Occurred()) {
2051             return _PyStatus_ERR("Failed to test __main__.__builtins__");
2052         }
2053         PyObject *bimod = PyImport_ImportModule("builtins");
2054         if (bimod == NULL) {
2055             return _PyStatus_ERR("Failed to retrieve builtins module");
2056         }
2057         if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
2058             return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
2059         }
2060         Py_DECREF(bimod);
2061     }
2062 
2063     /* Main is a little special - imp.is_builtin("__main__") will return
2064      * False, but BuiltinImporter is still the most appropriate initial
2065      * setting for its __loader__ attribute. A more suitable value will
2066      * be set if __main__ gets further initialized later in the startup
2067      * process.
2068      */
2069     loader = _PyDict_GetItemStringWithError(d, "__loader__");
2070     if (loader == NULL || loader == Py_None) {
2071         if (PyErr_Occurred()) {
2072             return _PyStatus_ERR("Failed to test __main__.__loader__");
2073         }
2074         PyObject *loader = PyObject_GetAttrString(interp->importlib,
2075                                                   "BuiltinImporter");
2076         if (loader == NULL) {
2077             return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
2078         }
2079         if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
2080             return _PyStatus_ERR("Failed to initialize __main__.__loader__");
2081         }
2082         Py_DECREF(loader);
2083     }
2084     return _PyStatus_OK();
2085 }
2086 
2087 /* Import the site module (not into __main__ though) */
2088 
2089 static PyStatus
init_import_site(void)2090 init_import_site(void)
2091 {
2092     PyObject *m;
2093     m = PyImport_ImportModule("site");
2094     if (m == NULL) {
2095         return _PyStatus_ERR("Failed to import the site module");
2096     }
2097     Py_DECREF(m);
2098     return _PyStatus_OK();
2099 }
2100 
2101 /* Check if a file descriptor is valid or not.
2102    Return 0 if the file descriptor is invalid, return non-zero otherwise. */
2103 static int
is_valid_fd(int fd)2104 is_valid_fd(int fd)
2105 {
2106 /* dup() is faster than fstat(): fstat() can require input/output operations,
2107    whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python
2108    startup. Problem: dup() doesn't check if the file descriptor is valid on
2109    some platforms.
2110 
2111    bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
2112    side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
2113    EBADF. FreeBSD has similar issue (bpo-32849).
2114 
2115    Only use dup() on platforms where dup() is enough to detect invalid FD in
2116    corner cases: on Linux and Windows (bpo-32849). */
2117 #if defined(__linux__) || defined(MS_WINDOWS)
2118     if (fd < 0) {
2119         return 0;
2120     }
2121     int fd2;
2122 
2123     _Py_BEGIN_SUPPRESS_IPH
2124     fd2 = dup(fd);
2125     if (fd2 >= 0) {
2126         close(fd2);
2127     }
2128     _Py_END_SUPPRESS_IPH
2129 
2130     return (fd2 >= 0);
2131 #else
2132     struct stat st;
2133     return (fstat(fd, &st) == 0);
2134 #endif
2135 }
2136 
2137 /* returns Py_None if the fd is not valid */
2138 static PyObject*
create_stdio(const PyConfig * config,PyObject * io,int fd,int write_mode,const char * name,const wchar_t * encoding,const wchar_t * errors)2139 create_stdio(const PyConfig *config, PyObject* io,
2140     int fd, int write_mode, const char* name,
2141     const wchar_t* encoding, const wchar_t* errors)
2142 {
2143     PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
2144     const char* mode;
2145     const char* newline;
2146     PyObject *line_buffering, *write_through;
2147     int buffering, isatty;
2148     _Py_IDENTIFIER(open);
2149     _Py_IDENTIFIER(isatty);
2150     _Py_IDENTIFIER(TextIOWrapper);
2151     _Py_IDENTIFIER(mode);
2152     const int buffered_stdio = config->buffered_stdio;
2153 
2154     if (!is_valid_fd(fd))
2155         Py_RETURN_NONE;
2156 
2157     /* stdin is always opened in buffered mode, first because it shouldn't
2158        make a difference in common use cases, second because TextIOWrapper
2159        depends on the presence of a read1() method which only exists on
2160        buffered streams.
2161     */
2162     if (!buffered_stdio && write_mode)
2163         buffering = 0;
2164     else
2165         buffering = -1;
2166     if (write_mode)
2167         mode = "wb";
2168     else
2169         mode = "rb";
2170     buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
2171                                  fd, mode, buffering,
2172                                  Py_None, Py_None, /* encoding, errors */
2173                                  Py_None, Py_False); /* newline, closefd */
2174     if (buf == NULL)
2175         goto error;
2176 
2177     if (buffering) {
2178         _Py_IDENTIFIER(raw);
2179         raw = _PyObject_GetAttrId(buf, &PyId_raw);
2180         if (raw == NULL)
2181             goto error;
2182     }
2183     else {
2184         raw = buf;
2185         Py_INCREF(raw);
2186     }
2187 
2188 #ifdef MS_WINDOWS
2189     /* Windows console IO is always UTF-8 encoded */
2190     if (PyWindowsConsoleIO_Check(raw))
2191         encoding = L"utf-8";
2192 #endif
2193 
2194     text = PyUnicode_FromString(name);
2195     if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
2196         goto error;
2197     res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
2198     if (res == NULL)
2199         goto error;
2200     isatty = PyObject_IsTrue(res);
2201     Py_DECREF(res);
2202     if (isatty == -1)
2203         goto error;
2204     if (!buffered_stdio)
2205         write_through = Py_True;
2206     else
2207         write_through = Py_False;
2208     if (buffered_stdio && (isatty || fd == fileno(stderr)))
2209         line_buffering = Py_True;
2210     else
2211         line_buffering = Py_False;
2212 
2213     Py_CLEAR(raw);
2214     Py_CLEAR(text);
2215 
2216 #ifdef MS_WINDOWS
2217     /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
2218        newlines to "\n".
2219        sys.stdout and sys.stderr: translate "\n" to "\r\n". */
2220     newline = NULL;
2221 #else
2222     /* sys.stdin: split lines at "\n".
2223        sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
2224     newline = "\n";
2225 #endif
2226 
2227     PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
2228     if (encoding_str == NULL) {
2229         Py_CLEAR(buf);
2230         goto error;
2231     }
2232 
2233     PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
2234     if (errors_str == NULL) {
2235         Py_CLEAR(buf);
2236         Py_CLEAR(encoding_str);
2237         goto error;
2238     }
2239 
2240     stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
2241                                     buf, encoding_str, errors_str,
2242                                     newline, line_buffering, write_through);
2243     Py_CLEAR(buf);
2244     Py_CLEAR(encoding_str);
2245     Py_CLEAR(errors_str);
2246     if (stream == NULL)
2247         goto error;
2248 
2249     if (write_mode)
2250         mode = "w";
2251     else
2252         mode = "r";
2253     text = PyUnicode_FromString(mode);
2254     if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
2255         goto error;
2256     Py_CLEAR(text);
2257     return stream;
2258 
2259 error:
2260     Py_XDECREF(buf);
2261     Py_XDECREF(stream);
2262     Py_XDECREF(text);
2263     Py_XDECREF(raw);
2264 
2265     if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) {
2266         /* Issue #24891: the file descriptor was closed after the first
2267            is_valid_fd() check was called. Ignore the OSError and set the
2268            stream to None. */
2269         PyErr_Clear();
2270         Py_RETURN_NONE;
2271     }
2272     return NULL;
2273 }
2274 
2275 /* Set builtins.open to io.open */
2276 static PyStatus
init_set_builtins_open(void)2277 init_set_builtins_open(void)
2278 {
2279     PyObject *iomod = NULL, *wrapper;
2280     PyObject *bimod = NULL;
2281     PyStatus res = _PyStatus_OK();
2282 
2283     if (!(iomod = PyImport_ImportModule("io"))) {
2284         goto error;
2285     }
2286 
2287     if (!(bimod = PyImport_ImportModule("builtins"))) {
2288         goto error;
2289     }
2290 
2291     if (!(wrapper = PyObject_GetAttrString(iomod, "open"))) {
2292         goto error;
2293     }
2294 
2295     /* Set builtins.open */
2296     if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
2297         Py_DECREF(wrapper);
2298         goto error;
2299     }
2300     Py_DECREF(wrapper);
2301     goto done;
2302 
2303 error:
2304     res = _PyStatus_ERR("can't initialize io.open");
2305 
2306 done:
2307     Py_XDECREF(bimod);
2308     Py_XDECREF(iomod);
2309     return res;
2310 }
2311 
2312 
2313 /* Create sys.stdin, sys.stdout and sys.stderr */
2314 static PyStatus
init_sys_streams(PyThreadState * tstate)2315 init_sys_streams(PyThreadState *tstate)
2316 {
2317     PyObject *iomod = NULL;
2318     PyObject *std = NULL;
2319     int fd;
2320     PyObject * encoding_attr;
2321     PyStatus res = _PyStatus_OK();
2322     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
2323 
2324     /* Check that stdin is not a directory
2325        Using shell redirection, you can redirect stdin to a directory,
2326        crashing the Python interpreter. Catch this common mistake here
2327        and output a useful error message. Note that under MS Windows,
2328        the shell already prevents that. */
2329 #ifndef MS_WINDOWS
2330     struct _Py_stat_struct sb;
2331     if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
2332         S_ISDIR(sb.st_mode)) {
2333         return _PyStatus_ERR("<stdin> is a directory, cannot continue");
2334     }
2335 #endif
2336 
2337     if (!(iomod = PyImport_ImportModule("io"))) {
2338         goto error;
2339     }
2340 
2341     /* Set sys.stdin */
2342     fd = fileno(stdin);
2343     /* Under some conditions stdin, stdout and stderr may not be connected
2344      * and fileno() may point to an invalid file descriptor. For example
2345      * GUI apps don't have valid standard streams by default.
2346      */
2347     std = create_stdio(config, iomod, fd, 0, "<stdin>",
2348                        config->stdio_encoding,
2349                        config->stdio_errors);
2350     if (std == NULL)
2351         goto error;
2352     PySys_SetObject("__stdin__", std);
2353     _PySys_SetObjectId(&PyId_stdin, std);
2354     Py_DECREF(std);
2355 
2356     /* Set sys.stdout */
2357     fd = fileno(stdout);
2358     std = create_stdio(config, iomod, fd, 1, "<stdout>",
2359                        config->stdio_encoding,
2360                        config->stdio_errors);
2361     if (std == NULL)
2362         goto error;
2363     PySys_SetObject("__stdout__", std);
2364     _PySys_SetObjectId(&PyId_stdout, std);
2365     Py_DECREF(std);
2366 
2367 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2368     /* Set sys.stderr, replaces the preliminary stderr */
2369     fd = fileno(stderr);
2370     std = create_stdio(config, iomod, fd, 1, "<stderr>",
2371                        config->stdio_encoding,
2372                        L"backslashreplace");
2373     if (std == NULL)
2374         goto error;
2375 
2376     /* Same as hack above, pre-import stderr's codec to avoid recursion
2377        when import.c tries to write to stderr in verbose mode. */
2378     encoding_attr = PyObject_GetAttrString(std, "encoding");
2379     if (encoding_attr != NULL) {
2380         const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
2381         if (std_encoding != NULL) {
2382             PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2383             Py_XDECREF(codec_info);
2384         }
2385         Py_DECREF(encoding_attr);
2386     }
2387     _PyErr_Clear(tstate);  /* Not a fatal error if codec isn't available */
2388 
2389     if (PySys_SetObject("__stderr__", std) < 0) {
2390         Py_DECREF(std);
2391         goto error;
2392     }
2393     if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
2394         Py_DECREF(std);
2395         goto error;
2396     }
2397     Py_DECREF(std);
2398 #endif
2399 
2400     goto done;
2401 
2402 error:
2403     res = _PyStatus_ERR("can't initialize sys standard streams");
2404 
2405 done:
2406     _Py_ClearStandardStreamEncoding();
2407     Py_XDECREF(iomod);
2408     return res;
2409 }
2410 
2411 
2412 static void
_Py_FatalError_DumpTracebacks(int fd,PyInterpreterState * interp,PyThreadState * tstate)2413 _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2414                               PyThreadState *tstate)
2415 {
2416     PUTS(fd, "\n");
2417 
2418     /* display the current Python stack */
2419     _Py_DumpTracebackThreads(fd, interp, tstate);
2420 }
2421 
2422 /* Print the current exception (if an exception is set) with its traceback,
2423    or display the current Python stack.
2424 
2425    Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2426    called on catastrophic cases.
2427 
2428    Return 1 if the traceback was displayed, 0 otherwise. */
2429 
2430 static int
_Py_FatalError_PrintExc(PyThreadState * tstate)2431 _Py_FatalError_PrintExc(PyThreadState *tstate)
2432 {
2433     PyObject *ferr, *res;
2434     PyObject *exception, *v, *tb;
2435     int has_tb;
2436 
2437     _PyErr_Fetch(tstate, &exception, &v, &tb);
2438     if (exception == NULL) {
2439         /* No current exception */
2440         return 0;
2441     }
2442 
2443     ferr = _PySys_GetObjectId(&PyId_stderr);
2444     if (ferr == NULL || ferr == Py_None) {
2445         /* sys.stderr is not set yet or set to None,
2446            no need to try to display the exception */
2447         return 0;
2448     }
2449 
2450     _PyErr_NormalizeException(tstate, &exception, &v, &tb);
2451     if (tb == NULL) {
2452         tb = Py_None;
2453         Py_INCREF(tb);
2454     }
2455     PyException_SetTraceback(v, tb);
2456     if (exception == NULL) {
2457         /* PyErr_NormalizeException() failed */
2458         return 0;
2459     }
2460 
2461     has_tb = (tb != Py_None);
2462     PyErr_Display(exception, v, tb);
2463     Py_XDECREF(exception);
2464     Py_XDECREF(v);
2465     Py_XDECREF(tb);
2466 
2467     /* sys.stderr may be buffered: call sys.stderr.flush() */
2468     res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
2469     if (res == NULL) {
2470         _PyErr_Clear(tstate);
2471     }
2472     else {
2473         Py_DECREF(res);
2474     }
2475 
2476     return has_tb;
2477 }
2478 
2479 /* Print fatal error message and abort */
2480 
2481 #ifdef MS_WINDOWS
2482 static void
fatal_output_debug(const char * msg)2483 fatal_output_debug(const char *msg)
2484 {
2485     /* buffer of 256 bytes allocated on the stack */
2486     WCHAR buffer[256 / sizeof(WCHAR)];
2487     size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2488     size_t msglen;
2489 
2490     OutputDebugStringW(L"Fatal Python error: ");
2491 
2492     msglen = strlen(msg);
2493     while (msglen) {
2494         size_t i;
2495 
2496         if (buflen > msglen) {
2497             buflen = msglen;
2498         }
2499 
2500         /* Convert the message to wchar_t. This uses a simple one-to-one
2501            conversion, assuming that the this error message actually uses
2502            ASCII only. If this ceases to be true, we will have to convert. */
2503         for (i=0; i < buflen; ++i) {
2504             buffer[i] = msg[i];
2505         }
2506         buffer[i] = L'\0';
2507         OutputDebugStringW(buffer);
2508 
2509         msg += buflen;
2510         msglen -= buflen;
2511     }
2512     OutputDebugStringW(L"\n");
2513 }
2514 #endif
2515 
2516 
2517 static void
fatal_error_dump_runtime(int fd,_PyRuntimeState * runtime)2518 fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime)
2519 {
2520     PUTS(fd, "Python runtime state: ");
2521     PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
2522     if (finalizing) {
2523         PUTS(fd, "finalizing (tstate=0x");
2524         _Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2);
2525         PUTS(fd, ")");
2526     }
2527     else if (runtime->initialized) {
2528         PUTS(fd, "initialized");
2529     }
2530     else if (runtime->core_initialized) {
2531         PUTS(fd, "core initialized");
2532     }
2533     else if (runtime->preinitialized) {
2534         PUTS(fd, "preinitialized");
2535     }
2536     else if (runtime->preinitializing) {
2537         PUTS(fd, "preinitializing");
2538     }
2539     else {
2540         PUTS(fd, "unknown");
2541     }
2542     PUTS(fd, "\n");
2543 }
2544 
2545 
2546 static inline void _Py_NO_RETURN
fatal_error_exit(int status)2547 fatal_error_exit(int status)
2548 {
2549     if (status < 0) {
2550 #if defined(MS_WINDOWS) && defined(_DEBUG)
2551         DebugBreak();
2552 #endif
2553         abort();
2554     }
2555     else {
2556         exit(status);
2557     }
2558 }
2559 
2560 
2561 // Dump the list of extension modules of sys.modules, excluding stdlib modules
2562 // (sys.stdlib_module_names), into fd file descriptor.
2563 //
2564 // This function is called by a signal handler in faulthandler: avoid memory
2565 // allocations and keep the implementation simple. For example, the list is not
2566 // sorted on purpose.
2567 void
_Py_DumpExtensionModules(int fd,PyInterpreterState * interp)2568 _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
2569 {
2570     if (interp == NULL) {
2571         return;
2572     }
2573     PyObject *modules = interp->modules;
2574     if (modules == NULL || !PyDict_Check(modules)) {
2575         return;
2576     }
2577 
2578     Py_ssize_t pos;
2579     PyObject *key, *value;
2580 
2581     // Avoid PyDict_GetItemString() which calls PyUnicode_FromString(),
2582     // memory cannot be allocated on the heap in a signal handler.
2583     // Iterate on the dict instead.
2584     PyObject *stdlib_module_names = NULL;
2585     if (interp->sysdict != NULL) {
2586         pos = 0;
2587         while (PyDict_Next(interp->sysdict, &pos, &key, &value)) {
2588             if (PyUnicode_Check(key)
2589                && PyUnicode_CompareWithASCIIString(key, "stdlib_module_names") == 0) {
2590                 stdlib_module_names = value;
2591                 break;
2592             }
2593         }
2594     }
2595     // If we failed to get sys.stdlib_module_names or it's not a frozenset,
2596     // don't exclude stdlib modules.
2597     if (stdlib_module_names != NULL && !PyFrozenSet_Check(stdlib_module_names)) {
2598         stdlib_module_names = NULL;
2599     }
2600 
2601     // List extensions
2602     int header = 1;
2603     Py_ssize_t count = 0;
2604     pos = 0;
2605     while (PyDict_Next(modules, &pos, &key, &value)) {
2606         if (!PyUnicode_Check(key)) {
2607             continue;
2608         }
2609         if (!_PyModule_IsExtension(value)) {
2610             continue;
2611         }
2612         // Use the module name from the sys.modules key,
2613         // don't attempt to get the module object name.
2614         if (stdlib_module_names != NULL) {
2615             int is_stdlib_ext = 0;
2616 
2617             Py_ssize_t i = 0;
2618             PyObject *item;
2619             Py_hash_t hash;
2620             while (_PySet_NextEntry(stdlib_module_names, &i, &item, &hash)) {
2621                 if (PyUnicode_Check(item)
2622                     && PyUnicode_Compare(key, item) == 0)
2623                 {
2624                     is_stdlib_ext = 1;
2625                     break;
2626                 }
2627             }
2628             if (is_stdlib_ext) {
2629                 // Ignore stdlib extension
2630                 continue;
2631             }
2632         }
2633 
2634         if (header) {
2635             PUTS(fd, "\nExtension modules: ");
2636             header = 0;
2637         }
2638         else {
2639             PUTS(fd, ", ");
2640         }
2641 
2642         _Py_DumpASCII(fd, key);
2643         count++;
2644     }
2645 
2646     if (count) {
2647         PUTS(fd, " (total: ");
2648         _Py_DumpDecimal(fd, count);
2649         PUTS(fd, ")");
2650         PUTS(fd, "\n");
2651     }
2652 }
2653 
2654 
2655 static void _Py_NO_RETURN
fatal_error(int fd,int header,const char * prefix,const char * msg,int status)2656 fatal_error(int fd, int header, const char *prefix, const char *msg,
2657             int status)
2658 {
2659     static int reentrant = 0;
2660 
2661     if (reentrant) {
2662         /* Py_FatalError() caused a second fatal error.
2663            Example: flush_std_files() raises a recursion error. */
2664         fatal_error_exit(status);
2665     }
2666     reentrant = 1;
2667 
2668     if (header) {
2669         PUTS(fd, "Fatal Python error: ");
2670         if (prefix) {
2671             PUTS(fd, prefix);
2672             PUTS(fd, ": ");
2673         }
2674         if (msg) {
2675             PUTS(fd, msg);
2676         }
2677         else {
2678             PUTS(fd, "<message not set>");
2679         }
2680         PUTS(fd, "\n");
2681     }
2682 
2683     _PyRuntimeState *runtime = &_PyRuntime;
2684     fatal_error_dump_runtime(fd, runtime);
2685 
2686     /* Check if the current thread has a Python thread state
2687        and holds the GIL.
2688 
2689        tss_tstate is NULL if Py_FatalError() is called from a C thread which
2690        has no Python thread state.
2691 
2692        tss_tstate != tstate if the current Python thread does not hold the GIL.
2693        */
2694     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
2695     PyInterpreterState *interp = NULL;
2696     PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
2697     if (tstate != NULL) {
2698         interp = tstate->interp;
2699     }
2700     else if (tss_tstate != NULL) {
2701         interp = tss_tstate->interp;
2702     }
2703     int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
2704 
2705     if (has_tstate_and_gil) {
2706         /* If an exception is set, print the exception with its traceback */
2707         if (!_Py_FatalError_PrintExc(tss_tstate)) {
2708             /* No exception is set, or an exception is set without traceback */
2709             _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
2710         }
2711     }
2712     else {
2713         _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
2714     }
2715 
2716     _Py_DumpExtensionModules(fd, interp);
2717 
2718     /* The main purpose of faulthandler is to display the traceback.
2719        This function already did its best to display a traceback.
2720        Disable faulthandler to prevent writing a second traceback
2721        on abort(). */
2722     _PyFaulthandler_Fini();
2723 
2724     /* Check if the current Python thread hold the GIL */
2725     if (has_tstate_and_gil) {
2726         /* Flush sys.stdout and sys.stderr */
2727         flush_std_files();
2728     }
2729 
2730 #ifdef MS_WINDOWS
2731     fatal_output_debug(msg);
2732 #endif /* MS_WINDOWS */
2733 
2734     fatal_error_exit(status);
2735 }
2736 
2737 
2738 #undef Py_FatalError
2739 
2740 void _Py_NO_RETURN
Py_FatalError(const char * msg)2741 Py_FatalError(const char *msg)
2742 {
2743     fatal_error(fileno(stderr), 1, NULL, msg, -1);
2744 }
2745 
2746 
2747 void _Py_NO_RETURN
_Py_FatalErrorFunc(const char * func,const char * msg)2748 _Py_FatalErrorFunc(const char *func, const char *msg)
2749 {
2750     fatal_error(fileno(stderr), 1, func, msg, -1);
2751 }
2752 
2753 
2754 void _Py_NO_RETURN
_Py_FatalErrorFormat(const char * func,const char * format,...)2755 _Py_FatalErrorFormat(const char *func, const char *format, ...)
2756 {
2757     static int reentrant = 0;
2758     if (reentrant) {
2759         /* _Py_FatalErrorFormat() caused a second fatal error */
2760         fatal_error_exit(-1);
2761     }
2762     reentrant = 1;
2763 
2764     FILE *stream = stderr;
2765     const int fd = fileno(stream);
2766     PUTS(fd, "Fatal Python error: ");
2767     if (func) {
2768         PUTS(fd, func);
2769         PUTS(fd, ": ");
2770     }
2771 
2772     va_list vargs;
2773 #ifdef HAVE_STDARG_PROTOTYPES
2774     va_start(vargs, format);
2775 #else
2776     va_start(vargs);
2777 #endif
2778     vfprintf(stream, format, vargs);
2779     va_end(vargs);
2780 
2781     fputs("\n", stream);
2782     fflush(stream);
2783 
2784     fatal_error(fd, 0, NULL, NULL, -1);
2785 }
2786 
2787 
2788 void _Py_NO_RETURN
Py_ExitStatusException(PyStatus status)2789 Py_ExitStatusException(PyStatus status)
2790 {
2791     if (_PyStatus_IS_EXIT(status)) {
2792         exit(status.exitcode);
2793     }
2794     else if (_PyStatus_IS_ERROR(status)) {
2795         fatal_error(fileno(stderr), 1, status.func, status.err_msg, 1);
2796     }
2797     else {
2798         Py_FatalError("Py_ExitStatusException() must not be called on success");
2799     }
2800 }
2801 
2802 
2803 /* Wait until threading._shutdown completes, provided
2804    the threading module was imported in the first place.
2805    The shutdown routine will wait until all non-daemon
2806    "threading" threads have completed. */
2807 static void
wait_for_thread_shutdown(PyThreadState * tstate)2808 wait_for_thread_shutdown(PyThreadState *tstate)
2809 {
2810     _Py_IDENTIFIER(_shutdown);
2811     PyObject *result;
2812     PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
2813     if (threading == NULL) {
2814         if (_PyErr_Occurred(tstate)) {
2815             PyErr_WriteUnraisable(NULL);
2816         }
2817         /* else: threading not imported */
2818         return;
2819     }
2820     result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
2821     if (result == NULL) {
2822         PyErr_WriteUnraisable(threading);
2823     }
2824     else {
2825         Py_DECREF(result);
2826     }
2827     Py_DECREF(threading);
2828 }
2829 
2830 #define NEXITFUNCS 32
Py_AtExit(void (* func)(void))2831 int Py_AtExit(void (*func)(void))
2832 {
2833     if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
2834         return -1;
2835     _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
2836     return 0;
2837 }
2838 
2839 static void
call_ll_exitfuncs(_PyRuntimeState * runtime)2840 call_ll_exitfuncs(_PyRuntimeState *runtime)
2841 {
2842     while (runtime->nexitfuncs > 0) {
2843         /* pop last function from the list */
2844         runtime->nexitfuncs--;
2845         void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2846         runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2847 
2848         exitfunc();
2849     }
2850 
2851     fflush(stdout);
2852     fflush(stderr);
2853 }
2854 
2855 void _Py_NO_RETURN
Py_Exit(int sts)2856 Py_Exit(int sts)
2857 {
2858     if (Py_FinalizeEx() < 0) {
2859         sts = 120;
2860     }
2861 
2862     exit(sts);
2863 }
2864 
2865 
2866 /*
2867  * The file descriptor fd is considered ``interactive'' if either
2868  *   a) isatty(fd) is TRUE, or
2869  *   b) the -i flag was given, and the filename associated with
2870  *      the descriptor is NULL or "<stdin>" or "???".
2871  */
2872 int
Py_FdIsInteractive(FILE * fp,const char * filename)2873 Py_FdIsInteractive(FILE *fp, const char *filename)
2874 {
2875     if (isatty((int)fileno(fp)))
2876         return 1;
2877     if (!Py_InteractiveFlag)
2878         return 0;
2879     return (filename == NULL) ||
2880            (strcmp(filename, "<stdin>") == 0) ||
2881            (strcmp(filename, "???") == 0);
2882 }
2883 
2884 
2885 int
_Py_FdIsInteractive(FILE * fp,PyObject * filename)2886 _Py_FdIsInteractive(FILE *fp, PyObject *filename)
2887 {
2888     if (isatty((int)fileno(fp))) {
2889         return 1;
2890     }
2891     if (!Py_InteractiveFlag) {
2892         return 0;
2893     }
2894     return (filename == NULL) ||
2895            (PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0) ||
2896            (PyUnicode_CompareWithASCIIString(filename, "???") == 0);
2897 }
2898 
2899 
2900 /* Wrappers around sigaction() or signal(). */
2901 
2902 PyOS_sighandler_t
PyOS_getsig(int sig)2903 PyOS_getsig(int sig)
2904 {
2905 #ifdef HAVE_SIGACTION
2906     struct sigaction context;
2907     if (sigaction(sig, NULL, &context) == -1)
2908         return SIG_ERR;
2909     return context.sa_handler;
2910 #else
2911     PyOS_sighandler_t handler;
2912 /* Special signal handling for the secure CRT in Visual Studio 2005 */
2913 #if defined(_MSC_VER) && _MSC_VER >= 1400
2914     switch (sig) {
2915     /* Only these signals are valid */
2916     case SIGINT:
2917     case SIGILL:
2918     case SIGFPE:
2919     case SIGSEGV:
2920     case SIGTERM:
2921     case SIGBREAK:
2922     case SIGABRT:
2923         break;
2924     /* Don't call signal() with other values or it will assert */
2925     default:
2926         return SIG_ERR;
2927     }
2928 #endif /* _MSC_VER && _MSC_VER >= 1400 */
2929     handler = signal(sig, SIG_IGN);
2930     if (handler != SIG_ERR)
2931         signal(sig, handler);
2932     return handler;
2933 #endif
2934 }
2935 
2936 /*
2937  * All of the code in this function must only use async-signal-safe functions,
2938  * listed at `man 7 signal` or
2939  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2940  */
2941 PyOS_sighandler_t
PyOS_setsig(int sig,PyOS_sighandler_t handler)2942 PyOS_setsig(int sig, PyOS_sighandler_t handler)
2943 {
2944 #ifdef HAVE_SIGACTION
2945     /* Some code in Modules/signalmodule.c depends on sigaction() being
2946      * used here if HAVE_SIGACTION is defined.  Fix that if this code
2947      * changes to invalidate that assumption.
2948      */
2949     struct sigaction context, ocontext;
2950     context.sa_handler = handler;
2951     sigemptyset(&context.sa_mask);
2952     /* Using SA_ONSTACK is friendlier to other C/C++/Golang-VM code that
2953      * extension module or embedding code may use where tiny thread stacks
2954      * are used.  https://bugs.python.org/issue43390 */
2955     context.sa_flags = SA_ONSTACK;
2956     if (sigaction(sig, &context, &ocontext) == -1)
2957         return SIG_ERR;
2958     return ocontext.sa_handler;
2959 #else
2960     PyOS_sighandler_t oldhandler;
2961     oldhandler = signal(sig, handler);
2962 #ifdef HAVE_SIGINTERRUPT
2963     siginterrupt(sig, 1);
2964 #endif
2965     return oldhandler;
2966 #endif
2967 }
2968 
2969 #ifdef __cplusplus
2970 }
2971 #endif
2972