summaryrefslogtreecommitdiffstats
path: root/Python/pathconfig.c
blob: 342a9448f795a734f9e8555d9fce6c993c58e441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* Path configuration like module_search_path (sys.path) */

#include "Python.h"
#include "osdefs.h"
#include "pycore_fileutils.h"
#include "pycore_pathconfig.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
#include <wchar.h>

#ifdef __cplusplus
extern "C" {
#endif


_PyPathConfig _Py_path_config = _PyPathConfig_INIT;


static int
copy_wstr(wchar_t **dst, const wchar_t *src)
{
    if (src != NULL) {
        *dst = _PyMem_RawWcsdup(src);
        if (*dst == NULL) {
            return -1;
        }
    }
    else {
        *dst = NULL;
    }
    return 0;
}


static void
_PyPathConfig_Clear(_PyPathConfig *config)
{
    /* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator,
       since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be
       called before Py_Initialize() which can changes the memory allocator. */
    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

#define CLEAR(ATTR) \
    do { \
        PyMem_RawFree(ATTR); \
        ATTR = NULL; \
    } while (0)

    CLEAR(config->prefix);
    CLEAR(config->program_full_path);
    CLEAR(config->exec_prefix);
#ifdef MS_WINDOWS
    CLEAR(config->dll_path);
#endif
    CLEAR(config->module_search_path);
    CLEAR(config->home);
    CLEAR(config->program_name);
#undef CLEAR

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}


/* Calculate the path configuration: initialize path_config from core_config */
static _PyInitError
_PyPathConfig_Calculate(_PyPathConfig *path_config,
                        const _PyCoreConfig *core_config)
{
    _PyInitError err;
    _PyPathConfig new_config = _PyPathConfig_INIT;

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    /* Calculate program_full_path, prefix, exec_prefix,
       dll_path (Windows), and module_search_path */
    err = _PyPathConfig_Calculate_impl(&new_config, core_config);
    if (_Py_INIT_FAILED(err)) {
        goto err;
    }

    /* Copy home and program_name from core_config */
    if (copy_wstr(&new_config.home, core_config->home) < 0) {
        err = _Py_INIT_NO_MEMORY();
        goto err;
    }
    if (copy_wstr(&new_config.program_name, core_config->program_name) < 0) {
        err = _Py_INIT_NO_MEMORY();
        goto err;
    }

    _PyPathConfig_Clear(path_config);
    *path_config = new_config;

    err = _Py_INIT_OK();
    goto done;

err:
    _PyPathConfig_Clear(&new_config);

done:
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
    return err;
}


_PyInitError
_PyPathConfig_SetGlobal(const _PyPathConfig *config)
{
    _PyInitError err;
    _PyPathConfig new_config = _PyPathConfig_INIT;

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

#define COPY_ATTR(ATTR) \
    do { \
        if (copy_wstr(&new_config.ATTR, config->ATTR) < 0) { \
            _PyPathConfig_Clear(&new_config); \
            err = _Py_INIT_NO_MEMORY(); \
            goto done; \
        } \
    } while (0)

    COPY_ATTR(program_full_path);
    COPY_ATTR(prefix);
    COPY_ATTR(exec_prefix);
#ifdef MS_WINDOWS
    COPY_ATTR(dll_path);
#endif
    COPY_ATTR(module_search_path);
    COPY_ATTR(program_name);
    COPY_ATTR(home);

    _PyPathConfig_Clear(&_Py_path_config);
    /* Steal new_config strings; don't clear new_config */
    _Py_path_config = new_config;

    err = _Py_INIT_OK();

done:
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
    return err;
}


void
_PyPathConfig_ClearGlobal(void)
{
    _PyPathConfig_Clear(&_Py_path_config);
}


static wchar_t*
wstrlist_join(wchar_t sep, int count, wchar_t **list)
{
    size_t len = 1;   /* NUL terminator */
    for (int i=0; i < count; i++) {
        if (i != 0) {
            len++;
        }
        len += wcslen(list[i]);
    }

    wchar_t *text = PyMem_RawMalloc(len * sizeof(wchar_t));
    if (text == NULL) {
        return NULL;
    }
    wchar_t *str = text;
    for (int i=0; i < count; i++) {
        wchar_t *path = list[i];
        if (i != 0) {
            *str++ = SEP;
        }
        len = wcslen(path);
        memcpy(str, path, len * sizeof(wchar_t));
        str += len;
    }
    *str = L'\0';

    return text;
}


/* Set the global path configuration from core_config. */
_PyInitError
_PyCoreConfig_SetPathConfig(const _PyCoreConfig *core_config)
{
    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    _PyInitError err;
    _PyPathConfig path_config = _PyPathConfig_INIT;

    path_config.module_search_path = wstrlist_join(DELIM,
                                                   core_config->nmodule_search_path,
                                                   core_config->module_search_paths);
    if (path_config.module_search_path == NULL) {
        goto no_memory;
    }

    if (copy_wstr(&path_config.program_full_path, core_config->executable) < 0) {
        goto no_memory;
    }
    if (copy_wstr(&path_config.prefix, core_config->prefix) < 0) {
        goto no_memory;
    }
    if (copy_wstr(&path_config.exec_prefix, core_config->exec_prefix) < 0) {
        goto no_memory;
    }
#ifdef MS_WINDOWS
    if (copy_wstr(&path_config.dll_path, core_config->dll_path) < 0) {
        goto no_memory;
    }
#endif
    if (copy_wstr(&path_config.program_name, core_config->program_name) < 0) {
        goto no_memory;
    }
    if (copy_wstr(&path_config.home, core_config->home) < 0) {
        goto no_memory;
    }

    err = _PyPathConfig_SetGlobal(&path_config);
    if (_Py_INIT_FAILED(err)) {
        goto done;
    }

    err = _Py_INIT_OK();
    goto done;

no_memory:
    err = _Py_INIT_NO_MEMORY();

done:
    _PyPathConfig_Clear(&path_config);
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
    return err;
}


static _PyInitError
core_config_init_module_search_paths(_PyCoreConfig *config,
                                     _PyPathConfig *path_config)
{
    assert(config->module_search_paths == NULL);
    assert(config->nmodule_search_path < 0);

    config->nmodule_search_path = 0;

    const wchar_t *sys_path = path_config->module_search_path;
    const wchar_t delim = DELIM;
    const wchar_t *p = sys_path;
    while (1) {
        p = wcschr(sys_path, delim);
        if (p == NULL) {
            p = sys_path + wcslen(sys_path); /* End of string */
        }

        size_t path_len = (p - sys_path);
        wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
        if (path == NULL) {
            return _Py_INIT_NO_MEMORY();
        }
        memcpy(path, sys_path, path_len * sizeof(wchar_t));
        path[path_len] = L'\0';

        _PyInitError err = _Py_wstrlist_append(&config->nmodule_search_path,
                                               &config->module_search_paths,
                                               path);
        PyMem_RawFree(path);
        if (_Py_INIT_FAILED(err)) {
            return err;
        }

        if (*p == '\0') {
            break;
        }
        sys_path = p + 1;
    }
    return _Py_INIT_OK();
}


static _PyInitError
_PyCoreConfig_CalculatePathConfig(_PyCoreConfig *config)
{
    _PyPathConfig path_config = _PyPathConfig_INIT;
    _PyInitError err;

    err = _PyPathConfig_Calculate(&path_config, config);
    if (_Py_INIT_FAILED(err)) {
        goto error;
    }

    if (config->nmodule_search_path < 0) {
        err = core_config_init_module_search_paths(config, &path_config);
        if (_Py_INIT_FAILED(err)) {
            goto error;
        }
    }

    if (config->executable == NULL) {
        if (copy_wstr(&config->executable,
                      path_config.program_full_path) < 0) {
            goto no_memory;
        }
    }

    if (config->prefix == NULL) {
        if (copy_wstr(&config->prefix, path_config.prefix) < 0) {
            goto no_memory;
        }
    }

    if (config->exec_prefix == NULL) {
        if (copy_wstr(&config->exec_prefix,
                      path_config.exec_prefix) < 0) {
            goto no_memory;
        }
    }

#ifdef MS_WINDOWS
    if (config->dll_path == NULL) {
        if (copy_wstr(&config->dll_path, path_config.dll_path) < 0) {
            goto no_memory;
        }
    }
#endif

    if (path_config.isolated != -1) {
        config->isolated = path_config.isolated;
    }
    if (path_config.site_import != -1) {
        config->site_import = path_config.site_import;
    }

    _PyPathConfig_Clear(&path_config);
    return _Py_INIT_OK();

no_memory:
    err = _Py_INIT_NO_MEMORY();

error:
    _PyPathConfig_Clear(&path_config);
    return err;
}


_PyInitError
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
{
    /* Do we need to calculate the path? */
    if ((config->nmodule_search_path < 0)
        || (config->executable == NULL)
        || (config->prefix == NULL)
#ifdef MS_WINDOWS
        || (config->dll_path == NULL)
#endif
        || (config->exec_prefix == NULL))
    {
        _PyInitError err = _PyCoreConfig_CalculatePathConfig(config);
        if (_Py_INIT_FAILED(err)) {
            return err;
        }
    }

    if (config->base_prefix == NULL) {
        if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
            return _Py_INIT_NO_MEMORY();
        }
    }

    if (config->base_exec_prefix == NULL) {
        if (copy_wstr(&config->base_exec_prefix,
                      config->exec_prefix) < 0) {
            return _Py_INIT_NO_MEMORY();
        }
    }
    return _Py_INIT_OK();
}


