Coverage Report

Created: 2026-05-30 06:18

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