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