-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
/
pylifecycle.c
2991 lines (2509 loc) · 81.3 KB
/
pylifecycle.c
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Python interpreter top-level routines, including init/exit */
#include "Python.h"
#include "pycore_ceval.h" // _PyEval_FiniGIL()
#include "pycore_context.h" // _PyContext_Init()
#include "pycore_fileutils.h" // _Py_ResetForceASCII()
#include "pycore_import.h" // _PyImport_BootstrapImp()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
#include "pycore_pathconfig.h" // _PyConfig_WritePathConfig()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pylifecycle.h" // _PyErr_Print()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
#include <locale.h> // setlocale()
#if defined(__APPLE__)
#include <mach-o/loader.h>
#endif
#ifdef HAVE_SIGNAL_H
# include <signal.h> // SIG_IGN
#endif
#ifdef HAVE_LANGINFO_H
# include <langinfo.h> // nl_langinfo(CODESET)
#endif
#ifdef MS_WINDOWS
# undef BYTE
# include "windows.h"
extern PyTypeObject PyWindowsConsoleIO_Type;
# define PyWindowsConsoleIO_Check(op) \
(PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
#endif
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
_Py_IDENTIFIER(flush);
_Py_IDENTIFIER(name);
_Py_IDENTIFIER(stdin);
_Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr);
_Py_IDENTIFIER(threading);
#ifdef __cplusplus
extern "C" {
#endif
/* Forward declarations */
static PyStatus add_main_module(PyInterpreterState *interp);
static PyStatus init_import_site(void);
static PyStatus init_set_builtins_open(void);
static PyStatus init_sys_streams(PyThreadState *tstate);
static void wait_for_thread_shutdown(PyThreadState *tstate);
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
int _Py_UnhandledKeyboardInterrupt = 0;
/* The following places the `_PyRuntime` structure in a location that can be
* found without any external information. This is meant to ease access to the
* interpreter state for various runtime debugging tools, but is *not* an
* officially supported feature */
#if defined(MS_WINDOWS)
#pragma section("PyRuntime", read, write)
__declspec(allocate("PyRuntime"))
#elif defined(__APPLE__)
__attribute__((
section(SEG_DATA ",PyRuntime")
))
#endif
_PyRuntimeState _PyRuntime
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
__attribute__ ((section (".PyRuntime")))
#endif
= _PyRuntimeState_INIT;
static int runtime_initialized = 0;
PyStatus
_PyRuntime_Initialize(void)
{
/* XXX We only initialize once in the process, which aligns with
the static initialization of the former globals now found in
_PyRuntime. However, _PyRuntime *should* be initialized with
every Py_Initialize() call, but doing so breaks the runtime.
This is because the runtime state is not properly finalized
currently. */
if (runtime_initialized) {
return _PyStatus_OK();
}
runtime_initialized = 1;
return _PyRuntimeState_Init(&_PyRuntime);
}
void
_PyRuntime_Finalize(void)
{
_PyRuntimeState_Fini(&_PyRuntime);
runtime_initialized = 0;
}
int
_Py_IsFinalizing(void)
{
return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
}
/* Hack to force loading of object files */
int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
PyOS_mystrnicmp; /* Python/pystrcmp.o */
/* 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;
}
/* 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 int
init_importlib(PyThreadState *tstate, PyObject *sysmod)
{
assert(!_PyErr_Occurred(tstate));
PyInterpreterState *interp = tstate->interp;
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
// Import _importlib through its frozen version, _frozen_importlib.
if (verbose) {
PySys_FormatStderr("import _frozen_importlib # frozen\n");
}
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
return -1;
}
PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed
if (importlib == NULL) {
return -1;
}
interp->importlib = Py_NewRef(importlib);
// Import the _imp module
if (verbose) {
PySys_FormatStderr("import _imp # builtin\n");
}
PyObject *imp_mod = _PyImport_BootstrapImp(tstate);
if (imp_mod == NULL) {
return -1;
}
if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
Py_DECREF(imp_mod);
return -1;
}
// Install importlib as the implementation of import
PyObject *value = PyObject_CallMethod(importlib, "_install",
"OO", sysmod, imp_mod);
Py_DECREF(imp_mod);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
assert(!_PyErr_Occurred(tstate));
return 0;
}
static PyStatus
init_importlib_external(PyThreadState *tstate)
{
PyObject *value;
value = PyObject_CallMethod(tstate->interp->importlib,
"_install_external_importers", "");
if (value == NULL) {
_PyErr_Print(tstate);
return _PyStatus_ERR("external importer setup failed");
}
Py_DECREF(value);
return _PyImportZip_Init(tstate);
}
/* 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(int warn)
{
#ifndef MS_WINDOWS
if (!warn) {
const char *locale_override = getenv("LC_ALL");
if (locale_override != NULL && *locale_override != '\0') {
/* Don't coerce C locale if the LC_ALL environment variable
is set */
return 0;
}
}
/* 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
}
#ifndef MS_WINDOWS
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 void
emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
{
const PyPreConfig *preconfig = &runtime->preconfig;
if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected(1)) {
PySys_FormatStderr("%s", _C_LOCALE_WARNING);
}
}
#endif /* !defined(MS_WINDOWS) */
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}
};
int
_Py_IsLocaleCoercionTarget(const char *ctype_loc)
{
const _LocaleCoercionTarget *target = NULL;
for (target = _TARGET_LOCALES; target->locale_name; target++) {
if (strcmp(ctype_loc, target->locale_name) == 0) {
return 1;
}
}
return 0;
}
#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 int
_coerce_default_locale_settings(int warn, 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 0;
}
if (warn) {
fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);
}
/* Reconfigure with the overridden environment variables */
_Py_SetLocaleFromEnv(LC_ALL);
return 1;
}
#endif
int
_Py_CoerceLegacyLocale(int warn)
{
int coerced = 0;
#ifdef PY_COERCE_C_LOCALE
char *oldloc = NULL;
oldloc = _PyMem_RawStrdup(setlocale(LC_CTYPE, NULL));
if (oldloc == NULL) {
return coerced;
}
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(_Py_FORCE_UTF8_LOCALE) && 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 */
coerced = _coerce_default_locale_settings(warn, target);
goto done;
}
}
}
/* No C locale warning here, as Py_Initialize will emit one later */
setlocale(LC_CTYPE, oldloc);
done:
PyMem_RawFree(oldloc);
#endif
return coerced;
}
/* _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)
{
char *res;
#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
res = setlocale(category, utf8_locale);
#else /* !defined(__ANDROID__) */
res = setlocale(category, "");
#endif
_Py_ResetForceASCII();
return res;
}
static int
interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
{
const PyConfig *config = &tstate->interp->config;
if (!only_update_path_config) {
PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
return -1;
}
}
if (_Py_IsMainInterpreter(tstate->interp)) {
PyStatus status = _PyConfig_WritePathConfig(config);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
return -1;
}
}
// Update the sys module for the new configuration
if (_PySys_UpdateConfig(tstate) < 0) {
return -1;
}
return 0;
}
int
_PyInterpreterState_SetConfig(const PyConfig *src_config)
{
PyThreadState *tstate = PyThreadState_Get();
int res = -1;
PyConfig config;
PyConfig_InitPythonConfig(&config);
PyStatus status = _PyConfig_Copy(&config, src_config);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
goto done;
}
status = PyConfig_Read(&config);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
goto done;
}
status = _PyConfig_Copy(&tstate->interp->config, &config);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
goto done;
}
res = interpreter_update_config(tstate, 0);
done:
PyConfig_Clear(&config);
return res;
}
/* Global initializations. Can be undone by Py_Finalize(). Don't
call this twice without an intervening Py_Finalize() call.
Every call to Py_InitializeFromConfig, 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.)
*/
static PyStatus
pyinit_core_reconfigure(_PyRuntimeState *runtime,
PyThreadState **tstate_p,
const PyConfig *config)
{
PyStatus status;
PyThreadState *tstate = _PyThreadState_GET();
if (!tstate) {
return _PyStatus_ERR("failed to read thread state");
}
*tstate_p = tstate;
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter");
}
status = _PyConfig_Write(config, runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyConfig_Copy(&interp->config, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
config = _PyInterpreterState_GetConfig(interp);
if (config->_install_importlib) {
status = _PyConfig_WritePathConfig(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
return _PyStatus_OK();
}
static PyStatus
pycore_init_runtime(_PyRuntimeState *runtime,
const PyConfig *config)
{
if (runtime->initialized) {
return _PyStatus_ERR("main interpreter already initialized");
}
PyStatus status = _PyConfig_Write(config, runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
/* 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 :(
*/
_PyRuntimeState_SetFinalizing(runtime, NULL);
status = _Py_HashRandomization_Init(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyInterpreterState_Enable(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_OK();
}
static PyStatus
init_interp_create_gil(PyThreadState *tstate)
{
PyStatus status;
/* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
only called here. */
_PyEval_FiniGIL(tstate->interp);
/* Auto-thread-state API */
status = _PyGILState_SetTstate(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
/* Create the GIL and take it */
status = _PyEval_InitGIL(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_OK();
}
static PyStatus
pycore_create_interpreter(_PyRuntimeState *runtime,
const PyConfig *config,
PyThreadState **tstate_p)
{
/* Auto-thread-state API */
PyStatus status = _PyGILState_Init(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter");
}
assert(_Py_IsMainInterpreter(interp));
status = _PyConfig_Copy(&interp->config, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
PyThreadState *tstate = PyThreadState_New(interp);
if (tstate == NULL) {
return _PyStatus_ERR("can't make first thread");
}
(void) PyThreadState_Swap(tstate);
status = init_interp_create_gil(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
*tstate_p = tstate;
return _PyStatus_OK();
}
static PyStatus
pycore_init_singletons(PyInterpreterState *interp)
{
PyStatus status;
if (_PyLong_Init(interp) < 0) {
return _PyStatus_ERR("can't init longs");
}
if (_Py_IsMainInterpreter(interp)) {
_PyFloat_Init();
}
status = _PyBytes_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyUnicode_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyTuple_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
return _PyStatus_OK();
}
static PyStatus
pycore_init_types(PyInterpreterState *interp)
{
PyStatus status;
int is_main_interp = _Py_IsMainInterpreter(interp);
if (is_main_interp) {
if (_PyStructSequence_Init() < 0) {
return _PyStatus_ERR("can't initialize structseq");
}
status = _PyTypes_Init();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (_PyLong_InitTypes() < 0) {
return _PyStatus_ERR("can't init int type");
}
status = _PyUnicode_InitTypes();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
if (is_main_interp) {
if (_PyFloat_InitTypes() < 0) {
return _PyStatus_ERR("can't init float");
}
}
status = _PyExc_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyErr_InitTypes();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (is_main_interp) {
if (!_PyContext_Init()) {
return _PyStatus_ERR("can't init context");
}
}
return _PyStatus_OK();
}
static PyStatus
pycore_init_builtins(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
PyObject *bimod = _PyBuiltin_Init(interp);
if (bimod == NULL) {
goto error;
}
if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
goto error;
}
PyObject *builtins_dict = PyModule_GetDict(bimod);
if (builtins_dict == NULL) {
goto error;
}
Py_INCREF(builtins_dict);
interp->builtins = builtins_dict;
PyStatus status = _PyBuiltins_AddExceptions(bimod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
interp->builtins_copy = PyDict_Copy(interp->builtins);
if (interp->builtins_copy == NULL) {
goto error;
}
Py_DECREF(bimod);
// Get the __import__ function
PyObject *import_func = _PyDict_GetItemStringWithError(interp->builtins,
"__import__");
if (import_func == NULL) {
goto error;
}
interp->import_func = Py_NewRef(import_func);
assert(!_PyErr_Occurred(tstate));
return _PyStatus_OK();
error:
Py_XDECREF(bimod);
return _PyStatus_ERR("can't initialize builtins module");
}
static PyStatus
pycore_interp_init(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
PyStatus status;
PyObject *sysmod = NULL;
// Create singletons before the first PyType_Ready() call, since
// PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
// and the empty tuple singletons (tp_bases).
status = pycore_init_singletons(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
// The GC must be initialized before the first GC collection.
status = _PyGC_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = pycore_init_types(interp);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
if (_PyWarnings_InitState(interp) < 0) {
return _PyStatus_ERR("can't initialize warnings");
}
status = _PyAtExit_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PySys_Create(tstate, &sysmod);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
status = pycore_init_builtins(tstate);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
if (config->_install_importlib) {
/* This call sets up builtin and frozen import support */
if (init_importlib(tstate, sysmod) < 0) {
return _PyStatus_ERR("failed to initialize importlib");
}
}
done:
/* sys.modules['sys'] contains a strong reference to the module */
Py_XDECREF(sysmod);
return status;
}
static PyStatus
pyinit_config(_PyRuntimeState *runtime,
PyThreadState **tstate_p,
const PyConfig *config)
{
PyStatus status = pycore_init_runtime(runtime, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
PyThreadState *tstate;
status = pycore_create_interpreter(runtime, config, &tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
*tstate_p = tstate;
status = pycore_interp_init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
/* Only when we get here is the runtime core fully initialized */
runtime->core_initialized = 1;
return _PyStatus_OK();
}
PyStatus
_Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
{
PyStatus status;
if (src_config == NULL) {
return _PyStatus_ERR("preinitialization config is NULL");
}
status = _PyRuntime_Initialize();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
_PyRuntimeState *runtime = &_PyRuntime;
if (runtime->preinitialized) {
/* If it's already configured: ignored the new configuration */
return _PyStatus_OK();
}
/* Note: preinitialized remains 1 on error, it is only set to 0
at exit on success. */
runtime->preinitializing = 1;
PyPreConfig config;
status = _PyPreConfig_InitFromPreConfig(&config, src_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyPreConfig_Read(&config, args);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
status = _PyPreConfig_Write(&config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
runtime->preinitializing = 0;
runtime->preinitialized = 1;
return _PyStatus_OK();
}
PyStatus
Py_PreInitializeFromBytesArgs(const PyPreConfig *src_config, Py_ssize_t argc, char **argv)
{
_PyArgv args = {.use_bytes_argv = 1, .argc = argc, .bytes_argv = argv};
return _Py_PreInitializeFromPyArgv(src_config, &args);
}
PyStatus
Py_PreInitializeFromArgs(const PyPreConfig *src_config, Py_ssize_t argc, wchar_t **argv)
{
_PyArgv args = {.use_bytes_argv = 0, .argc = argc, .wchar_argv = argv};
return _Py_PreInitializeFromPyArgv(src_config, &args);
}
PyStatus
Py_PreInitialize(const PyPreConfig *src_config)
{
return _Py_PreInitializeFromPyArgv(src_config, NULL);
}
PyStatus
_Py_PreInitializeFromConfig(const PyConfig *config,
const _PyArgv *args)
{
assert(config != NULL);
PyStatus status = _PyRuntime_Initialize();
if (_PyStatus_EXCEPTION(status)) {
return status;
}
_PyRuntimeState *runtime = &_PyRuntime;
if (runtime->preinitialized) {
/* Already initialized: do nothing */
return _PyStatus_OK();
}
PyPreConfig preconfig;
_PyPreConfig_InitFromConfig(&preconfig, config);
if (!config->parse_argv) {
return Py_PreInitialize(&preconfig);
}
else if (args == NULL) {
_PyArgv config_args = {
.use_bytes_argv = 0,
.argc = config->argv.length,
.wchar_argv = config->argv.items};
return _Py_PreInitializeFromPyArgv(&preconfig, &config_args);
}
else {
return _Py_PreInitializeFromPyArgv(&preconfig, args);
}
}
/* 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)
*/