static void
pathconfig_global_init(void)
{
    if (_Py_path_config.module_search_path != NULL) {
        /* Already initialized */
        return;
    }

    _PyInitError err;
    _PyCoreConfig config = _PyCoreConfig_INIT;

    err = _PyCoreConfig_Read(&config);
    if (_Py_INIT_FAILED(err)) {
        goto error;
    }

    err = _PyCoreConfig_SetPathConfig(&config);
    if (_Py_INIT_FAILED(err)) {
        goto error;
    }

    _PyCoreConfig_Clear(&config);
    return;

error:
    _PyCoreConfig_Clear(&config);
    _Py_FatalInitError(err);
}


/* External interface */

void
Py_SetPath(const wchar_t *path)
{
    if (path == NULL) {
        _PyPathConfig_Clear(&_Py_path_config);
        return;
    }

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    _PyPathConfig new_config;
    new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
    int alloc_error = (new_config.program_full_path == NULL);
    new_config.prefix = _PyMem_RawWcsdup(L"");
    alloc_error |= (new_config.prefix == NULL);
    new_config.exec_prefix = _PyMem_RawWcsdup(L"");
    alloc_error |= (new_config.exec_prefix == NULL);
#ifdef MS_WINDOWS
    new_config.dll_path = _PyMem_RawWcsdup(L"");
    alloc_error |= (new_config.dll_path == NULL);
#endif
    new_config.module_search_path = _PyMem_RawWcsdup(path);
    alloc_error |= (new_config.module_search_path == NULL);

    /* steal the home and program_name values (to leave them unchanged) */
    new_config.home = _Py_path_config.home;
    _Py_path_config.home = NULL;
    new_config.program_name = _Py_path_config.program_name;
    _Py_path_config.program_name = NULL;

    _PyPathConfig_Clear(&_Py_path_config);
    _Py_path_config = new_config;

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    if (alloc_error) {
        Py_FatalError("Py_SetPath() failed: out of memory");
    }
}


void
Py_SetPythonHome(const wchar_t *home)
{
    if (home == NULL) {
        return;
    }

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    PyMem_RawFree(_Py_path_config.home);
    _Py_path_config.home = _PyMem_RawWcsdup(home);

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    if (_Py_path_config.home == NULL) {
        Py_FatalError("Py_SetPythonHome() failed: out of memory");
    }
}


void
Py_SetProgramName(const wchar_t *program_name)
{
    if (program_name == NULL || program_name[0] == L'\0') {
        return;
    }

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    PyMem_RawFree(_Py_path_config.program_name);
    _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    if (_Py_path_config.program_name == NULL) {
        Py_FatalError("Py_SetProgramName() failed: out of memory");
    }
}

void
_Py_SetProgramFullPath(const wchar_t *program_full_path)
{
    if (program_full_path == NULL || program_full_path[0] == L'\0') {
        return;
    }

    PyMemAllocatorEx old_alloc;
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    PyMem_RawFree(_Py_path_config.program_full_path);
    _Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);

    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

    if (_Py_path_config.program_full_path == NULL) {
        Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");
    }
}


wchar_t *
Py_GetPath(void)
{
    pathconfig_global_init();
    return _Py_path_config.module_search_path;
}


wchar_t *
Py_GetPrefix(void)
{
    pathconfig_global_init();
    return _Py_path_config.prefix;
}


wchar_t *
Py_GetExecPrefix(void)
{
    pathconfig_global_init();
    return _Py_path_config.exec_prefix;
}


wchar_t *
Py_GetProgramFullPath(void)
{
    pathconfig_global_init();
    return _Py_path_config.program_full_path;
}


wchar_t*
Py_GetPythonHome(void)
{
    pathconfig_global_init();
    return _Py_path_config.home;
}


wchar_t *
Py_GetProgramName(void)
{
    pathconfig_global_init();
    return _Py_path_config.program_name;
}

/* Compute argv[0] which will be prepended to sys.argv */
PyObject*
_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
{
    wchar_t *argv0;
    wchar_t *p = NULL;
    Py_ssize_t n = 0;
    int have_script_arg = 0;
    int have_module_arg = 0;
#ifdef HAVE_READLINK
    wchar_t link[MAXPATHLEN+1];
    wchar_t argv0copy[2*MAXPATHLEN+1];
    int nr = 0;
#endif
#if defined(HAVE_REALPATH)
    wchar_t fullpath[MAXPATHLEN];
#elif defined(MS_WINDOWS)
    wchar_t fullpath[MAX_PATH];
#endif

    argv0 = argv[0];
    if (argc > 0 && argv0 != NULL) {
        have_module_arg = (wcscmp(argv0, L"-m") == 0);
        have_script_arg = !have_module_arg && (wcscmp(argv0, L"-c") != 0);
    }

    if (have_module_arg) {
        #if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
            _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
            argv0 = fullpath;
            n = wcslen(argv0);
        #else
            argv0 = L".";
            n = 1;
        #endif
    }

#ifdef HAVE_READLINK
    if (have_script_arg)
        nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
    if (nr > 0) {
        /* It's a symlink */
        link[nr] = '\0';
        if (link[0] == SEP)
            argv0 = link; /* Link to absolute path */
        else if (wcschr(link, SEP) == NULL)
            ; /* Link without path */
        else {
            /* Must join(dirname(argv0), link) */
            wchar_t *q = wcsrchr(argv0, SEP);
            if (q == NULL)
                argv0 = link; /* argv0 without path */
            else {
                /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */
                wcsncpy(argv0copy, argv0, MAXPATHLEN);
                q = wcsrchr(argv0copy, SEP);
                wcsncpy(q+1, link, MAXPATHLEN);
                q[MAXPATHLEN + 1] = L'\0';
                argv0 = argv0copy;
            }
        }
    }
#endif /* HAVE_READLINK */

#if SEP == '\\'
    /* Special case for Microsoft filename syntax */
    if (have_script_arg) {
        wchar_t *q;
#if defined(MS_WINDOWS)
        /* Replace the first element in argv with the full path. */
        wchar_t *ptemp;
        if (GetFullPathNameW(argv0,
                           Py_ARRAY_LENGTH(fullpath),
                           fullpath,
                           &ptemp)) {
            argv0 = fullpath;
        }
#endif
        p = wcsrchr(argv0, SEP);
        /* Test for alternate separator */
        q = wcsrchr(p ? p : argv0, '/');
        if (q != NULL)
            p = q;
        if (p != NULL) {
            n = p + 1 - argv0;
            if (n > 1 && p[-1] != ':')
                n--; /* Drop trailing separator */
        }
    }
#else /* All other filename syntaxes */
    if (have_script_arg) {
#if defined(HAVE_REALPATH)
        if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
            argv0 = fullpath;
        }
#endif
        p = wcsrchr(argv0, SEP);
    }
    if (p != NULL) {
        n = p + 1 - argv0;
#if SEP == '/' /* Special case for Unix filename syntax */
        if (n > 1)
            n--; /* Drop trailing separator */
#endif /* Unix */
    }
#endif /* All others */

    return PyUnicode_FromWideChar(argv0, n);
}


/* Search for a prefix value in an environment file (pyvenv.cfg).
   If found, copy it into the provided buffer. */
int
_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
                       wchar_t *value, size_t value_size)
{
    int result = 0; /* meaning not found */
    char buffer[MAXPATHLEN*2+1];  /* allow extra for key, '=', etc. */

    fseek(env_file, 0, SEEK_SET);
    while (!feof(env_file)) {
        char * p = fgets(buffer, MAXPATHLEN*2, env_file);

        if (p == NULL) {
            break;
        }

        size_t n = strlen(p);
        if (p[n - 1] != '\n') {
            /* line has overflowed - bail */
            break;
        }
        if (p[0] == '#') {
            /* Comment - skip */
            continue;
        }

        wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
        if (tmpbuffer) {
            wchar_t * state;
            wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
            if ((tok != NULL) && !wcscmp(tok, key)) {
                tok = wcstok(NULL, L" \t", &state);
                if ((tok != NULL) && !wcscmp(tok, L"=")) {
                    tok = wcstok(NULL, L"\r\n", &state);
                    if (tok != NULL) {
                        wcsncpy(value, tok, MAXPATHLEN);
                        result = 1;
                        PyMem_RawFree(tmpbuffer);
                        break;
                    }
                }
            }
            PyMem_RawFree(tmpbuffer);
        }
    }
    return result;
}

