-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
/
initconfig.c
4492 lines (3860 loc) · 123 KB
/
initconfig.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
#include "Python.h"
#include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors
#include "pycore_getopt.h" // _PyOS_GetOpt()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // _PyInterpreterState.runtime
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD
#include "pycore_pathconfig.h" // _Py_path_config
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig()
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_pystats.h" // _Py_StatsOn()
#include "pycore_sysmodule.h" // _PySys_SetIntMaxStrDigits()
#include "osdefs.h" // DELIM
#include <locale.h> // setlocale()
#include <stdlib.h> // getenv()
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
# ifdef HAVE_IO_H
# include <io.h>
# endif
# ifdef HAVE_FCNTL_H
# include <fcntl.h> // O_BINARY
# endif
#endif
#include "config_common.h"
/* --- PyConfig setters ------------------------------------------- */
typedef PyObject* (*config_sys_flag_setter) (int value);
static PyObject*
config_sys_flag_long(int value)
{
return PyLong_FromLong(value);
}
static PyObject*
config_sys_flag_not(int value)
{
value = (!value);
return config_sys_flag_long(value);
}
/* --- PyConfig spec ---------------------------------------------- */
typedef enum {
PyConfig_MEMBER_INT = 0,
PyConfig_MEMBER_UINT = 1,
PyConfig_MEMBER_ULONG = 2,
PyConfig_MEMBER_BOOL = 3,
PyConfig_MEMBER_WSTR = 10,
PyConfig_MEMBER_WSTR_OPT = 11,
PyConfig_MEMBER_WSTR_LIST = 12,
} PyConfigMemberType;
typedef enum {
// Option which cannot be get or set by PyConfig_Get() and PyConfig_Set()
PyConfig_MEMBER_INIT_ONLY = 0,
// Option which cannot be set by PyConfig_Set()
PyConfig_MEMBER_READ_ONLY = 1,
// Public option: can be get and set by PyConfig_Get() and PyConfig_Set()
PyConfig_MEMBER_PUBLIC = 2,
} PyConfigMemberVisibility;
typedef struct {
const char *attr;
int flag_index;
config_sys_flag_setter flag_setter;
} PyConfigSysSpec;
typedef struct {
const char *name;
size_t offset;
PyConfigMemberType type;
PyConfigMemberVisibility visibility;
PyConfigSysSpec sys;
} PyConfigSpec;
#define SPEC(MEMBER, TYPE, VISIBILITY, sys) \
{#MEMBER, offsetof(PyConfig, MEMBER), \
PyConfig_MEMBER_##TYPE, PyConfig_MEMBER_##VISIBILITY, sys}
#define SYS_ATTR(name) {name, -1, NULL}
#define SYS_FLAG_SETTER(index, setter) {NULL, index, setter}
#define SYS_FLAG(index) SYS_FLAG_SETTER(index, NULL)
#define NO_SYS SYS_ATTR(NULL)
// Update _test_embed_set_config when adding new members
static const PyConfigSpec PYCONFIG_SPEC[] = {
// --- Public options -----------
SPEC(argv, WSTR_LIST, PUBLIC, SYS_ATTR("argv")),
SPEC(base_exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_exec_prefix")),
SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")),
SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")),
SPEC(bytes_warning, UINT, PUBLIC, SYS_FLAG(9)),
SPEC(exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("exec_prefix")),
SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")),
SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)),
SPEC(int_max_str_digits, UINT, PUBLIC, NO_SYS),
SPEC(interactive, BOOL, PUBLIC, SYS_FLAG(2)),
SPEC(module_search_paths, WSTR_LIST, PUBLIC, SYS_ATTR("path")),
SPEC(optimization_level, UINT, PUBLIC, SYS_FLAG(3)),
SPEC(parser_debug, BOOL, PUBLIC, SYS_FLAG(0)),
SPEC(platlibdir, WSTR, PUBLIC, SYS_ATTR("platlibdir")),
SPEC(prefix, WSTR_OPT, PUBLIC, SYS_ATTR("prefix")),
SPEC(pycache_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("pycache_prefix")),
SPEC(quiet, BOOL, PUBLIC, SYS_FLAG(10)),
SPEC(stdlib_dir, WSTR_OPT, PUBLIC, SYS_ATTR("_stdlib_dir")),
SPEC(use_environment, BOOL, PUBLIC, SYS_FLAG_SETTER(7, config_sys_flag_not)),
SPEC(verbose, UINT, PUBLIC, SYS_FLAG(8)),
SPEC(warnoptions, WSTR_LIST, PUBLIC, SYS_ATTR("warnoptions")),
SPEC(write_bytecode, BOOL, PUBLIC, SYS_FLAG_SETTER(4, config_sys_flag_not)),
SPEC(xoptions, WSTR_LIST, PUBLIC, SYS_ATTR("_xoptions")),
// --- Read-only options -----------
#ifdef Py_STATS
SPEC(_pystats, BOOL, READ_ONLY, NO_SYS),
#endif
SPEC(buffered_stdio, BOOL, READ_ONLY, NO_SYS),
SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS),
SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS),
SPEC(configure_c_stdio, BOOL, READ_ONLY, NO_SYS),
SPEC(cpu_count, INT, READ_ONLY, NO_SYS),
SPEC(dev_mode, BOOL, READ_ONLY, NO_SYS), // sys.flags.dev_mode
SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS),
SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS),
#ifdef Py_GIL_DISABLED
SPEC(enable_gil, INT, READ_ONLY, NO_SYS),
SPEC(tlbc_enabled, INT, READ_ONLY, NO_SYS),
#endif
SPEC(faulthandler, BOOL, READ_ONLY, NO_SYS),
SPEC(filesystem_encoding, WSTR, READ_ONLY, NO_SYS),
SPEC(filesystem_errors, WSTR, READ_ONLY, NO_SYS),
SPEC(hash_seed, ULONG, READ_ONLY, NO_SYS),
SPEC(home, WSTR_OPT, READ_ONLY, NO_SYS),
SPEC(import_time, BOOL, READ_ONLY, NO_SYS),
SPEC(install_signal_handlers, BOOL, READ_ONLY, NO_SYS),
SPEC(isolated, BOOL, READ_ONLY, NO_SYS), // sys.flags.isolated
#ifdef MS_WINDOWS
SPEC(legacy_windows_stdio, BOOL, READ_ONLY, NO_SYS),
#endif
SPEC(malloc_stats, BOOL, READ_ONLY, NO_SYS),
SPEC(orig_argv, WSTR_LIST, READ_ONLY, SYS_ATTR("orig_argv")),
SPEC(parse_argv, BOOL, READ_ONLY, NO_SYS),
SPEC(pathconfig_warnings, BOOL, READ_ONLY, NO_SYS),
SPEC(perf_profiling, UINT, READ_ONLY, NO_SYS),
SPEC(program_name, WSTR, READ_ONLY, NO_SYS),
SPEC(run_command, WSTR_OPT, READ_ONLY, NO_SYS),
SPEC(run_filename, WSTR_OPT, READ_ONLY, NO_SYS),
SPEC(run_module, WSTR_OPT, READ_ONLY, NO_SYS),
#ifdef Py_DEBUG
SPEC(run_presite, WSTR_OPT, READ_ONLY, NO_SYS),
#endif
SPEC(safe_path, BOOL, READ_ONLY, NO_SYS),
SPEC(show_ref_count, BOOL, READ_ONLY, NO_SYS),
SPEC(site_import, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_site
SPEC(skip_source_first_line, BOOL, READ_ONLY, NO_SYS),
SPEC(stdio_encoding, WSTR, READ_ONLY, NO_SYS),
SPEC(stdio_errors, WSTR, READ_ONLY, NO_SYS),
SPEC(tracemalloc, UINT, READ_ONLY, NO_SYS),
SPEC(use_frozen_modules, BOOL, READ_ONLY, NO_SYS),
SPEC(use_hash_seed, BOOL, READ_ONLY, NO_SYS),
#ifdef __APPLE__
SPEC(use_system_logger, BOOL, PUBLIC, NO_SYS),
#endif
SPEC(user_site_directory, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_user_site
SPEC(warn_default_encoding, BOOL, READ_ONLY, NO_SYS),
// --- Init-only options -----------
SPEC(_config_init, UINT, INIT_ONLY, NO_SYS),
SPEC(_init_main, BOOL, INIT_ONLY, NO_SYS),
SPEC(_install_importlib, BOOL, INIT_ONLY, NO_SYS),
SPEC(_is_python_build, BOOL, INIT_ONLY, NO_SYS),
SPEC(module_search_paths_set, BOOL, INIT_ONLY, NO_SYS),
SPEC(pythonpath_env, WSTR_OPT, INIT_ONLY, NO_SYS),
SPEC(sys_path_0, WSTR_OPT, INIT_ONLY, NO_SYS),
// Array terminator
{NULL, 0, 0, 0, NO_SYS},
};
#undef SPEC
#define SPEC(MEMBER, TYPE, VISIBILITY) \
{#MEMBER, offsetof(PyPreConfig, MEMBER), PyConfig_MEMBER_##TYPE, \
PyConfig_MEMBER_##VISIBILITY, NO_SYS}
static const PyConfigSpec PYPRECONFIG_SPEC[] = {
// --- Read-only options -----------
SPEC(allocator, INT, READ_ONLY),
SPEC(coerce_c_locale, BOOL, READ_ONLY),
SPEC(coerce_c_locale_warn, BOOL, READ_ONLY),
SPEC(configure_locale, BOOL, READ_ONLY),
#ifdef MS_WINDOWS
SPEC(legacy_windows_fs_encoding, BOOL, READ_ONLY),
#endif
SPEC(utf8_mode, BOOL, READ_ONLY),
// --- Init-only options -----------
// Members already present in PYCONFIG_SPEC
SPEC(_config_init, INT, INIT_ONLY),
SPEC(dev_mode, BOOL, INIT_ONLY),
SPEC(isolated, BOOL, INIT_ONLY),
SPEC(parse_argv, BOOL, INIT_ONLY),
SPEC(use_environment, BOOL, INIT_ONLY),
// Array terminator
{NULL, 0, 0, 0, NO_SYS},
};
#undef SPEC
#undef SYS_ATTR
#undef SYS_FLAG_SETTER
#undef SYS_FLAG
#undef NO_SYS
// Forward declarations
static PyObject*
config_get(const PyConfig *config, const PyConfigSpec *spec,
int use_sys);
/* --- Command line options --------------------------------------- */
/* Short usage message (with %s for argv0) */
static const char usage_line[] =
"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
/* Long help message */
/* Lines sorted by option name; keep in sync with usage_envvars* below */
static const char usage_help[] = "\
Options (and corresponding environment variables):\n\
-b : issue warnings about converting bytes/bytearray to str and comparing\n\
bytes/bytearray with str or bytes with int. (-bb: issue errors)\n\
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
-c cmd : program passed in as string (terminates option list)\n\
-d : turn on parser debugging output (for experts only, only works on\n\
debug builds); also PYTHONDEBUG=x\n\
-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
-h : print this help message and exit (also -? or --help)\n\
-i : inspect interactively after running script; forces a prompt even\n\
if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
-I : isolate Python from the user's environment (implies -E and -s)\n\
-m mod : run library module as a script (terminates option list)\n\
-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
.pyc extension; also PYTHONOPTIMIZE=x\n\
-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
.pyc extension\n\
-P : don't prepend a potentially unsafe path to sys.path; also\n\
PYTHONSAFEPATH\n\
-q : don't print version and copyright messages on interactive startup\n\
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE=x\n\
-S : don't imply 'import site' on initialization\n\
-u : force the stdout and stderr streams to be unbuffered;\n\
this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
can be supplied multiple times to increase verbosity\n\
-V : print the Python version number and exit (also --version)\n\
when given twice, print more information about the build\n\
-W arg : warning control; arg is action:message:category:module:lineno\n\
also PYTHONWARNINGS=arg\n\
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
-X opt : set implementation-specific option\n\
--check-hash-based-pycs always|default|never:\n\
control how Python invalidates hash-based .pyc files\n\
--help-env: print help about Python environment variables and exit\n\
--help-xoptions: print help about implementation-specific -X options and exit\n\
--help-all: print complete help information and exit\n\
\n\
Arguments:\n\
file : program read from script file\n\
- : program read from stdin (default; interactive mode if a tty)\n\
arg ...: arguments passed to program in sys.argv[1:]\n\
";
static const char usage_xoptions[] = "\
The following implementation-specific options are available:\n\
-X cpu_count=N: override the return value of os.cpu_count();\n\
-X cpu_count=default cancels overriding; also PYTHON_CPU_COUNT\n\
-X dev : enable Python Development Mode; also PYTHONDEVMODE\n\
-X faulthandler: dump the Python traceback on fatal errors;\n\
also PYTHONFAULTHANDLER\n\
-X frozen_modules=[on|off]: whether to use frozen modules; the default is \"on\"\n\
for installed Python and \"off\" for a local build;\n\
also PYTHON_FROZEN_MODULES\n\
"
#ifdef Py_GIL_DISABLED
"-X gil=[0|1]: enable (1) or disable (0) the GIL; also PYTHON_GIL\n"
#endif
"\
-X importtime: show how long each import takes; also PYTHONPROFILEIMPORTTIME\n\
-X int_max_str_digits=N: limit the size of int<->str conversions;\n\
0 disables the limit; also PYTHONINTMAXSTRDIGITS\n\
-X no_debug_ranges: don't include extra location information in code objects;\n\
also PYTHONNODEBUGRANGES\n\
-X perf: support the Linux \"perf\" profiler; also PYTHONPERFSUPPORT=1\n\
"
#ifdef Py_DEBUG
"-X presite=MOD: import this module before site; also PYTHON_PRESITE\n"
#endif
"\
-X pycache_prefix=PATH: write .pyc files to a parallel tree instead of to the\n\
code tree; also PYTHONPYCACHEPREFIX\n\
"
#ifdef Py_STATS
"-X pystats: enable pystats collection at startup; also PYTHONSTATS\n"
#endif
"\
-X showrefcount: output the total reference count and number of used\n\
memory blocks when the program finishes or after each statement in\n\
the interactive interpreter; only works on debug builds\n"
#ifdef Py_GIL_DISABLED
"-X tlbc=[0|1]: enable (1) or disable (0) thread-local bytecode. Also\n\
PYTHON_TLBC\n"
#endif
"\
-X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
-X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\
-X warn_default_encoding: enable opt-in EncodingWarning for 'encoding=None';\n\
also PYTHONWARNDEFAULTENCODING\
";
/* Envvars that don't have equivalent command-line options are listed first */
static const char usage_envvars[] =
"Environment variables that change behavior:\n"
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
" default module search path. The result is sys.path.\n"
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
" The default module search path uses %s.\n"
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
"PYTHONCASEOK : ignore case in 'import' statements (Windows)\n"
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
" to seed the hashes of str and bytes objects. It can also be\n"
" set to an integer in the range [0,4294967295] to get hash\n"
" values with a predictable seed.\n"
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
" install debug hooks.\n"
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
" display of locale coercion and locale compatibility warnings\n"
" on stderr.\n"
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
" debugger. It can be set to the callable of your debugger of\n"
" choice.\n"
"PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n"
" various kinds of output. Setting it to 0 deactivates\n"
" this behavior.\n"
"PYTHON_HISTORY : the location of a .python_history file.\n"
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
#ifdef Py_TRACE_REFS
"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n"
"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n"
#endif
#ifdef __APPLE__
"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n"
#endif
#ifdef MS_WINDOWS
"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n"
"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n"
#endif
"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n"
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
"\n"
"These variables have equivalent command-line options (see --help for details):\n"
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
"PYTHONDEBUG : enable parser debug mode (-d)\n"
"PYTHONDEVMODE : enable Python Development Mode (-X dev)\n"
"PYTHONDONTWRITEBYTECODE: don't write .pyc files (-B)\n"
"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors (-X faulthandler)\n"
"PYTHON_FROZEN_MODULES: whether to use frozen modules; the default is \"on\"\n"
" for installed Python and \"off\" for a local build\n"
" (-X frozen_modules)\n"
#ifdef Py_GIL_DISABLED
"PYTHON_GIL : when set to 0, disables the GIL (-X gil)\n"
#endif
"PYTHONINSPECT : inspect interactively after running script (-i)\n"
"PYTHONINTMAXSTRDIGITS: limit the size of int<->str conversions;\n"
" 0 disables the limit (-X int_max_str_digits=N)\n"
"PYTHONNODEBUGRANGES: don't include extra location information in code objects\n"
" (-X no_debug_ranges)\n"
"PYTHONNOUSERSITE: disable user site directory (-s)\n"
"PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n"
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n"
" (-X perf_jit)\n"
#ifdef Py_DEBUG
"PYTHON_PRESITE: import this module before site (-X presite)\n"
#endif
"PYTHONPROFILEIMPORTTIME: show how long each import takes (-X importtime)\n"
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files\n"
" (-X pycache_prefix)\n"
"PYTHONSAFEPATH : don't prepend a potentially unsafe path to sys.path.\n"
#ifdef Py_STATS
"PYTHONSTATS : turns on statistics gathering (-X pystats)\n"
#endif
#ifdef Py_GIL_DISABLED
"PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n"
#endif
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
"PYTHONVERBOSE : trace import statements (-v)\n"
"PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'\n"
" (-X warn_default_encoding)\n"
"PYTHONWARNINGS : warning control (-W)\n"
;
#if defined(MS_WINDOWS)
# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
#else
# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
#endif
/* --- Global configuration variables ----------------------------- */
/* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
stdin and stdout error handler to "surrogateescape". */
int Py_UTF8Mode = 0;
int Py_DebugFlag = 0; /* Needed by parser.c */
int Py_VerboseFlag = 0; /* Needed by import.c */
int Py_QuietFlag = 0; /* Needed by sysmodule.c */
int Py_InteractiveFlag = 0; /* Previously, was used by Py_FdIsInteractive() */
int Py_InspectFlag = 0; /* Needed to determine whether to exit at SystemExit */
int Py_OptimizeFlag = 0; /* Needed by compile.c */
int Py_NoSiteFlag = 0; /* Suppress 'import site' */
int Py_BytesWarningFlag = 0; /* Warn on str(bytes) and str(buffer) */
int Py_FrozenFlag = 0; /* Needed by getpath.c */
int Py_IgnoreEnvironmentFlag = 0; /* e.g. PYTHONPATH, PYTHONHOME */
int Py_DontWriteBytecodeFlag = 0; /* 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
static PyObject *
_Py_GetGlobalVariablesAsDict(void)
{
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
PyObject *dict, *obj;
dict = PyDict_New();
if (dict == NULL) {
return NULL;
}
#define SET_ITEM(KEY, EXPR) \
do { \
obj = (EXPR); \
if (obj == NULL) { \
return NULL; \
} \
int res = PyDict_SetItemString(dict, (KEY), obj); \
Py_DECREF(obj); \
if (res < 0) { \
goto fail; \
} \
} while (0)
#define SET_ITEM_INT(VAR) \
SET_ITEM(#VAR, PyLong_FromLong(VAR))
#define FROM_STRING(STR) \
((STR != NULL) ? \
PyUnicode_FromString(STR) \
: Py_NewRef(Py_None))
#define SET_ITEM_STR(VAR) \
SET_ITEM(#VAR, FROM_STRING(VAR))
SET_ITEM_STR(Py_FileSystemDefaultEncoding);
SET_ITEM_INT(Py_HasFileSystemDefaultEncoding);
SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors);
SET_ITEM_INT(_Py_HasFileSystemDefaultEncodeErrors);
SET_ITEM_INT(Py_UTF8Mode);
SET_ITEM_INT(Py_DebugFlag);
SET_ITEM_INT(Py_VerboseFlag);
SET_ITEM_INT(Py_QuietFlag);
SET_ITEM_INT(Py_InteractiveFlag);
SET_ITEM_INT(Py_InspectFlag);
SET_ITEM_INT(Py_OptimizeFlag);
SET_ITEM_INT(Py_NoSiteFlag);
SET_ITEM_INT(Py_BytesWarningFlag);
SET_ITEM_INT(Py_FrozenFlag);
SET_ITEM_INT(Py_IgnoreEnvironmentFlag);
SET_ITEM_INT(Py_DontWriteBytecodeFlag);
SET_ITEM_INT(Py_NoUserSiteDirectory);
SET_ITEM_INT(Py_UnbufferedStdioFlag);
SET_ITEM_INT(Py_HashRandomizationFlag);
SET_ITEM_INT(Py_IsolatedFlag);
#ifdef MS_WINDOWS
SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag);
SET_ITEM_INT(Py_LegacyWindowsStdioFlag);
#endif
return dict;
fail:
Py_DECREF(dict);
return NULL;
#undef FROM_STRING
#undef SET_ITEM
#undef SET_ITEM_INT
#undef SET_ITEM_STR
_Py_COMP_DIAG_POP
}
char*
Py_GETENV(const char *name)
{
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
if (Py_IgnoreEnvironmentFlag) {
return NULL;
}
return getenv(name);
_Py_COMP_DIAG_POP
}
/* --- PyStatus ----------------------------------------------- */
PyStatus PyStatus_Ok(void)
{ return _PyStatus_OK(); }
PyStatus PyStatus_Error(const char *err_msg)
{
assert(err_msg != NULL);
return (PyStatus){._type = _PyStatus_TYPE_ERROR,
.err_msg = err_msg};
}
PyStatus PyStatus_NoMemory(void)
{ return PyStatus_Error("memory allocation failed"); }
PyStatus PyStatus_Exit(int exitcode)
{ return _PyStatus_EXIT(exitcode); }
int PyStatus_IsError(PyStatus status)
{ return _PyStatus_IS_ERROR(status); }
int PyStatus_IsExit(PyStatus status)
{ return _PyStatus_IS_EXIT(status); }
int PyStatus_Exception(PyStatus status)
{ return _PyStatus_EXCEPTION(status); }
void
_PyErr_SetFromPyStatus(PyStatus status)
{
if (!_PyStatus_IS_ERROR(status)) {
PyErr_Format(PyExc_SystemError,
"_PyErr_SetFromPyStatus() status is not an error");
return;
}
const char *err_msg = status.err_msg;
if (err_msg == NULL || strlen(err_msg) == 0) {
PyErr_Format(PyExc_SystemError,
"_PyErr_SetFromPyStatus() status has no error message");
return;
}
if (strcmp(err_msg, _PyStatus_NO_MEMORY_ERRMSG) == 0) {
PyErr_NoMemory();
return;
}
const char *func = status.func;
if (func) {
PyErr_Format(PyExc_RuntimeError, "%s: %s", func, err_msg);
}
else {
PyErr_Format(PyExc_RuntimeError, "%s", err_msg);
}
}
/* --- PyWideStringList ------------------------------------------------ */
#ifndef NDEBUG
int
_PyWideStringList_CheckConsistency(const PyWideStringList *list)
{
assert(list->length >= 0);
if (list->length != 0) {
assert(list->items != NULL);
}
for (Py_ssize_t i = 0; i < list->length; i++) {
assert(list->items[i] != NULL);
}
return 1;
}
#endif /* Py_DEBUG */
void
_PyWideStringList_Clear(PyWideStringList *list)
{
assert(_PyWideStringList_CheckConsistency(list));
for (Py_ssize_t i=0; i < list->length; i++) {
PyMem_RawFree(list->items[i]);
}
PyMem_RawFree(list->items);
list->length = 0;
list->items = NULL;
}
int
_PyWideStringList_Copy(PyWideStringList *list, const PyWideStringList *list2)
{
assert(_PyWideStringList_CheckConsistency(list));
assert(_PyWideStringList_CheckConsistency(list2));
if (list2->length == 0) {
_PyWideStringList_Clear(list);
return 0;
}
PyWideStringList copy = _PyWideStringList_INIT;
size_t size = list2->length * sizeof(list2->items[0]);
copy.items = PyMem_RawMalloc(size);
if (copy.items == NULL) {
return -1;
}
for (Py_ssize_t i=0; i < list2->length; i++) {
wchar_t *item = _PyMem_RawWcsdup(list2->items[i]);
if (item == NULL) {
_PyWideStringList_Clear(©);
return -1;
}
copy.items[i] = item;
copy.length = i + 1;
}
_PyWideStringList_Clear(list);
*list = copy;
return 0;
}
PyStatus
PyWideStringList_Insert(PyWideStringList *list,
Py_ssize_t index, const wchar_t *item)
{
Py_ssize_t len = list->length;
if (len == PY_SSIZE_T_MAX) {
/* length+1 would overflow */
return _PyStatus_NO_MEMORY();
}
if (index < 0) {
return _PyStatus_ERR("PyWideStringList_Insert index must be >= 0");
}
if (index > len) {
index = len;
}
wchar_t *item2 = _PyMem_RawWcsdup(item);
if (item2 == NULL) {
return _PyStatus_NO_MEMORY();
}
size_t size = (len + 1) * sizeof(list->items[0]);
wchar_t **items2 = (wchar_t **)PyMem_RawRealloc(list->items, size);
if (items2 == NULL) {
PyMem_RawFree(item2);
return _PyStatus_NO_MEMORY();
}
if (index < len) {
memmove(&items2[index + 1],
&items2[index],
(len - index) * sizeof(items2[0]));
}
items2[index] = item2;
list->items = items2;
list->length++;
return _PyStatus_OK();
}
PyStatus
PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
{
return PyWideStringList_Insert(list, list->length, item);
}
PyStatus
_PyWideStringList_Extend(PyWideStringList *list, const PyWideStringList *list2)
{
for (Py_ssize_t i = 0; i < list2->length; i++) {
PyStatus status = PyWideStringList_Append(list, list2->items[i]);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
return _PyStatus_OK();
}
static int
_PyWideStringList_Find(PyWideStringList *list, const wchar_t *item)
{
for (Py_ssize_t i = 0; i < list->length; i++) {
if (wcscmp(list->items[i], item) == 0) {
return 1;
}
}
return 0;
}
PyObject*
_PyWideStringList_AsList(const PyWideStringList *list)
{
assert(_PyWideStringList_CheckConsistency(list));
PyObject *pylist = PyList_New(list->length);
if (pylist == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; i < list->length; i++) {
PyObject *item = PyUnicode_FromWideChar(list->items[i], -1);
if (item == NULL) {
Py_DECREF(pylist);
return NULL;
}
PyList_SET_ITEM(pylist, i, item);
}
return pylist;
}
static PyObject*
_PyWideStringList_AsTuple(const PyWideStringList *list)
{
assert(_PyWideStringList_CheckConsistency(list));
PyObject *tuple = PyTuple_New(list->length);
if (tuple == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; i < list->length; i++) {
PyObject *item = PyUnicode_FromWideChar(list->items[i], -1);
if (item == NULL) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, i, item);
}
return tuple;
}
/* --- Py_GetArgcArgv() ------------------------------------------- */
void
_Py_ClearArgcArgv(void)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
_PyWideStringList_Clear(&_PyRuntime.orig_argv);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
static int
_Py_SetArgcArgv(Py_ssize_t argc, wchar_t * const *argv)
{
const PyWideStringList argv_list = {.length = argc, .items = (wchar_t **)argv};
int res;
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
// XXX _PyRuntime.orig_argv only gets cleared by Py_Main(),
// so it currently leaks for embedders.
res = _PyWideStringList_Copy(&_PyRuntime.orig_argv, &argv_list);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
}
// _PyConfig_Write() calls _Py_SetArgcArgv() with PyConfig.orig_argv.
void
Py_GetArgcArgv(int *argc, wchar_t ***argv)
{
*argc = (int)_PyRuntime.orig_argv.length;
*argv = _PyRuntime.orig_argv.items;
}
/* --- PyConfig ---------------------------------------------- */
#define MAX_HASH_SEED 4294967295UL
#ifndef NDEBUG
static int
config_check_consistency(const PyConfig *config)
{
/* Check config consistency */
assert(config->isolated >= 0);
assert(config->use_environment >= 0);
assert(config->dev_mode >= 0);
assert(config->install_signal_handlers >= 0);
assert(config->use_hash_seed >= 0);
assert(config->hash_seed <= MAX_HASH_SEED);
assert(config->faulthandler >= 0);
assert(config->tracemalloc >= 0);
assert(config->import_time >= 0);
assert(config->code_debug_ranges >= 0);
assert(config->show_ref_count >= 0);
assert(config->dump_refs >= 0);
assert(config->malloc_stats >= 0);
assert(config->site_import >= 0);
assert(config->bytes_warning >= 0);
assert(config->warn_default_encoding >= 0);
assert(config->inspect >= 0);
assert(config->interactive >= 0);
assert(config->optimization_level >= 0);
assert(config->parser_debug >= 0);
assert(config->write_bytecode >= 0);
assert(config->verbose >= 0);
assert(config->quiet >= 0);
assert(config->user_site_directory >= 0);
assert(config->parse_argv >= 0);
assert(config->configure_c_stdio >= 0);
assert(config->buffered_stdio >= 0);
assert(_PyWideStringList_CheckConsistency(&config->orig_argv));
assert(_PyWideStringList_CheckConsistency(&config->argv));
/* sys.argv must be non-empty: empty argv is replaced with [''] */
assert(config->argv.length >= 1);
assert(_PyWideStringList_CheckConsistency(&config->xoptions));
assert(_PyWideStringList_CheckConsistency(&config->warnoptions));
assert(_PyWideStringList_CheckConsistency(&config->module_search_paths));
assert(config->module_search_paths_set >= 0);
assert(config->filesystem_encoding != NULL);
assert(config->filesystem_errors != NULL);
assert(config->stdio_encoding != NULL);
assert(config->stdio_errors != NULL);
#ifdef MS_WINDOWS
assert(config->legacy_windows_stdio >= 0);
#endif
/* -c and -m options are exclusive */
assert(!(config->run_command != NULL && config->run_module != NULL));
assert(config->check_hash_pycs_mode != NULL);
assert(config->_install_importlib >= 0);
assert(config->pathconfig_warnings >= 0);
assert(config->_is_python_build >= 0);
assert(config->safe_path >= 0);
assert(config->int_max_str_digits >= 0);
// cpu_count can be -1 if the user doesn't override it.
assert(config->cpu_count != 0);
// config->use_frozen_modules is initialized later
// by _PyConfig_InitImportConfig().
#ifdef __APPLE__
assert(config->use_system_logger >= 0);
#endif
#ifdef Py_STATS
assert(config->_pystats >= 0);
#endif
return 1;
}
#endif
/* Free memory allocated in config, but don't clear all attributes */
void
PyConfig_Clear(PyConfig *config)
{
#define CLEAR(ATTR) \
do { \
PyMem_RawFree(ATTR); \
ATTR = NULL; \
} while (0)
CLEAR(config->pycache_prefix);
CLEAR(config->pythonpath_env);
CLEAR(config->home);
CLEAR(config->program_name);
_PyWideStringList_Clear(&config->argv);
_PyWideStringList_Clear(&config->warnoptions);
_PyWideStringList_Clear(&config->xoptions);
_PyWideStringList_Clear(&config->module_search_paths);
config->module_search_paths_set = 0;
CLEAR(config->stdlib_dir);
CLEAR(config->executable);
CLEAR(config->base_executable);
CLEAR(config->prefix);
CLEAR(config->base_prefix);
CLEAR(config->exec_prefix);
CLEAR(config->base_exec_prefix);
CLEAR(config->platlibdir);
CLEAR(config->sys_path_0);
CLEAR(config->filesystem_encoding);
CLEAR(config->filesystem_errors);
CLEAR(config->stdio_encoding);
CLEAR(config->stdio_errors);
CLEAR(config->run_command);
CLEAR(config->run_module);
CLEAR(config->run_filename);
CLEAR(config->check_hash_pycs_mode);
#ifdef Py_DEBUG
CLEAR(config->run_presite);
#endif
_PyWideStringList_Clear(&config->orig_argv);
#undef CLEAR
}
void
_PyConfig_InitCompatConfig(PyConfig *config)
{
memset(config, 0, sizeof(*config));
config->_config_init = (int)_PyConfig_INIT_COMPAT;
config->isolated = -1;
config->use_environment = -1;
config->dev_mode = -1;
config->install_signal_handlers = 1;
config->use_hash_seed = -1;
config->faulthandler = -1;
config->tracemalloc = -1;
config->perf_profiling = -1;
config->module_search_paths_set = 0;
config->parse_argv = 0;
config->site_import = -1;
config->bytes_warning = -1;
config->warn_default_encoding = 0;
config->inspect = -1;
config->interactive = -1;
config->optimization_level = -1;
config->parser_debug= -1;
config->write_bytecode = -1;
config->verbose = -1;
config->quiet = -1;
config->user_site_directory = -1;
config->configure_c_stdio = 0;
config->buffered_stdio = -1;
config->_install_importlib = 1;
config->check_hash_pycs_mode = NULL;
config->pathconfig_warnings = -1;
config->_init_main = 1;
#ifdef MS_WINDOWS
config->legacy_windows_stdio = -1;
#endif
#ifdef Py_DEBUG
config->use_frozen_modules = 0;
#else
config->use_frozen_modules = 1;
#endif
config->safe_path = 0;
config->int_max_str_digits = -1;
config->_is_python_build = 0;
config->code_debug_ranges = 1;
config->cpu_count = -1;
#ifdef __APPLE__
config->use_system_logger = 0;
#endif
#ifdef Py_GIL_DISABLED
config->enable_gil = _PyConfig_GIL_DEFAULT;
config->tlbc_enabled = 1;