FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Python/initconfig.c at main · python/cpython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.3k
Star
74.9k
Code
Issues
5k+
Pull requests
2.5k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Python
/
initconfig.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
5026 lines (4361 loc) · 142 KB
Breadcrumbs
cpython
/
Python
/
initconfig.c
Copy path
File metadata and controls
5026 lines (4361 loc) · 142 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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_DefaultRawMalloc()
#include
"pycore_pyhash.h"
// _Py_HashSecret
#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
#ifdef
__APPLE__
/* Enable system log by default on non-macOS Apple platforms */
# if
defined(
TARGET_OS_IPHONE
)
&&
TARGET_OS_IPHONE
#define
USE_SYSTEM_LOGGER_DEFAULT
1;
# else
#define
USE_SYSTEM_LOGGER_DEFAULT
0;
# 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
{
int
*
ptr
;
int
not
;
}
PyConfigGlobalVar
;
typedef
struct
{
const
char
*
name
;
size_t
offset
;
PyConfigMemberType
type
;
PyConfigMemberVisibility
visibility
;
PyConfigSysSpec
sys
;
PyConfigGlobalVar
global_var
;
}
PyConfigSpec
;
#define
SPEC
(
MEMBER
,
TYPE
,
VISIBILITY
,
sys
,
global_var
) \
{#MEMBER, offsetof(PyConfig, MEMBER), \
PyConfig_MEMBER_##TYPE, PyConfig_MEMBER_##VISIBILITY, sys, global_var}
#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)
#define
GLOBAL
(
ptr
,
not
) {ptr, not}
#define
NO_GLOBAL
GLOBAL(NULL, 0)
// Ignore deprecations on global variables such as Py_IsolatedFlag
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
// Update _test_embed_set_config when adding new members
static
const
PyConfigSpec
PYCONFIG_SPEC
[]
=
{
// --- Public options -----------
SPEC
(
argv
,
WSTR_LIST
,
PUBLIC
,
SYS_ATTR
(
"argv"
),
NO_GLOBAL
),
SPEC
(
base_exec_prefix
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"base_exec_prefix"
),
NO_GLOBAL
),
SPEC
(
base_executable
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"_base_executable"
),
NO_GLOBAL
),
SPEC
(
base_prefix
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"base_prefix"
),
NO_GLOBAL
),
SPEC
(
bytes_warning
,
UINT
,
PUBLIC
,
SYS_FLAG
(
9
),
GLOBAL
(
&
Py_BytesWarningFlag
,
0
)),
SPEC
(
cpu_count
,
INT
,
PUBLIC
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
lazy_imports
,
INT
,
PUBLIC
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
exec_prefix
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"exec_prefix"
),
NO_GLOBAL
),
SPEC
(
executable
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"executable"
),
NO_GLOBAL
),
SPEC
(
inspect
,
BOOL
,
PUBLIC
,
SYS_FLAG
(
1
),
GLOBAL
(
&
Py_InspectFlag
,
0
)),
SPEC
(
int_max_str_digits
,
UINT
,
PUBLIC
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
interactive
,
BOOL
,
PUBLIC
,
SYS_FLAG
(
2
),
GLOBAL
(
&
Py_InteractiveFlag
,
0
)),
SPEC
(
module_search_paths
,
WSTR_LIST
,
PUBLIC
,
SYS_ATTR
(
"path"
),
NO_GLOBAL
),
SPEC
(
optimization_level
,
UINT
,
PUBLIC
,
SYS_FLAG
(
3
),
GLOBAL
(
&
Py_OptimizeFlag
,
0
)),
SPEC
(
parser_debug
,
BOOL
,
PUBLIC
,
SYS_FLAG
(
0
),
GLOBAL
(
&
Py_DebugFlag
,
0
)),
SPEC
(
platlibdir
,
WSTR
,
PUBLIC
,
SYS_ATTR
(
"platlibdir"
),
NO_GLOBAL
),
SPEC
(
prefix
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"prefix"
),
NO_GLOBAL
),
SPEC
(
pycache_prefix
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"pycache_prefix"
),
NO_GLOBAL
),
SPEC
(
quiet
,
BOOL
,
PUBLIC
,
SYS_FLAG
(
10
),
GLOBAL
(
&
Py_QuietFlag
,
0
)),
SPEC
(
stdlib_dir
,
WSTR_OPT
,
PUBLIC
,
SYS_ATTR
(
"_stdlib_dir"
),
NO_GLOBAL
),
SPEC
(
use_environment
,
BOOL
,
PUBLIC
,
SYS_FLAG_SETTER
(
7
,
config_sys_flag_not
),
GLOBAL
(
&
Py_IgnoreEnvironmentFlag
,
1
)),
SPEC
(
verbose
,
UINT
,
PUBLIC
,
SYS_FLAG
(
8
),
GLOBAL
(
&
Py_VerboseFlag
,
0
)),
SPEC
(
warnoptions
,
WSTR_LIST
,
PUBLIC
,
SYS_ATTR
(
"warnoptions"
),
NO_GLOBAL
),
SPEC
(
write_bytecode
,
BOOL
,
PUBLIC
,
SYS_FLAG_SETTER
(
4
,
config_sys_flag_not
),
GLOBAL
(
&
Py_DontWriteBytecodeFlag
,
1
)),
SPEC
(
xoptions
,
WSTR_LIST
,
PUBLIC
,
SYS_ATTR
(
"_xoptions"
),
NO_GLOBAL
),
// --- Read-only options -----------
#ifdef
Py_STATS
SPEC
(
_pystats
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#endif
SPEC
(
buffered_stdio
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_UnbufferedStdioFlag
,
1
)),
SPEC
(
check_hash_pycs_mode
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
code_debug_ranges
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
configure_c_stdio
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
dev_mode
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
// sys.flags.dev_mode
SPEC
(
dump_refs
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
dump_refs_file
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#ifdef
Py_GIL_DISABLED
SPEC
(
enable_gil
,
INT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
tlbc_enabled
,
INT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#endif
SPEC
(
faulthandler
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
filesystem_encoding
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
filesystem_errors
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
hash_seed
,
ULONG
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
home
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
thread_inherit_context
,
INT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
context_aware_warnings
,
INT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
import_time
,
UINT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
install_signal_handlers
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
isolated
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_IsolatedFlag
,
0
)),
// sys.flags.isolated
#ifdef
MS_WINDOWS
SPEC
(
legacy_windows_stdio
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_LegacyWindowsStdioFlag
,
0
)),
#endif
SPEC
(
malloc_stats
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
pymalloc_hugepages
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
orig_argv
,
WSTR_LIST
,
READ_ONLY
,
SYS_ATTR
(
"orig_argv"
),
NO_GLOBAL
),
SPEC
(
parse_argv
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
pathconfig_warnings
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_FrozenFlag
,
1
)),
SPEC
(
perf_profiling
,
UINT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
remote_debug
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
program_name
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
run_command
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
run_filename
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
run_module
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#ifdef
Py_DEBUG
SPEC
(
run_presite
,
WSTR_OPT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#endif
SPEC
(
safe_path
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
show_ref_count
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
site_import
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_NoSiteFlag
,
1
)),
// sys.flags.no_site
SPEC
(
skip_source_first_line
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
stdio_encoding
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
stdio_errors
,
WSTR
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
tracemalloc
,
UINT
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
use_frozen_modules
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
use_hash_seed
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#ifdef
__APPLE__
SPEC
(
use_system_logger
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
#endif
SPEC
(
user_site_directory
,
BOOL
,
READ_ONLY
,
NO_SYS
,
GLOBAL
(
&
Py_NoUserSiteDirectory
,
1
)),
// sys.flags.no_user_site
SPEC
(
warn_default_encoding
,
BOOL
,
READ_ONLY
,
NO_SYS
,
NO_GLOBAL
),
// --- Init-only options -----------
SPEC
(
_config_init
,
UINT
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
_init_main
,
BOOL
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
_install_importlib
,
BOOL
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
_is_python_build
,
BOOL
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
module_search_paths_set
,
BOOL
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
pythonpath_env
,
WSTR_OPT
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
SPEC
(
sys_path_0
,
WSTR_OPT
,
INIT_ONLY
,
NO_SYS
,
NO_GLOBAL
),
// 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
},
};
// End of ignoring deprecations on global variables
_Py_COMP_DIAG_POP
#undef
SPEC
#undef
SYS_ATTR
#undef
SYS_FLAG_SETTER
#undef
SYS_FLAG
#undef
NO_SYS
#undef
GLOBAL
#undef
NO_GLOBAL
// Forward declarations
static
PyObject
*
config_get
(
const
PyConfig
*
config
,
const
PyConfigSpec
*
spec
,
int
use_sys
);
static
void
initconfig_free_wstr
(
wchar_t
*
member
);
static
void
initconfig_free_wstr_list
(
PyWideStringList
*
list
);
static
void
initconfig_free_config
(
const
PyConfig
*
config
);
/* --- Command line options --------------------------------------- */
/*
* Help text markup (matching Lib/_colorize.py Argparse theme).
*
* Color spans, #X{...} where "}" resets to default color:
* #b{...} label bold yellow
* #B{...} summary label yellow
* #E{...} env var (primary) bold cyan
* #e{...} env var reference cyan
* #h{...} heading bold blue
* #L{...} long option bold cyan
* #s{...} short option bold green
* #S{...} summary short opt green
*
* Runtime substitutions (no "{" follows):
* #P program name (bold magenta)
* #D path separator (DELIM)
* #H PYTHONHOMEHELP default search path
*
* fprint_help() walks the string, expanding color codes only when colorize=1
* and substituting runtime values regardless.
*/
#if
defined(
MS_WINDOWS
)
# define
PYTHONHOMEHELP
"<prefix>\\python{major}{minor}"
#else
# define
PYTHONHOMEHELP
"<prefix>/lib/pythonX.X"
#endif
/* Determine if we can emit ANSI color codes on the given stream.
* Logic mirrors Lib/_colorize.py:can_colorize(). */
static
int
_Py_can_colorize
(
FILE
*
f
)
{
const
char
*
env
;
env
=
Py_GETENV
(
"PYTHON_COLORS"
);
if
(
env
) {
if
(
strcmp
(
env
,
"0"
)
==
0
) {
return
0
;
}
if
(
strcmp
(
env
,
"1"
)
==
0
) {
return
1
;
}
}
if
(
getenv
(
"NO_COLOR"
)) {
return
0
;
}
if
(
getenv
(
"FORCE_COLOR"
)) {
return
1
;
}
env
=
getenv
(
"TERM"
);
if
(
env
&&
strcmp
(
env
,
"dumb"
)
==
0
) {
return
0
;
}
#if
defined(
MS_WINDOWS
)
&&
defined(
HAVE_WINDOWS_CONSOLE_IO
)
{
DWORD
mode
=
0
;
DWORD
nStdHandle
=
(
f
==
stderr
) ?
STD_ERROR_HANDLE
:
STD_OUTPUT_HANDLE
;
HANDLE
handle
=
GetStdHandle
(
nStdHandle
);
if
(!
GetConsoleMode
(
handle
,
&
mode
)
||
!(
mode
&
ENABLE_VIRTUAL_TERMINAL_PROCESSING
))
{
return
0
;
}
}
#endif
return
isatty
(
fileno
(
f
));
}
/* Walk help text, expanding markup:
* #X{...} color span (only emitted when colorize=1; '}' resets).
* #X runtime substitution (program name, DELIM, PYTHONHOMEHELP).
* See the markup table above the macro/comment block. */
static
void
fprint_help
(
FILE
*
f
,
const
char
*
text
,
int
colorize
,
const
wchar_t
*
program
)
{
for
(
const
char
*
p
=
text
;
*
p
; ) {
if
(
*
p
==
'#'
&&
p
[
1
]) {
char
code
=
p
[
1
];
if
(
p
[
2
]
==
'{'
) {
/* Color span open */
const
char
*
seq
=
NULL
;
switch
(
code
) {
case
'h'
:
seq
=
"\x1b[1;34m"
;
break
;
// heading
case
'E'
:
seq
=
"\x1b[1;36m"
;
break
;
// env var primary
case
'e'
:
seq
=
"\x1b[36m"
;
break
;
// env var reference
case
'L'
:
seq
=
"\x1b[1;36m"
;
break
;
// long option
case
'b'
:
seq
=
"\x1b[1;33m"
;
break
;
// label
case
'B'
:
seq
=
"\x1b[33m"
;
break
;
// summary label
case
's'
:
seq
=
"\x1b[1;32m"
;
break
;
// short option
case
'S'
:
seq
=
"\x1b[32m"
;
break
;
// summary short option
}
if
(
colorize
&&
seq
)
fputs
(
seq
,
f
);
p
+=
3
;
// skip "#X{"
continue
;
}
/* Runtime substitution */
switch
(
code
) {
case
'P'
:
// program name with bold magenta
if
(
colorize
)
fputs
(
"\x1b[1;35m"
,
f
);
if
(
program
)
fprintf
(
f
,
"%ls"
,
program
);
if
(
colorize
)
fputs
(
"\x1b[0m"
,
f
);
break
;
case
'D'
:
fputc
((
char
)
DELIM
,
f
);
break
;
case
'H'
:
fputs
(
PYTHONHOMEHELP
,
f
);
break
;
default
:
// unknown: emit literally
fputc
(
'#'
,
f
);
fputc
(
code
,
f
);
break
;
}
p
+=
2
;
// skip "#X"
continue
;
}
if
(
*
p
==
'}'
) {
if
(
colorize
)
fputs
(
"\x1b[0m"
,
f
);
p
++
;
continue
;
}
fputc
(
*
p
++
,
f
);
}
}
/* Short usage message */
static
const
char
usage_line
[]
=
"#h{usage:} #P [#S{option}] #S{...} "
"[#S{-c} #B{cmd} | #S{-m} #B{mod} | #S{file} | #S{-}] "
"[#S{arg}] #S{...}\n"
;
/* Long help message */
/* Lines sorted by option name; keep in sync with usage_envvars* below */
static
const
char
usage_help
[]
=
"#h{Options (and corresponding environment variables):}\n"
"#s{-b} : issue warnings about converting bytes/bytearray to str and comparing\n"
" bytes/bytearray with str or bytes with int. (#S{-bb}: issue errors)\n"
" deprecated since 3.15 and will become no-op in 3.17.\n"
"#s{-B} : don't write .pyc files on import; also #e{PYTHONDONTWRITEBYTECODE}#B{=x}\n"
"#s{-c} #b{cmd} : program passed in as string (terminates option list)\n"
"#s{-d} : turn on parser debugging output (for experts only, only works on\n"
" debug builds); also #e{PYTHONDEBUG}#B{=x}\n"
"#s{-E} : ignore #e{PYTHON*} environment variables (such as #e{PYTHONPATH})\n"
"#s{-h} : print this help message and exit (also #S{-?} or #e{--help})\n"
"#s{-i} : inspect interactively after running script; forces a prompt even\n"
" if stdin does not appear to be a terminal; also #e{PYTHONINSPECT}#B{=x}\n"
"#s{-I} : isolate Python from the user's environment (implies #S{-E}, #S{-P} and #S{-s})\n"
"#s{-m} #b{mod} : run library module as a script (terminates option list)\n"
"#s{-O} : remove assert and __debug__-dependent statements; add .opt-1 before\n"
" .pyc extension; also #e{PYTHONOPTIMIZE}#B{=x}\n"
"#s{-OO} : do #S{-O} changes and also discard docstrings; add .opt-2 before\n"
" .pyc extension\n"
"#s{-P} : don't prepend a potentially unsafe path to sys.path; also\n"
" #e{PYTHONSAFEPATH}\n"
"#s{-q} : don't print version and copyright messages on interactive startup\n"
"#s{-s} : don't add user site directory to sys.path; also #e{PYTHONNOUSERSITE}#B{=x}\n"
"#s{-S} : don't imply 'import site' on initialization\n"
"#s{-u} : force the stdout and stderr streams to be unbuffered;\n"
" this option has no effect on stdin; also #e{PYTHONUNBUFFERED}#B{=x}\n"
"#s{-v} : verbose (trace import statements); also #e{PYTHONVERBOSE}#B{=x}\n"
" can be supplied multiple times to increase verbosity\n"
"#s{-V} : print the Python version number and exit (also #e{--version})\n"
" when given twice, print more information about the build\n"
"#s{-W} #b{arg} : warning control; #B{arg} is action:message:category:module:lineno\n"
" also #e{PYTHONWARNINGS}#B{=arg}\n"
"#s{-x} : skip first line of source, allowing use of non-Unix forms of #!cmd\n"
"#s{-X} #b{opt} : set implementation-specific option\n"
"#L{--check-hash-based-pycs} #b{always|default|never}:\n"
" control how Python invalidates hash-based .pyc files\n"
"#L{--help-env}: print help about Python environment variables and exit\n"
"#L{--help-xoptions}: print help about implementation-specific #S{-X} options and exit\n"
"#L{--help-all}: print complete help information and exit\n"
"\n"
"#h{Arguments:}\n"
"#s{file} : program read from script file\n"
"#s{-} : program read from stdin (default; interactive mode if a tty)\n"
"#s{arg} #b{...}: arguments passed to program in sys.argv[1:]\n"
;
static
const
char
usage_xoptions
[]
=
"#h{The following implementation-specific options are available:}\n"
"#s{-X} #L{context_aware_warnings}#b{=[0|1]}: if true (#B{1}) then the warnings module will\n"
" use a context variables; if false (#B{0}) then the warnings module will\n"
" use module globals, which is not concurrent-safe; set to true for\n"
" free-threaded builds and false otherwise; also\n"
" #e{PYTHON_CONTEXT_AWARE_WARNINGS}\n"
"#s{-X} #L{cpu_count}#b{=N}: override the return value of os.cpu_count();\n"
" #S{-X} #e{cpu_count}#B{=default} cancels overriding; also #e{PYTHON_CPU_COUNT}\n"
"#s{-X} #L{dev} : enable Python Development Mode; also #e{PYTHONDEVMODE}\n"
"#s{-X} #L{disable-remote-debug}: disable remote debugging; also #e{PYTHON_DISABLE_REMOTE_DEBUG}\n"
"#s{-X} #L{faulthandler}: dump the Python traceback on fatal errors;\n"
" also #e{PYTHONFAULTHANDLER}\n"
"#s{-X} #L{frozen_modules}#b{=[on|off]}: whether to use frozen modules; the default is \"#B{on}\"\n"
" for installed Python and \"#B{off}\" for a local build;\n"
" also #e{PYTHON_FROZEN_MODULES}\n"
#ifdef
Py_GIL_DISABLED
"#s{-X} #L{gil}#b{=[0|1]}: enable (#B{1}) or disable (#B{0}) the GIL; also #e{PYTHON_GIL}\n"
#endif
"#s{-X} #L{importtime}#b{[=2]}: show how long each import takes; use #S{-X} #e{importtime}#B{=2} to\n"
" log imports of already-loaded modules; also #e{PYTHONPROFILEIMPORTTIME}\n"
"#s{-X} #L{int_max_str_digits}#b{=N}: limit the size of int<->str conversions;\n"
" 0 disables the limit; also #e{PYTHONINTMAXSTRDIGITS}\n"
"#s{-X} #L{lazy_imports}#b{=[all|normal]}: control global lazy imports;\n"
" default is #B{normal}; also #e{PYTHON_LAZY_IMPORTS}\n"
"#s{-X} #L{no_debug_ranges}: don't include extra location information in code objects;\n"
" also #e{PYTHONNODEBUGRANGES}\n"
"#s{-X} #L{pathconfig_warnings}#b{=[0|1]}: if true (#B{1}) then path configuration is allowed\n"
" to log warnings into stderr; if false (#B{0}) suppress these warnings;\n"
" set to true by default; also #e{PYTHON_PATHCONFIG_WARNINGS}\n"
"#s{-X} #L{perf}: support the Linux \"perf\" profiler; also #e{PYTHONPERFSUPPORT}#B{=1}\n"
"#s{-X} #L{perf_jit}: support the Linux \"perf\" profiler with DWARF support;\n"
" also #e{PYTHON_PERF_JIT_SUPPORT}#B{=1}\n"
#ifdef
Py_DEBUG
"#s{-X} #L{presite}#b{=MOD}: import this module before site; also #e{PYTHON_PRESITE}\n"
#endif
"#s{-X} #L{pycache_prefix}#b{=PATH}: write .pyc files to a parallel tree instead of to the\n"
" code tree; also #e{PYTHONPYCACHEPREFIX}\n"
#ifdef
Py_STATS
"#s{-X} #L{pystats}: enable pystats collection at startup; also #e{PYTHONSTATS}\n"
#endif
"#s{-X} #L{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"
"#s{-X} #L{thread_inherit_context}#b{=[0|1]}: enable (#B{1}) or disable (#B{0}) threads inheriting\n"
" context vars by default; enabled by default in the free-threaded\n"
" build and disabled otherwise; also #e{PYTHON_THREAD_INHERIT_CONTEXT}\n"
#ifdef
Py_GIL_DISABLED
"#s{-X} #L{tlbc}#b{=[0|1]}: enable (#B{1}) or disable (#B{0}) thread-local bytecode. Also\n"
" #e{PYTHON_TLBC}\n"
#endif
"#s{-X} #L{tracemalloc}#b{[=N]}: trace Python memory allocations; N sets a traceback limit\n"
" of #B{N} frames (default: #B{1}); also #e{PYTHONTRACEMALLOC}#B{=N}\n"
"#s{-X} #L{utf8}#b{[=0|1]}: enable (#B{1}) or disable (#B{0}) UTF-8 mode; also #e{PYTHONUTF8}\n"
"#s{-X} #L{warn_default_encoding}: enable opt-in EncodingWarning for 'encoding=None';\n"
" also #e{PYTHONWARNDEFAULTENCODING}\n"
;
/* Envvars that don't have equivalent command-line options are listed first */
static
const
char
usage_envvars
[]
=
"#h{Environment variables that change behavior:}\n"
"#E{PYTHONASYNCIODEBUG}: enable asyncio debug mode\n"
"#E{PYTHON_BASIC_REPL}: use the traditional parser-based REPL\n"
"#E{PYTHONBREAKPOINT}: if this variable is set to #B{0}, it disables the default\n"
" debugger. It can be set to the callable of your debugger of\n"
" choice.\n"
"#E{PYTHONCASEOK} : ignore case in 'import' statements (Windows)\n"
"#E{PYTHONCOERCECLOCALE}: if this variable is set to #B{0}, it disables the locale\n"
" coercion behavior. Use #e{PYTHONCOERCECLOCALE}#B{=warn} to request\n"
" display of locale coercion and locale compatibility warnings\n"
" on stderr.\n"
"#E{PYTHON_COLORS} : if this variable is set to #B{1}, the interpreter will colorize\n"
" various kinds of output. Setting it to #B{0} deactivates\n"
" this behavior.\n"
#ifdef
Py_TRACE_REFS
"#E{PYTHONDUMPREFS} : dump objects and reference counts still alive after shutdown\n"
"#E{PYTHONDUMPREFSFILE}: dump objects and reference counts to the specified file\n"
#endif
#ifdef
__APPLE__
"#E{PYTHONEXECUTABLE}: set sys.argv[0] to this value (macOS only)\n"
#endif
"#E{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"
"#E{PYTHON_HISTORY} : the location of a .python_history file.\n"
"#E{PYTHONHOME} : alternate <prefix> directory (or <prefix>#D<exec_prefix>).\n"
" The default module search path uses #H.\n"
"#E{PYTHONIOENCODING}: encoding[:errors] used for stdin/stdout/stderr\n"
#ifdef
MS_WINDOWS
"#E{PYTHONLEGACYWINDOWSFSENCODING}: use legacy \"mbcs\" encoding for file system\n"
"#E{PYTHONLEGACYWINDOWSSTDIO}: use legacy Windows stdio\n"
#endif
"#E{PYTHONMALLOC} : set the Python memory allocators and/or install debug hooks\n"
" on Python memory allocators. Use #e{PYTHONMALLOC}#B{=debug} to\n"
" install debug hooks.\n"
"#E{PYTHONMALLOCSTATS}: print memory allocator statistics\n"
"#E{PYTHONPATH} : '#D'-separated list of directories prefixed to the\n"
" default module search path. The result is sys.path.\n"
"#E{PYTHONPLATLIBDIR}: override sys.platlibdir\n"
"#E{PYTHONSTARTUP} : file executed on interactive startup (no default)\n"
"#E{PYTHONUSERBASE} : defines the user base directory (site.USER_BASE)\n"
"\n"
"#h{These variables have equivalent command-line options (see }#e{--help} for details):\n"
"#E{PYTHON_CONTEXT_AWARE_WARNINGS}: if true (#B{1}), enable thread-safe warnings\n"
" module behaviour (#S{-X} #e{context_aware_warnings})\n"
"#E{PYTHON_CPU_COUNT}: override the return value of os.cpu_count() (#S{-X} #e{cpu_count})\n"
"#E{PYTHONDEBUG} : enable parser debug mode (#S{-d})\n"
"#E{PYTHONDEVMODE} : enable Python Development Mode (#S{-X} #e{dev})\n"
"#E{PYTHONDONTWRITEBYTECODE}: don't write .pyc files (#S{-B})\n"
"#E{PYTHONFAULTHANDLER}: dump the Python traceback on fatal errors (#S{-X} #e{faulthandler})\n"
"#E{PYTHON_FROZEN_MODULES}: whether to use frozen modules; the default is \"#B{on}\"\n"
" for installed Python and \"#B{off}\" for a local build\n"
" (#S{-X} #e{frozen_modules})\n"
#ifdef
Py_GIL_DISABLED
"#E{PYTHON_GIL} : when set to #B{0}, disables the GIL (#S{-X} #e{gil})\n"
#endif
"#E{PYTHONINSPECT} : inspect interactively after running script (#S{-i})\n"
"#E{PYTHONINTMAXSTRDIGITS}: limit the size of int<->str conversions;\n"
" 0 disables the limit (#S{-X} #e{int_max_str_digits}#B{=N})\n"
"#E{PYTHON_LAZY_IMPORTS}: control global lazy imports (#S{-X} #e{lazy_imports})\n"
"#E{PYTHONNODEBUGRANGES}: don't include extra location information in code objects\n"
" (#S{-X} #e{no_debug_ranges})\n"
"#E{PYTHONNOUSERSITE}: disable user site directory (#S{-s})\n"
"#E{PYTHONOPTIMIZE} : enable level 1 optimizations (#S{-O})\n"
"#E{PYTHON_PERF_JIT_SUPPORT}: enable Linux \"perf\" profiler support with JIT\n"
" (#S{-X} #e{perf_jit})\n"
"#E{PYTHONPERFSUPPORT}: support the Linux \"perf\" profiler (#S{-X} #e{perf})\n"
#ifdef
Py_DEBUG
"#E{PYTHON_PRESITE}: import this module before site (#S{-X} #e{presite})\n"
#endif
"#E{PYTHONPROFILEIMPORTTIME}: show how long each import takes (#S{-X} #e{importtime})\n"
"#E{PYTHONPYCACHEPREFIX}: root directory for bytecode cache (pyc) files\n"
" (#S{-X} #e{pycache_prefix})\n"
"#E{PYTHONSAFEPATH} : don't prepend a potentially unsafe path to sys.path.\n"
#ifdef
Py_STATS
"#E{PYTHONSTATS} : turns on statistics gathering (#S{-X} #e{pystats})\n"
#endif
"#E{PYTHON_THREAD_INHERIT_CONTEXT}: if true (#B{1}), threads inherit context vars\n"
" (#S{-X} #e{thread_inherit_context})\n"
#ifdef
Py_GIL_DISABLED
"#E{PYTHON_TLBC} : when set to #B{0}, disables thread-local bytecode (#S{-X} #e{tlbc})\n"
#endif
"#E{PYTHONTRACEMALLOC}: trace Python memory allocations (#S{-X} #e{tracemalloc})\n"
"#E{PYTHONUNBUFFERED}: disable stdout/stderr buffering (#S{-u})\n"
"#E{PYTHONUTF8} : control the UTF-8 mode (#S{-X} #e{utf8})\n"
"#E{PYTHONVERBOSE} : trace import statements (#S{-v})\n"
"#E{PYTHONWARNDEFAULTENCODING}: enable opt-in EncodingWarning for 'encoding=None'\n"
" (#S{-X} #e{warn_default_encoding})\n"
"#E{PYTHONWARNINGS} : warning control (#S{-W})\n"
;
/* --- Global configuration variables ----------------------------- */
/* UTF-8 mode (PEP 540): if equal 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) { \
goto fail; \
} \
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 */
static
void
_PyWideStringList_ClearEx
(
PyWideStringList
*
list
,
bool
use_default_allocator
)
{
assert
(
_PyWideStringList_CheckConsistency
(
list
));
for
(
Py_ssize_t
i
=
0
;
i
<
list
->
length
;
i
++
) {
if
(
use_default_allocator
) {
_PyMem_DefaultRawFree
(
list
->
items
[
i
]);
}
else
{
PyMem_RawFree
(
list
->
items
[
i
]);
}
}
if
(
use_default_allocator
) {
_PyMem_DefaultRawFree
(
list
->
items
);
}
else
{
PyMem_RawFree
(
list
->
items
);
}
list
->
length
=
0
;
list
->
items
=
NULL
;
}
void
_PyWideStringList_Clear
(
PyWideStringList
*
list
)
{
_PyWideStringList_ClearEx
(
list
, false);
}
static
int
_PyWideStringList_CopyEx
(
PyWideStringList
*
list
,
const
PyWideStringList
*
list2
,
bool
use_default_allocator
)
{
assert
(
_PyWideStringList_CheckConsistency
(
list
));
assert
(
_PyWideStringList_CheckConsistency
(
list2
));
if
(
list2
->
length
==
0
) {
_PyWideStringList_ClearEx
(
list
,
use_default_allocator
);
return
0
;
}
PyWideStringList
copy
=
_PyWideStringList_INIT
;
size_t
size
=
list2
->
length
*
sizeof
(
list2
->
items
[
0
]);
if
(
use_default_allocator
) {
copy
.
items
=
_PyMem_DefaultRawMalloc
(
size
);
}
else
{
copy
.
items
=
PyMem_RawMalloc
(
size
);
}
if
(
copy
.
items
==
NULL
) {
return
-1
;
}
for
(
Py_ssize_t
i
=
0
;
i
<
list2
->
length
;
i
++
) {
wchar_t
*
item
;
if
(
use_default_allocator
) {
item
=
_PyMem_DefaultRawWcsdup
(
list2
->
items
[
i
]);
}
else
{
item
=
_PyMem_RawWcsdup
(
list2
->
items
[
i
]);
}
if
(
item
==
NULL
) {
_PyWideStringList_ClearEx
(
&
copy
,
use_default_allocator
);
return
-1
;
}
copy
.
items
[
i
]
=
item
;
copy
.
length
=
i
+
1
;
}
_PyWideStringList_ClearEx
(
list
,
use_default_allocator
);
*
list
=
copy
;
return
0
;
}
int
_PyWideStringList_Copy
(
PyWideStringList
*
list
,
const
PyWideStringList
*
list2
)
{
return
_PyWideStringList_CopyEx
(
list
,
list2
, false);
}
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() ------------------------------------------- */
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL