Coverage Report

Created: 2026-05-16 06:46

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