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