Coverage Report

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