forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinitconfig.c
2437 lines (2087 loc) · 69.3 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 "osdefs.h" /* DELIM */
#include "pycore_initconfig.h"
#include "pycore_fileutils.h"
#include "pycore_getopt.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h" /* _PyRuntime */
#include "pycore_pathconfig.h"
#include <locale.h> /* setlocale() */
#ifdef HAVE_LANGINFO_H
# include <langinfo.h> /* nl_langinfo(CODESET) */
#endif
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
# include <windows.h> /* GetACP() */
# ifdef HAVE_IO_H
# include <io.h>
# endif
# ifdef HAVE_FCNTL_H
# include <fcntl.h> /* O_BINARY */
# endif
#endif
/* --- 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 usage message, split into parts < 512 bytes */
static const char usage_1[] = "\
Options and arguments (and corresponding environment variables):\n\
-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
and comparing bytes/bytearray with str. (-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 : debug output from parser; also PYTHONDEBUG=x\n\
-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
-h : print this help message and exit (also --help)\n\
";
static const char usage_2[] = "\
-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\
-q : don't print version and copyright messages on interactive startup\n\
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
-S : don't imply 'import site' on initialization\n\
";
static const char usage_3[] = "\
-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\
";
static const char usage_4[] = "\
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\n\
Other environment variables:\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\
";
static const char usage_5[] =
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
" The default module search path uses %s.\n"
"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
static const char usage_6[] =
"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
" to seed the hashes of str, bytes and datetime objects. It can also be\n"
" set to an integer in the range [0,4294967295] to get hash values with a\n"
" predictable seed.\n"
"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
" hooks.\n"
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
" locale coercion and locale compatibility warnings 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 choice.\n"
"PYTHONDEVMODE: enable the development mode.\n"
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\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; /* Needed by Py_FdIsInteractive() below */
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)
{
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_INCREF(Py_None), 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
}
/* --- PyStatus ----------------------------------------------- */
PyStatus PyStatus_Ok(void)
{ return _PyStatus_OK(); }
PyStatus PyStatus_Error(const char *err_msg)
{
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); }
/* --- 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_Append(PyWideStringList *list, const wchar_t *item)
{
if (list->length == PY_SSIZE_T_MAX) {
/* lenght+1 would overflow */
return _PyStatus_NO_MEMORY();
}
wchar_t *item2 = _PyMem_RawWcsdup(item);
if (item2 == NULL) {
return _PyStatus_NO_MEMORY();
}
size_t size = (list->length + 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();
}
items2[list->length] = item2;
list->items = items2;
list->length++;
return _PyStatus_OK();
}
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;
}
/* --- Py_SetStandardStreamEncoding() ----------------------------- */
/* Helper to allow an embedding application to override the normal
* mechanism that attempts to figure out an appropriate IO encoding
*/
static char *_Py_StandardStreamEncoding = NULL;
static char *_Py_StandardStreamErrors = NULL;
int
Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
{
if (Py_IsInitialized()) {
/* This is too late to have any effect */
return -1;
}
int res = 0;
/* Py_SetStandardStreamEncoding() can be called before Py_Initialize(),
but Py_Initialize() can change the allocator. Use a known allocator
to be able to release the memory later. */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
/* Can't call PyErr_NoMemory() on errors, as Python hasn't been
* initialised yet.
*
* However, the raw memory allocators are initialised appropriately
* as C static variables, so _PyMem_RawStrdup is OK even though
* Py_Initialize hasn't been called yet.
*/
if (encoding) {
PyMem_RawFree(_Py_StandardStreamEncoding);
_Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
if (!_Py_StandardStreamEncoding) {
res = -2;
goto done;
}
}
if (errors) {
PyMem_RawFree(_Py_StandardStreamErrors);
_Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
if (!_Py_StandardStreamErrors) {
PyMem_RawFree(_Py_StandardStreamEncoding);
_Py_StandardStreamEncoding = NULL;
res = -3;
goto done;
}
}
#ifdef MS_WINDOWS
if (_Py_StandardStreamEncoding) {
/* Overriding the stream encoding implies legacy streams */
Py_LegacyWindowsStdioFlag = 1;
}
#endif
done:
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
}
void
_Py_ClearStandardStreamEncoding(void)
{
/* Use the same allocator than Py_SetStandardStreamEncoding() */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
/* We won't need them anymore. */
if (_Py_StandardStreamEncoding) {
PyMem_RawFree(_Py_StandardStreamEncoding);
_Py_StandardStreamEncoding = NULL;
}
if (_Py_StandardStreamErrors) {
PyMem_RawFree(_Py_StandardStreamErrors);
_Py_StandardStreamErrors = NULL;
}
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
/* --- Py_GetArgcArgv() ------------------------------------------- */
/* For Py_GetArgcArgv(); set by _Py_SetArgcArgv() */
static PyWideStringList orig_argv = {.length = 0, .items = NULL};
void
_Py_ClearArgcArgv(void)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
_PyWideStringList_Clear(&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);
res = _PyWideStringList_Copy(&orig_argv, &argv_list);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
}
/* Make the *original* argc/argv available to other modules.
This is rare, but it is needed by the secureware extension. */
void
Py_GetArgcArgv(int *argc, wchar_t ***argv)
{
*argc = (int)orig_argv.length;
*argv = orig_argv.items;
}
/* --- PyConfig ---------------------------------------------- */
#define DECODE_LOCALE_ERR(NAME, LEN) \
(((LEN) == -2) \
? _PyStatus_ERR("cannot decode " NAME) \
: _PyStatus_NO_MEMORY())
/* 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->executable);
CLEAR(config->prefix);
CLEAR(config->base_prefix);
CLEAR(config->exec_prefix);
CLEAR(config->base_exec_prefix);
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);
#undef CLEAR
}
void
_PyConfig_InitCompatConfig(PyConfig *config)
{
memset(config, 0, sizeof(*config));
config->_config_version = _Py_CONFIG_VERSION;
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->module_search_paths_set = 0;
config->parse_argv = 0;
config->site_import = -1;
config->bytes_warning = -1;
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
}
static void
config_init_defaults(PyConfig *config)
{
_PyConfig_InitCompatConfig(config);
config->isolated = 0;
config->use_environment = 1;
config->site_import = 1;
config->bytes_warning = 0;
config->inspect = 0;
config->interactive = 0;
config->optimization_level = 0;
config->parser_debug= 0;
config->write_bytecode = 1;
config->verbose = 0;
config->quiet = 0;
config->user_site_directory = 1;
config->buffered_stdio = 1;
config->pathconfig_warnings = 1;
#ifdef MS_WINDOWS
config->legacy_windows_stdio = 0;
#endif
}
PyStatus
PyConfig_InitPythonConfig(PyConfig *config)
{
config_init_defaults(config);
config->_config_init = (int)_PyConfig_INIT_PYTHON;
config->configure_c_stdio = 1;
config->parse_argv = 1;
return _PyStatus_OK();
}
PyStatus
PyConfig_InitIsolatedConfig(PyConfig *config)
{
config_init_defaults(config);
config->_config_init = (int)_PyConfig_INIT_ISOLATED;
config->isolated = 1;
config->use_environment = 0;
config->user_site_directory = 0;
config->dev_mode = 0;
config->install_signal_handlers = 0;
config->use_hash_seed = 0;
config->faulthandler = 0;
config->tracemalloc = 0;
config->pathconfig_warnings = 0;
#ifdef MS_WINDOWS
config->legacy_windows_stdio = 0;
#endif
return _PyStatus_OK();
}
/* Copy str into *config_str (duplicate the string) */
PyStatus
PyConfig_SetString(PyConfig *config, wchar_t **config_str, const wchar_t *str)
{
PyStatus status = _Py_PreInitializeFromConfig(config, NULL);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
wchar_t *str2;
if (str != NULL) {
str2 = _PyMem_RawWcsdup(str);
if (str2 == NULL) {
return _PyStatus_NO_MEMORY();
}
}
else {
str2 = NULL;
}
PyMem_RawFree(*config_str);
*config_str = str2;
return _PyStatus_OK();
}
static PyStatus
config_set_bytes_string(PyConfig *config, wchar_t **config_str,
const char *str, const char *decode_err_msg)
{
PyStatus status = _Py_PreInitializeFromConfig(config, NULL);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
wchar_t *str2;
if (str != NULL) {
size_t len;
str2 = Py_DecodeLocale(str, &len);
if (str2 == NULL) {
if (len == (size_t)-2) {
return _PyStatus_ERR(decode_err_msg);
}
else {
return _PyStatus_NO_MEMORY();
}
}
}
else {
str2 = NULL;
}
PyMem_RawFree(*config_str);
*config_str = str2;
return _PyStatus_OK();
}
#define CONFIG_SET_BYTES_STR(config, config_str, str, NAME) \
config_set_bytes_string(config, config_str, str, "cannot decode " NAME)
/* Decode str using Py_DecodeLocale() and set the result into *config_str.
Pre-initialize Python if needed to ensure that encodings are properly
configured. */
PyStatus
PyConfig_SetBytesString(PyConfig *config, wchar_t **config_str,
const char *str)
{
return CONFIG_SET_BYTES_STR(config, config_str, str, "string");
}
PyStatus
_PyConfig_Copy(PyConfig *config, const PyConfig *config2)
{
PyStatus status;
PyConfig_Clear(config);
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
#define COPY_WSTR_ATTR(ATTR) \
do { \
status = PyConfig_SetString(config, &config->ATTR, config2->ATTR); \
if (_PyStatus_EXCEPTION(status)) { \
return status; \
} \
} while (0)
#define COPY_WSTRLIST(LIST) \
do { \
if (_PyWideStringList_Copy(&config->LIST, &config2->LIST) < 0 ) { \
return _PyStatus_NO_MEMORY(); \
} \
} while (0)
COPY_ATTR(_config_init);
COPY_ATTR(isolated);
COPY_ATTR(use_environment);
COPY_ATTR(dev_mode);
COPY_ATTR(install_signal_handlers);
COPY_ATTR(use_hash_seed);
COPY_ATTR(hash_seed);
COPY_ATTR(_install_importlib);
COPY_ATTR(faulthandler);
COPY_ATTR(tracemalloc);
COPY_ATTR(import_time);
COPY_ATTR(show_ref_count);
COPY_ATTR(show_alloc_count);
COPY_ATTR(dump_refs);
COPY_ATTR(malloc_stats);
COPY_WSTR_ATTR(pycache_prefix);
COPY_WSTR_ATTR(pythonpath_env);
COPY_WSTR_ATTR(home);
COPY_WSTR_ATTR(program_name);
COPY_ATTR(parse_argv);
COPY_WSTRLIST(argv);
COPY_WSTRLIST(warnoptions);
COPY_WSTRLIST(xoptions);
COPY_WSTRLIST(module_search_paths);
COPY_ATTR(module_search_paths_set);
COPY_WSTR_ATTR(executable);
COPY_WSTR_ATTR(prefix);
COPY_WSTR_ATTR(base_prefix);
COPY_WSTR_ATTR(exec_prefix);
COPY_WSTR_ATTR(base_exec_prefix);
COPY_ATTR(site_import);
COPY_ATTR(bytes_warning);
COPY_ATTR(inspect);
COPY_ATTR(interactive);
COPY_ATTR(optimization_level);
COPY_ATTR(parser_debug);
COPY_ATTR(write_bytecode);
COPY_ATTR(verbose);
COPY_ATTR(quiet);
COPY_ATTR(user_site_directory);
COPY_ATTR(configure_c_stdio);
COPY_ATTR(buffered_stdio);
COPY_WSTR_ATTR(filesystem_encoding);
COPY_WSTR_ATTR(filesystem_errors);
COPY_WSTR_ATTR(stdio_encoding);
COPY_WSTR_ATTR(stdio_errors);
#ifdef MS_WINDOWS
COPY_ATTR(legacy_windows_stdio);
#endif
COPY_ATTR(skip_source_first_line);
COPY_WSTR_ATTR(run_command);
COPY_WSTR_ATTR(run_module);
COPY_WSTR_ATTR(run_filename);
COPY_WSTR_ATTR(check_hash_pycs_mode);
COPY_ATTR(pathconfig_warnings);
COPY_ATTR(_init_main);
#undef COPY_ATTR
#undef COPY_WSTR_ATTR
#undef COPY_WSTRLIST
return _PyStatus_OK();
}
static PyObject *
config_as_dict(const PyConfig *config)
{
PyObject *dict;
dict = PyDict_New();
if (dict == NULL) {
return NULL;
}
#define SET_ITEM(KEY, EXPR) \
do { \
PyObject *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(ATTR) \
SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
#define SET_ITEM_UINT(ATTR) \
SET_ITEM(#ATTR, PyLong_FromUnsignedLong(config->ATTR))
#define FROM_WSTRING(STR) \
((STR != NULL) ? \
PyUnicode_FromWideChar(STR, -1) \
: (Py_INCREF(Py_None), Py_None))
#define SET_ITEM_WSTR(ATTR) \
SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
#define SET_ITEM_WSTRLIST(LIST) \
SET_ITEM(#LIST, _PyWideStringList_AsList(&config->LIST))
SET_ITEM_INT(_config_init);
SET_ITEM_INT(isolated);
SET_ITEM_INT(use_environment);
SET_ITEM_INT(dev_mode);
SET_ITEM_INT(install_signal_handlers);
SET_ITEM_INT(use_hash_seed);
SET_ITEM_UINT(hash_seed);
SET_ITEM_INT(faulthandler);
SET_ITEM_INT(tracemalloc);
SET_ITEM_INT(import_time);
SET_ITEM_INT(show_ref_count);
SET_ITEM_INT(show_alloc_count);
SET_ITEM_INT(dump_refs);
SET_ITEM_INT(malloc_stats);
SET_ITEM_WSTR(filesystem_encoding);
SET_ITEM_WSTR(filesystem_errors);
SET_ITEM_WSTR(pycache_prefix);
SET_ITEM_WSTR(program_name);
SET_ITEM_INT(parse_argv);
SET_ITEM_WSTRLIST(argv);
SET_ITEM_WSTRLIST(xoptions);
SET_ITEM_WSTRLIST(warnoptions);
SET_ITEM_WSTR(pythonpath_env);
SET_ITEM_WSTR(home);
SET_ITEM_WSTRLIST(module_search_paths);
SET_ITEM_WSTR(executable);
SET_ITEM_WSTR(prefix);
SET_ITEM_WSTR(base_prefix);
SET_ITEM_WSTR(exec_prefix);
SET_ITEM_WSTR(base_exec_prefix);
SET_ITEM_INT(site_import);
SET_ITEM_INT(bytes_warning);
SET_ITEM_INT(inspect);
SET_ITEM_INT(interactive);
SET_ITEM_INT(optimization_level);
SET_ITEM_INT(parser_debug);
SET_ITEM_INT(write_bytecode);
SET_ITEM_INT(verbose);
SET_ITEM_INT(quiet);
SET_ITEM_INT(user_site_directory);
SET_ITEM_INT(configure_c_stdio);
SET_ITEM_INT(buffered_stdio);
SET_ITEM_WSTR(stdio_encoding);
SET_ITEM_WSTR(stdio_errors);
#ifdef MS_WINDOWS
SET_ITEM_INT(legacy_windows_stdio);
#endif
SET_ITEM_INT(skip_source_first_line);
SET_ITEM_WSTR(run_command);
SET_ITEM_WSTR(run_module);
SET_ITEM_WSTR(run_filename);
SET_ITEM_INT(_install_importlib);
SET_ITEM_WSTR(check_hash_pycs_mode);
SET_ITEM_INT(pathconfig_warnings);
SET_ITEM_INT(_init_main);
return dict;
fail:
Py_DECREF(dict);
return NULL;
#undef FROM_WSTRING
#undef SET_ITEM
#undef SET_ITEM_INT
#undef SET_ITEM_UINT
#undef SET_ITEM_WSTR
#undef SET_ITEM_WSTRLIST
}
static const char*
config_get_env(const PyConfig *config, const char *name)
{
return _Py_GetEnv(config->use_environment, name);
}
/* Get a copy of the environment variable as wchar_t*.
Return 0 on success, but *dest can be NULL.
Return -1 on memory allocation failure. Return -2 on decoding error. */
static PyStatus
config_get_env_dup(PyConfig *config,
wchar_t **dest,
wchar_t *wname, char *name,
const char *decode_err_msg)
{
assert(*dest == NULL);
assert(config->use_environment >= 0);
if (!config->use_environment) {
*dest = NULL;
return _PyStatus_OK();
}
#ifdef MS_WINDOWS
const wchar_t *var = _wgetenv(wname);
if (!var || var[0] == '\0') {
*dest = NULL;
return _PyStatus_OK();
}
return PyConfig_SetString(config, dest, var);
#else
const char *var = getenv(name);
if (!var || var[0] == '\0') {
*dest = NULL;
return _PyStatus_OK();
}
return config_set_bytes_string(config, dest, var, decode_err_msg);
#endif
}
#define CONFIG_GET_ENV_DUP(CONFIG, DEST, WNAME, NAME) \
config_get_env_dup(CONFIG, DEST, WNAME, NAME, "cannot decode " NAME)
static void
config_get_global_vars(PyConfig *config)
{
if (config->_config_init != _PyConfig_INIT_COMPAT) {
/* Python and Isolated configuration ignore global variables */
return;
}
#define COPY_FLAG(ATTR, VALUE) \
if (config->ATTR == -1) { \
config->ATTR = VALUE; \
}
#define COPY_NOT_FLAG(ATTR, VALUE) \
if (config->ATTR == -1) { \
config->ATTR = !(VALUE); \
}
COPY_FLAG(isolated, Py_IsolatedFlag);
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
COPY_FLAG(inspect, Py_InspectFlag);
COPY_FLAG(interactive, Py_InteractiveFlag);
COPY_FLAG(optimization_level, Py_OptimizeFlag);
COPY_FLAG(parser_debug, Py_DebugFlag);
COPY_FLAG(verbose, Py_VerboseFlag);
COPY_FLAG(quiet, Py_QuietFlag);
#ifdef MS_WINDOWS
COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
#endif
COPY_NOT_FLAG(pathconfig_warnings, Py_FrozenFlag);
COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag);
COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
#undef COPY_FLAG
#undef COPY_NOT_FLAG
}