Coverage Report

Created: 2026-06-21 06:15

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