Skip to content

Commit 2f5791d

Browse files
authored
gh-158001: No longer read global config vars in PyConfig_Read() (#158014)
PyConfig_Read() and _PyPreConfig_Read() no longer read global config variables (such as Py_BytesWarningFlag). Instead, PyConfig_Read() now copies PyPreConfig members (isolated, use_environment and dev_mode). _PyPreConfig_Read() still reads the last global configuration variable: Py_UTF8Mode.
1 parent 9232c21 commit 2f5791d

4 files changed

Lines changed: 77 additions & 152 deletions

File tree

‎Lib/test/test_embed.py‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,24 +1095,11 @@ def test_init_compat_config(self):
10951095
self.check_all_configs("test_init_compat_config", api=API_COMPAT)
10961096

10971097
def test_init_global_config(self):
1098+
# Test Py_UTF8Mode global configuration variable
10981099
preconfig = {
10991100
'utf8_mode': True,
11001101
}
1101-
config = {
1102-
'site_import': False,
1103-
'bytes_warning': True,
1104-
'warnoptions': ['default::BytesWarning'],
1105-
'inspect': True,
1106-
'interactive': True,
1107-
'optimization_level': 2,
1108-
'write_bytecode': False,
1109-
'verbose': True,
1110-
'quiet': True,
1111-
'buffered_stdio': False,
1112-
'remote_debug': True,
1113-
'user_site_directory': False,
1114-
'pathconfig_warnings': False,
1115-
}
1102+
config = {}
11161103
self.check_all_configs("test_init_global_config", config, preconfig,
11171104
api=API_COMPAT)
11181105

‎Programs/_testembed.c‎

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -597,43 +597,9 @@ static int test_init_compat_config(void)
597597

598598
static int test_init_global_config(void)
599599
{
600-
/* FIXME: test Py_IgnoreEnvironmentFlag */
601-
602600
putenv("PYTHONUTF8=0");
603601
Py_UTF8Mode = 1;
604602

605-
/* Py_IsolatedFlag is not tested */
606-
Py_NoSiteFlag = 1;
607-
Py_BytesWarningFlag = 1;
608-
609-
putenv("PYTHONINSPECT=");
610-
Py_InspectFlag = 1;
611-
612-
putenv("PYTHONOPTIMIZE=0");
613-
Py_InteractiveFlag = 1;
614-
615-
putenv("PYTHONDEBUG=0");
616-
Py_OptimizeFlag = 2;
617-
618-
/* Py_DebugFlag is not tested */
619-
620-
putenv("PYTHONDONTWRITEBYTECODE=");
621-
Py_DontWriteBytecodeFlag = 1;
622-
623-
putenv("PYTHONVERBOSE=0");
624-
Py_VerboseFlag = 1;
625-
626-
Py_QuietFlag = 1;
627-
Py_NoUserSiteDirectory = 1;
628-
629-
putenv("PYTHONUNBUFFERED=");
630-
Py_UnbufferedStdioFlag = 1;
631-
632-
Py_FrozenFlag = 1;
633-
634-
/* FIXME: test Py_LegacyWindowsFSEncodingFlag */
635-
/* FIXME: test Py_LegacyWindowsStdioFlag */
636-
637603
_testembed_initialize();
638604
dump_config();
639605
Py_Finalize();
@@ -734,52 +700,41 @@ static int test_init_from_config(void)
734700
config_set_string(&config, &config.platlibdir, L"my_platlibdir");
735701

736702
putenv("PYTHONVERBOSE=0");
737-
Py_VerboseFlag = 0;
738703
config.verbose = 1;
739704

740-
Py_NoSiteFlag = 0;
741705
config.site_import = 0;
742706

743-
Py_BytesWarningFlag = 0;
744707
config.bytes_warning = 1;
745708

746709
putenv("PYTHONINSPECT=");
747-
Py_InspectFlag = 0;
748710
config.inspect = 1;
749711

750-
Py_InteractiveFlag = 0;
751712
config.interactive = 1;
752713

753714
putenv("PYTHONOPTIMIZE=0");
754-
Py_OptimizeFlag = 1;
755715
config.optimization_level = 2;
756716

757717
/* FIXME: test parser_debug */
758718

759719
putenv("PYTHONDONTWRITEBYTECODE=");
760-
Py_DontWriteBytecodeFlag = 0;
761720
config.write_bytecode = 0;
762721

763-
Py_QuietFlag = 0;
764722
config.quiet = 1;
765723

766724
config.configure_c_stdio = 1;
767725

768726
putenv("PYTHONUNBUFFERED=");
769-
Py_UnbufferedStdioFlag = 0;
770727
config.buffered_stdio = 0;
771728

772729
putenv("PYTHONIOENCODING=cp424");
773730
config_set_string(&config, &config.stdio_encoding, L"iso8859-1");
774731
config_set_string(&config, &config.stdio_errors, L"replace");
775732

776733
putenv("PYTHONNOUSERSITE=");
777-
Py_NoUserSiteDirectory = 0;
778734
config.user_site_directory = 0;
779735

780736
config_set_string(&config, &config.check_hash_pycs_mode, L"always");
781737

782-
Py_FrozenFlag = 0;
783738
config.pathconfig_warnings = 0;
784739

785740
config.safe_path = 1;
@@ -882,7 +837,6 @@ static void set_all_env_vars(void)
882837
static int test_init_compat_env(void)
883838
{
884839
/* Test initialization from environment variables */
885-
Py_IgnoreEnvironmentFlag = 0;
886840
set_all_env_vars();
887841
_testembed_initialize();
888842
dump_config();
@@ -918,7 +872,6 @@ static void set_all_env_vars_dev_mode(void)
918872
static int test_init_env_dev_mode(void)
919873
{
920874
/* Test initialization from environment variables */
921-
Py_IgnoreEnvironmentFlag = 0;
922875
set_all_env_vars_dev_mode();
923876
_testembed_initialize();
924877
dump_config();
@@ -930,7 +883,6 @@ static int test_init_env_dev_mode(void)
930883
static int test_init_env_dev_mode_alloc(void)
931884
{
932885
/* Test initialization from environment variables */
933-
Py_IgnoreEnvironmentFlag = 0;
934886
set_all_env_vars_dev_mode();
935887
#ifndef Py_GIL_DISABLED
936888
putenv("PYTHONMALLOC=malloc");
@@ -950,7 +902,6 @@ static int test_init_isolated_flag(void)
950902
PyConfig config;
951903
PyConfig_InitPythonConfig(&config);
952904

953-
Py_IsolatedFlag = 0;
954905
config.isolated = 1;
955906
// These options are set to 1 by isolated=1
956907
config.safe_path = 0;
@@ -1010,7 +961,6 @@ static int test_preinit_isolated2(void)
1010961
PyConfig config;
1011962
_PyConfig_InitCompatConfig(&config);
1012963

1013-
Py_IsolatedFlag = 0;
1014964
config.isolated = 1;
1015965

1016966
config_set_program_name(&config);
@@ -1081,28 +1031,6 @@ static int test_preinit_parse_argv(void)
10811031

10821032

10831033

1084-
static void set_all_global_config_variables(void)
1085-
{
1086-
Py_IsolatedFlag = 0;
1087-
Py_IgnoreEnvironmentFlag = 0;
1088-
Py_BytesWarningFlag = 2;
1089-
Py_InspectFlag = 1;
1090-
Py_InteractiveFlag = 1;
1091-
Py_OptimizeFlag = 1;
1092-
Py_DebugFlag = 1;
1093-
Py_VerboseFlag = 1;
1094-
Py_QuietFlag = 1;
1095-
Py_FrozenFlag = 0;
1096-
Py_UnbufferedStdioFlag = 1;
1097-
Py_NoSiteFlag = 1;
1098-
Py_DontWriteBytecodeFlag = 1;
1099-
Py_NoUserSiteDirectory = 1;
1100-
#ifdef MS_WINDOWS
1101-
Py_LegacyWindowsStdioFlag = 1;
1102-
#endif
1103-
}
1104-
1105-
11061034
static int check_preinit_isolated_config(int preinit)
11071035
{
11081036
PyStatus status;
@@ -1111,9 +1039,6 @@ static int check_preinit_isolated_config(int preinit)
11111039
/* environment variables must be ignored */
11121040
set_all_env_vars();
11131041

1114-
/* global configuration variables must be ignored */
1115-
set_all_global_config_variables();
1116-
11171042
if (preinit) {
11181043
PyPreConfig preconfig;
11191044
PyPreConfig_InitIsolatedConfig(&preconfig);
@@ -1158,19 +1083,6 @@ static int test_init_isolated_config(void)
11581083

11591084
static int check_init_python_config(int preinit)
11601085
{
1161-
/* global configuration variables must be ignored */
1162-
set_all_global_config_variables();
1163-
Py_IsolatedFlag = 1;
1164-
Py_IgnoreEnvironmentFlag = 1;
1165-
Py_FrozenFlag = 1;
1166-
Py_UnbufferedStdioFlag = 1;
1167-
Py_NoSiteFlag = 1;
1168-
Py_DontWriteBytecodeFlag = 1;
1169-
Py_NoUserSiteDirectory = 1;
1170-
#ifdef MS_WINDOWS
1171-
Py_LegacyWindowsStdioFlag = 1;
1172-
#endif
1173-
11741086
if (preinit) {
11751087
PyPreConfig preconfig;
11761088
PyPreConfig_InitPythonConfig(&preconfig);
@@ -1276,7 +1188,6 @@ static int test_open_code_hook(void)
12761188
return 2;
12771189
}
12781190

1279-
Py_IgnoreEnvironmentFlag = 0;
12801191
_testembed_initialize();
12811192
result = 0;
12821193

@@ -1339,7 +1250,6 @@ static int _test_audit(Py_ssize_t setValue)
13391250
{
13401251
Py_ssize_t sawSet = 0;
13411252

1342-
Py_IgnoreEnvironmentFlag = 0;
13431253
PySys_AddAuditHook(_audit_hook, &sawSet);
13441254
_testembed_initialize();
13451255

@@ -1451,7 +1361,6 @@ static int _audit_subinterpreter_hook(const char *event, PyObject *args, void *u
14511361

14521362
static int test_audit_subinterpreter(void)
14531363
{
1454-
Py_IgnoreEnvironmentFlag = 0;
14551364
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
14561365
_testembed_initialize();
14571366

@@ -1501,7 +1410,6 @@ static int test_audit_run_command(void)
15011410
AuditRunCommandTest test = {"cpython.run_command"};
15021411
wchar_t *argv[] = {PROGRAM_NAME, L"-c", L"pass"};
15031412

1504-
Py_IgnoreEnvironmentFlag = 0;
15051413
PySys_AddAuditHook(_audit_hook_run, (void*)&test);
15061414

15071415
return Py_Main(Py_ARRAY_LENGTH(argv), argv);
@@ -1512,7 +1420,6 @@ static int test_audit_run_file(void)
15121420
AuditRunCommandTest test = {"cpython.run_file"};
15131421
wchar_t *argv[] = {PROGRAM_NAME, L"filename.py"};
15141422

1515-
Py_IgnoreEnvironmentFlag = 0;
15161423
PySys_AddAuditHook(_audit_hook_run, (void*)&test);
15171424

15181425
return Py_Main(Py_ARRAY_LENGTH(argv), argv);

‎Python/initconfig.c‎

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,31 +1818,19 @@ config_get_env_dup(PyConfig *config,
18181818

18191819

18201820
static void
1821-
config_get_global_vars(PyConfig *config)
1821+
config_read_preconfig(PyConfig *config)
18221822
{
1823-
if (config->_config_init != _PyConfig_INIT_COMPAT) {
1824-
/* Python and Isolated configuration ignore global variables */
1825-
return;
1826-
}
1827-
1828-
const PyConfigSpec *spec = PYCONFIG_SPEC;
1829-
for (; spec->name != NULL; spec++) {
1830-
if (spec->global_var.ptr == NULL) {
1831-
continue;
1832-
}
1833-
assert(spec->type == PyConfig_MEMBER_INT
1834-
|| spec->type == PyConfig_MEMBER_UINT
1835-
|| spec->type == PyConfig_MEMBER_BOOL);
1836-
int *member = config_get_spec_member(config, spec);
1837-
if (*member != -1) {
1838-
continue;
1839-
}
1840-
int value = *spec->global_var.ptr;
1841-
if (spec->global_var.not) {
1842-
value = !value;
1823+
#define COPY_FLAG(ATTR) \
1824+
if (config->ATTR == -1) { \
1825+
config->ATTR = preconfig->ATTR; \
18431826
}
1844-
*member = value;
1845-
}
1827+
1828+
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
1829+
COPY_FLAG(isolated);
1830+
COPY_FLAG(use_environment);
1831+
COPY_FLAG(dev_mode);
1832+
1833+
#undef COPY_FLAG
18461834
}
18471835

18481836

@@ -3748,7 +3736,56 @@ _PyConfig_Read(PyConfig *config, int compute_path_config)
37483736
return status;
37493737
}
37503738

3751-
config_get_global_vars(config);
3739+
config_read_preconfig(config);
3740+
3741+
// Set default values
3742+
if (config->bytes_warning < 0) {
3743+
config->bytes_warning = 0;
3744+
}
3745+
if (config->inspect < 0) {
3746+
config->inspect = 0;
3747+
}
3748+
if (config->interactive < 0) {
3749+
config->interactive = 0;
3750+
}
3751+
if (config->optimization_level < 0) {
3752+
config->optimization_level = 0;
3753+
}
3754+
if (config->parser_debug < 0) {
3755+
config->parser_debug = 0;
3756+
}
3757+
if (config->quiet < 0) {
3758+
config->quiet = 0;
3759+
}
3760+
if (config->use_environment < 0) {
3761+
config->use_environment = 0;
3762+
}
3763+
if (config->verbose < 0) {
3764+
config->verbose = 0;
3765+
}
3766+
if (config->write_bytecode < 0) {
3767+
config->write_bytecode = 1;
3768+
}
3769+
if (config->buffered_stdio < 0) {
3770+
config->buffered_stdio = 1;
3771+
}
3772+
if (config->isolated < 0) {
3773+
config->isolated = 0;
3774+
}
3775+
#ifdef MS_WINDOWS
3776+
if (config->legacy_windows_stdio < 0) {
3777+
config->legacy_windows_stdio = 0;
3778+
}
3779+
#endif
3780+
if (config->pathconfig_warnings < 0) {
3781+
config->pathconfig_warnings = 1;
3782+
}
3783+
if (config->site_import < 0) {
3784+
config->site_import = 1;
3785+
}
3786+
if (config->user_site_directory < 0) {
3787+
config->user_site_directory = 1;
3788+
}
37523789

37533790
#ifdef __CYGWIN__
37543791
status = config_argv0_add_exe(config);

0 commit comments

Comments
 (0)