#ifdef __cplusplus
}
#endif
0; /* Needed by compile.c */ int Py_NoSiteFlag; /* Suppress 'import site' */ int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ int Py_FrozenFlag; /* Needed by getpath.c */ int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.pyc) */ int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */ #ifdef MS_WINDOWS int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */ int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ #endif /* Hack to force loading of object files */ int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ PyOS_mystrnicmp; /* Python/pystrcmp.o */ /* PyModule_GetWarningsModule is no longer necessary as of 2.6 since _warnings is builtin. This API should not be used. */ PyObject * PyModule_GetWarningsModule(void) { return PyImport_ImportModule("warnings"); } /* APIs to access the initialization flags * * Can be called prior to Py_Initialize. */ int _Py_IsCoreInitialized(void) { return _PyRuntime.core_initialized; } int Py_IsInitialized(void) { return _PyRuntime.initialized; } /* Helper to allow an embedding application to override the normal * mechanism that attempts to figure out an appropriate IO encoding */ static char *_Py_StandardStreamEncoding = NULL; static char *_Py_StandardStreamErrors = NULL; int Py_SetStandardStreamEncoding(const char *encoding, const char *errors) { if (Py_IsInitialized()) { /* This is too late to have any effect */ return -1; } /* Can't call PyErr_NoMemory() on errors, as Python hasn't been * initialised yet. * * However, the raw memory allocators are initialised appropriately * as C static variables, so _PyMem_RawStrdup is OK even though * Py_Initialize hasn't been called yet. */ if (encoding) { _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding); if (!_Py_StandardStreamEncoding) { return -2; } } if (errors) { _Py_StandardStreamErrors = _PyMem_RawStrdup(errors); if (!_Py_StandardStreamErrors) { if (_Py_StandardStreamEncoding) { PyMem_RawFree(_Py_StandardStreamEncoding); } return -3; } } #ifdef MS_WINDOWS if (_Py_StandardStreamEncoding) { /* Overriding the stream encoding implies legacy streams */ Py_LegacyWindowsStdioFlag = 1; } #endif return 0; } /* Global initializations. Can be undone by Py_FinalizeEx(). Don't call this twice without an intervening Py_FinalizeEx() call. When initializations fail, a fatal error is issued and the function does not return. On return, the first thread and interpreter state have been created. Locking: you must hold the interpreter lock while calling this. (If the lock has not yet been initialized, that's equivalent to having the lock, but you cannot use multiple threads.) */ static char* get_codec_name(const char *encoding) { const char *name_utf8; char *name_str; PyObject *codec, *name = NULL; codec = _PyCodec_Lookup(encoding); if (!codec) goto error; name = _PyObject_GetAttrId(codec, &PyId_name); Py_CLEAR(codec); if (!name) goto error; name_utf8 = PyUnicode_AsUTF8(name); if (name_utf8 == NULL) goto error; name_str = _PyMem_RawStrdup(name_utf8); Py_DECREF(name); if (name_str == NULL) { PyErr_NoMemory(); return NULL; } return name_str; error: Py_XDECREF(codec); Py_XDECREF(name); return NULL; } static char* get_locale_encoding(void) { #if defined(HAVE_LANGINFO_H) && defined(CODESET) char* codeset = nl_langinfo(CODESET); if (!codeset || codeset[0] == '\0') { PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); return NULL; } return get_codec_name(codeset); #elif defined(__ANDROID__) return get_codec_name("UTF-8"); #else PyErr_SetNone(PyExc_NotImplementedError); return NULL; #endif } static _PyInitError initimport(PyInterpreterState *interp, PyObject *sysmod) { PyObject *importlib; PyObject *impmod; PyObject *value; _PyInitError err; /* Import _importlib through its frozen version, _frozen_importlib. */ if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { return _Py_INIT_ERR("can't import _frozen_importlib"); } else if (Py_VerboseFlag) { PySys_FormatStderr("import _frozen_importlib # frozen\n"); } importlib = PyImport_AddModule("_frozen_importlib"); if (importlib == NULL) { return _Py_INIT_ERR("couldn't get _frozen_importlib from sys.modules"); } interp->importlib = importlib; Py_INCREF(interp->importlib); interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); if (interp->import_func == NULL) return _Py_INIT_ERR("__import__ not found"); Py_INCREF(interp->import_func); /* Import the _imp module */ impmod = PyInit_imp(); if (impmod == NULL) { return _Py_INIT_ERR("can't import _imp"); } else if (Py_VerboseFlag) { PySys_FormatStderr("import _imp # builtin\n"); } if (_PyImport_SetModuleString("_imp", impmod) < 0) { return _Py_INIT_ERR("can't save _imp to sys.modules"); } /* Install importlib as the implementation of import */ value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); if (value != NULL) { Py_DECREF(value); value = PyObject_CallMethod(importlib, "_install_external_importers", ""); } if (value == NULL) { PyErr_Print(); return _Py_INIT_ERR("importlib install failed"); } Py_DECREF(value); Py_DECREF(impmod); err = _PyImportZip_Init(); if (_Py_INIT_FAILED(err)) { return err; } return _Py_INIT_OK(); } static _PyInitError initexternalimport(PyInterpreterState *interp) { PyObject *value; value = PyObject_CallMethod(interp->importlib, "_install_external_importers", ""); if (value == NULL) { PyErr_Print(); return _Py_INIT_ERR("external importer setup failed"); } Py_DECREF(value); return _Py_INIT_OK(); } /* Helper functions to better handle the legacy C locale * * The legacy C locale assumes ASCII as the default text encoding, which * causes problems not only for the CPython runtime, but also other * components like GNU readline. * * Accordingly, when the CLI detects it, it attempts to coerce it to a * more capable UTF-8 based alternative as follows: * * if (_Py_LegacyLocaleDetected()) { * _Py_CoerceLegacyLocale(); * } * * See the documentation of the PYTHONCOERCECLOCALE setting for more details. * * Locale coercion also impacts the default error handler for the standard * streams: while the usual default is "strict", the default for the legacy * C locale and for any of the coercion target locales is "surrogateescape". */ int _Py_LegacyLocaleDetected(void) { #ifndef MS_WINDOWS /* On non-Windows systems, the C locale is considered a legacy locale */ /* XXX (ncoghlan): some platforms (notably Mac OS X) don't appear to treat * the POSIX locale as a simple alias for the C locale, so * we may also want to check for that explicitly. */ const char *ctype_loc = setlocale(LC_CTYPE, NULL); return ctype_loc != NULL && strcmp(ctype_loc, "C") == 0; #else /* Windows uses code pages instead of locales, so no locale is legacy */ return 0; #endif } static const char *_C_LOCALE_WARNING = "Python runtime initialized with LC_CTYPE=C (a locale with default ASCII " "encoding), which may cause Unicode compatibility problems. Using C.UTF-8, " "C.utf8, or UTF-8 (if available) as alternative Unicode-compatible " "locales is recommended.\n"; static int _legacy_locale_warnings_enabled(void) { const char *coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); return (coerce_c_locale != NULL && strncmp(coerce_c_locale, "warn", 5) == 0); } static void _emit_stderr_warning_for_legacy_locale(void) { if (_legacy_locale_warnings_enabled()) { if (_Py_LegacyLocaleDetected()) { fprintf(stderr, "%s", _C_LOCALE_WARNING); } } } typedef struct _CandidateLocale { const char *locale_name; /* The locale to try as a coercion target */ } _LocaleCoercionTarget; static _LocaleCoercionTarget _TARGET_LOCALES[] = { {"C.UTF-8"}, {"C.utf8"}, {"UTF-8"}, {NULL} }; static char * get_default_standard_stream_error_handler(void) { const char *ctype_loc = setlocale(LC_CTYPE, NULL); if (ctype_loc != NULL) { /* "surrogateescape" is the default in the legacy C locale */ if (strcmp(ctype_loc, "C") == 0) { return "surrogateescape"; } #ifdef PY_COERCE_C_LOCALE /* "surrogateescape" is the default in locale coercion target locales */ const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { if (strcmp(ctype_loc, target->locale_name) == 0) { return "surrogateescape"; } } #endif } /* Otherwise return NULL to request the typical default error handler */ return NULL; } #ifdef PY_COERCE_C_LOCALE static const char *_C_LOCALE_COERCION_WARNING = "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; static void _coerce_default_locale_settings(const _LocaleCoercionTarget *target) { const char *newloc = target->locale_name; /* Reset locale back to currently configured defaults */ _Py_SetLocaleFromEnv(LC_ALL); /* Set the relevant locale environment variable */ if (setenv("LC_CTYPE", newloc, 1)) { fprintf(stderr, "Error setting LC_CTYPE, skipping C locale coercion\n"); return; } if (_legacy_locale_warnings_enabled()) { fprintf(stderr, _C_LOCALE_COERCION_WARNING, newloc); } /* Reconfigure with the overridden environment variables */ _Py_SetLocaleFromEnv(LC_ALL); } #endif void _Py_CoerceLegacyLocale(void) { #ifdef PY_COERCE_C_LOCALE /* We ignore the Python -E and -I flags here, as the CLI needs to sort out * the locale settings *before* we try to do anything with the command * line arguments. For cross-platform debugging purposes, we also need * to give end users a way to force even scripts that are otherwise * isolated from their environment to use the legacy ASCII-centric C * locale. * * Ignoring -E and -I is safe from a security perspective, as we only use * the setting to turn *off* the implicit locale coercion, and anyone with * access to the process environment already has the ability to set * `LC_ALL=C` to override the C level locale settings anyway. */ const char *coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); if (coerce_c_locale == NULL || strncmp(coerce_c_locale, "0", 2) != 0) { /* PYTHONCOERCECLOCALE is not set, or is set to something other than "0" */ const char *locale_override = getenv("LC_ALL"); if (locale_override == NULL || *locale_override == '\0') { /* LC_ALL is also not set (or is set to an empty string) */ const _LocaleCoercionTarget *target = NULL; for (target = _TARGET_LOCALES; target->locale_name; target++) { const char *new_locale = setlocale(LC_CTYPE, target->locale_name); if (new_locale != NULL) { #if !defined(__APPLE__) && !defined(__ANDROID__) && \ defined(HAVE_LANGINFO_H) && defined(CODESET) /* Also ensure that nl_langinfo works in this locale */ char *codeset = nl_langinfo(CODESET); if (!codeset || *codeset == '\0') { /* CODESET is not set or empty, so skip coercion */ new_locale = NULL; _Py_SetLocaleFromEnv(LC_CTYPE); continue; } #endif /* Successfully configured locale, so make it the default */ _coerce_default_locale_settings(target); return; } } } } /* No C locale warning here, as Py_Initialize will emit one later */ #endif } /* _Py_SetLocaleFromEnv() is a wrapper around setlocale(category, "") to * isolate the idiosyncrasies of different libc implementations. It reads the * appropriate environment variable and uses its value to select the locale for * 'category'. */ char * _Py_SetLocaleFromEnv(int category) { #ifdef __ANDROID__ const char *locale; const char **pvar; #ifdef PY_COERCE_C_LOCALE const char *coerce_c_locale; #endif const char *utf8_locale = "C.UTF-8"; const char *env_var_set[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL, }; /* Android setlocale(category, "") doesn't check the environment variables * and incorrectly sets the "C" locale at API 24 and older APIs. We only * check the environment variables listed in env_var_set. */ for (pvar=env_var_set; *pvar; pvar++) { locale = getenv(*pvar); if (locale != NULL && *locale != '\0') { if (strcmp(locale, utf8_locale) == 0 || strcmp(locale, "en_US.UTF-8") == 0) { return setlocale(category, utf8_locale); } return setlocale(category, "C"); } } /* Android uses UTF-8, so explicitly set the locale to C.UTF-8 if none of * LC_ALL, LC_CTYPE, or LANG is set to a non-empty string. * Quote from POSIX section "8.2 Internationalization Variables": * "4. If the LANG environment variable is not set or is set to the empty * string, the implementation-defined default locale shall be used." */ #ifdef PY_COERCE_C_LOCALE coerce_c_locale = getenv("PYTHONCOERCECLOCALE"); if (coerce_c_locale == NULL || strcmp(coerce_c_locale, "0") != 0) { /* Some other ported code may check the environment variables (e.g. in * extension modules), so we make sure that they match the locale * configuration */ if (setenv("LC_CTYPE", utf8_locale, 1)) { fprintf(stderr, "Warning: failed setting the LC_CTYPE " "environment variable to %s\n", utf8_locale); } } #endif return setlocale(category, utf8_locale); #else /* __ANDROID__ */ return setlocale(category, ""); #endif /* __ANDROID__ */ } /* Global initializations. Can be undone by Py_Finalize(). Don't call this twice without an intervening Py_Finalize() call. Every call to Py_InitializeCore, Py_Initialize or Py_InitializeEx must have a corresponding call to Py_Finalize. Locking: you must hold the interpreter lock while calling these APIs. (If the lock has not yet been initialized, that's equivalent to having the lock, but you cannot use multiple threads.) */ /* Begin interpreter initialization * * On return, the first thread and interpreter state have been created, * but the compiler, signal handling, multithreading and * multiple interpreter support, and codec infrastructure are not yet * available. * * The import system will support builtin and frozen modules only. * The only supported io is writing to sys.stderr * * If any operation invoked by this function fails, a fatal error is * issued and the function does not return. * * Any code invoked from this function should *not* assume it has access * to the Python C API (unless the API is explicitly listed as being * safe to call without calling Py_Initialize first) */ /* TODO: Progressively move functionality from Py_BeginInitialization to * Py_ReadConfig and Py_EndInitialization */ _PyInitError _Py_InitializeCore(const _PyCoreConfig *config) { PyInterpreterState *interp; PyThreadState *tstate; PyObject *bimod, *sysmod, *pstderr; _PyCoreConfig core_config = _PyCoreConfig_INIT; _PyMainInterpreterConfig preinit_config = _PyMainInterpreterConfig_INIT; _PyInitError err; err = _PyRuntime_Initialize(); if (_Py_INIT_FAILED(err)) { return err; } if (config != NULL) { core_config = *config; } if (_PyMem_SetupAllocators(core_config.allocator) < 0) { return _Py_INIT_ERR("Unknown PYTHONMALLOC allocator"); } if (_PyRuntime.initialized) { return _Py_INIT_ERR("main interpreter already initialized"); } if (_PyRuntime.core_initialized) { return _Py_INIT_ERR("runtime core already initialized"); } /* Py_Finalize leaves _Py_Finalizing set in order to help daemon * threads behave a little more gracefully at interpreter shutdown. * We clobber it here so the new interpreter can start with a clean * slate. * * However, this may still lead to misbehaviour if there are daemon * threads still hanging around from a previous Py_Initialize/Finalize * pair :( */ _PyRuntime.finalizing = NULL; #ifndef MS_WINDOWS /* Set up the LC_CTYPE locale, so we can obtain the locale's charset without having to switch locales. */ _Py_SetLocaleFromEnv(LC_CTYPE); _emit_stderr_warning_for_legacy_locale(); #endif err = _Py_HashRandomization_Init(&core_config); if (_Py_INIT_FAILED(err)) { return err; } if (!core_config.use_hash_seed || core_config.hash_seed) { /* Random or non-zero hash seed */ Py_HashRandomizationFlag = 1; } err = _PyInterpreterState_Enable(&_PyRuntime); if (_Py_INIT_FAILED(err)) { return err; } interp = PyInterpreterState_New(); if (interp == NULL) return _Py_INIT_ERR("can't make main interpreter"); interp->core_config = core_config; interp->config = preinit_config; tstate = PyThreadState_New(interp); if (tstate == NULL) return _Py_INIT_ERR("can't make first thread"); (void) PyThreadState_Swap(tstate); /* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because destroying the GIL might fail when it is being referenced from another running thread (see issue #9901). Instead we destroy the previously created GIL here, which ensures that we can call Py_Initialize / Py_FinalizeEx multiple times. */ _PyEval_FiniThreads(); /* Auto-thread-state API */ _PyGILState_Init(interp, tstate); _Py_ReadyTypes(); if (!_PyFrame_Init()) return _Py_INIT_ERR("can't init frames"); if (!_PyLong_Init()) return _Py_INIT_ERR("can't init longs"); if (!PyByteArray_Init()) return _Py_INIT_ERR("can't init bytearray"); if (!_PyFloat_Init()) return _Py_INIT_ERR("can't init float"); PyObject *modules = PyDict_New(); if (modules == NULL) return _Py_INIT_ERR("can't make modules dictionary"); interp->modules = modules; err = _PySys_BeginInit(&sysmod); if (_Py_INIT_FAILED(err)) { return err; } interp->sysdict = PyModule_GetDict(sysmod); if (interp->sysdict == NULL) { return _Py_INIT_ERR("can't initialize sys dict"); } Py_INCREF(interp->sysdict); PyDict_SetItemString(interp->sysdict, "modules", modules); _PyImport_FixupBuiltin(sysmod, "sys", modules); /* Init Unicode implementation; relies on the codec registry */ if (_PyUnicode_Init() < 0) return _Py_INIT_ERR("can't initialize unicode"); if (_PyStructSequence_Init() < 0) return _Py_INIT_ERR("can't initialize structseq"); bimod = _PyBuiltin_Init(); if (bimod == NULL) return _Py_INIT_ERR("can't initialize builtins modules"); _PyImport_FixupBuiltin(bimod, "builtins", modules); interp->builtins = PyModule_GetDict(bimod); if (interp->builtins == NULL) return _Py_INIT_ERR("can't initialize builtins dict"); Py_INCREF(interp->builtins); /* initialize builtin exceptions */ _PyExc_Init(bimod); /* Set up a preliminary stderr printer until we have enough infrastructure for the io module in place. */ pstderr = PyFile_NewStdPrinter(fileno(stderr)); if (pstderr == NULL) return _Py_INIT_ERR("can't set preliminary stderr"); _PySys_SetObjectId(&PyId_stderr, pstderr); PySys_SetObject("__stderr__", pstderr); Py_DECREF(pstderr); err = _PyImport_Init(); if (_Py_INIT_FAILED(err)) { return err; } err = _PyImportHooks_Init(); if (_Py_INIT_FAILED(err)) { return err; } /* Initialize _warnings. */ if (_PyWarnings_InitWithConfig(&interp->core_config) == NULL) { return _Py_INIT_ERR("can't initialize warnings"); } /* This call sets up builtin and frozen import support */ if (!interp->core_config._disable_importlib) { err = initimport(interp, sysmod); if (_Py_INIT_FAILED(err)) { return err; } } /* Only when we get here is the runtime core fully initialized */ _PyRuntime.core_initialized = 1; return _Py_INIT_OK(); } /* Read configuration settings from standard locations * * This function doesn't make any changes to the interpreter state - it * merely populates any missing configuration settings. This allows an * embedding application to completely override a config option by * setting it before calling this function, or else modify the default * setting before passing the fully populated config to Py_EndInitialization. * * More advanced selective initialization tricks are possible by calling * this function multiple times with various preconfigured settings. */ _PyInitError _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config) { /* Signal handlers are installed by default */ if (config->install_signal_handlers < 0) { config->install_signal_handlers = 1; } if (config->program_name == NULL) { config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); if (config->program_name == NULL) { return _Py_INIT_NO_MEMORY(); } } return _Py_INIT_OK(); } void _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config) { #define CLEAR(ATTR) \ do { \ PyMem_RawFree(ATTR); \ ATTR = NULL; \ } while (0) CLEAR(config->module_search_path_env); CLEAR(config->home); CLEAR(config->program_name); #undef CLEAR } /* Update interpreter state based on supplied configuration settings * * After calling this function, most of the restrictions on the interpreter * are lifted. The only remaining incomplete settings are those related * to the main module (sys.argv[0], __main__ metadata) * * Calling this when the interpreter is not initializing, is already * initialized or without a valid current thread state is a fatal error. * Other errors should be reported as normal Python exceptions with a * non-zero return code. */ _PyInitError _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) { PyInterpreterState *interp; PyThreadState *tstate; _PyInitError err; if (!_PyRuntime.core_initialized) { return _Py_INIT_ERR("runtime core not initialized"); } if (_PyRuntime.initialized) { return _Py_INIT_ERR("main interpreter already initialized"); } /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); if (!tstate) return _Py_INIT_ERR("failed to read thread state"); interp = tstate->interp; if (!interp) return _Py_INIT_ERR("failed to get interpreter"); /* Now finish configuring the main interpreter */ interp->config = *config; if (interp->core_config._disable_importlib) { /* Special mode for freeze_importlib: run with no import system * * This means anything which needs support from extension modules * or pure Python code in the standard library won't work. */ _PyRuntime.initialized = 1; return _Py_INIT_OK(); } /* TODO: Report exceptions rather than fatal errors below here */ if (_PyTime_Init() < 0) return _Py_INIT_ERR("can't initialize time"); /* GetPath may initialize state that _PySys_EndInit locks in, and so has to be called first. */ err = _PyPathConfig_Init(&interp->config); if (_Py_INIT_FAILED(err)) { return err; } wchar_t *sys_path = Py_GetPath(); /* Finish setting up the sys module and import system */ PySys_SetPath(sys_path); if (_PySys_EndInit(interp->sysdict) < 0) return _Py_INIT_ERR("can't finish initializing sys"); err = initexternalimport(interp); if (_Py_INIT_FAILED(err)) { return err; } /* initialize the faulthandler module */ err = _PyFaulthandler_Init(interp->core_config.faulthandler); if (_Py_INIT_FAILED(err)) { return err; } err = initfsencoding(interp); if (_Py_INIT_FAILED(err)) { return err; } if (interp->config.install_signal_handlers) { err = initsigs(); /* Signal handling stuff, including initintr() */ if (_Py_INIT_FAILED(err)) { return err; } } if (_PyTraceMalloc_Init(interp->core_config.tracemalloc) < 0) return _Py_INIT_ERR("can't initialize tracemalloc"); err = add_main_module(interp); if (_Py_INIT_FAILED(err)) { return err; } err = init_sys_streams(); if (_Py_INIT_FAILED(err)) { return err; } /* Initialize warnings. */ if (PySys_HasWarnOptions()) { PyObject *warnings_module = PyImport_ImportModule("warnings"); if (warnings_module == NULL) { fprintf(stderr, "'import warnings' failed; traceback:\n"); PyErr_Print(); } Py_XDECREF(warnings_module); } _PyRuntime.initialized = 1; if (!Py_NoSiteFlag) { err = initsite(); /* Module site */ if (_Py_INIT_FAILED(err)) { return err; } } return _Py_INIT_OK(); } #undef _INIT_DEBUG_PRINT _PyInitError _Py_InitializeEx_Private(int install_sigs, int install_importlib) { _PyCoreConfig core_config = _PyCoreConfig_INIT; _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; _PyInitError err; /* TODO: Moar config options! */ core_config.ignore_environment = Py_IgnoreEnvironmentFlag; core_config._disable_importlib = !install_importlib; config.install_signal_handlers = install_sigs; err = _Py_InitializeCore(&core_config); if (_Py_INIT_FAILED(err)) { return err; } /* TODO: Print any exceptions raised by these operations */ err = _PyMainInterpreterConfig_Read(&config); if (_Py_INIT_FAILED(err)) { return err; } err = _Py_InitializeMainInterpreter(&config); if (_Py_INIT_FAILED(err)) { return err; } return _Py_INIT_OK(); } void Py_InitializeEx(int install_sigs) { _PyInitError err = _Py_InitializeEx_Private(install_sigs, 1); if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } } void Py_Initialize(void) { Py_InitializeEx(1); } #ifdef COUNT_ALLOCS extern void dump_counts(FILE*); #endif /* Flush stdout and stderr */ static int file_is_closed(PyObject *fobj) { int r; PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); if (tmp == NULL) { PyErr_Clear(); return 0; } r = PyObject_IsTrue(tmp); Py_DECREF(tmp); if (r < 0) PyErr_Clear(); return r > 0; } static int flush_std_files(void) { PyObject *fout = _PySys_GetObjectId(&PyId_stdout); PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); PyObject *tmp; int status = 0; if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); if (tmp == NULL) { PyErr_WriteUnraisable(fout); status = -1; } else Py_DECREF(tmp); } if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); if (tmp == NULL) { PyErr_Clear(); status = -1; } else Py_DECREF(tmp); } return status; } /* Undo the effect of Py_Initialize(). Beware: if multiple interpreter and/or thread states exist, these are not wiped out; only the current thread and interpreter state are deleted. But since everything else is deleted, those other interpreter and thread states should no longer be used. (XXX We should do better, e.g. wipe out all interpreters and threads.) Locking: as above. */ int Py_FinalizeEx(void) { PyInterpreterState *interp; PyThreadState *tstate; int status = 0; if (!_PyRuntime.initialized) return status; wait_for_thread_shutdown(); /* The interpreter is still entirely intact at this point, and the * exit funcs may be relying on that. In particular, if some thread * or exit func is still waiting to do an import, the import machinery * expects Py_IsInitialized() to return true. So don't say the * interpreter is uninitialized until after the exit funcs have run. * Note that Threading.py uses an exit func to do a join on all the * threads created thru it, so this also protects pending imports in * the threads created via Threading. */ call_py_exitfuncs(); /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); interp = tstate->interp; /* Remaining threads (e.g. daemon threads) will automatically exit after taking the GIL (in PyEval_RestoreThread()). */ _PyRuntime.finalizing = tstate; _PyRuntime.initialized = 0; _PyRuntime.core_initialized = 0; /* Flush sys.stdout and sys.stderr */ if (flush_std_files() < 0) { status = -1; } /* Disable signal handling */ PyOS_FiniInterrupts(); /* Collect garbage. This may call finalizers; it's nice to call these * before all modules are destroyed. * XXX If a __del__ or weakref callback is triggered here, and tries to * XXX import a module, bad things can happen, because Python no * XXX longer believes it's initialized. * XXX Fatal Python error: Interpreter not initialized (version mismatch?) * XXX is easy to provoke that way. I've also seen, e.g., * XXX Exception exceptions.ImportError: 'No module named sha' * XXX in <function callback at 0x008F5718> ignored * XXX but I'm unclear on exactly how that one happens. In any case, * XXX I haven't seen a real-life report of either of these. */ _PyGC_CollectIfEnabled(); #ifdef COUNT_ALLOCS /* With COUNT_ALLOCS, it helps to run GC multiple times: each collection might release some types from the type list, so they become garbage. */ while (_PyGC_CollectIfEnabled() > 0) /* nothing */; #endif /* Destroy all modules */ PyImport_Cleanup(); /* Flush sys.stdout and sys.stderr (again, in case more was printed) */ if (flush_std_files() < 0) { status = -1; } /* Collect final garbage. This disposes of cycles created by * class definitions, for example. * XXX This is disabled because it caused too many problems. If * XXX a __del__ or weakref callback triggers here, Python code has * XXX a hard time running, because even the sys module has been * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). * XXX One symptom is a sequence of information-free messages * XXX coming from threads (if a __del__ or callback is invoked, * XXX other threads can execute too, and any exception they encounter * XXX triggers a comedy of errors as subsystem after subsystem * XXX fails to find what it *expects* to find in sys to help report * XXX the exception and consequent unexpected failures). I've also * XXX seen segfaults then, after adding print statements to the * XXX Python code getting called. */ #if 0 _PyGC_CollectIfEnabled(); #endif /* Disable tracemalloc after all Python objects have been destroyed, so it is possible to use tracemalloc in objects destructor. */ _PyTraceMalloc_Fini(); /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ _PyImport_Fini(); /* Cleanup typeobject.c's internal caches. */ _PyType_Fini(); /* unload faulthandler module */ _PyFaulthandler_Fini(); /* Debugging stuff */ #ifdef COUNT_ALLOCS dump_counts(stderr); #endif /* dump hash stats */ _PyHash_Fini(); #ifdef Py_REF_DEBUG if (interp->core_config.show_ref_count) { _PyDebug_PrintTotalRefs(); } #endif #ifdef Py_TRACE_REFS /* Display all objects still alive -- this can invoke arbitrary * __repr__ overrides, so requires a mostly-intact interpreter. * Alas, a lot of stuff may still be alive now that will be cleaned * up later. */ if (Py_GETENV("PYTHONDUMPREFS")) _Py_PrintReferences(stderr); #endif /* Py_TRACE_REFS */ /* Clear interpreter state and all thread states. */ PyInterpreterState_Clear(interp); /* Now we decref the exception classes. After this point nothing can raise an exception. That's okay, because each Fini() method below has been checked to make sure no exceptions are ever raised. */ _PyExc_Fini(); /* Sundry finalizers */ PyMethod_Fini(); PyFrame_Fini(); PyCFunction_Fini(); PyTuple_Fini(); PyList_Fini(); PySet_Fini(); PyBytes_Fini(); PyByteArray_Fini(); PyLong_Fini(); PyFloat_Fini(); PyDict_Fini(); PySlice_Fini(); _PyGC_Fini(); _Py_HashRandomization_Fini(); _PyArg_Fini(); PyAsyncGen_Fini(); /* Cleanup Unicode implementation */ _PyUnicode_Fini(); /* reset file system default encoding */ if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { PyMem_RawFree((char*)Py_FileSystemDefaultEncoding); Py_FileSystemDefaultEncoding = NULL; } /* XXX Still allocated: - various static ad-hoc pointers to interned strings - int and float free list blocks - whatever various modules and libraries allocate */ PyGrammar_RemoveAccelerators(&_PyParser_Grammar); /* Cleanup auto-thread-state */ _PyGILState_Fini(); /* Delete current thread. After this, many C API calls become crashy. */ PyThreadState_Swap(NULL); PyInterpreterState_Delete(interp); #ifdef Py_TRACE_REFS /* Display addresses (& refcnts) of all objects still alive. * An address can be used to find the repr of the object, printed * above by _Py_PrintReferences. */ if (Py_GETENV("PYTHONDUMPREFS")) _Py_PrintReferenceAddresses(stderr); #endif /* Py_TRACE_REFS */ #ifdef WITH_PYMALLOC if (_PyMem_PymallocEnabled()) { char *opt = Py_GETENV("PYTHONMALLOCSTATS"); if (opt != NULL && *opt != '\0') _PyObject_DebugMallocStats(stderr); } #endif call_ll_exitfuncs(); _PyPathConfig_Fini(); _PyRuntime_Finalize(); return status; } void Py_Finalize(void) { Py_FinalizeEx(); } /* Create and initialize a new interpreter and thread, and return the new thread. This requires that Py_Initialize() has been called first. Unsuccessful initialization yields a NULL pointer. Note that *no* exception information is available even in this case -- the exception information is held in the thread, and there is no thread. Locking: as above. */ static _PyInitError new_interpreter(PyThreadState **tstate_p) { PyInterpreterState *interp; PyThreadState *tstate, *save_tstate; PyObject *bimod, *sysmod; _PyInitError err; if (!_PyRuntime.initialized) { return _Py_INIT_ERR("Py_Initialize must be called first"); } /* Issue #10915, #15751: The GIL API doesn't work with multiple interpreters: disable PyGILState_Check(). */ _PyGILState_check_enabled = 0; interp = PyInterpreterState_New(); if (interp == NULL) { *tstate_p = NULL; return _Py_INIT_OK(); } tstate = PyThreadState_New(interp); if (tstate == NULL) { PyInterpreterState_Delete(interp); *tstate_p = NULL; return _Py_INIT_OK(); } save_tstate = PyThreadState_Swap(tstate); /* Copy the current interpreter config into the new interpreter */ if (save_tstate != NULL) { interp->core_config = save_tstate->interp->core_config; interp->config = save_tstate->interp->config; } else { /* No current thread state, copy from the main interpreter */ PyInterpreterState *main_interp = PyInterpreterState_Main(); interp->core_config = main_interp->core_config; interp->config = main_interp->config; } err = _PyPathConfig_Init(&interp->config); if (_Py_INIT_FAILED(err)) { return err; } wchar_t *sys_path = Py_GetPath(); /* XXX The following is lax in error checking */ PyObject *modules = PyDict_New(); if (modules == NULL) { return _Py_INIT_ERR("can't make modules dictionary"); } interp->modules = modules; sysmod = _PyImport_FindBuiltin("sys", modules); if (sysmod != NULL) { interp->sysdict = PyModule_GetDict(sysmod); if (interp->sysdict == NULL) goto handle_error; Py_INCREF(interp->sysdict); PyDict_SetItemString(interp->sysdict, "modules", modules); PySys_SetPath(sys_path); _PySys_EndInit(interp->sysdict); } bimod = _PyImport_FindBuiltin("builtins", modules); if (bimod != NULL) { interp->builtins = PyModule_GetDict(bimod); if (interp->builtins == NULL) goto handle_error; Py_INCREF(interp->builtins); } /* initialize builtin exceptions */ _PyExc_Init(bimod); if (bimod != NULL && sysmod != NULL) { PyObject *pstderr; /* Set up a preliminary stderr printer until we have enough infrastructure for the io module in place. */ pstderr = PyFile_NewStdPrinter(fileno(stderr)); if (pstderr == NULL) { return _Py_INIT_ERR("can't set preliminary stderr"); } _PySys_SetObjectId(&PyId_stderr, pstderr); PySys_SetObject("__stderr__", pstderr); Py_DECREF(pstderr); err = _PyImportHooks_Init(); if (_Py_INIT_FAILED(err)) { return err; } err = initimport(interp, sysmod); if (_Py_INIT_FAILED(err)) { return err; } err = initexternalimport(interp); if (_Py_INIT_FAILED(err)) { return err; } err = initfsencoding(interp); if (_Py_INIT_FAILED(err)) { return err; } err = init_sys_streams(); if (_Py_INIT_FAILED(err)) { return err; } err = add_main_module(interp); if (_Py_INIT_FAILED(err)) { return err; } if (!Py_NoSiteFlag) { err = initsite(); if (_Py_INIT_FAILED(err)) { return err; } } } if (PyErr_Occurred()) { goto handle_error; } *tstate_p = tstate; return _Py_INIT_OK(); handle_error: /* Oops, it didn't work. Undo it all. */ PyErr_PrintEx(0); PyThreadState_Clear(tstate); PyThreadState_Swap(save_tstate); PyThreadState_Delete(tstate); PyInterpreterState_Delete(interp); *tstate_p = NULL; return _Py_INIT_OK(); } PyThreadState * Py_NewInterpreter(void) { PyThreadState *tstate; _PyInitError err = new_interpreter(&tstate); if (_Py_INIT_FAILED(err)) { _Py_FatalInitError(err); } return tstate; } /* Delete an interpreter and its last thread. This requires that the given thread state is current, that the thread has no remaining frames, and that it is its interpreter's only remaining thread. It is a fatal error to violate these constraints. (Py_FinalizeEx() doesn't have these constraints -- it zaps everything, regardless.) Locking: as above. */ void Py_EndInterpreter(PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; if (tstate != PyThreadState_GET()) Py_FatalError("Py_EndInterpreter: thread is not current"); if (tstate->frame != NULL) Py_FatalError("Py_EndInterpreter: thread still has a frame"); wait_for_thread_shutdown(); if (tstate != interp->tstate_head || tstate->next != NULL) Py_FatalError("Py_EndInterpreter: not the last thread"); PyImport_Cleanup(); PyInterpreterState_Clear(interp); PyThreadState_Swap(NULL); PyInterpreterState_Delete(interp); } #ifdef MS_WINDOWS static wchar_t *progname = L"python"; #else static wchar_t *progname = L"python3"; #endif void Py_SetProgramName(wchar_t *pn) { if (pn && *pn) progname = pn; } wchar_t * Py_GetProgramName(void) { return progname; } static wchar_t *default_home = NULL; void Py_SetPythonHome(wchar_t *home) { default_home = home; } wchar_t* Py_GetPythonHome(void) { /* Use a static buffer to avoid heap memory allocation failure. Py_GetPythonHome() doesn't allow to report error, and the caller doesn't release memory. */ static wchar_t buffer[MAXPATHLEN+1]; if (default_home) { return default_home; } char *home = Py_GETENV("PYTHONHOME"); if (!home) { return NULL; } size_t size = Py_ARRAY_LENGTH(buffer); size_t r = mbstowcs(buffer, home, size); if (r == (size_t)-1 || r >= size) { /* conversion failed or the static buffer is too small */ return NULL; } return buffer; } /* Add the __main__ module */ static _PyInitError add_main_module(PyInterpreterState *interp) { PyObject *m, *d, *loader, *ann_dict; m = PyImport_AddModule("__main__"); if (m == NULL) return _Py_INIT_ERR("can't create __main__ module"); d = PyModule_GetDict(m); ann_dict = PyDict_New(); if ((ann_dict == NULL) || (PyDict_SetItemString(d, "__annotations__", ann_dict) < 0)) { return _Py_INIT_ERR("Failed to initialize __main__.__annotations__"); } Py_DECREF(ann_dict); if (PyDict_GetItemString(d, "__builtins__") == NULL) { PyObject *bimod = PyImport_ImportModule("builtins"); if (bimod == NULL) { return _Py_INIT_ERR("Failed to retrieve builtins module"); } if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { return _Py_INIT_ERR("Failed to initialize __main__.__builtins__"); } Py_DECREF(bimod); } /* Main is a little special - imp.is_builtin("__main__") will return * False, but BuiltinImporter is still the most appropriate initial * setting for its __loader__ attribute. A more suitable value will * be set if __main__ gets further initialized later in the startup * process. */ loader = PyDict_GetItemString(d, "__loader__"); if (loader == NULL || loader == Py_None) { PyObject *loader = PyObject_GetAttrString(interp->importlib, "BuiltinImporter"); if (loader == NULL) { return _Py_INIT_ERR("Failed to retrieve BuiltinImporter"); } if (PyDict_SetItemString(d, "__loader__", loader) < 0) { return _Py_INIT_ERR("Failed to initialize __main__.__loader__"); } Py_DECREF(loader); } return _Py_INIT_OK(); } static _PyInitError initfsencoding(PyInterpreterState *interp) { PyObject *codec; #ifdef MS_WINDOWS if (Py_LegacyWindowsFSEncodingFlag) { Py_FileSystemDefaultEncoding = "mbcs"; Py_FileSystemDefaultEncodeErrors = "replace"; } else { Py_FileSystemDefaultEncoding = "utf-8"; Py_FileSystemDefaultEncodeErrors = "surrogatepass"; } #else if (Py_FileSystemDefaultEncoding == NULL) { Py_FileSystemDefaultEncoding = get_locale_encoding(); if (Py_FileSystemDefaultEncoding == NULL) { return _Py_INIT_ERR("Unable to get the locale encoding"); } Py_HasFileSystemDefaultEncoding = 0; interp->fscodec_initialized = 1; return _Py_INIT_OK(); } #endif /* the encoding is mbcs, utf-8 or ascii */ codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); if (!codec) { /* Such error can only occurs in critical situations: no more * memory, import a module of the standard library failed, * etc. */ return _Py_INIT_ERR("unable to load the file system codec"); } Py_DECREF(codec); interp->fscodec_initialized = 1; return _Py_INIT_OK(); } /* Import the site module (not into __main__ though) */ static _PyInitError initsite(void) { PyObject *m; m = PyImport_ImportModule("site"); if (m == NULL) { return _Py_INIT_USER_ERR("Failed to import the site module"); } Py_DECREF(m); return _Py_INIT_OK(); } /* Check if a file descriptor is valid or not. Return 0 if the file descriptor is invalid, return non-zero otherwise. */ static int is_valid_fd(int fd) { #ifdef __APPLE__ /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect such error. */ struct stat st; return (fstat(fd, &st) == 0); #else int fd2; if (fd < 0) return 0; _Py_BEGIN_SUPPRESS_IPH /* Prefer dup() over fstat(). fstat() can require input/output whereas dup() doesn't, there is a low risk of EMFILE/ENFILE at Python startup. */ fd2 = dup(fd); if (fd2 >= 0) close(fd2); _Py_END_SUPPRESS_IPH return fd2 >= 0; #endif } /* returns Py_None if the fd is not valid */ static PyObject* create_stdio(PyObject* io, int fd, int write_mode, const char* name, const char* encoding, const char* errors) { PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; const char* mode; const char* newline; PyObject *line_buffering, *write_through; int buffering, isatty; _Py_IDENTIFIER(open); _Py_IDENTIFIER(isatty); _Py_IDENTIFIER(TextIOWrapper); _Py_IDENTIFIER(mode); if (!is_valid_fd(fd)) Py_RETURN_NONE; /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper depends on the presence of a read1() method which only exists on buffered streams. */ if (Py_UnbufferedStdioFlag && write_mode) buffering = 0; else buffering = -1; if (write_mode) mode = "wb"; else mode = "rb"; buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", fd, mode, buffering, Py_None, Py_None, /* encoding, errors */ Py_None, 0); /* newline, closefd */ if (buf == NULL) goto error; if (buffering) { _Py_IDENTIFIER(raw); raw = _PyObject_GetAttrId(buf, &PyId_raw); if (raw == NULL) goto error; } else { raw = buf; Py_INCREF(raw); } #ifdef MS_WINDOWS /* Windows console IO is always UTF-8 encoded */ if (PyWindowsConsoleIO_Check(raw)) encoding = "utf-8"; #endif text = PyUnicode_FromString(name); if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) goto error; res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); if (res == NULL) goto error; isatty = PyObject_IsTrue(res); Py_DECREF(res); if (isatty == -1) goto error; if (Py_UnbufferedStdioFlag) write_through = Py_True; else write_through = Py_False; if (isatty && !Py_UnbufferedStdioFlag) line_buffering = Py_True; else line_buffering = Py_False; Py_CLEAR(raw); Py_CLEAR(text); #ifdef MS_WINDOWS /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" newlines to "\n". sys.stdout and sys.stderr: translate "\n" to "\r\n". */ newline = NULL; #else /* sys.stdin: split lines at "\n". sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ newline = "\n"; #endif stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO", buf, encoding, errors, newline, line_buffering, write_through); Py_CLEAR(buf); if (stream == NULL) goto error; if (write_mode) mode = "w"; else mode = "r"; text = PyUnicode_FromString(mode); if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) goto error; Py_CLEAR(text); return stream; error: Py_XDECREF(buf); Py_XDECREF(stream); Py_XDECREF(text); Py_XDECREF(raw); if (PyErr_ExceptionMatches(PyExc_OSError) && !is_valid_fd(fd)) { /* Issue #24891: the file descriptor was closed after the first is_valid_fd() check was called. Ignore the OSError and set the stream to None. */ PyErr_Clear(); Py_RETURN_NONE; } return NULL; } /* Initialize sys.stdin, stdout, stderr and builtins.open */ static _PyInitError init_sys_streams(void) { PyObject *iomod = NULL, *wrapper; PyObject *bimod = NULL; PyObject *m; PyObject *std = NULL; int fd; PyObject * encoding_attr; char *pythonioencoding = NULL, *encoding, *errors; _PyInitError res = _Py_INIT_OK(); /* Hack to avoid a nasty recursion issue when Python is invoked in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { goto error; } Py_DECREF(m); if (!(m = PyImport_ImportModule("encodings.latin_1"))) { goto error; } Py_DECREF(m); if (!(bimod = PyImport_ImportModule("builtins"))) { goto error; } if (!(iomod = PyImport_ImportModule("io"))) { goto error; } if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { goto error; } /* Set builtins.open */ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { Py_DECREF(wrapper); goto error; } Py_DECREF(wrapper); encoding = _Py_StandardStreamEncoding; errors = _Py_StandardStreamErrors; if (!encoding || !errors) { pythonioencoding = Py_GETENV("PYTHONIOENCODING"); if (pythonioencoding) { char *err; pythonioencoding = _PyMem_Strdup(pythonioencoding); if (pythonioencoding == NULL) { PyErr_NoMemory(); goto error; } err = strchr(pythonioencoding, ':'); if (err) { *err = '\0'; err++; if (*err && !errors) { errors = err; } } if (*pythonioencoding && !encoding) { encoding = pythonioencoding; } } if (!errors && !(pythonioencoding && *pythonioencoding)) { /* Choose the default error handler based on the current locale */ errors = get_default_standard_stream_error_handler(); } } /* Set sys.stdin */ fd = fileno(stdin); /* Under some conditions stdin, stdout and stderr may not be connected * and fileno() may point to an invalid file descriptor. For example * GUI apps don't have valid standard streams by default. */ std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); if (std == NULL) goto error; PySys_SetObject("__stdin__", std); _PySys_SetObjectId(&PyId_stdin, std); Py_DECREF(std); /* Set sys.stdout */ fd = fileno(stdout); std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); if (std == NULL) goto error; PySys_SetObject("__stdout__", std); _PySys_SetObjectId(&PyId_stdout, std); Py_DECREF(std); #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ /* Set sys.stderr, replaces the preliminary stderr */ fd = fileno(stderr); std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); if (std == NULL) goto error; /* Same as hack above, pre-import stderr's codec to avoid recursion when import.c tries to write to stderr in verbose mode. */ encoding_attr = PyObject_GetAttrString(std, "encoding"); if (encoding_attr != NULL) { const char *std_encoding = PyUnicode_AsUTF8(encoding_attr); if (std_encoding != NULL) { PyObject *codec_info = _PyCodec_Lookup(std_encoding); Py_XDECREF(codec_info); } Py_DECREF(encoding_attr); } PyErr_Clear(); /* Not a fatal error if codec isn't available */ if (PySys_SetObject("__stderr__", std) < 0) { Py_DECREF(std); goto error; } if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { Py_DECREF(std); goto error; } Py_DECREF(std); #endif goto done; error: res = _Py_INIT_ERR("can't initialize sys standard streams"); done: /* We won't need them anymore. */ if (_Py_StandardStreamEncoding) { PyMem_RawFree(_Py_StandardStreamEncoding); _Py_StandardStreamEncoding = NULL; } if (_Py_StandardStreamErrors) { PyMem_RawFree(_Py_StandardStreamErrors); _Py_StandardStreamErrors = NULL; } PyMem_Free(pythonioencoding); Py_XDECREF(bimod); Py_XDECREF(iomod); return res; } static void _Py_FatalError_DumpTracebacks(int fd) { fputc('\n', stderr); fflush(stderr); /* display the current Python stack */ _Py_DumpTracebackThreads(fd, NULL, NULL); } /* Print the current exception (if an exception is set) with its traceback, or display the current Python stack. Don't call PyErr_PrintEx() and the except hook, because Py_FatalError() is called on catastrophic cases. Return 1 if the traceback was displayed, 0 otherwise. */ static int _Py_FatalError_PrintExc(int fd) { PyObject *ferr, *res; PyObject *exception, *v, *tb; int has_tb; if (PyThreadState_GET() == NULL) { /* The GIL is released: trying to acquire it is likely to deadlock, just give up. */ return 0; } PyErr_Fetch(&exception, &v, &tb); if (exception == NULL) { /* No current exception */ return 0; } ferr = _PySys_GetObjectId(&PyId_stderr); if (ferr == NULL || ferr == Py_None) { /* sys.stderr is not set yet or set to None, no need to try to display the exception */ return 0; } PyErr_NormalizeException(&exception, &v, &tb); if (tb == NULL) { tb = Py_None; Py_INCREF(tb); } PyException_SetTraceback(v, tb); if (exception == NULL) { /* PyErr_NormalizeException() failed */ return 0; } has_tb = (tb != Py_None); PyErr_Display(exception, v, tb); Py_XDECREF(exception); Py_XDECREF(v); Py_XDECREF(tb); /* sys.stderr may be buffered: call sys.stderr.flush() */ res = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); if (res == NULL) PyErr_Clear(); else Py_DECREF(res); return has_tb; } /* Print fatal error message and abort */ #ifdef MS_WINDOWS static void fatal_output_debug(const char *msg) { /* buffer of 256 bytes allocated on the stack */ WCHAR buffer[256 / sizeof(WCHAR)]; size_t buflen = Py_ARRAY_LENGTH(buffer) - 1; size_t msglen; OutputDebugStringW(L"Fatal Python error: "); msglen = strlen(msg); while (msglen) { size_t i; if (buflen > msglen) { buflen = msglen; } /* Convert the message to wchar_t. This uses a simple one-to-one conversion, assuming that the this error message actually uses ASCII only. If this ceases to be true, we will have to convert. */ for (i=0; i < buflen; ++i) { buffer[i] = msg[i]; } buffer[i] = L'\0'; OutputDebugStringW(buffer); msg += buflen; msglen -= buflen; } OutputDebugStringW(L"\n"); } #endif static void _Py_NO_RETURN fatal_error(const char *prefix, const char *msg, int status) { const int fd = fileno(stderr); static int reentrant = 0; if (reentrant) { /* Py_FatalError() caused a second fatal error. Example: flush_std_files() raises a recursion error. */ goto exit; } reentrant = 1; fprintf(stderr, "Fatal Python error: "); if (prefix) { fputs(prefix, stderr); fputs(": ", stderr); } if (msg) { fputs(msg, stderr); } else { fprintf(stderr, "<message not set>"); } fputs("\n", stderr); fflush(stderr); /* it helps in Windows debug build */ /* Print the exception (if an exception is set) with its traceback, * or display the current Python stack. */ if (!_Py_FatalError_PrintExc(fd)) { _Py_FatalError_DumpTracebacks(fd); } /* The main purpose of faulthandler is to display the traceback. This function already did its best to display a traceback. Disable faulthandler to prevent writing a second traceback on abort(). */ _PyFaulthandler_Fini(); /* Check if the current Python thread hold the GIL */ if (PyThreadState_GET() != NULL) { /* Flush sys.stdout and sys.stderr */ flush_std_files(); } #ifdef MS_WINDOWS fatal_output_debug(msg); #endif /* MS_WINDOWS */ exit: if (status < 0) { #if defined(MS_WINDOWS) && defined(_DEBUG) DebugBreak(); #endif abort(); } else { exit(status); } } void Py_FatalError(const char *msg) { fatal_error(NULL, msg, -1); } void _Py_FatalInitError(_PyInitError err) { /* On "user" error: exit with status 1. For all other errors, call abort(). */ int status = err.user_err ? 1 : -1; fatal_error(err.prefix, err.msg, status); } /* Clean up and exit */ # include "pythread.h" /* For the atexit module. */ void _Py_PyAtExit(void (*func)(void)) { _PyRuntime.pyexitfunc = func; } static void call_py_exitfuncs(void) { if (_PyRuntime.pyexitfunc == NULL) return; (*_PyRuntime.pyexitfunc)(); PyErr_Clear(); } /* Wait until threading._shutdown completes, provided the threading module was imported in the first place. The shutdown routine will wait until all non-daemon "threading" threads have completed. */ static void wait_for_thread_shutdown(void) { _Py_IDENTIFIER(_shutdown); PyObject *result; PyObject *threading = _PyImport_GetModuleId(&PyId_threading); if (threading == NULL) { /* threading not imported */ PyErr_Clear(); return; } result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); if (result == NULL) { PyErr_WriteUnraisable(threading); } else { Py_DECREF(result); } Py_DECREF(threading); } #define NEXITFUNCS 32 int Py_AtExit(void (*func)(void)) { if (_PyRuntime.nexitfuncs >= NEXITFUNCS) return -1; _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func; return 0; } static void call_ll_exitfuncs(void) { while (_PyRuntime.nexitfuncs > 0) (*_PyRuntime.exitfuncs[--_PyRuntime.nexitfuncs])(); fflush(stdout); fflush(stderr); } void Py_Exit(int sts) { if (Py_FinalizeEx() < 0) { sts = 120; } exit(sts); } static _PyInitError initsigs(void) { #ifdef SIGPIPE PyOS_setsig(SIGPIPE, SIG_IGN); #endif #ifdef SIGXFZ PyOS_setsig(SIGXFZ, SIG_IGN); #endif #ifdef SIGXFSZ PyOS_setsig(SIGXFSZ, SIG_IGN); #endif PyOS_InitInterrupts(); /* May imply initsignal() */ if (PyErr_Occurred()) { return _Py_INIT_ERR("can't import signal"); } return _Py_INIT_OK(); } /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. * * All of the code in this function must only use async-signal-safe functions, * listed at `man 7 signal` or * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. */ void _Py_RestoreSignals(void) { #ifdef SIGPIPE PyOS_setsig(SIGPIPE, SIG_DFL); #endif #ifdef SIGXFZ PyOS_setsig(SIGXFZ, SIG_DFL); #endif #ifdef SIGXFSZ PyOS_setsig(SIGXFSZ, SIG_DFL); #endif } /* * The file descriptor fd is considered ``interactive'' if either * a) isatty(fd) is TRUE, or * b) the -i flag was given, and the filename associated with * the descriptor is NULL or "<stdin>" or "???". */ int Py_FdIsInteractive(FILE *fp, const char *filename) { if (isatty((int)fileno(fp))) return 1; if (!Py_InteractiveFlag) return 0; return (filename == NULL) || (strcmp(filename, "<stdin>") == 0) || (strcmp(filename, "???") == 0); } /* Wrappers around sigaction() or signal(). */ PyOS_sighandler_t PyOS_getsig(int sig) { #ifdef HAVE_SIGACTION struct sigaction context; if (sigaction(sig, NULL, &context) == -1) return SIG_ERR; return context.sa_handler; #else PyOS_sighandler_t handler; /* Special signal handling for the secure CRT in Visual Studio 2005 */ #if defined(_MSC_VER) && _MSC_VER >= 1400 switch (sig) { /* Only these signals are valid */ case SIGINT: case SIGILL: case SIGFPE: case SIGSEGV: case SIGTERM: case SIGBREAK: case SIGABRT: break; /* Don't call signal() with other values or it will assert */ default: return SIG_ERR; } #endif /* _MSC_VER && _MSC_VER >= 1400 */ handler = signal(sig, SIG_IGN); if (handler != SIG_ERR) signal(sig, handler); return handler; #endif } /* * All of the code in this function must only use async-signal-safe functions, * listed at `man 7 signal` or * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. */ PyOS_sighandler_t PyOS_setsig(int sig, PyOS_sighandler_t handler) { #ifdef HAVE_SIGACTION /* Some code in Modules/signalmodule.c depends on sigaction() being * used here if HAVE_SIGACTION is defined. Fix that if this code * changes to invalidate that assumption. */ struct sigaction context, ocontext; context.sa_handler = handler; sigemptyset(&context.sa_mask); context.sa_flags = 0; if (sigaction(sig, &context, &ocontext) == -1) return SIG_ERR; return ocontext.sa_handler; #else PyOS_sighandler_t oldhandler; oldhandler = signal(sig, handler); #ifdef HAVE_SIGINTERRUPT siginterrupt(sig, 1); #endif return oldhandler; #endif } #ifdef __cplusplus } #endif