• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Python interpreter top-level routines, including init/exit */
2 
3 #include "Python.h"
4 
5 #include "pycore_call.h"          // _PyObject_CallMethod()
6 #include "pycore_ceval.h"         // _PyEval_FiniGIL()
7 #include "pycore_codecs.h"        // _PyCodec_Lookup()
8 #include "pycore_context.h"       // _PyContext_Init()
9 #include "pycore_dict.h"          // _PyDict_Fini()
10 #include "pycore_exceptions.h"    // _PyExc_InitTypes()
11 #include "pycore_fileutils.h"     // _Py_ResetForceASCII()
12 #include "pycore_floatobject.h"   // _PyFloat_InitTypes()
13 #include "pycore_genobject.h"     // _PyAsyncGen_Fini()
14 #include "pycore_global_objects_fini_generated.h"  // "_PyStaticObjects_CheckRefcnt()
15 #include "pycore_import.h"        // _PyImport_BootstrapImp()
16 #include "pycore_initconfig.h"    // _PyStatus_OK()
17 #include "pycore_list.h"          // _PyList_Fini()
18 #include "pycore_long.h"          // _PyLong_InitTypes()
19 #include "pycore_object.h"        // _PyDebug_PrintTotalRefs()
20 #include "pycore_pathconfig.h"    // _PyPathConfig_UpdateGlobal()
21 #include "pycore_pyerrors.h"      // _PyErr_Occurred()
22 #include "pycore_pylifecycle.h"   // _PyErr_Print()
23 #include "pycore_pymem.h"         // _PyObject_DebugMallocStats()
24 #include "pycore_pystate.h"       // _PyThreadState_GET()
25 #include "pycore_runtime.h"       // _Py_ID()
26 #include "pycore_runtime_init.h"  // _PyRuntimeState_INIT
27 #include "pycore_setobject.h"     // _PySet_NextEntry()
28 #include "pycore_sliceobject.h"   // _PySlice_Fini()
29 #include "pycore_sysmodule.h"     // _PySys_ClearAuditHooks()
30 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads()
31 #include "pycore_typeobject.h"    // _PyTypes_InitTypes()
32 #include "pycore_typevarobject.h" // _Py_clear_generic_types()
33 #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
34 #include "pycore_weakref.h"       // _PyWeakref_GET_REF()
35 #include "pycore_obmalloc.h"      // _PyMem_init_obmalloc()
36 
37 #include "opcode.h"
38 
39 #include <locale.h>               // setlocale()
40 #include <stdlib.h>               // getenv()
41 #ifdef HAVE_UNISTD_H
42 #  include <unistd.h>             // isatty()
43 #endif
44 
45 #if defined(__APPLE__)
46 #  include <mach-o/loader.h>
47 #endif
48 
49 #ifdef HAVE_SIGNAL_H
50 #  include <signal.h>             // SIG_IGN
51 #endif
52 
53 #ifdef HAVE_LANGINFO_H
54 #  include <langinfo.h>           // nl_langinfo(CODESET)
55 #endif
56 
57 #ifdef HAVE_FCNTL_H
58 #  include <fcntl.h>              // F_GETFD
59 #endif
60 
61 #ifdef MS_WINDOWS
62 #  undef BYTE
63 #endif
64 
65 #define PUTS(fd, str) (void)_Py_write_noraise(fd, str, (int)strlen(str))
66 
67 
68 /* Forward declarations */
69 static PyStatus add_main_module(PyInterpreterState *interp);
70 static PyStatus init_import_site(void);
71 static PyStatus init_set_builtins_open(void);
72 static PyStatus init_sys_streams(PyThreadState *tstate);
73 #ifdef __ANDROID__
74 static PyStatus init_android_streams(PyThreadState *tstate);
75 #endif
76 static void wait_for_thread_shutdown(PyThreadState *tstate);
77 static void finalize_subinterpreters(void);
78 static void call_ll_exitfuncs(_PyRuntimeState *runtime);
79 
80 /* The following places the `_PyRuntime` structure in a location that can be
81  * found without any external information. This is meant to ease access to the
82  * interpreter state for various runtime debugging tools, but is *not* an
83  * officially supported feature */
84 
85 /* Suppress deprecation warning for PyBytesObject.ob_shash */
86 _Py_COMP_DIAG_PUSH
87 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
88 
89 #if defined(MS_WINDOWS)
90 
91 #pragma section("PyRuntime", read, write)
92 __declspec(allocate("PyRuntime"))
93 
94 #elif defined(__APPLE__)
95 
96 __attribute__((
97     section(SEG_DATA ",PyRuntime")
98 ))
99 
100 #endif
101 
102 _PyRuntimeState _PyRuntime
103 #if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
104 __attribute__ ((section (".PyRuntime")))
105 #endif
106 = _PyRuntimeState_INIT(_PyRuntime, _Py_Debug_Cookie);
107 _Py_COMP_DIAG_POP
108 
109 static int runtime_initialized = 0;
110 
111 PyStatus
_PyRuntime_Initialize(void)112 _PyRuntime_Initialize(void)
113 {
114     /* XXX We only initialize once in the process, which aligns with
115        the static initialization of the former globals now found in
116        _PyRuntime.  However, _PyRuntime *should* be initialized with
117        every Py_Initialize() call, but doing so breaks the runtime.
118        This is because the runtime state is not properly finalized
119        currently. */
120     if (runtime_initialized) {
121         return _PyStatus_OK();
122     }
123     runtime_initialized = 1;
124 
125     return _PyRuntimeState_Init(&_PyRuntime);
126 }
127 
128 void
_PyRuntime_Finalize(void)129 _PyRuntime_Finalize(void)
130 {
131     _PyRuntimeState_Fini(&_PyRuntime);
132     runtime_initialized = 0;
133 }
134 
135 int
Py_IsFinalizing(void)136 Py_IsFinalizing(void)
137 {
138     return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
139 }
140 
141 /* Hack to force loading of object files */
142 int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
143     PyOS_mystrnicmp; /* Python/pystrcmp.o */
144 
145 
146 /* APIs to access the initialization flags
147  *
148  * Can be called prior to Py_Initialize.
149  */
150 
151 int
_Py_IsCoreInitialized(void)152 _Py_IsCoreInitialized(void)
153 {
154     return _PyRuntime.core_initialized;
155 }
156 
157 int
Py_IsInitialized(void)158 Py_IsInitialized(void)
159 {
160     return _PyRuntime.initialized;
161 }
162 
163 
164 /* Helper functions to better handle the legacy C locale
165  *
166  * The legacy C locale assumes ASCII as the default text encoding, which
167  * causes problems not only for the CPython runtime, but also other
168  * components like GNU readline.
169  *
170  * Accordingly, when the CLI detects it, it attempts to coerce it to a
171  * more capable UTF-8 based alternative as follows:
172  *
173  *     if (_Py_LegacyLocaleDetected()) {
174  *         _Py_CoerceLegacyLocale();
175  *     }
176  *
177  * See the documentation of the PYTHONCOERCECLOCALE setting for more details.
178  *
179  * Locale coercion also impacts the default error handler for the standard
180  * streams: while the usual default is "strict", the default for the legacy
181  * C locale and for any of the coercion target locales is "surrogateescape".
182  */
183 
184 int
_Py_LegacyLocaleDetected(int warn)185 _Py_LegacyLocaleDetected(int warn)
186 {
187 #ifndef MS_WINDOWS
188     if (!warn) {
189         const char *locale_override = getenv("LC_ALL");
190         if (locale_override != NULL && *locale_override != '\0') {
191             /* Don't coerce C locale if the LC_ALL environment variable
192                is set */
193             return 0;
194         }
195     }
196 
197     /* On non-Windows systems, the C locale is considered a legacy locale */
198     /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat
199      *                 the POSIX locale as a simple alias for the C locale, so
200      *                 we may also want to check for that explicitly.
201      */
202     const char *ctype_loc = setlocale(LC_CTYPE, NULL);
203     return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0;
204 #else
205     /* Windows uses code pages instead of locales, so no locale is legacy */
206     return 0;
207 #endif
208 }
209 
210 #ifndef MS_WINDOWS
211 static const char *_C_LOCALE_WARNING =
212     "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "
213     "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "
214     "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible "
215     "locales is recommended.\n";
216 
217 static void
emit_stderr_warning_for_legacy_locale(_PyRuntimeState * runtime)218 emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
219 {
220     const PyPreConfig *preconfig = &runtime->preconfig;
221     if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
222         PySys_FormatStderr("%s", _C_LOCALE_WARNING);
223     }
224 }
225 #endif   /* !defined(MS_WINDOWS) */
226 
227 typedef struct _CandidateLocale {
228     const char *locale_name; /* The locale to try as a coercion target */
229 } _LocaleCoercionTarget;
230 
231 static _LocaleCoercionTarget _TARGET_LOCALES[] = {
232     {"C.UTF-8"},
233     {"C.utf8"},
234     {"UTF-8"},
235     {NULL}
236 };
237 
238 
239 int
_Py_IsLocaleCoercionTarget(const char * ctype_loc)240 _Py_IsLocaleCoercionTarget(const char *ctype_loc)
241 {
242     const _LocaleCoercionTarget *target = NULL;
243     for (target = _TARGET_LOCALES; target->locale_name; target++) {
244         if (strcmp(ctype_loc, target->locale_name) == 0) {
245             return 1;
246         }
247     }
248     return 0;
249 }
250 
251 
252 #ifdef PY_COERCE_C_LOCALE
253 static const char C_LOCALE_COERCION_WARNING[] =
254     "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
255     "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
256 
257 static int
_coerce_default_locale_settings(int warn,const _LocaleCoercionTarget * target)258 _coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)
259 {
260     const char *newloc = target->locale_name;
261 
262     /* Reset locale back to currently configured defaults */
263     _Py_SetLocaleFromEnv(LC_ALL);
264 
265     /* Set the relevant locale environment variable */
266     if (setenv("LC_CTYPE", newloc, 1)) {
267         fprintf(stderr,
268                 "Error setting LC_CTYPE, skipping C locale coercion\n");
269         return 0;
270     }
271     if (warn) {
272         fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
273     }
274 
275     /* Reconfigure with the overridden environment variables */
276     _Py_SetLocaleFromEnv(LC_ALL);
277     return 1;
278 }
279 #endif
280 
281 int
_Py_CoerceLegacyLocale(int warn)282 _Py_CoerceLegacyLocale(int warn)
283 {
284     int coerced = 0;
285 #ifdef PY_COERCE_C_LOCALE
286     char *oldloc = NULL;
287 
288     oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
289     if (oldloc == NULL) {
290         return coerced;
291     }
292 
293     const char *locale_override = getenv("LC_ALL");
294     if (locale_override == NULL || *locale_override == '\0') {
295         /* LC_ALL is also not set (or is set to an empty string) */
296         const _LocaleCoercionTarget *target = NULL;
297         for (target = _TARGET_LOCALES; target->locale_name; target++) {
298             const char *new_locale = setlocale(LC_CTYPE,
299                                                target->locale_name);
300             if (new_locale != NULL) {
301 #if !defined(_Py_FORCE_UTF8_LOCALE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
302                 /* Also ensure that nl_langinfo works in this locale */
303                 char *codeset = nl_langinfo(CODESET);
304                 if (!codeset || *codeset == '\0') {
305                     /* CODESET is not set or empty, so skip coercion */
306                     new_locale = NULL;
307                     _Py_SetLocaleFromEnv(LC_CTYPE);
308                     continue;
309                 }
310 #endif
311                 /* Successfully configured locale, so make it the default */
312                 coerced = _coerce_default_locale_settings(warn, target);
313                 goto done;
314             }
315         }
316     }
317     /* No C locale warning here, as Py_Initialize will emit one later */
318 
319     setlocale(LC_CTYPE, oldloc);
320 
321 done:
322     PyMem_RawFree(oldloc);
323 #endif
324     return coerced;
325 }
326 
327 /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to
328  * isolate the idiosyncrasies of different libc implementations. It reads the
329  * appropriate environment variable and uses its value to select the locale for
330  * 'category'. */
331 char *
_Py_SetLocaleFromEnv(int category)332 _Py_SetLocaleFromEnv(int category)
333 {
334     char *res;
335 #ifdef __ANDROID__
336     const char *locale;
337     const char **pvar;
338 #ifdef PY_COERCE_C_LOCALE
339     const char *coerce_c_locale;
340 #endif
341     const char *utf8_locale = "C.UTF-8";
342     const char *env_var_set[] = {
343         "LC_ALL",
344         "LC_CTYPE",
345         "LANG",
346         NULL,
347     };
348 
349     /* Android setlocale(category, "") doesn't check the environment variables
350      * and incorrectly sets the "C" locale at API 24 and older APIs. We only
351      * check the environment variables listed in env_var_set. */
352     for (pvar=env_var_set; *pvar; pvar++) {
353         locale = getenv(*pvar);
354         if (locale != NULL && *locale != '\0') {
355             if (strcmp(locale, utf8_locale) == 0 ||
356                     strcmp(locale, "en_US.UTF-8") == 0) {
357                 return setlocale(category, utf8_locale);
358             }
359             return setlocale(category, "C");
360         }
361     }
362 
363     /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of
364      * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string.
365      * Quote from POSIX section "8.2 Internationalization Variables":
366      * "4. If the LANG environment variable is not set or is set to the empty
367      * string, the implementation-defined default locale shall be used." */
368 
369 #ifdef PY_COERCE_C_LOCALE
370     coerce_c_locale = getenv("PYTHONCOERCECLOCALE");
371     if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) {
372         /* Some other ported code may check the environment variables (e.g. in
373          * extension modules), so we make sure that they match the locale
374          * configuration */
375         if (setenv("LC_CTYPE", utf8_locale, 1)) {
376             fprintf(stderr, "Warning: failed setting the LC_CTYPE "
377                             "environment variable to %s\n", utf8_locale);
378         }
379     }
380 #endif
381     res = setlocale(category, utf8_locale);
382 #else /* !defined(__ANDROID__) */
383     res = setlocale(category, "");
384 #endif
385     _Py_ResetForceASCII();
386     return res;
387 }
388 
389 
390 static int
interpreter_update_config(PyThreadState * tstate,int only_update_path_config)391 interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
392 {
393     const PyConfig *config = &tstate->interp->config;
394 
395     if (!only_update_path_config) {
396         PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
397         if (_PyStatus_EXCEPTION(status)) {
398             _PyErr_SetFromPyStatus(status);
399             return -1;
400         }
401     }
402 
403     if (_Py_IsMainInterpreter(tstate->interp)) {
404         PyStatus status = _PyPathConfig_UpdateGlobal(config);
405         if (_PyStatus_EXCEPTION(status)) {
406             _PyErr_SetFromPyStatus(status);
407             return -1;
408         }
409     }
410 
411     tstate->interp->long_state.max_str_digits = config->int_max_str_digits;
412 
413     // Update the sys module for the new configuration
414     if (_PySys_UpdateConfig(tstate) < 0) {
415         return -1;
416     }
417     return 0;
418 }
419 
420 
421 int
_PyInterpreterState_SetConfig(const PyConfig * src_config)422 _PyInterpreterState_SetConfig(const PyConfig *src_config)
423 {
424     PyThreadState *tstate = _PyThreadState_GET();
425     int res = -1;
426 
427     PyConfig config;
428     PyConfig_InitPythonConfig(&config);
429     PyStatus status = _PyConfig_Copy(&config, src_config);
430     if (_PyStatus_EXCEPTION(status)) {
431         _PyErr_SetFromPyStatus(status);
432         goto done;
433     }
434 
435     status = _PyConfig_Read(&config, 1);
436     if (_PyStatus_EXCEPTION(status)) {
437         _PyErr_SetFromPyStatus(status);
438         goto done;
439     }
440 
441     status = _PyConfig_Copy(&tstate->interp->config, &config);
442     if (_PyStatus_EXCEPTION(status)) {
443         _PyErr_SetFromPyStatus(status);
444         goto done;
445     }
446 
447     res = interpreter_update_config(tstate, 0);
448 
449 done:
450     PyConfig_Clear(&config);
451     return res;
452 }
453 
454 
455 /* Global initializations.  Can be undone by Py_Finalize().  Don't
456    call this twice without an intervening Py_Finalize() call.
457 
458    Every call to Py_InitializeFromConfig, Py_Initialize or Py_InitializeEx
459    must have a corresponding call to Py_Finalize.
460 
461    Locking: you must hold the interpreter lock while calling these APIs.
462    (If the lock has not yet been initialized, that's equivalent to
463    having the lock, but you cannot use multiple threads.)
464 
465 */
466 
467 static PyStatus
pyinit_core_reconfigure(_PyRuntimeState * runtime,PyThreadState ** tstate_p,const PyConfig * config)468 pyinit_core_reconfigure(_PyRuntimeState *runtime,
469                         PyThreadState **tstate_p,
470                         const PyConfig *config)
471 {
472     PyStatus status;
473     PyThreadState *tstate = _PyThreadState_GET();
474     if (!tstate) {
475         return _PyStatus_ERR("failed to read thread state");
476     }
477     *tstate_p = tstate;
478 
479     PyInterpreterState *interp = tstate->interp;
480     if (interp == NULL) {
481         return _PyStatus_ERR("can't make main interpreter");
482     }
483     assert(interp->_ready);
484 
485     status = _PyConfig_Write(config, runtime);
486     if (_PyStatus_EXCEPTION(status)) {
487         return status;
488     }
489 
490     status = _PyConfig_Copy(&interp->config, config);
491     if (_PyStatus_EXCEPTION(status)) {
492         return status;
493     }
494     config = _PyInterpreterState_GetConfig(interp);
495 
496     if (config->_install_importlib) {
497         status = _PyPathConfig_UpdateGlobal(config);
498         if (_PyStatus_EXCEPTION(status)) {
499             return status;
500         }
501     }
502     return _PyStatus_OK();
503 }
504 
505 
506 static PyStatus
pycore_init_runtime(_PyRuntimeState * runtime,const PyConfig * config)507 pycore_init_runtime(_PyRuntimeState *runtime,
508                     const PyConfig *config)
509 {
510     if (runtime->initialized) {
511         return _PyStatus_ERR("main interpreter already initialized");
512     }
513 
514     PyStatus status = _PyConfig_Write(config, runtime);
515     if (_PyStatus_EXCEPTION(status)) {
516         return status;
517     }
518 
519     /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
520      * threads behave a little more gracefully at interpreter shutdown.
521      * We clobber it here so the new interpreter can start with a clean
522      * slate.
523      *
524      * However, this may still lead to misbehaviour if there are daemon
525      * threads still hanging around from a previous Py_Initialize/Finalize
526      * pair :(
527      */
528     _PyRuntimeState_SetFinalizing(runtime, NULL);
529 
530     _Py_InitVersion();
531 
532     status = _Py_HashRandomization_Init(config);
533     if (_PyStatus_EXCEPTION(status)) {
534         return status;
535     }
536 
537     status = _PyImport_Init();
538     if (_PyStatus_EXCEPTION(status)) {
539         return status;
540     }
541 
542     status = _PyInterpreterState_Enable(runtime);
543     if (_PyStatus_EXCEPTION(status)) {
544         return status;
545     }
546     return _PyStatus_OK();
547 }
548 
549 
550 static PyStatus
init_interp_settings(PyInterpreterState * interp,const PyInterpreterConfig * config)551 init_interp_settings(PyInterpreterState *interp,
552                      const PyInterpreterConfig *config)
553 {
554     assert(interp->feature_flags == 0);
555 
556     if (config->use_main_obmalloc) {
557         interp->feature_flags |= Py_RTFLAGS_USE_MAIN_OBMALLOC;
558     }
559     else if (!config->check_multi_interp_extensions) {
560         /* The reason: PyModuleDef.m_base.m_copy leaks objects between
561            interpreters. */
562         return _PyStatus_ERR("per-interpreter obmalloc does not support "
563                              "single-phase init extension modules");
564     }
565 #ifdef Py_GIL_DISABLED
566     if (!_Py_IsMainInterpreter(interp) &&
567         !config->check_multi_interp_extensions)
568     {
569         return _PyStatus_ERR("The free-threaded build does not support "
570                              "single-phase init extension modules in "
571                              "subinterpreters");
572     }
573 #endif
574 
575     if (config->allow_fork) {
576         interp->feature_flags |= Py_RTFLAGS_FORK;
577     }
578     if (config->allow_exec) {
579         interp->feature_flags |= Py_RTFLAGS_EXEC;
580     }
581     // Note that fork+exec is always allowed.
582 
583     if (config->allow_threads) {
584         interp->feature_flags |= Py_RTFLAGS_THREADS;
585     }
586     if (config->allow_daemon_threads) {
587         interp->feature_flags |= Py_RTFLAGS_DAEMON_THREADS;
588     }
589 
590     if (config->check_multi_interp_extensions) {
591         interp->feature_flags |= Py_RTFLAGS_MULTI_INTERP_EXTENSIONS;
592     }
593 
594     switch (config->gil) {
595     case PyInterpreterConfig_DEFAULT_GIL: break;
596     case PyInterpreterConfig_SHARED_GIL: break;
597     case PyInterpreterConfig_OWN_GIL: break;
598     default:
599         return _PyStatus_ERR("invalid interpreter config 'gil' value");
600     }
601 
602     return _PyStatus_OK();
603 }
604 
605 
606 static void
init_interp_create_gil(PyThreadState * tstate,int gil)607 init_interp_create_gil(PyThreadState *tstate, int gil)
608 {
609     /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
610        only called here. */
611     // XXX This is broken with a per-interpreter GIL.
612     _PyEval_FiniGIL(tstate->interp);
613 
614     /* Auto-thread-state API */
615     _PyGILState_SetTstate(tstate);
616 
617     int own_gil = (gil == PyInterpreterConfig_OWN_GIL);
618 
619     /* Create the GIL and take it */
620     _PyEval_InitGIL(tstate, own_gil);
621 }
622 
623 static int
builtins_dict_watcher(PyDict_WatchEvent event,PyObject * dict,PyObject * key,PyObject * new_value)624 builtins_dict_watcher(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
625 {
626     PyInterpreterState *interp = _PyInterpreterState_GET();
627 #ifdef _Py_TIER2
628     if (interp->rare_events.builtin_dict < _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS) {
629         _Py_Executors_InvalidateAll(interp, 1);
630     }
631 #endif
632     RARE_EVENT_INTERP_INC(interp, builtin_dict);
633     return 0;
634 }
635 
636 static PyStatus
pycore_create_interpreter(_PyRuntimeState * runtime,const PyConfig * src_config,PyThreadState ** tstate_p)637 pycore_create_interpreter(_PyRuntimeState *runtime,
638                           const PyConfig *src_config,
639                           PyThreadState **tstate_p)
640 {
641     PyStatus status;
642     PyInterpreterState *interp;
643     status = _PyInterpreterState_New(NULL, &interp);
644     if (_PyStatus_EXCEPTION(status)) {
645         return status;
646     }
647     assert(interp != NULL);
648     assert(_Py_IsMainInterpreter(interp));
649     _PyInterpreterState_SetWhence(interp, _PyInterpreterState_WHENCE_RUNTIME);
650     interp->_ready = 1;
651 
652     status = _PyConfig_Copy(&interp->config, src_config);
653     if (_PyStatus_EXCEPTION(status)) {
654         return status;
655     }
656 
657     /* Auto-thread-state API */
658     status = _PyGILState_Init(interp);
659     if (_PyStatus_EXCEPTION(status)) {
660         return status;
661     }
662 
663     PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
664     // The main interpreter always has its own GIL and supports single-phase
665     // init extensions.
666     config.gil = PyInterpreterConfig_OWN_GIL;
667     config.check_multi_interp_extensions = 0;
668     status = init_interp_settings(interp, &config);
669     if (_PyStatus_EXCEPTION(status)) {
670         return status;
671     }
672 
673     // This could be done in init_interpreter() (in pystate.c) if it
674     // didn't depend on interp->feature_flags being set already.
675     status = _PyObject_InitState(interp);
676     if (_PyStatus_EXCEPTION(status)) {
677         return status;
678     }
679 
680     // initialize the interp->obmalloc state.  This must be done after
681     // the settings are loaded (so that feature_flags are set) but before
682     // any calls are made to obmalloc functions.
683     if (_PyMem_init_obmalloc(interp) < 0) {
684         return  _PyStatus_NO_MEMORY();
685     }
686 
687     PyThreadState *tstate = _PyThreadState_New(interp,
688                                                _PyThreadState_WHENCE_INIT);
689     if (tstate == NULL) {
690         return _PyStatus_ERR("can't make first thread");
691     }
692     runtime->main_tstate = tstate;
693     _PyThreadState_Bind(tstate);
694 
695     init_interp_create_gil(tstate, config.gil);
696 
697     *tstate_p = tstate;
698     return _PyStatus_OK();
699 }
700 
701 
702 static PyStatus
pycore_init_global_objects(PyInterpreterState * interp)703 pycore_init_global_objects(PyInterpreterState *interp)
704 {
705     PyStatus status;
706 
707     _PyFloat_InitState(interp);
708 
709     status = _PyUnicode_InitGlobalObjects(interp);
710     if (_PyStatus_EXCEPTION(status)) {
711         return status;
712     }
713 
714     _PyUnicode_InitState(interp);
715 
716     if (_Py_IsMainInterpreter(interp)) {
717         _Py_GetConstant_Init();
718     }
719 
720     return _PyStatus_OK();
721 }
722 
723 
724 static PyStatus
pycore_init_types(PyInterpreterState * interp)725 pycore_init_types(PyInterpreterState *interp)
726 {
727     PyStatus status;
728 
729     status = _PyTypes_InitTypes(interp);
730     if (_PyStatus_EXCEPTION(status)) {
731         return status;
732     }
733 
734     status = _PyLong_InitTypes(interp);
735     if (_PyStatus_EXCEPTION(status)) {
736         return status;
737     }
738 
739     status = _PyUnicode_InitTypes(interp);
740     if (_PyStatus_EXCEPTION(status)) {
741         return status;
742     }
743 
744     status = _PyFloat_InitTypes(interp);
745     if (_PyStatus_EXCEPTION(status)) {
746         return status;
747     }
748 
749     if (_PyExc_InitTypes(interp) < 0) {
750         return _PyStatus_ERR("failed to initialize an exception type");
751     }
752 
753     status = _PyExc_InitGlobalObjects(interp);
754     if (_PyStatus_EXCEPTION(status)) {
755         return status;
756     }
757 
758     status = _PyExc_InitState(interp);
759     if (_PyStatus_EXCEPTION(status)) {
760         return status;
761     }
762 
763     status = _PyErr_InitTypes(interp);
764     if (_PyStatus_EXCEPTION(status)) {
765         return status;
766     }
767 
768     status = _PyContext_Init(interp);
769     if (_PyStatus_EXCEPTION(status)) {
770         return status;
771     }
772 
773     status = _PyXI_InitTypes(interp);
774     if (_PyStatus_EXCEPTION(status)) {
775         return status;
776     }
777 
778     return _PyStatus_OK();
779 }
780 
781 static PyStatus
pycore_init_builtins(PyThreadState * tstate)782 pycore_init_builtins(PyThreadState *tstate)
783 {
784     PyInterpreterState *interp = tstate->interp;
785 
786     PyObject *bimod = _PyBuiltin_Init(interp);
787     if (bimod == NULL) {
788         goto error;
789     }
790 
791     PyObject *modules = _PyImport_GetModules(interp);
792     if (_PyImport_FixupBuiltin(tstate, bimod, "builtins", modules) < 0) {
793         goto error;
794     }
795 
796     PyObject *builtins_dict = PyModule_GetDict(bimod);
797     if (builtins_dict == NULL) {
798         goto error;
799     }
800     interp->builtins = Py_NewRef(builtins_dict);
801 
802     PyObject *isinstance = PyDict_GetItemWithError(builtins_dict, &_Py_ID(isinstance));
803     if (!isinstance) {
804         goto error;
805     }
806     interp->callable_cache.isinstance = isinstance;
807 
808     PyObject *len = PyDict_GetItemWithError(builtins_dict, &_Py_ID(len));
809     if (!len) {
810         goto error;
811     }
812     interp->callable_cache.len = len;
813 
814     PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
815     if (list_append == NULL) {
816         goto error;
817     }
818     interp->callable_cache.list_append = list_append;
819 
820     PyObject *object__getattribute__ = _PyType_Lookup(&PyBaseObject_Type, &_Py_ID(__getattribute__));
821     if (object__getattribute__ == NULL) {
822         goto error;
823     }
824     interp->callable_cache.object__getattribute__ = object__getattribute__;
825 
826     if (_PyBuiltins_AddExceptions(bimod) < 0) {
827         return _PyStatus_ERR("failed to add exceptions to builtins");
828     }
829 
830     interp->builtins_copy = PyDict_Copy(interp->builtins);
831     if (interp->builtins_copy == NULL) {
832         goto error;
833     }
834     Py_DECREF(bimod);
835 
836     if (_PyImport_InitDefaultImportFunc(interp) < 0) {
837         goto error;
838     }
839 
840     assert(!_PyErr_Occurred(tstate));
841     return _PyStatus_OK();
842 
843 error:
844     Py_XDECREF(bimod);
845     return _PyStatus_ERR("can't initialize builtins module");
846 }
847 
848 
849 static PyStatus
pycore_interp_init(PyThreadState * tstate)850 pycore_interp_init(PyThreadState *tstate)
851 {
852     PyInterpreterState *interp = tstate->interp;
853     PyStatus status;
854     PyObject *sysmod = NULL;
855 
856     // Create singletons before the first PyType_Ready() call, since
857     // PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
858     // and the empty tuple singletons (tp_bases).
859     status = pycore_init_global_objects(interp);
860     if (_PyStatus_EXCEPTION(status)) {
861         return status;
862     }
863 
864     status = _PyCode_Init(interp);
865     if (_PyStatus_EXCEPTION(status)) {
866         return status;
867     }
868 
869     status = _PyDtoa_Init(interp);
870     if (_PyStatus_EXCEPTION(status)) {
871         return status;
872     }
873 
874     // The GC must be initialized before the first GC collection.
875     status = _PyGC_Init(interp);
876     if (_PyStatus_EXCEPTION(status)) {
877         return status;
878     }
879 
880     status = pycore_init_types(interp);
881     if (_PyStatus_EXCEPTION(status)) {
882         goto done;
883     }
884 
885     if (_PyWarnings_InitState(interp) < 0) {
886         return _PyStatus_ERR("can't initialize warnings");
887     }
888 
889     status = _PyAtExit_Init(interp);
890     if (_PyStatus_EXCEPTION(status)) {
891         return status;
892     }
893 
894     status = _PySys_Create(tstate, &sysmod);
895     if (_PyStatus_EXCEPTION(status)) {
896         goto done;
897     }
898 
899     status = pycore_init_builtins(tstate);
900     if (_PyStatus_EXCEPTION(status)) {
901         goto done;
902     }
903 
904     status = _PyXI_Init(interp);
905     if (_PyStatus_EXCEPTION(status)) {
906         goto done;
907     }
908 
909     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
910 
911     status = _PyImport_InitCore(tstate, sysmod, config->_install_importlib);
912     if (_PyStatus_EXCEPTION(status)) {
913         goto done;
914     }
915 
916 done:
917     /* sys.modules['sys'] contains a strong reference to the module */
918     Py_XDECREF(sysmod);
919     return status;
920 }
921 
922 
923 static PyStatus
pyinit_config(_PyRuntimeState * runtime,PyThreadState ** tstate_p,const PyConfig * config)924 pyinit_config(_PyRuntimeState *runtime,
925               PyThreadState **tstate_p,
926               const PyConfig *config)
927 {
928     PyStatus status = pycore_init_runtime(runtime, config);
929     if (_PyStatus_EXCEPTION(status)) {
930         return status;
931     }
932 
933     PyThreadState *tstate;
934     status = pycore_create_interpreter(runtime, config, &tstate);
935     if (_PyStatus_EXCEPTION(status)) {
936         return status;
937     }
938     *tstate_p = tstate;
939 
940     status = pycore_interp_init(tstate);
941     if (_PyStatus_EXCEPTION(status)) {
942         return status;
943     }
944 
945     /* Only when we get here is the runtime core fully initialized */
946     runtime->core_initialized = 1;
947     return _PyStatus_OK();
948 }
949 
950 
951 PyStatus
_Py_PreInitializeFromPyArgv(const PyPreConfig * src_config,const _PyArgv * args)952 _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
953 {
954     PyStatus status;
955 
956     if (src_config == NULL) {
957         return _PyStatus_ERR("preinitialization config is NULL");
958     }
959 
960     status = _PyRuntime_Initialize();
961     if (_PyStatus_EXCEPTION(status)) {
962         return status;
963     }
964     _PyRuntimeState *runtime = &_PyRuntime;
965 
966     if (runtime->preinitialized) {
967         /* If it's already configured: ignored the new configuration */
968         return _PyStatus_OK();
969     }
970 
971     /* Note: preinitialized remains 1 on error, it is only set to 0
972        at exit on success. */
973     runtime->preinitializing = 1;
974 
975     PyPreConfig config;
976 
977     status = _PyPreConfig_InitFromPreConfig(&config, src_config);
978     if (_PyStatus_EXCEPTION(status)) {
979         return status;
980     }
981 
982     status = _PyPreConfig_Read(&config, args);
983     if (_PyStatus_EXCEPTION(status)) {
984         return status;
985     }
986 
987     status = _PyPreConfig_Write(&config);
988     if (_PyStatus_EXCEPTION(status)) {
989         return status;
990     }
991 
992     runtime->preinitializing = 0;
993     runtime->preinitialized = 1;
994     return _PyStatus_OK();
995 }
996 
997 
998 PyStatus
Py_PreInitializeFromBytesArgs(const PyPreConfig * src_config,Py_ssize_t argc,char ** argv)999 Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
1000 {
1001     _PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
1002     return _Py_PreInitializeFromPyArgv(src_config, &args);
1003 }
1004 
1005 
1006 PyStatus
Py_PreInitializeFromArgs(const PyPreConfig * src_config,Py_ssize_t argc,wchar_t ** argv)1007 Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
1008 {
1009     _PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
1010     return _Py_PreInitializeFromPyArgv(src_config, &args);
1011 }
1012 
1013 
1014 PyStatus
Py_PreInitialize(const PyPreConfig * src_config)1015 Py_PreInitialize(const PyPreConfig *src_config)
1016 {
1017     return _Py_PreInitializeFromPyArgv(src_config, NULL);
1018 }
1019 
1020 
1021 PyStatus
_Py_PreInitializeFromConfig(const PyConfig * config,const _PyArgv * args)1022 _Py_PreInitializeFromConfig(const PyConfig *config,
1023                             const _PyArgv *args)
1024 {
1025     assert(config != NULL);
1026 
1027     PyStatus status = _PyRuntime_Initialize();
1028     if (_PyStatus_EXCEPTION(status)) {
1029         return status;
1030     }
1031     _PyRuntimeState *runtime = &_PyRuntime;
1032 
1033     if (runtime->preinitialized) {
1034         /* Already initialized: do nothing */
1035         return _PyStatus_OK();
1036     }
1037 
1038     PyPreConfig preconfig;
1039 
1040     _PyPreConfig_InitFromConfig(&preconfig, config);
1041 
1042     if (!config->parse_argv) {
1043         return Py_PreInitialize(&preconfig);
1044     }
1045     else if (args == NULL) {
1046         _PyArgv config_args = {
1047             .use_bytes_argv = 0,
1048             .argc = config->argv.length,
1049             .wchar_argv = config->argv.items};
1050         return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
1051     }
1052     else {
1053         return _Py_PreInitializeFromPyArgv(&preconfig, args);
1054     }
1055 }
1056 
1057 
1058 /* Begin interpreter initialization
1059  *
1060  * On return, the first thread and interpreter state have been created,
1061  * but the compiler, signal handling, multithreading and
1062  * multiple interpreter support, and codec infrastructure are not yet
1063  * available.
1064  *
1065  * The import system will support builtin and frozen modules only.
1066  * The only supported io is writing to sys.stderr
1067  *
1068  * If any operation invoked by this function fails, a fatal error is
1069  * issued and the function does not return.
1070  *
1071  * Any code invoked from this function should *not* assume it has access
1072  * to the Python C API (unless the API is explicitly listed as being
1073  * safe to call without calling Py_Initialize first)
1074  */
1075 static PyStatus
pyinit_core(_PyRuntimeState * runtime,const PyConfig * src_config,PyThreadState ** tstate_p)1076 pyinit_core(_PyRuntimeState *runtime,
1077             const PyConfig *src_config,
1078             PyThreadState **tstate_p)
1079 {
1080     PyStatus status;
1081 
1082     status = _Py_PreInitializeFromConfig(src_config, NULL);
1083     if (_PyStatus_EXCEPTION(status)) {
1084         return status;
1085     }
1086 
1087     PyConfig config;
1088     PyConfig_InitPythonConfig(&config);
1089 
1090     status = _PyConfig_Copy(&config, src_config);
1091     if (_PyStatus_EXCEPTION(status)) {
1092         goto done;
1093     }
1094 
1095     // Read the configuration, but don't compute the path configuration
1096     // (it is computed in the main init).
1097     status = _PyConfig_Read(&config, 0);
1098     if (_PyStatus_EXCEPTION(status)) {
1099         goto done;
1100     }
1101 
1102     if (!runtime->core_initialized) {
1103         status = pyinit_config(runtime, tstate_p, &config);
1104     }
1105     else {
1106         status = pyinit_core_reconfigure(runtime, tstate_p, &config);
1107     }
1108     if (_PyStatus_EXCEPTION(status)) {
1109         goto done;
1110     }
1111 
1112 done:
1113     PyConfig_Clear(&config);
1114     return status;
1115 }
1116 
1117 
1118 /* Py_Initialize() has already been called: update the main interpreter
1119    configuration. Example of bpo-34008: Py_Main() called after
1120    Py_Initialize(). */
1121 static PyStatus
pyinit_main_reconfigure(PyThreadState * tstate)1122 pyinit_main_reconfigure(PyThreadState *tstate)
1123 {
1124     if (interpreter_update_config(tstate, 0) < 0) {
1125         return _PyStatus_ERR("fail to reconfigure Python");
1126     }
1127     return _PyStatus_OK();
1128 }
1129 
1130 
1131 #ifdef Py_DEBUG
1132 static void
run_presite(PyThreadState * tstate)1133 run_presite(PyThreadState *tstate)
1134 {
1135     PyInterpreterState *interp = tstate->interp;
1136     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
1137 
1138     if (!config->run_presite) {
1139         return;
1140     }
1141 
1142     PyObject *presite_modname = PyUnicode_FromWideChar(
1143         config->run_presite,
1144         wcslen(config->run_presite)
1145     );
1146     if (presite_modname == NULL) {
1147         fprintf(stderr, "Could not convert pre-site module name to unicode\n");
1148     }
1149     else {
1150         PyObject *presite = PyImport_Import(presite_modname);
1151         if (presite == NULL) {
1152             fprintf(stderr, "pre-site import failed:\n");
1153             _PyErr_Print(tstate);
1154         }
1155         Py_XDECREF(presite);
1156         Py_DECREF(presite_modname);
1157     }
1158 }
1159 #endif
1160 
1161 
1162 static PyStatus
init_interp_main(PyThreadState * tstate)1163 init_interp_main(PyThreadState *tstate)
1164 {
1165     assert(!_PyErr_Occurred(tstate));
1166 
1167     PyStatus status;
1168     int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
1169     PyInterpreterState *interp = tstate->interp;
1170     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
1171 
1172     if (!config->_install_importlib) {
1173         /* Special mode for freeze_importlib: run with no import system
1174          *
1175          * This means anything which needs support from extension modules
1176          * or pure Python code in the standard library won't work.
1177          */
1178         if (is_main_interp) {
1179             interp->runtime->initialized = 1;
1180         }
1181         return _PyStatus_OK();
1182     }
1183 
1184     // Initialize the import-related configuration.
1185     status = _PyConfig_InitImportConfig(&interp->config);
1186     if (_PyStatus_EXCEPTION(status)) {
1187         return status;
1188     }
1189 
1190     if (interpreter_update_config(tstate, 1) < 0) {
1191         return _PyStatus_ERR("failed to update the Python config");
1192     }
1193 
1194     status = _PyImport_InitExternal(tstate);
1195     if (_PyStatus_EXCEPTION(status)) {
1196         return status;
1197     }
1198 
1199     if (is_main_interp) {
1200         /* initialize the faulthandler module */
1201         status = _PyFaulthandler_Init(config->faulthandler);
1202         if (_PyStatus_EXCEPTION(status)) {
1203             return status;
1204         }
1205     }
1206 
1207     status = _PyUnicode_InitEncodings(tstate);
1208     if (_PyStatus_EXCEPTION(status)) {
1209         return status;
1210     }
1211 
1212     if (is_main_interp) {
1213         if (_PySignal_Init(config->install_signal_handlers) < 0) {
1214             return _PyStatus_ERR("can't initialize signals");
1215         }
1216 
1217         if (config->tracemalloc) {
1218            if (_PyTraceMalloc_Start(config->tracemalloc) < 0) {
1219                 return _PyStatus_ERR("can't start tracemalloc");
1220             }
1221         }
1222 
1223 #ifdef PY_HAVE_PERF_TRAMPOLINE
1224         if (config->perf_profiling) {
1225             _PyPerf_Callbacks *cur_cb;
1226             if (config->perf_profiling == 1) {
1227                 cur_cb = &_Py_perfmap_callbacks;
1228             }
1229             else {
1230                 cur_cb = &_Py_perfmap_jit_callbacks;
1231             }
1232             if (_PyPerfTrampoline_SetCallbacks(cur_cb) < 0 ||
1233                     _PyPerfTrampoline_Init(config->perf_profiling) < 0) {
1234                 return _PyStatus_ERR("can't initialize the perf trampoline");
1235             }
1236         }
1237 #endif
1238     }
1239 
1240     status = init_sys_streams(tstate);
1241     if (_PyStatus_EXCEPTION(status)) {
1242         return status;
1243     }
1244 
1245     status = init_set_builtins_open();
1246     if (_PyStatus_EXCEPTION(status)) {
1247         return status;
1248     }
1249 
1250 #ifdef __ANDROID__
1251     status = init_android_streams(tstate);
1252     if (_PyStatus_EXCEPTION(status)) {
1253         return status;
1254     }
1255 #endif
1256 
1257 #ifdef Py_DEBUG
1258     run_presite(tstate);
1259 #endif
1260 
1261     status = add_main_module(interp);
1262     if (_PyStatus_EXCEPTION(status)) {
1263         return status;
1264     }
1265 
1266     if (is_main_interp) {
1267         /* Initialize warnings. */
1268         PyObject *warnoptions = PySys_GetObject("warnoptions");
1269         if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
1270         {
1271             PyObject *warnings_module = PyImport_ImportModule("warnings");
1272             if (warnings_module == NULL) {
1273                 fprintf(stderr, "'import warnings' failed; traceback:\n");
1274                 _PyErr_Print(tstate);
1275             }
1276             Py_XDECREF(warnings_module);
1277         }
1278 
1279         interp->runtime->initialized = 1;
1280     }
1281 
1282     if (config->site_import) {
1283         status = init_import_site();
1284         if (_PyStatus_EXCEPTION(status)) {
1285             return status;
1286         }
1287     }
1288 
1289     if (is_main_interp) {
1290 #ifndef MS_WINDOWS
1291         emit_stderr_warning_for_legacy_locale(interp->runtime);
1292 #endif
1293     }
1294 
1295     // Turn on experimental tier 2 (uops-based) optimizer
1296     // This is also needed when the JIT is enabled
1297 #ifdef _Py_TIER2
1298     if (is_main_interp) {
1299         int enabled = 1;
1300 #if _Py_TIER2 & 2
1301         enabled = 0;
1302 #endif
1303         char *env = Py_GETENV("PYTHON_JIT");
1304         if (env && *env != '\0') {
1305             // PYTHON_JIT=0|1 overrides the default
1306             enabled = *env != '0';
1307         }
1308         if (enabled) {
1309             PyObject *opt = _PyOptimizer_NewUOpOptimizer();
1310             if (opt == NULL) {
1311                 return _PyStatus_ERR("can't initialize optimizer");
1312             }
1313             if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) {
1314                 return _PyStatus_ERR("can't install optimizer");
1315             }
1316             Py_DECREF(opt);
1317         }
1318     }
1319 #endif
1320 
1321     if (!is_main_interp) {
1322         // The main interpreter is handled in Py_Main(), for now.
1323         if (config->sys_path_0 != NULL) {
1324             PyObject *path0 = PyUnicode_FromWideChar(config->sys_path_0, -1);
1325             if (path0 == NULL) {
1326                 return _PyStatus_ERR("can't initialize sys.path[0]");
1327             }
1328             PyObject *sysdict = interp->sysdict;
1329             if (sysdict == NULL) {
1330                 Py_DECREF(path0);
1331                 return _PyStatus_ERR("can't initialize sys.path[0]");
1332             }
1333             PyObject *sys_path = PyDict_GetItemWithError(sysdict, &_Py_ID(path));
1334             if (sys_path == NULL) {
1335                 Py_DECREF(path0);
1336                 return _PyStatus_ERR("can't initialize sys.path[0]");
1337             }
1338             int res = PyList_Insert(sys_path, 0, path0);
1339             Py_DECREF(path0);
1340             if (res) {
1341                 return _PyStatus_ERR("can't initialize sys.path[0]");
1342             }
1343         }
1344     }
1345 
1346 
1347     interp->dict_state.watchers[0] = &builtins_dict_watcher;
1348     if (PyDict_Watch(0, interp->builtins) != 0) {
1349         return _PyStatus_ERR("failed to set builtin dict watcher");
1350     }
1351 
1352     assert(!_PyErr_Occurred(tstate));
1353 
1354     return _PyStatus_OK();
1355 }
1356 
1357 
1358 /* Update interpreter state based on supplied configuration settings
1359  *
1360  * After calling this function, most of the restrictions on the interpreter
1361  * are lifted. The only remaining incomplete settings are those related
1362  * to the main module (sys.argv[0], __main__ metadata)
1363  *
1364  * Calling this when the interpreter is not initializing, is already
1365  * initialized or without a valid current thread state is a fatal error.
1366  * Other errors should be reported as normal Python exceptions with a
1367  * non-zero return code.
1368  */
1369 static PyStatus
pyinit_main(PyThreadState * tstate)1370 pyinit_main(PyThreadState *tstate)
1371 {
1372     PyInterpreterState *interp = tstate->interp;
1373     if (!interp->runtime->core_initialized) {
1374         return _PyStatus_ERR("runtime core not initialized");
1375     }
1376 
1377     if (interp->runtime->initialized) {
1378         return pyinit_main_reconfigure(tstate);
1379     }
1380 
1381     PyStatus status = init_interp_main(tstate);
1382     if (_PyStatus_EXCEPTION(status)) {
1383         return status;
1384     }
1385     return _PyStatus_OK();
1386 }
1387 
1388 
1389 PyStatus
Py_InitializeFromConfig(const PyConfig * config)1390 Py_InitializeFromConfig(const PyConfig *config)
1391 {
1392     if (config == NULL) {
1393         return _PyStatus_ERR("initialization config is NULL");
1394     }
1395 
1396     PyStatus status;
1397 
1398     status = _PyRuntime_Initialize();
1399     if (_PyStatus_EXCEPTION(status)) {
1400         return status;
1401     }
1402     _PyRuntimeState *runtime = &_PyRuntime;
1403 
1404     PyThreadState *tstate = NULL;
1405     status = pyinit_core(runtime, config, &tstate);
1406     if (_PyStatus_EXCEPTION(status)) {
1407         return status;
1408     }
1409     config = _PyInterpreterState_GetConfig(tstate->interp);
1410 
1411     if (config->_init_main) {
1412         status = pyinit_main(tstate);
1413         if (_PyStatus_EXCEPTION(status)) {
1414             return status;
1415         }
1416     }
1417 
1418     return _PyStatus_OK();
1419 }
1420 
1421 
1422 void
Py_InitializeEx(int install_sigs)1423 Py_InitializeEx(int install_sigs)
1424 {
1425     PyStatus status;
1426 
1427     status = _PyRuntime_Initialize();
1428     if (_PyStatus_EXCEPTION(status)) {
1429         Py_ExitStatusException(status);
1430     }
1431     _PyRuntimeState *runtime = &_PyRuntime;
1432 
1433     if (runtime->initialized) {
1434         /* bpo-33932: Calling Py_Initialize() twice does nothing. */
1435         return;
1436     }
1437 
1438     PyConfig config;
1439     _PyConfig_InitCompatConfig(&config);
1440 
1441     config.install_signal_handlers = install_sigs;
1442 
1443     status = Py_InitializeFromConfig(&config);
1444     PyConfig_Clear(&config);
1445     if (_PyStatus_EXCEPTION(status)) {
1446         Py_ExitStatusException(status);
1447     }
1448 }
1449 
1450 void
Py_Initialize(void)1451 Py_Initialize(void)
1452 {
1453     Py_InitializeEx(1);
1454 }
1455 
1456 
1457 PyStatus
_Py_InitializeMain(void)1458 _Py_InitializeMain(void)
1459 {
1460     PyStatus status = _PyRuntime_Initialize();
1461     if (_PyStatus_EXCEPTION(status)) {
1462         return status;
1463     }
1464     PyThreadState *tstate = _PyThreadState_GET();
1465     return pyinit_main(tstate);
1466 }
1467 
1468 
1469 static void
finalize_modules_delete_special(PyThreadState * tstate,int verbose)1470 finalize_modules_delete_special(PyThreadState *tstate, int verbose)
1471 {
1472     // List of names to clear in sys
1473     static const char * const sys_deletes[] = {
1474         "path", "argv", "ps1", "ps2", "last_exc",
1475         "last_type", "last_value", "last_traceback",
1476         "__interactivehook__",
1477         // path_hooks and path_importer_cache are cleared
1478         // by _PyImport_FiniExternal().
1479         // XXX Clear meta_path in _PyImport_FiniCore().
1480         "meta_path",
1481         NULL
1482     };
1483 
1484     static const char * const sys_files[] = {
1485         "stdin", "__stdin__",
1486         "stdout", "__stdout__",
1487         "stderr", "__stderr__",
1488         NULL
1489     };
1490 
1491     PyInterpreterState *interp = tstate->interp;
1492     if (verbose) {
1493         PySys_WriteStderr("# clear builtins._\n");
1494     }
1495     if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
1496         PyErr_FormatUnraisable("Exception ignored on setting builtin variable _");
1497     }
1498 
1499     const char * const *p;
1500     for (p = sys_deletes; *p != NULL; p++) {
1501         if (_PySys_ClearAttrString(interp, *p, verbose) < 0) {
1502             PyErr_FormatUnraisable("Exception ignored on clearing sys.%s", *p);
1503         }
1504     }
1505     for (p = sys_files; *p != NULL; p+=2) {
1506         const char *name = p[0];
1507         const char *orig_name = p[1];
1508         if (verbose) {
1509             PySys_WriteStderr("# restore sys.%s\n", name);
1510         }
1511         PyObject *value;
1512         if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) {
1513             PyErr_FormatUnraisable("Exception ignored on restoring sys.%s", name);
1514         }
1515         if (value == NULL) {
1516             value = Py_NewRef(Py_None);
1517         }
1518         if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
1519             PyErr_FormatUnraisable("Exception ignored on restoring sys.%s", name);
1520         }
1521         Py_DECREF(value);
1522     }
1523 }
1524 
1525 
1526 static PyObject*
finalize_remove_modules(PyObject * modules,int verbose)1527 finalize_remove_modules(PyObject *modules, int verbose)
1528 {
1529     PyObject *weaklist = PyList_New(0);
1530     if (weaklist == NULL) {
1531         PyErr_FormatUnraisable("Exception ignored on removing modules");
1532     }
1533 
1534 #define STORE_MODULE_WEAKREF(name, mod) \
1535         if (weaklist != NULL) { \
1536             PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1537             if (wr) { \
1538                 PyObject *tup = PyTuple_Pack(2, name, wr); \
1539                 if (!tup || PyList_Append(weaklist, tup) < 0) { \
1540                     PyErr_FormatUnraisable("Exception ignored on removing modules"); \
1541                 } \
1542                 Py_XDECREF(tup); \
1543                 Py_DECREF(wr); \
1544             } \
1545             else { \
1546                 PyErr_FormatUnraisable("Exception ignored on removing modules"); \
1547             } \
1548         }
1549 
1550 #define CLEAR_MODULE(name, mod) \
1551         if (PyModule_Check(mod)) { \
1552             if (verbose && PyUnicode_Check(name)) { \
1553                 PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
1554             } \
1555             STORE_MODULE_WEAKREF(name, mod); \
1556             if (PyObject_SetItem(modules, name, Py_None) < 0) { \
1557                 PyErr_FormatUnraisable("Exception ignored on removing modules"); \
1558             } \
1559         }
1560 
1561     if (PyDict_CheckExact(modules)) {
1562         Py_ssize_t pos = 0;
1563         PyObject *key, *value;
1564         while (PyDict_Next(modules, &pos, &key, &value)) {
1565             CLEAR_MODULE(key, value);
1566         }
1567     }
1568     else {
1569         PyObject *iterator = PyObject_GetIter(modules);
1570         if (iterator == NULL) {
1571             PyErr_FormatUnraisable("Exception ignored on removing modules");
1572         }
1573         else {
1574             PyObject *key;
1575             while ((key = PyIter_Next(iterator))) {
1576                 PyObject *value = PyObject_GetItem(modules, key);
1577                 if (value == NULL) {
1578                     PyErr_FormatUnraisable("Exception ignored on removing modules");
1579                     continue;
1580                 }
1581                 CLEAR_MODULE(key, value);
1582                 Py_DECREF(value);
1583                 Py_DECREF(key);
1584             }
1585             if (PyErr_Occurred()) {
1586                 PyErr_FormatUnraisable("Exception ignored on removing modules");
1587             }
1588             Py_DECREF(iterator);
1589         }
1590     }
1591 #undef CLEAR_MODULE
1592 #undef STORE_MODULE_WEAKREF
1593 
1594     return weaklist;
1595 }
1596 
1597 
1598 static void
finalize_clear_modules_dict(PyObject * modules)1599 finalize_clear_modules_dict(PyObject *modules)
1600 {
1601     if (PyDict_CheckExact(modules)) {
1602         PyDict_Clear(modules);
1603     }
1604     else {
1605         if (PyObject_CallMethodNoArgs(modules, &_Py_ID(clear)) == NULL) {
1606             PyErr_FormatUnraisable("Exception ignored on clearing sys.modules");
1607         }
1608     }
1609 }
1610 
1611 
1612 static void
finalize_restore_builtins(PyThreadState * tstate)1613 finalize_restore_builtins(PyThreadState *tstate)
1614 {
1615     PyInterpreterState *interp = tstate->interp;
1616     PyObject *dict = PyDict_Copy(interp->builtins);
1617     if (dict == NULL) {
1618         PyErr_FormatUnraisable("Exception ignored on restoring builtins");
1619     }
1620     PyDict_Clear(interp->builtins);
1621     if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
1622         PyErr_FormatUnraisable("Exception ignored on restoring builtins");
1623     }
1624     Py_XDECREF(dict);
1625 }
1626 
1627 
1628 static void
finalize_modules_clear_weaklist(PyInterpreterState * interp,PyObject * weaklist,int verbose)1629 finalize_modules_clear_weaklist(PyInterpreterState *interp,
1630                                 PyObject *weaklist, int verbose)
1631 {
1632     // First clear modules imported later
1633     for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
1634         PyObject *tup = PyList_GET_ITEM(weaklist, i);
1635         PyObject *name = PyTuple_GET_ITEM(tup, 0);
1636         PyObject *mod = _PyWeakref_GET_REF(PyTuple_GET_ITEM(tup, 1));
1637         if (mod == NULL) {
1638             continue;
1639         }
1640         assert(PyModule_Check(mod));
1641         PyObject *dict = _PyModule_GetDict(mod);  // borrowed reference
1642         if (dict == interp->builtins || dict == interp->sysdict) {
1643             Py_DECREF(mod);
1644             continue;
1645         }
1646         if (verbose && PyUnicode_Check(name)) {
1647             PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
1648         }
1649         _PyModule_Clear(mod);
1650         Py_DECREF(mod);
1651     }
1652 }
1653 
1654 
1655 static void
finalize_clear_sys_builtins_dict(PyInterpreterState * interp,int verbose)1656 finalize_clear_sys_builtins_dict(PyInterpreterState *interp, int verbose)
1657 {
1658     // Clear sys dict
1659     if (verbose) {
1660         PySys_FormatStderr("# cleanup[3] wiping sys\n");
1661     }
1662     _PyModule_ClearDict(interp->sysdict);
1663 
1664     // Clear builtins dict
1665     if (verbose) {
1666         PySys_FormatStderr("# cleanup[3] wiping builtins\n");
1667     }
1668     _PyModule_ClearDict(interp->builtins);
1669 }
1670 
1671 
1672 /* Clear modules, as good as we can */
1673 // XXX Move most of this to import.c.
1674 static void
finalize_modules(PyThreadState * tstate)1675 finalize_modules(PyThreadState *tstate)
1676 {
1677     PyInterpreterState *interp = tstate->interp;
1678 
1679 #ifdef _Py_TIER2
1680     // Invalidate all executors and turn off tier 2 optimizer
1681     _Py_Executors_InvalidateAll(interp, 0);
1682     _PyOptimizerObject *old = _Py_SetOptimizer(interp, NULL);
1683     Py_XDECREF(old);
1684 #endif
1685 
1686     // Stop watching __builtin__ modifications
1687     PyDict_Unwatch(0, interp->builtins);
1688 
1689     PyObject *modules = _PyImport_GetModules(interp);
1690     if (modules == NULL) {
1691         // Already done
1692         return;
1693     }
1694     int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
1695 
1696     // Delete some special builtins._ and sys attributes first.  These are
1697     // common places where user values hide and people complain when their
1698     // destructors fail.  Since the modules containing them are
1699     // deleted *last* of all, they would come too late in the normal
1700     // destruction order.  Sigh.
1701     //
1702     // XXX Perhaps these precautions are obsolete. Who knows?
1703     finalize_modules_delete_special(tstate, verbose);
1704 
1705     // Remove all modules from sys.modules, hoping that garbage collection
1706     // can reclaim most of them: set all sys.modules values to None.
1707     //
1708     // We prepare a list which will receive (name, weakref) tuples of
1709     // modules when they are removed from sys.modules.  The name is used
1710     // for diagnosis messages (in verbose mode), while the weakref helps
1711     // detect those modules which have been held alive.
1712     PyObject *weaklist = finalize_remove_modules(modules, verbose);
1713 
1714     // Clear the modules dict
1715     finalize_clear_modules_dict(modules);
1716 
1717     // Restore the original builtins dict, to ensure that any
1718     // user data gets cleared.
1719     finalize_restore_builtins(tstate);
1720 
1721     // Collect garbage
1722     _PyGC_CollectNoFail(tstate);
1723 
1724     // Dump GC stats before it's too late, since it uses the warnings
1725     // machinery.
1726     _PyGC_DumpShutdownStats(interp);
1727 
1728     if (weaklist != NULL) {
1729         // Now, if there are any modules left alive, clear their globals to
1730         // minimize potential leaks.  All C extension modules actually end
1731         // up here, since they are kept alive in the interpreter state.
1732         //
1733         // The special treatment of "builtins" here is because even
1734         // when it's not referenced as a module, its dictionary is
1735         // referenced by almost every module's __builtins__.  Since
1736         // deleting a module clears its dictionary (even if there are
1737         // references left to it), we need to delete the "builtins"
1738         // module last.  Likewise, we don't delete sys until the very
1739         // end because it is implicitly referenced (e.g. by print).
1740         //
1741         // Since dict is ordered in CPython 3.6+, modules are saved in
1742         // importing order.  First clear modules imported later.
1743         finalize_modules_clear_weaklist(interp, weaklist, verbose);
1744         Py_DECREF(weaklist);
1745     }
1746 
1747     // Clear sys and builtins modules dict
1748     finalize_clear_sys_builtins_dict(interp, verbose);
1749 
1750     // Clear module dict copies stored in the interpreter state:
1751     // clear PyInterpreterState.modules_by_index and
1752     // clear PyModuleDef.m_base.m_copy (of extensions not using the multi-phase
1753     // initialization API)
1754     _PyImport_ClearModulesByIndex(interp);
1755 
1756     // Clear and delete the modules directory.  Actual modules will
1757     // still be there only if imported during the execution of some
1758     // destructor.
1759     _PyImport_ClearModules(interp);
1760 
1761     // Collect garbage once more
1762     _PyGC_CollectNoFail(tstate);
1763 }
1764 
1765 
1766 /* Flush stdout and stderr */
1767 
1768 static int
file_is_closed(PyObject * fobj)1769 file_is_closed(PyObject *fobj)
1770 {
1771     int r;
1772     PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
1773     if (tmp == NULL) {
1774         PyErr_Clear();
1775         return 0;
1776     }
1777     r = PyObject_IsTrue(tmp);
1778     Py_DECREF(tmp);
1779     if (r < 0)
1780         PyErr_Clear();
1781     return r > 0;
1782 }
1783 
1784 
1785 static int
flush_std_files(void)1786 flush_std_files(void)
1787 {
1788     PyThreadState *tstate = _PyThreadState_GET();
1789     PyObject *fout = _PySys_GetAttr(tstate, &_Py_ID(stdout));
1790     PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
1791     int status = 0;
1792 
1793     if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
1794         if (_PyFile_Flush(fout) < 0) {
1795             PyErr_FormatUnraisable("Exception ignored on flushing sys.stdout");
1796             status = -1;
1797         }
1798     }
1799 
1800     if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
1801         if (_PyFile_Flush(ferr) < 0) {
1802             PyErr_Clear();
1803             status = -1;
1804         }
1805     }
1806 
1807     return status;
1808 }
1809 
1810 /* Undo the effect of Py_Initialize().
1811 
1812    Beware: if multiple interpreter and/or thread states exist, these
1813    are not wiped out; only the current thread and interpreter state
1814    are deleted.  But since everything else is deleted, those other
1815    interpreter and thread states should no longer be used.
1816 
1817    (XXX We should do better, e.g. wipe out all interpreters and
1818    threads.)
1819 
1820    Locking: as above.
1821 
1822 */
1823 
1824 
1825 static void
finalize_interp_types(PyInterpreterState * interp)1826 finalize_interp_types(PyInterpreterState *interp)
1827 {
1828     _PyTypes_FiniExtTypes(interp);
1829     _PyUnicode_FiniTypes(interp);
1830     _PySys_FiniTypes(interp);
1831     _PyXI_FiniTypes(interp);
1832     _PyExc_Fini(interp);
1833     _PyFloat_FiniType(interp);
1834     _PyLong_FiniTypes(interp);
1835     _PyThread_FiniType(interp);
1836     // XXX fini collections module static types (_PyStaticType_Dealloc())
1837     // XXX fini IO module static types (_PyStaticType_Dealloc())
1838     _PyErr_FiniTypes(interp);
1839     _PyTypes_FiniTypes(interp);
1840 
1841     _PyTypes_Fini(interp);
1842 
1843     _PyCode_Fini(interp);
1844 
1845     // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
1846     // a dict internally.
1847     _PyUnicode_ClearInterned(interp);
1848 
1849     _PyUnicode_Fini(interp);
1850 
1851 #ifndef Py_GIL_DISABLED
1852     // With Py_GIL_DISABLED:
1853     // the freelists for the current thread state have already been cleared.
1854     struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
1855     _PyObject_ClearFreeLists(freelists, 1);
1856 #endif
1857 
1858 #ifdef Py_DEBUG
1859     _PyStaticObjects_CheckRefcnt(interp);
1860 #endif
1861 }
1862 
1863 
1864 static void
finalize_interp_clear(PyThreadState * tstate)1865 finalize_interp_clear(PyThreadState *tstate)
1866 {
1867     int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
1868 
1869     _PyXI_Fini(tstate->interp);
1870     _PyExc_ClearExceptionGroupType(tstate->interp);
1871     _Py_clear_generic_types(tstate->interp);
1872 
1873     /* Clear interpreter state and all thread states */
1874     _PyInterpreterState_Clear(tstate);
1875 
1876     /* Clear all loghooks */
1877     /* Both _PySys_Audit function and users still need PyObject, such as tuple.
1878        Call _PySys_ClearAuditHooks when PyObject available. */
1879     if (is_main_interp) {
1880         _PySys_ClearAuditHooks(tstate);
1881     }
1882 
1883     if (is_main_interp) {
1884         _Py_HashRandomization_Fini();
1885         _PyArg_Fini();
1886         _Py_ClearFileSystemEncoding();
1887         _PyPerfTrampoline_Fini();
1888         _PyPerfTrampoline_FreeArenas();
1889     }
1890 
1891     finalize_interp_types(tstate->interp);
1892 
1893     /* Finalize dtoa at last so that finalizers calling repr of float doesn't crash */
1894     _PyDtoa_Fini(tstate->interp);
1895 
1896     /* Free any delayed free requests immediately */
1897     _PyMem_FiniDelayed(tstate->interp);
1898 
1899     /* finalize_interp_types may allocate Python objects so we may need to
1900        abandon mimalloc segments again */
1901     _PyThreadState_ClearMimallocHeaps(tstate);
1902 }
1903 
1904 
1905 static void
finalize_interp_delete(PyInterpreterState * interp)1906 finalize_interp_delete(PyInterpreterState *interp)
1907 {
1908     /* Cleanup auto-thread-state */
1909     _PyGILState_Fini(interp);
1910 
1911     /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
1912        fail when it is being awaited by another running daemon thread (see
1913        bpo-9901). Instead pycore_create_interpreter() destroys the previously
1914        created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
1915        called multiple times. */
1916 
1917     PyInterpreterState_Delete(interp);
1918 }
1919 
1920 
1921 /* Conceptually, there isn't a good reason for Py_Finalize()
1922    to be called in any other thread than the one where Py_Initialize()
1923    was called.  Consequently, it would make sense to fail if the thread
1924    or thread state (or interpreter) don't match.  However, such
1925    constraints have never been enforced, and, as unlikely as it may be,
1926    there may be users relying on the unconstrained behavior.  Thus,
1927    we do our best here to accommodate that possibility. */
1928 
1929 static PyThreadState *
resolve_final_tstate(_PyRuntimeState * runtime)1930 resolve_final_tstate(_PyRuntimeState *runtime)
1931 {
1932     PyThreadState *main_tstate = runtime->main_tstate;
1933     assert(main_tstate != NULL);
1934     assert(main_tstate->thread_id == runtime->main_thread);
1935     PyInterpreterState *main_interp = _PyInterpreterState_Main();
1936     assert(main_tstate->interp == main_interp);
1937 
1938     PyThreadState *tstate = _PyThreadState_GET();
1939     if (_Py_IsMainThread()) {
1940         if (tstate != main_tstate) {
1941             /* This implies that Py_Finalize() was called while
1942                a non-main interpreter was active or while the main
1943                tstate was temporarily swapped out with another.
1944                Neither case should be allowed, but, until we get around
1945                to fixing that (and Py_Exit()), we're letting it go. */
1946             (void)PyThreadState_Swap(main_tstate);
1947         }
1948     }
1949     else {
1950         /* This is another unfortunate case where Py_Finalize() was
1951            called when it shouldn't have been.  We can't simply switch
1952            over to the main thread.  At the least, however, we can make
1953            sure the main interpreter is active. */
1954         if (!_Py_IsMainInterpreter(tstate->interp)) {
1955             /* We don't go to the trouble of updating runtime->main_tstate
1956                since it will be dead soon anyway. */
1957             main_tstate =
1958                 _PyThreadState_New(main_interp, _PyThreadState_WHENCE_FINI);
1959             if (main_tstate != NULL) {
1960                 _PyThreadState_Bind(main_tstate);
1961                 (void)PyThreadState_Swap(main_tstate);
1962             }
1963             else {
1964                 /* Fall back to the current tstate.  It's better than nothing. */
1965                 main_tstate = tstate;
1966             }
1967         }
1968     }
1969     assert(main_tstate != NULL);
1970 
1971     /* We might want to warn if main_tstate->current_frame != NULL. */
1972 
1973     return main_tstate;
1974 }
1975 
1976 static int
_Py_Finalize(_PyRuntimeState * runtime)1977 _Py_Finalize(_PyRuntimeState *runtime)
1978 {
1979     int status = 0;
1980 
1981     /* Bail out early if already finalized (or never initialized). */
1982     if (!runtime->initialized) {
1983         return status;
1984     }
1985 
1986     /* Get final thread state pointer. */
1987     PyThreadState *tstate = resolve_final_tstate(runtime);
1988 
1989     // Block some operations.
1990     tstate->interp->finalizing = 1;
1991 
1992     // Wrap up existing "threading"-module-created, non-daemon threads.
1993     wait_for_thread_shutdown(tstate);
1994 
1995     // Make any remaining pending calls.
1996     _Py_FinishPendingCalls(tstate);
1997 
1998     /* The interpreter is still entirely intact at this point, and the
1999      * exit funcs may be relying on that.  In particular, if some thread
2000      * or exit func is still waiting to do an import, the import machinery
2001      * expects Py_IsInitialized() to return true.  So don't say the
2002      * runtime is uninitialized until after the exit funcs have run.
2003      * Note that Threading.py uses an exit func to do a join on all the
2004      * threads created thru it, so this also protects pending imports in
2005      * the threads created via Threading.
2006      */
2007 
2008     _PyAtExit_Call(tstate->interp);
2009 
2010     assert(_PyThreadState_GET() == tstate);
2011 
2012     /* Copy the core config, PyInterpreterState_Delete() free
2013        the core config memory */
2014 #ifdef Py_REF_DEBUG
2015     int show_ref_count = tstate->interp->config.show_ref_count;
2016 #endif
2017 #ifdef Py_TRACE_REFS
2018     int dump_refs = tstate->interp->config.dump_refs;
2019     wchar_t *dump_refs_file = tstate->interp->config.dump_refs_file;
2020 #endif
2021 #ifdef WITH_PYMALLOC
2022     int malloc_stats = tstate->interp->config.malloc_stats;
2023 #endif
2024 
2025     /* Ensure that remaining threads are detached */
2026     _PyEval_StopTheWorldAll(runtime);
2027 
2028     /* Remaining daemon threads will automatically exit
2029        when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
2030     _PyInterpreterState_SetFinalizing(tstate->interp, tstate);
2031     _PyRuntimeState_SetFinalizing(runtime, tstate);
2032     runtime->initialized = 0;
2033     runtime->core_initialized = 0;
2034 
2035     // XXX Call something like _PyImport_Disable() here?
2036 
2037     /* Destroy the state of all threads of the interpreter, except of the
2038        current thread. In practice, only daemon threads should still be alive,
2039        except if wait_for_thread_shutdown() has been cancelled by CTRL+C.
2040        Clear frames of other threads to call objects destructors. Destructors
2041        will be called in the current Python thread. Since
2042        _PyRuntimeState_SetFinalizing() has been called, no other Python thread
2043        can take the GIL at this point: if they try, they will exit
2044        immediately. We start the world once we are the only thread state left,
2045        before we call destructors. */
2046     PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
2047     _PyEval_StartTheWorldAll(runtime);
2048     _PyThreadState_DeleteList(list);
2049 
2050     /* At this point no Python code should be running at all.
2051        The only thread state left should be the main thread of the main
2052        interpreter (AKA tstate), in which this code is running right now.
2053        There may be other OS threads running but none of them will have
2054        thread states associated with them, nor will be able to create
2055        new thread states.
2056 
2057        Thus tstate is the only possible thread state from here on out.
2058        It may still be used during finalization to run Python code as
2059        needed or provide runtime state (e.g. sys.modules) but that will
2060        happen sparingly.  Furthermore, the order of finalization aims
2061        to not need a thread (or interpreter) state as soon as possible.
2062      */
2063     // XXX Make sure we are preventing the creating of any new thread states
2064     // (or interpreters).
2065 
2066     /* Flush sys.stdout and sys.stderr */
2067     if (flush_std_files() < 0) {
2068         status = -1;
2069     }
2070 
2071     /* Disable signal handling */
2072     _PySignal_Fini();
2073 
2074     /* Collect garbage.  This may call finalizers; it's nice to call these
2075      * before all modules are destroyed.
2076      * XXX If a __del__ or weakref callback is triggered here, and tries to
2077      * XXX import a module, bad things can happen, because Python no
2078      * XXX longer believes it's initialized.
2079      * XXX     Fatal Python error: Interpreter not initialized (version mismatch?)
2080      * XXX is easy to provoke that way.  I've also seen, e.g.,
2081      * XXX     Exception exceptions.ImportError: 'No module named sha'
2082      * XXX         in <function callback at 0x008F5718> ignored
2083      * XXX but I'm unclear on exactly how that one happens.  In any case,
2084      * XXX I haven't seen a real-life report of either of these.
2085      */
2086     PyGC_Collect();
2087 
2088     /* Destroy all modules */
2089     _PyImport_FiniExternal(tstate->interp);
2090     finalize_modules(tstate);
2091 
2092     /* Clean up any lingering subinterpreters. */
2093     finalize_subinterpreters();
2094 
2095     /* Print debug stats if any */
2096     _PyEval_Fini();
2097 
2098     /* Flush sys.stdout and sys.stderr (again, in case more was printed) */
2099     if (flush_std_files() < 0) {
2100         status = -1;
2101     }
2102 
2103     /* Collect final garbage.  This disposes of cycles created by
2104      * class definitions, for example.
2105      * XXX This is disabled because it caused too many problems.  If
2106      * XXX a __del__ or weakref callback triggers here, Python code has
2107      * XXX a hard time running, because even the sys module has been
2108      * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
2109      * XXX One symptom is a sequence of information-free messages
2110      * XXX coming from threads (if a __del__ or callback is invoked,
2111      * XXX other threads can execute too, and any exception they encounter
2112      * XXX triggers a comedy of errors as subsystem after subsystem
2113      * XXX fails to find what it *expects* to find in sys to help report
2114      * XXX the exception and consequent unexpected failures).  I've also
2115      * XXX seen segfaults then, after adding print statements to the
2116      * XXX Python code getting called.
2117      */
2118 #if 0
2119     _PyGC_CollectIfEnabled();
2120 #endif
2121 
2122     /* Disable tracemalloc after all Python objects have been destroyed,
2123        so it is possible to use tracemalloc in objects destructor. */
2124     _PyTraceMalloc_Fini();
2125 
2126     /* Finalize any remaining import state */
2127     // XXX Move these up to where finalize_modules() is currently.
2128     _PyImport_FiniCore(tstate->interp);
2129     _PyImport_Fini();
2130 
2131     /* unload faulthandler module */
2132     _PyFaulthandler_Fini();
2133 
2134     /* dump hash stats */
2135     _PyHash_Fini();
2136 
2137 #ifdef Py_TRACE_REFS
2138     /* Display all objects still alive -- this can invoke arbitrary
2139      * __repr__ overrides, so requires a mostly-intact interpreter.
2140      * Alas, a lot of stuff may still be alive now that will be cleaned
2141      * up later.
2142      */
2143 
2144     FILE *dump_refs_fp = NULL;
2145     if (dump_refs_file != NULL) {
2146         dump_refs_fp = _Py_wfopen(dump_refs_file, L"w");
2147         if (dump_refs_fp == NULL) {
2148             fprintf(stderr, "PYTHONDUMPREFSFILE: cannot create file: %ls\n", dump_refs_file);
2149         }
2150     }
2151 
2152     if (dump_refs) {
2153         _Py_PrintReferences(tstate->interp, stderr);
2154     }
2155 
2156     if (dump_refs_fp != NULL) {
2157         _Py_PrintReferences(tstate->interp, dump_refs_fp);
2158     }
2159 #endif /* Py_TRACE_REFS */
2160 
2161     /* At this point there's almost no other Python code that will run,
2162        nor interpreter state needed.  The only possibility is the
2163        finalizers of the objects stored on tstate (and tstate->interp),
2164        which are triggered via finalize_interp_clear().
2165 
2166        For now we operate as though none of those finalizers actually
2167        need an operational thread state or interpreter.  In reality,
2168        those finalizers may rely on some part of tstate or
2169        tstate->interp, and/or may raise exceptions
2170        or otherwise fail.
2171      */
2172     // XXX Do this sooner during finalization.
2173     // XXX Ensure finalizer errors are handled properly.
2174 
2175     finalize_interp_clear(tstate);
2176 
2177 #ifdef Py_TRACE_REFS
2178     /* Display addresses (& refcnts) of all objects still alive.
2179      * An address can be used to find the repr of the object, printed
2180      * above by _Py_PrintReferences. */
2181     if (dump_refs) {
2182         _Py_PrintReferenceAddresses(tstate->interp, stderr);
2183     }
2184     if (dump_refs_fp != NULL) {
2185         _Py_PrintReferenceAddresses(tstate->interp, dump_refs_fp);
2186         fclose(dump_refs_fp);
2187     }
2188 #endif /* Py_TRACE_REFS */
2189 
2190 #ifdef WITH_PYMALLOC
2191     if (malloc_stats) {
2192         _PyObject_DebugMallocStats(stderr);
2193     }
2194 #endif
2195 
2196     finalize_interp_delete(tstate->interp);
2197 
2198 #ifdef Py_REF_DEBUG
2199     if (show_ref_count) {
2200         _PyDebug_PrintTotalRefs();
2201     }
2202     _Py_FinalizeRefTotal(runtime);
2203 #endif
2204     _Py_FinalizeAllocatedBlocks(runtime);
2205 
2206     call_ll_exitfuncs(runtime);
2207 
2208     _PyRuntime_Finalize();
2209     return status;
2210 }
2211 
2212 int
Py_FinalizeEx(void)2213 Py_FinalizeEx(void)
2214 {
2215     return _Py_Finalize(&_PyRuntime);
2216 }
2217 
2218 void
Py_Finalize(void)2219 Py_Finalize(void)
2220 {
2221     (void)_Py_Finalize(&_PyRuntime);
2222 }
2223 
2224 
2225 /* Create and initialize a new interpreter and thread, and return the
2226    new thread.  This requires that Py_Initialize() has been called
2227    first.
2228 
2229    Unsuccessful initialization yields a NULL pointer.  Note that *no*
2230    exception information is available even in this case -- the
2231    exception information is held in the thread, and there is no
2232    thread.
2233 
2234    Locking: as above.
2235 
2236 */
2237 
2238 static PyStatus
new_interpreter(PyThreadState ** tstate_p,const PyInterpreterConfig * config,long whence)2239 new_interpreter(PyThreadState **tstate_p,
2240                 const PyInterpreterConfig *config, long whence)
2241 {
2242     PyStatus status;
2243 
2244     status = _PyRuntime_Initialize();
2245     if (_PyStatus_EXCEPTION(status)) {
2246         return status;
2247     }
2248     _PyRuntimeState *runtime = &_PyRuntime;
2249 
2250     if (!runtime->initialized) {
2251         return _PyStatus_ERR("Py_Initialize must be called first");
2252     }
2253 
2254     /* Issue #10915, #15751: The GIL API doesn't work with multiple
2255        interpreters: disable PyGILState_Check(). */
2256     runtime->gilstate.check_enabled = 0;
2257 
2258     PyInterpreterState *interp = PyInterpreterState_New();
2259     if (interp == NULL) {
2260         *tstate_p = NULL;
2261         return _PyStatus_OK();
2262     }
2263     _PyInterpreterState_SetWhence(interp, whence);
2264     interp->_ready = 1;
2265 
2266     // XXX Might new_interpreter() have been called without the GIL held?
2267     PyThreadState *save_tstate = _PyThreadState_GET();
2268     PyThreadState *tstate = NULL;
2269 
2270     /* From this point until the init_interp_create_gil() call,
2271        we must not do anything that requires that the GIL be held
2272        (or otherwise exist).  That applies whether or not the new
2273        interpreter has its own GIL (e.g. the main interpreter). */
2274     if (save_tstate != NULL) {
2275         _PyThreadState_Detach(save_tstate);
2276     }
2277 
2278     /* Copy the current interpreter config into the new interpreter */
2279     const PyConfig *src_config;
2280     if (save_tstate != NULL) {
2281         src_config = _PyInterpreterState_GetConfig(save_tstate->interp);
2282     }
2283     else
2284     {
2285         /* No current thread state, copy from the main interpreter */
2286         PyInterpreterState *main_interp = _PyInterpreterState_Main();
2287         src_config = _PyInterpreterState_GetConfig(main_interp);
2288     }
2289 
2290     /* This does not require that the GIL be held. */
2291     status = _PyConfig_Copy(&interp->config, src_config);
2292     if (_PyStatus_EXCEPTION(status)) {
2293         goto error;
2294     }
2295 
2296     /* This does not require that the GIL be held. */
2297     status = init_interp_settings(interp, config);
2298     if (_PyStatus_EXCEPTION(status)) {
2299         goto error;
2300     }
2301 
2302     // This could be done in init_interpreter() (in pystate.c) if it
2303     // didn't depend on interp->feature_flags being set already.
2304     status = _PyObject_InitState(interp);
2305     if (_PyStatus_EXCEPTION(status)) {
2306         return status;
2307     }
2308 
2309     // initialize the interp->obmalloc state.  This must be done after
2310     // the settings are loaded (so that feature_flags are set) but before
2311     // any calls are made to obmalloc functions.
2312     if (_PyMem_init_obmalloc(interp) < 0) {
2313         status = _PyStatus_NO_MEMORY();
2314         goto error;
2315     }
2316 
2317     tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INIT);
2318     if (tstate == NULL) {
2319         status = _PyStatus_NO_MEMORY();
2320         goto error;
2321     }
2322 
2323     _PyThreadState_Bind(tstate);
2324     init_interp_create_gil(tstate, config->gil);
2325 
2326     /* No objects have been created yet. */
2327 
2328     status = pycore_interp_init(tstate);
2329     if (_PyStatus_EXCEPTION(status)) {
2330         goto error;
2331     }
2332 
2333     status = init_interp_main(tstate);
2334     if (_PyStatus_EXCEPTION(status)) {
2335         goto error;
2336     }
2337 
2338     *tstate_p = tstate;
2339     return _PyStatus_OK();
2340 
2341 error:
2342     *tstate_p = NULL;
2343     if (tstate != NULL) {
2344         PyThreadState_Clear(tstate);
2345         _PyThreadState_Detach(tstate);
2346         PyThreadState_Delete(tstate);
2347     }
2348     if (save_tstate != NULL) {
2349         _PyThreadState_Attach(save_tstate);
2350     }
2351     PyInterpreterState_Delete(interp);
2352 
2353     return status;
2354 }
2355 
2356 PyStatus
Py_NewInterpreterFromConfig(PyThreadState ** tstate_p,const PyInterpreterConfig * config)2357 Py_NewInterpreterFromConfig(PyThreadState **tstate_p,
2358                             const PyInterpreterConfig *config)
2359 {
2360     long whence = _PyInterpreterState_WHENCE_CAPI;
2361     return new_interpreter(tstate_p, config, whence);
2362 }
2363 
2364 PyThreadState *
Py_NewInterpreter(void)2365 Py_NewInterpreter(void)
2366 {
2367     PyThreadState *tstate = NULL;
2368     long whence = _PyInterpreterState_WHENCE_LEGACY_CAPI;
2369     const PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
2370     PyStatus status = new_interpreter(&tstate, &config, whence);
2371     if (_PyStatus_EXCEPTION(status)) {
2372         Py_ExitStatusException(status);
2373     }
2374     return tstate;
2375 }
2376 
2377 /* Delete an interpreter and its last thread.  This requires that the
2378    given thread state is current, that the thread has no remaining
2379    frames, and that it is its interpreter's only remaining thread.
2380    It is a fatal error to violate these constraints.
2381 
2382    (Py_FinalizeEx() doesn't have these constraints -- it zaps
2383    everything, regardless.)
2384 
2385    Locking: as above.
2386 
2387 */
2388 
2389 void
Py_EndInterpreter(PyThreadState * tstate)2390 Py_EndInterpreter(PyThreadState *tstate)
2391 {
2392     PyInterpreterState *interp = tstate->interp;
2393 
2394     if (tstate != _PyThreadState_GET()) {
2395         Py_FatalError("thread is not current");
2396     }
2397     if (tstate->current_frame != NULL) {
2398         Py_FatalError("thread still has a frame");
2399     }
2400     interp->finalizing = 1;
2401 
2402     // Wrap up existing "threading"-module-created, non-daemon threads.
2403     wait_for_thread_shutdown(tstate);
2404 
2405     // Make any remaining pending calls.
2406     _Py_FinishPendingCalls(tstate);
2407 
2408     _PyAtExit_Call(tstate->interp);
2409 
2410     if (tstate != interp->threads.head || tstate->next != NULL) {
2411         Py_FatalError("not the last thread");
2412     }
2413 
2414     /* Remaining daemon threads will automatically exit
2415        when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
2416     _PyInterpreterState_SetFinalizing(interp, tstate);
2417 
2418     // XXX Call something like _PyImport_Disable() here?
2419 
2420     _PyImport_FiniExternal(tstate->interp);
2421     finalize_modules(tstate);
2422     _PyImport_FiniCore(tstate->interp);
2423 
2424     finalize_interp_clear(tstate);
2425     finalize_interp_delete(tstate->interp);
2426 }
2427 
2428 int
_Py_IsInterpreterFinalizing(PyInterpreterState * interp)2429 _Py_IsInterpreterFinalizing(PyInterpreterState *interp)
2430 {
2431     /* We check the runtime first since, in a daemon thread,
2432        interp might be dangling pointer. */
2433     PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
2434     if (finalizing == NULL) {
2435         finalizing = _PyInterpreterState_GetFinalizing(interp);
2436     }
2437     return finalizing != NULL;
2438 }
2439 
2440 static void
finalize_subinterpreters(void)2441 finalize_subinterpreters(void)
2442 {
2443     PyThreadState *final_tstate = _PyThreadState_GET();
2444     PyInterpreterState *main_interp = _PyInterpreterState_Main();
2445     assert(final_tstate->interp == main_interp);
2446     _PyRuntimeState *runtime = main_interp->runtime;
2447     struct pyinterpreters *interpreters = &runtime->interpreters;
2448 
2449     /* Get the first interpreter in the list. */
2450     HEAD_LOCK(runtime);
2451     PyInterpreterState *interp = interpreters->head;
2452     if (interp == main_interp) {
2453         interp = interp->next;
2454     }
2455     HEAD_UNLOCK(runtime);
2456 
2457     /* Bail out if there are no subinterpreters left. */
2458     if (interp == NULL) {
2459         return;
2460     }
2461 
2462     /* Warn the user if they forgot to clean up subinterpreters. */
2463     (void)PyErr_WarnEx(
2464             PyExc_RuntimeWarning,
2465             "remaining subinterpreters; "
2466             "destroy them with _interpreters.destroy()",
2467             0);
2468 
2469     /* Swap out the current tstate, which we know must belong
2470        to the main interpreter. */
2471     _PyThreadState_Detach(final_tstate);
2472 
2473     /* Clean up all remaining subinterpreters. */
2474     while (interp != NULL) {
2475         assert(!_PyInterpreterState_IsRunningMain(interp));
2476 
2477         /* Find the tstate to use for fini.  We assume the interpreter
2478            will have at most one tstate at this point. */
2479         PyThreadState *tstate = interp->threads.head;
2480         if (tstate != NULL) {
2481             /* Ideally we would be able to use tstate as-is, and rely
2482                on it being in a ready state: no exception set, not
2483                running anything (tstate->current_frame), matching the
2484                current thread ID (tstate->thread_id).  To play it safe,
2485                we always delete it and use a fresh tstate instead. */
2486             assert(tstate != final_tstate);
2487             _PyThreadState_Attach(tstate);
2488             PyThreadState_Clear(tstate);
2489             _PyThreadState_Detach(tstate);
2490             PyThreadState_Delete(tstate);
2491         }
2492         tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
2493 
2494         /* Destroy the subinterpreter. */
2495         _PyThreadState_Attach(tstate);
2496         Py_EndInterpreter(tstate);
2497         assert(_PyThreadState_GET() == NULL);
2498 
2499         /* Advance to the next interpreter. */
2500         HEAD_LOCK(runtime);
2501         interp = interpreters->head;
2502         if (interp == main_interp) {
2503             interp = interp->next;
2504         }
2505         HEAD_UNLOCK(runtime);
2506     }
2507 
2508     /* Switch back to the main interpreter. */
2509     _PyThreadState_Attach(final_tstate);
2510 }
2511 
2512 
2513 /* Add the __main__ module */
2514 
2515 static PyStatus
add_main_module(PyInterpreterState * interp)2516 add_main_module(PyInterpreterState *interp)
2517 {
2518     PyObject *m, *d, *ann_dict;
2519     m = PyImport_AddModuleObject(&_Py_ID(__main__));
2520     if (m == NULL)
2521         return _PyStatus_ERR("can't create __main__ module");
2522 
2523     d = PyModule_GetDict(m);
2524     ann_dict = PyDict_New();
2525     if ((ann_dict == NULL) ||
2526         (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) {
2527         return _PyStatus_ERR("Failed to initialize __main__.__annotations__");
2528     }
2529     Py_DECREF(ann_dict);
2530 
2531     int has_builtins = PyDict_ContainsString(d, "__builtins__");
2532     if (has_builtins < 0) {
2533         return _PyStatus_ERR("Failed to test __main__.__builtins__");
2534     }
2535     if (!has_builtins) {
2536         PyObject *bimod = PyImport_ImportModule("builtins");
2537         if (bimod == NULL) {
2538             return _PyStatus_ERR("Failed to retrieve builtins module");
2539         }
2540         if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
2541             return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
2542         }
2543         Py_DECREF(bimod);
2544     }
2545 
2546     /* Main is a little special - BuiltinImporter is the most appropriate
2547      * initial setting for its __loader__ attribute. A more suitable value
2548      * will be set if __main__ gets further initialized later in the startup
2549      * process.
2550      */
2551     PyObject *loader;
2552     if (PyDict_GetItemStringRef(d, "__loader__", &loader) < 0) {
2553         return _PyStatus_ERR("Failed to test __main__.__loader__");
2554     }
2555     int has_loader = !(loader == NULL || loader == Py_None);
2556     Py_XDECREF(loader);
2557     if (!has_loader) {
2558         PyObject *loader = _PyImport_GetImportlibLoader(interp,
2559                                                         "BuiltinImporter");
2560         if (loader == NULL) {
2561             return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
2562         }
2563         if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
2564             return _PyStatus_ERR("Failed to initialize __main__.__loader__");
2565         }
2566         Py_DECREF(loader);
2567     }
2568     return _PyStatus_OK();
2569 }
2570 
2571 /* Import the site module (not into __main__ though) */
2572 
2573 static PyStatus
init_import_site(void)2574 init_import_site(void)
2575 {
2576     PyObject *m;
2577     m = PyImport_ImportModule("site");
2578     if (m == NULL) {
2579         return _PyStatus_ERR("Failed to import the site module");
2580     }
2581     Py_DECREF(m);
2582     return _PyStatus_OK();
2583 }
2584 
2585 /* returns Py_None if the fd is not valid */
2586 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)2587 create_stdio(const PyConfig *config, PyObject* io,
2588     int fd, int write_mode, const char* name,
2589     const wchar_t* encoding, const wchar_t* errors)
2590 {
2591     PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
2592     const char* mode;
2593     const char* newline;
2594     PyObject *line_buffering, *write_through;
2595     int buffering, isatty;
2596     const int buffered_stdio = config->buffered_stdio;
2597 
2598     if (!_Py_IsValidFD(fd)) {
2599         Py_RETURN_NONE;
2600     }
2601 
2602     /* stdin is always opened in buffered mode, first because it shouldn't
2603        make a difference in common use cases, second because TextIOWrapper
2604        depends on the presence of a read1() method which only exists on
2605        buffered streams.
2606     */
2607     if (!buffered_stdio && write_mode)
2608         buffering = 0;
2609     else
2610         buffering = -1;
2611     if (write_mode)
2612         mode = "wb";
2613     else
2614         mode = "rb";
2615     buf = _PyObject_CallMethod(io, &_Py_ID(open), "isiOOOO",
2616                                fd, mode, buffering,
2617                                Py_None, Py_None, /* encoding, errors */
2618                                Py_None, Py_False); /* newline, closefd */
2619     if (buf == NULL)
2620         goto error;
2621 
2622     if (buffering) {
2623         raw = PyObject_GetAttr(buf, &_Py_ID(raw));
2624         if (raw == NULL)
2625             goto error;
2626     }
2627     else {
2628         raw = Py_NewRef(buf);
2629     }
2630 
2631 #ifdef HAVE_WINDOWS_CONSOLE_IO
2632     /* Windows console IO is always UTF-8 encoded */
2633     PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
2634             &_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
2635     if (winconsoleio_type == NULL) {
2636         goto error;
2637     }
2638     int is_subclass = PyObject_TypeCheck(raw, winconsoleio_type);
2639     Py_DECREF(winconsoleio_type);
2640     if (is_subclass) {
2641         encoding = L"utf-8";
2642     }
2643 #endif
2644 
2645     text = PyUnicode_FromString(name);
2646     if (text == NULL || PyObject_SetAttr(raw, &_Py_ID(name), text) < 0)
2647         goto error;
2648     res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
2649     if (res == NULL)
2650         goto error;
2651     isatty = PyObject_IsTrue(res);
2652     Py_DECREF(res);
2653     if (isatty == -1)
2654         goto error;
2655     if (!buffered_stdio)
2656         write_through = Py_True;
2657     else
2658         write_through = Py_False;
2659     if (buffered_stdio && (isatty || fd == fileno(stderr)))
2660         line_buffering = Py_True;
2661     else
2662         line_buffering = Py_False;
2663 
2664     Py_CLEAR(raw);
2665     Py_CLEAR(text);
2666 
2667 #ifdef MS_WINDOWS
2668     /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
2669        newlines to "\n".
2670        sys.stdout and sys.stderr: translate "\n" to "\r\n". */
2671     newline = NULL;
2672 #else
2673     /* sys.stdin: split lines at "\n".
2674        sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
2675     newline = "\n";
2676 #endif
2677 
2678     PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
2679     if (encoding_str == NULL) {
2680         Py_CLEAR(buf);
2681         goto error;
2682     }
2683 
2684     PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
2685     if (errors_str == NULL) {
2686         Py_CLEAR(buf);
2687         Py_CLEAR(encoding_str);
2688         goto error;
2689     }
2690 
2691     stream = _PyObject_CallMethod(io, &_Py_ID(TextIOWrapper), "OOOsOO",
2692                                   buf, encoding_str, errors_str,
2693                                   newline, line_buffering, write_through);
2694     Py_CLEAR(buf);
2695     Py_CLEAR(encoding_str);
2696     Py_CLEAR(errors_str);
2697     if (stream == NULL)
2698         goto error;
2699 
2700     if (write_mode)
2701         mode = "w";
2702     else
2703         mode = "r";
2704     text = PyUnicode_FromString(mode);
2705     if (!text || PyObject_SetAttr(stream, &_Py_ID(mode), text) < 0)
2706         goto error;
2707     Py_CLEAR(text);
2708     return stream;
2709 
2710 error:
2711     Py_XDECREF(buf);
2712     Py_XDECREF(stream);
2713     Py_XDECREF(text);
2714     Py_XDECREF(raw);
2715 
2716     if (PyErr_ExceptionMatches(PyExc_OSError) && !_Py_IsValidFD(fd)) {
2717         /* Issue #24891: the file descriptor was closed after the first
2718            _Py_IsValidFD() check was called. Ignore the OSError and set the
2719            stream to None. */
2720         PyErr_Clear();
2721         Py_RETURN_NONE;
2722     }
2723     return NULL;
2724 }
2725 
2726 /* Set builtins.open to io.open */
2727 static PyStatus
init_set_builtins_open(void)2728 init_set_builtins_open(void)
2729 {
2730     PyObject *wrapper;
2731     PyObject *bimod = NULL;
2732     PyStatus res = _PyStatus_OK();
2733 
2734     if (!(bimod = PyImport_ImportModule("builtins"))) {
2735         goto error;
2736     }
2737 
2738     if (!(wrapper = _PyImport_GetModuleAttrString("io", "open"))) {
2739         goto error;
2740     }
2741 
2742     /* Set builtins.open */
2743     if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
2744         Py_DECREF(wrapper);
2745         goto error;
2746     }
2747     Py_DECREF(wrapper);
2748     goto done;
2749 
2750 error:
2751     res = _PyStatus_ERR("can't initialize io.open");
2752 
2753 done:
2754     Py_XDECREF(bimod);
2755     return res;
2756 }
2757 
2758 
2759 /* Create sys.stdin, sys.stdout and sys.stderr */
2760 static PyStatus
init_sys_streams(PyThreadState * tstate)2761 init_sys_streams(PyThreadState *tstate)
2762 {
2763     PyObject *iomod = NULL;
2764     PyObject *std = NULL;
2765     int fd;
2766     PyObject * encoding_attr;
2767     PyStatus res = _PyStatus_OK();
2768     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
2769 
2770     /* Check that stdin is not a directory
2771        Using shell redirection, you can redirect stdin to a directory,
2772        crashing the Python interpreter. Catch this common mistake here
2773        and output a useful error message. Note that under MS Windows,
2774        the shell already prevents that. */
2775 #ifndef MS_WINDOWS
2776     struct _Py_stat_struct sb;
2777     if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
2778         S_ISDIR(sb.st_mode)) {
2779         return _PyStatus_ERR("<stdin> is a directory, cannot continue");
2780     }
2781 #endif
2782 
2783     if (!(iomod = PyImport_ImportModule("io"))) {
2784         goto error;
2785     }
2786 
2787     /* Set sys.stdin */
2788     fd = fileno(stdin);
2789     /* Under some conditions stdin, stdout and stderr may not be connected
2790      * and fileno() may point to an invalid file descriptor. For example
2791      * GUI apps don't have valid standard streams by default.
2792      */
2793     std = create_stdio(config, iomod, fd, 0, "<stdin>",
2794                        config->stdio_encoding,
2795                        config->stdio_errors);
2796     if (std == NULL)
2797         goto error;
2798     PySys_SetObject("__stdin__", std);
2799     _PySys_SetAttr(&_Py_ID(stdin), std);
2800     Py_DECREF(std);
2801 
2802     /* Set sys.stdout */
2803     fd = fileno(stdout);
2804     std = create_stdio(config, iomod, fd, 1, "<stdout>",
2805                        config->stdio_encoding,
2806                        config->stdio_errors);
2807     if (std == NULL)
2808         goto error;
2809     PySys_SetObject("__stdout__", std);
2810     _PySys_SetAttr(&_Py_ID(stdout), std);
2811     Py_DECREF(std);
2812 
2813 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
2814     /* Set sys.stderr, replaces the preliminary stderr */
2815     fd = fileno(stderr);
2816     std = create_stdio(config, iomod, fd, 1, "<stderr>",
2817                        config->stdio_encoding,
2818                        L"backslashreplace");
2819     if (std == NULL)
2820         goto error;
2821 
2822     /* Same as hack above, pre-import stderr's codec to avoid recursion
2823        when import.c tries to write to stderr in verbose mode. */
2824     encoding_attr = PyObject_GetAttrString(std, "encoding");
2825     if (encoding_attr != NULL) {
2826         const char *std_encoding = PyUnicode_AsUTF8(encoding_attr);
2827         if (std_encoding != NULL) {
2828             PyObject *codec_info = _PyCodec_Lookup(std_encoding);
2829             Py_XDECREF(codec_info);
2830         }
2831         Py_DECREF(encoding_attr);
2832     }
2833     _PyErr_Clear(tstate);  /* Not a fatal error if codec isn't available */
2834 
2835     if (PySys_SetObject("__stderr__", std) < 0) {
2836         Py_DECREF(std);
2837         goto error;
2838     }
2839     if (_PySys_SetAttr(&_Py_ID(stderr), std) < 0) {
2840         Py_DECREF(std);
2841         goto error;
2842     }
2843     Py_DECREF(std);
2844 #endif
2845 
2846     goto done;
2847 
2848 error:
2849     res = _PyStatus_ERR("can't initialize sys standard streams");
2850 
2851 done:
2852     Py_XDECREF(iomod);
2853     return res;
2854 }
2855 
2856 
2857 #ifdef __ANDROID__
2858 #include <android/log.h>
2859 
2860 static PyObject *
android_log_write_impl(PyObject * self,PyObject * args)2861 android_log_write_impl(PyObject *self, PyObject *args)
2862 {
2863     int prio = 0;
2864     const char *tag = NULL;
2865     const char *text = NULL;
2866     if (!PyArg_ParseTuple(args, "isy", &prio, &tag, &text)) {
2867         return NULL;
2868     }
2869 
2870     // Despite its name, this function is part of the public API
2871     // (https://developer.android.com/ndk/reference/group/logging).
2872     __android_log_write(prio, tag, text);
2873     Py_RETURN_NONE;
2874 }
2875 
2876 
2877 static PyMethodDef android_log_write_method = {
2878     "android_log_write", android_log_write_impl, METH_VARARGS
2879 };
2880 
2881 
2882 static PyStatus
init_android_streams(PyThreadState * tstate)2883 init_android_streams(PyThreadState *tstate)
2884 {
2885     PyStatus status = _PyStatus_OK();
2886     PyObject *_android_support = NULL;
2887     PyObject *android_log_write = NULL;
2888     PyObject *result = NULL;
2889 
2890     _android_support = PyImport_ImportModule("_android_support");
2891     if (_android_support == NULL) {
2892         goto error;
2893     }
2894 
2895     android_log_write = PyCFunction_New(&android_log_write_method, NULL);
2896     if (android_log_write == NULL) {
2897         goto error;
2898     }
2899 
2900     // These log priorities match those used by Java's System.out and System.err.
2901     result = PyObject_CallMethod(
2902         _android_support, "init_streams", "Oii",
2903         android_log_write, ANDROID_LOG_INFO, ANDROID_LOG_WARN);
2904     if (result == NULL) {
2905         goto error;
2906     }
2907 
2908     goto done;
2909 
2910 error:
2911     _PyErr_Print(tstate);
2912     status = _PyStatus_ERR("failed to initialize Android streams");
2913 
2914 done:
2915     Py_XDECREF(result);
2916     Py_XDECREF(android_log_write);
2917     Py_XDECREF(_android_support);
2918     return status;
2919 }
2920 
2921 #endif  // __ANDROID__
2922 
2923 
2924 static void
_Py_FatalError_DumpTracebacks(int fd,PyInterpreterState * interp,PyThreadState * tstate)2925 _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
2926                               PyThreadState *tstate)
2927 {
2928     PUTS(fd, "\n");
2929 
2930     /* display the current Python stack */
2931     _Py_DumpTracebackThreads(fd, interp, tstate);
2932 }
2933 
2934 /* Print the current exception (if an exception is set) with its traceback,
2935    or display the current Python stack.
2936 
2937    Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is
2938    called on catastrophic cases.
2939 
2940    Return 1 if the traceback was displayed, 0 otherwise. */
2941 
2942 static int
_Py_FatalError_PrintExc(PyThreadState * tstate)2943 _Py_FatalError_PrintExc(PyThreadState *tstate)
2944 {
2945     PyObject *exc = _PyErr_GetRaisedException(tstate);
2946     if (exc == NULL) {
2947         /* No current exception */
2948         return 0;
2949     }
2950 
2951     PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
2952     if (ferr == NULL || ferr == Py_None) {
2953         /* sys.stderr is not set yet or set to None,
2954            no need to try to display the exception */
2955         Py_DECREF(exc);
2956         return 0;
2957     }
2958 
2959     PyErr_DisplayException(exc);
2960 
2961     PyObject *tb = PyException_GetTraceback(exc);
2962     int has_tb = (tb != NULL) && (tb != Py_None);
2963     Py_XDECREF(tb);
2964     Py_DECREF(exc);
2965 
2966     /* sys.stderr may be buffered: call sys.stderr.flush() */
2967     if (_PyFile_Flush(ferr) < 0) {
2968         _PyErr_Clear(tstate);
2969     }
2970 
2971     return has_tb;
2972 }
2973 
2974 /* Print fatal error message and abort */
2975 
2976 #ifdef MS_WINDOWS
2977 static void
fatal_output_debug(const char * msg)2978 fatal_output_debug(const char *msg)
2979 {
2980     /* buffer of 256 bytes allocated on the stack */
2981     WCHAR buffer[256 / sizeof(WCHAR)];
2982     size_t buflen = Py_ARRAY_LENGTH(buffer) - 1;
2983     size_t msglen;
2984 
2985     OutputDebugStringW(L"Fatal Python error: ");
2986 
2987     msglen = strlen(msg);
2988     while (msglen) {
2989         size_t i;
2990 
2991         if (buflen > msglen) {
2992             buflen = msglen;
2993         }
2994 
2995         /* Convert the message to wchar_t. This uses a simple one-to-one
2996            conversion, assuming that the this error message actually uses
2997            ASCII only. If this ceases to be true, we will have to convert. */
2998         for (i=0; i < buflen; ++i) {
2999             buffer[i] = msg[i];
3000         }
3001         buffer[i] = L'\0';
3002         OutputDebugStringW(buffer);
3003 
3004         msg += buflen;
3005         msglen -= buflen;
3006     }
3007     OutputDebugStringW(L"\n");
3008 }
3009 #endif
3010 
3011 
3012 static void
fatal_error_dump_runtime(int fd,_PyRuntimeState * runtime)3013 fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime)
3014 {
3015     PUTS(fd, "Python runtime state: ");
3016     PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
3017     if (finalizing) {
3018         PUTS(fd, "finalizing (tstate=0x");
3019         _Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2);
3020         PUTS(fd, ")");
3021     }
3022     else if (runtime->initialized) {
3023         PUTS(fd, "initialized");
3024     }
3025     else if (runtime->core_initialized) {
3026         PUTS(fd, "core initialized");
3027     }
3028     else if (runtime->preinitialized) {
3029         PUTS(fd, "preinitialized");
3030     }
3031     else if (runtime->preinitializing) {
3032         PUTS(fd, "preinitializing");
3033     }
3034     else {
3035         PUTS(fd, "unknown");
3036     }
3037     PUTS(fd, "\n");
3038 }
3039 
3040 
3041 static inline void _Py_NO_RETURN
fatal_error_exit(int status)3042 fatal_error_exit(int status)
3043 {
3044     if (status < 0) {
3045 #if defined(MS_WINDOWS) && defined(_DEBUG)
3046         DebugBreak();
3047 #endif
3048         abort();
3049     }
3050     else {
3051         exit(status);
3052     }
3053 }
3054 
3055 static inline int
acquire_dict_lock_for_dump(PyObject * obj)3056 acquire_dict_lock_for_dump(PyObject *obj)
3057 {
3058 #ifdef Py_GIL_DISABLED
3059     PyMutex *mutex = &obj->ob_mutex;
3060     if (_PyMutex_LockTimed(mutex, 0, 0) == PY_LOCK_ACQUIRED) {
3061         return 1;
3062     }
3063     return 0;
3064 #else
3065     return 1;
3066 #endif
3067 }
3068 
3069 static inline void
release_dict_lock_for_dump(PyObject * obj)3070 release_dict_lock_for_dump(PyObject *obj)
3071 {
3072 #ifdef Py_GIL_DISABLED
3073     PyMutex *mutex = &obj->ob_mutex;
3074     // We can not call PyMutex_Unlock because it's not async-signal-safe.
3075     // So not to wake up other threads, we just use a simple atomic store in here.
3076     _Py_atomic_store_uint8(&mutex->_bits, _Py_UNLOCKED);
3077 #endif
3078 }
3079 
3080 // Dump the list of extension modules of sys.modules, excluding stdlib modules
3081 // (sys.stdlib_module_names), into fd file descriptor.
3082 //
3083 // This function is called by a signal handler in faulthandler: avoid memory
3084 // allocations and keep the implementation simple. For example, the list is not
3085 // sorted on purpose.
3086 void
_Py_DumpExtensionModules(int fd,PyInterpreterState * interp)3087 _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
3088 {
3089     if (interp == NULL) {
3090         return;
3091     }
3092     PyObject *modules = _PyImport_GetModules(interp);
3093     if (modules == NULL || !PyDict_Check(modules)) {
3094         return;
3095     }
3096 
3097     Py_ssize_t pos;
3098     PyObject *key, *value;
3099 
3100     // Avoid PyDict_GetItemString() which calls PyUnicode_FromString(),
3101     // memory cannot be allocated on the heap in a signal handler.
3102     // Iterate on the dict instead.
3103     PyObject *stdlib_module_names = NULL;
3104     if (interp->sysdict != NULL) {
3105         pos = 0;
3106         if (!acquire_dict_lock_for_dump(interp->sysdict)) {
3107             // If we cannot acquire the lock, just don't dump the list of extension modules.
3108             return;
3109         }
3110         while (_PyDict_Next(interp->sysdict, &pos, &key, &value, NULL)) {
3111             if (PyUnicode_Check(key)
3112                && PyUnicode_CompareWithASCIIString(key, "stdlib_module_names") == 0) {
3113                 stdlib_module_names = value;
3114                 break;
3115             }
3116         }
3117         release_dict_lock_for_dump(interp->sysdict);
3118     }
3119     // If we failed to get sys.stdlib_module_names or it's not a frozenset,
3120     // don't exclude stdlib modules.
3121     if (stdlib_module_names != NULL && !PyFrozenSet_Check(stdlib_module_names)) {
3122         stdlib_module_names = NULL;
3123     }
3124 
3125     // List extensions
3126     int header = 1;
3127     Py_ssize_t count = 0;
3128     pos = 0;
3129     if (!acquire_dict_lock_for_dump(modules)) {
3130         // If we cannot acquire the lock, just don't dump the list of extension modules.
3131         return;
3132     }
3133     while (_PyDict_Next(modules, &pos, &key, &value, NULL)) {
3134         if (!PyUnicode_Check(key)) {
3135             continue;
3136         }
3137         if (!_PyModule_IsExtension(value)) {
3138             continue;
3139         }
3140         // Use the module name from the sys.modules key,
3141         // don't attempt to get the module object name.
3142         if (stdlib_module_names != NULL) {
3143             int is_stdlib_ext = 0;
3144 
3145             Py_ssize_t i = 0;
3146             PyObject *item;
3147             Py_hash_t hash;
3148             // if stdlib_module_names is not NULL, it is always a frozenset.
3149             while (_PySet_NextEntry(stdlib_module_names, &i, &item, &hash)) {
3150                 if (PyUnicode_Check(item)
3151                     && PyUnicode_Compare(key, item) == 0)
3152                 {
3153                     is_stdlib_ext = 1;
3154                     break;
3155                 }
3156             }
3157             if (is_stdlib_ext) {
3158                 // Ignore stdlib extension
3159                 continue;
3160             }
3161         }
3162 
3163         if (header) {
3164             PUTS(fd, "\nExtension modules: ");
3165             header = 0;
3166         }
3167         else {
3168             PUTS(fd, ", ");
3169         }
3170 
3171         _Py_DumpASCII(fd, key);
3172         count++;
3173     }
3174     release_dict_lock_for_dump(modules);
3175 
3176     if (count) {
3177         PUTS(fd, " (total: ");
3178         _Py_DumpDecimal(fd, count);
3179         PUTS(fd, ")");
3180         PUTS(fd, "\n");
3181     }
3182 }
3183 
3184 
3185 static void _Py_NO_RETURN
fatal_error(int fd,int header,const char * prefix,const char * msg,int status)3186 fatal_error(int fd, int header, const char *prefix, const char *msg,
3187             int status)
3188 {
3189     static int reentrant = 0;
3190 
3191     if (reentrant) {
3192         /* Py_FatalError() caused a second fatal error.
3193            Example: flush_std_files() raises a recursion error. */
3194         fatal_error_exit(status);
3195     }
3196     reentrant = 1;
3197 
3198     if (header) {
3199         PUTS(fd, "Fatal Python error: ");
3200         if (prefix) {
3201             PUTS(fd, prefix);
3202             PUTS(fd, ": ");
3203         }
3204         if (msg) {
3205             PUTS(fd, msg);
3206         }
3207         else {
3208             PUTS(fd, "<message not set>");
3209         }
3210         PUTS(fd, "\n");
3211     }
3212 
3213     _PyRuntimeState *runtime = &_PyRuntime;
3214     fatal_error_dump_runtime(fd, runtime);
3215 
3216     /* Check if the current thread has a Python thread state
3217        and holds the GIL.
3218 
3219        tss_tstate is NULL if Py_FatalError() is called from a C thread which
3220        has no Python thread state.
3221 
3222        tss_tstate != tstate if the current Python thread does not hold the GIL.
3223        */
3224     PyThreadState *tstate = _PyThreadState_GET();
3225     PyInterpreterState *interp = NULL;
3226     PyThreadState *tss_tstate = PyGILState_GetThisThreadState();
3227     if (tstate != NULL) {
3228         interp = tstate->interp;
3229     }
3230     else if (tss_tstate != NULL) {
3231         interp = tss_tstate->interp;
3232     }
3233     int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate);
3234 
3235     if (has_tstate_and_gil) {
3236         /* If an exception is set, print the exception with its traceback */
3237         if (!_Py_FatalError_PrintExc(tss_tstate)) {
3238             /* No exception is set, or an exception is set without traceback */
3239             _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
3240         }
3241     }
3242     else {
3243         _Py_FatalError_DumpTracebacks(fd, interp, tss_tstate);
3244     }
3245 
3246     _Py_DumpExtensionModules(fd, interp);
3247 
3248     /* The main purpose of faulthandler is to display the traceback.
3249        This function already did its best to display a traceback.
3250        Disable faulthandler to prevent writing a second traceback
3251        on abort(). */
3252     _PyFaulthandler_Fini();
3253 
3254     /* Check if the current Python thread hold the GIL */
3255     if (has_tstate_and_gil) {
3256         /* Flush sys.stdout and sys.stderr */
3257         flush_std_files();
3258     }
3259 
3260 #ifdef MS_WINDOWS
3261     fatal_output_debug(msg);
3262 #endif /* MS_WINDOWS */
3263 
3264     fatal_error_exit(status);
3265 }
3266 
3267 
3268 #undef Py_FatalError
3269 
3270 void _Py_NO_RETURN
Py_FatalError(const char * msg)3271 Py_FatalError(const char *msg)
3272 {
3273     fatal_error(fileno(stderr), 1, NULL, msg, -1);
3274 }
3275 
3276 
3277 void _Py_NO_RETURN
_Py_FatalErrorFunc(const char * func,const char * msg)3278 _Py_FatalErrorFunc(const char *func, const char *msg)
3279 {
3280     fatal_error(fileno(stderr), 1, func, msg, -1);
3281 }
3282 
3283 
3284 void _Py_NO_RETURN
_Py_FatalErrorFormat(const char * func,const char * format,...)3285 _Py_FatalErrorFormat(const char *func, const char *format, ...)
3286 {
3287     static int reentrant = 0;
3288     if (reentrant) {
3289         /* _Py_FatalErrorFormat() caused a second fatal error */
3290         fatal_error_exit(-1);
3291     }
3292     reentrant = 1;
3293 
3294     FILE *stream = stderr;
3295     const int fd = fileno(stream);
3296     PUTS(fd, "Fatal Python error: ");
3297     if (func) {
3298         PUTS(fd, func);
3299         PUTS(fd, ": ");
3300     }
3301 
3302     va_list vargs;
3303     va_start(vargs, format);
3304     vfprintf(stream, format, vargs);
3305     va_end(vargs);
3306 
3307     fputs("\n", stream);
3308     fflush(stream);
3309 
3310     fatal_error(fd, 0, NULL, NULL, -1);
3311 }
3312 
3313 
3314 void _Py_NO_RETURN
_Py_FatalRefcountErrorFunc(const char * func,const char * msg)3315 _Py_FatalRefcountErrorFunc(const char *func, const char *msg)
3316 {
3317     _Py_FatalErrorFormat(func,
3318                          "%s: bug likely caused by a refcount error "
3319                          "in a C extension",
3320                          msg);
3321 }
3322 
3323 
3324 void _Py_NO_RETURN
Py_ExitStatusException(PyStatus status)3325 Py_ExitStatusException(PyStatus status)
3326 {
3327     if (_PyStatus_IS_EXIT(status)) {
3328         exit(status.exitcode);
3329     }
3330     else if (_PyStatus_IS_ERROR(status)) {
3331         fatal_error(fileno(stderr), 1, status.func, status.err_msg, 1);
3332     }
3333     else {
3334         Py_FatalError("Py_ExitStatusException() must not be called on success");
3335     }
3336 }
3337 
3338 
3339 /* Wait until threading._shutdown completes, provided
3340    the threading module was imported in the first place.
3341    The shutdown routine will wait until all non-daemon
3342    "threading" threads have completed. */
3343 static void
wait_for_thread_shutdown(PyThreadState * tstate)3344 wait_for_thread_shutdown(PyThreadState *tstate)
3345 {
3346     PyObject *result;
3347     PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
3348     if (threading == NULL) {
3349         if (_PyErr_Occurred(tstate)) {
3350             PyErr_FormatUnraisable("Exception ignored on threading shutdown");
3351         }
3352         /* else: threading not imported */
3353         return;
3354     }
3355     result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
3356     if (result == NULL) {
3357         PyErr_FormatUnraisable("Exception ignored on threading shutdown");
3358     }
3359     else {
3360         Py_DECREF(result);
3361     }
3362     Py_DECREF(threading);
3363 }
3364 
Py_AtExit(void (* func)(void))3365 int Py_AtExit(void (*func)(void))
3366 {
3367     struct _atexit_runtime_state *state = &_PyRuntime.atexit;
3368     PyMutex_Lock(&state->mutex);
3369     if (state->ncallbacks >= NEXITFUNCS) {
3370         PyMutex_Unlock(&state->mutex);
3371         return -1;
3372     }
3373     state->callbacks[state->ncallbacks++] = func;
3374     PyMutex_Unlock(&state->mutex);
3375     return 0;
3376 }
3377 
3378 static void
call_ll_exitfuncs(_PyRuntimeState * runtime)3379 call_ll_exitfuncs(_PyRuntimeState *runtime)
3380 {
3381     atexit_callbackfunc exitfunc;
3382     struct _atexit_runtime_state *state = &runtime->atexit;
3383 
3384     PyMutex_Lock(&state->mutex);
3385     while (state->ncallbacks > 0) {
3386         /* pop last function from the list */
3387         state->ncallbacks--;
3388         exitfunc = state->callbacks[state->ncallbacks];
3389         state->callbacks[state->ncallbacks] = NULL;
3390 
3391         PyMutex_Unlock(&state->mutex);
3392         exitfunc();
3393         PyMutex_Lock(&state->mutex);
3394     }
3395     PyMutex_Unlock(&state->mutex);
3396 
3397     fflush(stdout);
3398     fflush(stderr);
3399 }
3400 
3401 void _Py_NO_RETURN
Py_Exit(int sts)3402 Py_Exit(int sts)
3403 {
3404     PyThreadState *tstate = _PyThreadState_GET();
3405     if (tstate != NULL && _PyThreadState_IsRunningMain(tstate)) {
3406         _PyInterpreterState_SetNotRunningMain(tstate->interp);
3407     }
3408     if (_Py_Finalize(&_PyRuntime) < 0) {
3409         sts = 120;
3410     }
3411 
3412     exit(sts);
3413 }
3414 
3415 
3416 /*
3417  * The file descriptor fd is considered ``interactive'' if either
3418  *   a) isatty(fd) is TRUE, or
3419  *   b) the -i flag was given, and the filename associated with
3420  *      the descriptor is NULL or "<stdin>" or "???".
3421  */
3422 int
Py_FdIsInteractive(FILE * fp,const char * filename)3423 Py_FdIsInteractive(FILE *fp, const char *filename)
3424 {
3425     if (isatty(fileno(fp))) {
3426         return 1;
3427     }
3428     if (!_Py_GetConfig()->interactive) {
3429         return 0;
3430     }
3431     return ((filename == NULL)
3432             || (strcmp(filename, "<stdin>") == 0)
3433             || (strcmp(filename, "???") == 0));
3434 }
3435 
3436 
3437 int
_Py_FdIsInteractive(FILE * fp,PyObject * filename)3438 _Py_FdIsInteractive(FILE *fp, PyObject *filename)
3439 {
3440     if (isatty(fileno(fp))) {
3441         return 1;
3442     }
3443     if (!_Py_GetConfig()->interactive) {
3444         return 0;
3445     }
3446     return ((filename == NULL)
3447             || (PyUnicode_CompareWithASCIIString(filename, "<stdin>") == 0)
3448             || (PyUnicode_CompareWithASCIIString(filename, "???") == 0));
3449 }
3450 
3451 
3452 /* Wrappers around sigaction() or signal(). */
3453 
3454 PyOS_sighandler_t
PyOS_getsig(int sig)3455 PyOS_getsig(int sig)
3456 {
3457 #ifdef HAVE_SIGACTION
3458     struct sigaction context;
3459     if (sigaction(sig, NULL, &context) == -1)
3460         return SIG_ERR;
3461     return context.sa_handler;
3462 #else
3463     PyOS_sighandler_t handler;
3464 /* Special signal handling for the secure CRT in Visual Studio 2005 */
3465 #if defined(_MSC_VER) && _MSC_VER >= 1400
3466     switch (sig) {
3467     /* Only these signals are valid */
3468     case SIGINT:
3469     case SIGILL:
3470     case SIGFPE:
3471     case SIGSEGV:
3472     case SIGTERM:
3473     case SIGBREAK:
3474     case SIGABRT:
3475         break;
3476     /* Don't call signal() with other values or it will assert */
3477     default:
3478         return SIG_ERR;
3479     }
3480 #endif /* _MSC_VER && _MSC_VER >= 1400 */
3481     handler = signal(sig, SIG_IGN);
3482     if (handler != SIG_ERR)
3483         signal(sig, handler);
3484     return handler;
3485 #endif
3486 }
3487 
3488 /*
3489  * All of the code in this function must only use async-signal-safe functions,
3490  * listed at `man 7 signal` or
3491  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
3492  */
3493 PyOS_sighandler_t
PyOS_setsig(int sig,PyOS_sighandler_t handler)3494 PyOS_setsig(int sig, PyOS_sighandler_t handler)
3495 {
3496 #ifdef HAVE_SIGACTION
3497     /* Some code in Modules/signalmodule.c depends on sigaction() being
3498      * used here if HAVE_SIGACTION is defined.  Fix that if this code
3499      * changes to invalidate that assumption.
3500      */
3501     struct sigaction context, ocontext;
3502     context.sa_handler = handler;
3503     sigemptyset(&context.sa_mask);
3504     /* Using SA_ONSTACK is friendlier to other C/C++/Golang-VM code that
3505      * extension module or embedding code may use where tiny thread stacks
3506      * are used.  https://bugs.python.org/issue43390 */
3507     context.sa_flags = SA_ONSTACK;
3508     if (sigaction(sig, &context, &ocontext) == -1)
3509         return SIG_ERR;
3510     return ocontext.sa_handler;
3511 #else
3512     PyOS_sighandler_t oldhandler;
3513     oldhandler = signal(sig, handler);
3514 #ifdef HAVE_SIGINTERRUPT
3515     siginterrupt(sig, 1);
3516 #endif
3517     return oldhandler;
3518 #endif
3519 }
3520