Coverage Report

Created: 2022-07-08 09:39

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