bpo-34170: Cleanup pymain_read_conf() (GH-8476) · python/cpython@d145775 (original) (raw)
`@@ -169,9 +169,9 @@ pymain_usage(int error, const wchar_t* program)
`
169
169
`static const char*
`
170
170
`config_get_env_var(const _PyCoreConfig *config, const char *name)
`
171
171
`{
`
172
``
`-
assert(config->ignore_environment >= 0);
`
``
172
`+
assert(config->use_environment >= 0);
`
173
173
``
174
``
`-
if (config->ignore_environment) {
`
``
174
`+
if (!config->use_environment) {
`
175
175
`return NULL;
`
176
176
` }
`
177
177
``
`@@ -188,9 +188,9 @@ config_get_env_var(const _PyCoreConfig *config, const char *name)
`
188
188
`static int
`
189
189
`config_get_env_var_dup(const _PyCoreConfig *config, wchar_t **dest, wchar_t *wname, char *name)
`
190
190
`{
`
191
``
`-
assert(config->ignore_environment >= 0);
`
``
191
`+
assert(config->use_environment >= 0);
`
192
192
``
193
``
`-
if (config->ignore_environment) {
`
``
193
`+
if (!config->use_environment) {
`
194
194
`*dest = NULL;
`
195
195
`return 0;
`
196
196
` }
`
`@@ -557,7 +557,6 @@ _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
`
557
557
` config->ATTR = !(VALUE); \
`
558
558
` }
`
559
559
``
560
``
`-
COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
`
561
560
`COPY_FLAG(utf8_mode, Py_UTF8Mode);
`
562
561
`COPY_FLAG(isolated, Py_IsolatedFlag);
`
563
562
`COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
`
`@@ -573,6 +572,7 @@ _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
`
573
572
`COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
`
574
573
`#endif
`
575
574
``
``
575
`+
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
`
576
576
`COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
`
577
577
`COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
`
578
578
`COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
`
`@@ -599,7 +599,6 @@ _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
`
599
599
` VAR = !config->ATTR; \
`
600
600
` }
`
601
601
``
602
``
`-
COPY_FLAG(ignore_environment, Py_IgnoreEnvironmentFlag);
`
603
602
`COPY_FLAG(utf8_mode, Py_UTF8Mode);
`
604
603
`COPY_FLAG(isolated, Py_IsolatedFlag);
`
605
604
`COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
`
`@@ -615,6 +614,7 @@ _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config)
`
615
614
`COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
`
616
615
`#endif
`
617
616
``
``
617
`+
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
`
618
618
`COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
`
619
619
`COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
`
620
620
`COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
`
`@@ -702,7 +702,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
`
702
702
` } while (0)
`
703
703
``
704
704
`COPY_ATTR(install_signal_handlers);
`
705
``
`-
COPY_ATTR(ignore_environment);
`
``
705
`+
COPY_ATTR(use_environment);
`
706
706
`COPY_ATTR(use_hash_seed);
`
707
707
`COPY_ATTR(hash_seed);
`
708
708
`COPY_ATTR(_install_importlib);
`
`@@ -818,7 +818,7 @@ pymain_clear_config(_PyCoreConfig *config)
`
818
818
``
819
819
``
820
820
`static void
`
821
``
`-
pymain_free_raw(_PyMain *pymain)
`
``
821
`+
pymain_free(_PyMain *pymain)
`
822
822
`{
`
823
823
`_PyImport_Fini2();
`
824
824
``
`@@ -839,13 +839,6 @@ pymain_free_raw(_PyMain *pymain)
`
839
839
`orig_argv = NULL;
`
840
840
``
841
841
`PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
`
842
``
`-
}
`
843
``
-
844
``
-
845
``
`-
static void
`
846
``
`-
pymain_free(_PyMain *pymain)
`
847
``
`-
{
`
848
``
`-
pymain_free_raw(pymain);
`
849
842
``
850
843
`#ifdef INSURE
`
851
844
`/* Insure++ is a memory analysis tool that aids in discovering
`
`@@ -1002,9 +995,7 @@ pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
`
1002
995
`break;
`
1003
996
``
1004
997
`case 'I':
`
1005
``
`-
config->ignore_environment++;
`
1006
998
`config->isolated++;
`
1007
``
`-
config->user_site_directory = 0;
`
1008
999
`break;
`
1009
1000
``
1010
1001
`/* case 'J': reserved for Jython */
`
`@@ -1026,7 +1017,7 @@ pymain_parse_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
`
1026
1017
`break;
`
1027
1018
``
1028
1019
`case 'E':
`
1029
``
`-
config->ignore_environment++;
`
``
1020
`+
config->use_environment = 0;
`
1030
1021
`break;
`
1031
1022
``
1032
1023
`case 't':
`
`@@ -1246,10 +1237,9 @@ config_init_warnoptions(_PyCoreConfig *config, _PyCmdline *cmdline)
`
1246
1237
` Return 0 on success.
`
1247
1238
` Set pymain->err and return -1 on error. */
`
1248
1239
`static _PyInitError
`
1249
``
`-
cmdline_init_env_warnoptions(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
`
``
1240
`+
cmdline_init_env_warnoptions(_PyMain *pymain, const _PyCoreConfig *config,
`
``
1241
`+
_PyCmdline *cmdline)
`
1250
1242
`{
`
1251
``
`-
assert(!config->ignore_environment);
`
1252
``
-
1253
1243
`wchar_t *env;
`
1254
1244
`int res = config_get_env_var_dup(config, &env,
`
1255
1245
`L"PYTHONWARNINGS", "PYTHONWARNINGS");
`
`@@ -1728,10 +1718,6 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
`
1728
1718
`int nframe;
`
1729
1719
`int valid;
`
1730
1720
``
1731
``
`-
if (config->tracemalloc >= 0) {
`
1732
``
`-
return _Py_INIT_OK();
`
1733
``
`-
}
`
1734
``
-
1735
1721
`const char *env = config_get_env_var(config, "PYTHONTRACEMALLOC");
`
1736
1722
`if (env) {
`
1737
1723
`if (!pymain_str_to_int(env, &nframe)) {
`
`@@ -1866,12 +1852,6 @@ config_init_hash_seed(_PyCoreConfig *config)
`
1866
1852
`static _PyInitError
`
1867
1853
`config_init_utf8_mode(_PyCoreConfig *config)
`
1868
1854
`{
`
1869
``
`-
/* The option was already set in config.Py_UTF8Mode,
`
1870
``
`-
by Py_LegacyWindowsFSEncodingFlag or PYTHONLEGACYWINDOWSFSENCODING. */
`
1871
``
`-
if (config->utf8_mode >= 0) {
`
1872
``
`-
return _Py_INIT_OK();
`
1873
``
`-
}
`
1874
``
-
1875
1855
`const wchar_t *xopt = config_get_xoption(config, L"utf8");
`
1876
1856
`if (xopt) {
`
1877
1857
`wchar_t *sep = wcschr(xopt, L'=');
`
`@@ -1915,7 +1895,7 @@ config_init_utf8_mode(_PyCoreConfig *config)
`
1915
1895
`static _PyInitError
`
1916
1896
`config_read_env_vars(_PyCoreConfig *config)
`
1917
1897
`{
`
1918
``
`-
assert(!config->ignore_environment);
`
``
1898
`+
assert(config->use_environment > 0);
`
1919
1899
``
1920
1900
`/* Get environment variables */
`
1921
1901
`get_env_flag(config, &config->debug, "PYTHONDEBUG");
`
`@@ -1987,9 +1967,11 @@ static _PyInitError
`
1987
1967
`config_read_complex_options(_PyCoreConfig *config)
`
1988
1968
`{
`
1989
1969
`/* More complex options configured by env var and -X option */
`
1990
``
`-
if (config_get_env_var(config, "PYTHONFAULTHANDLER")
`
1991
``
`-
|| config_get_xoption(config, L"faulthandler")) {
`
1992
``
`-
config->faulthandler = 1;
`
``
1970
`+
if (config->faulthandler < 0) {
`
``
1971
`+
if (config_get_env_var(config, "PYTHONFAULTHANDLER")
`
``
1972
`+
|| config_get_xoption(config, L"faulthandler")) {
`
``
1973
`+
config->faulthandler = 1;
`
``
1974
`+
}
`
1993
1975
` }
`
1994
1976
`if (config_get_env_var(config, "PYTHONPROFILEIMPORTTIME")
`
1995
1977
`|| config_get_xoption(config, L"importtime")) {
`
`@@ -2001,16 +1983,20 @@ config_read_complex_options(_PyCoreConfig *config)
`
2001
1983
`config->dev_mode = 1;
`
2002
1984
` }
`
2003
1985
``
2004
``
`-
_PyInitError err = pymain_init_tracemalloc(config);
`
2005
``
`-
if (_Py_INIT_FAILED(err)) {
`
2006
``
`-
return err;
`
``
1986
`+
_PyInitError err;
`
``
1987
`+
if (config->tracemalloc < 0) {
`
``
1988
`+
err = pymain_init_tracemalloc(config);
`
``
1989
`+
if (_Py_INIT_FAILED(err)) {
`
``
1990
`+
return err;
`
``
1991
`+
}
`
2007
1992
` }
`
2008
1993
``
2009
``
`-
err = pymain_init_pycache_prefix(config);
`
2010
``
`-
if (_Py_INIT_FAILED(err)) {
`
2011
``
`-
return err;
`
``
1994
`+
if (config->pycache_prefix == NULL) {
`
``
1995
`+
err = pymain_init_pycache_prefix(config);
`
``
1996
`+
if (_Py_INIT_FAILED(err)) {
`
``
1997
`+
return err;
`
``
1998
`+
}
`
2012
1999
` }
`
2013
``
-
2014
2000
`return _Py_INIT_OK();
`
2015
2001
`}
`
2016
2002
``
`@@ -2036,16 +2022,22 @@ pymain_read_conf_impl(_PyMain *pymain, _PyCoreConfig *config,
`
2036
2022
`return -1;
`
2037
2023
` }
`
2038
2024
``
2039
``
`-
assert(config->ignore_environment >= 0);
`
2040
``
`-
if (!config->ignore_environment) {
`
``
2025
`+
err = _PyCoreConfig_Read(config);
`
``
2026
`+
if (_Py_INIT_FAILED(err)) {
`
``
2027
`+
pymain->err = err;
`
``
2028
`+
return -1;
`
``
2029
`+
}
`
``
2030
+
``
2031
`+
assert(config->use_environment >= 0);
`
``
2032
`+
if (config->use_environment) {
`
2041
2033
`err = cmdline_init_env_warnoptions(pymain, config, cmdline);
`
2042
2034
`if (_Py_INIT_FAILED(err)) {
`
2043
2035
`pymain->err = err;
`
2044
2036
`return -1;
`
2045
2037
` }
`
2046
2038
` }
`
2047
2039
``
2048
``
`-
err = _PyCoreConfig_Read(config);
`
``
2040
`+
err = config_init_warnoptions(config, cmdline);
`
2049
2041
`if (_Py_INIT_FAILED(err)) {
`
2050
2042
`pymain->err = err;
`
2051
2043
`return -1;
`
`@@ -2135,8 +2127,8 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
`
2135
2127
`break;
`
2136
2128
` }
`
2137
2129
``
2138
``
`-
/* Reset the configuration before reading the configuration,
`
2139
``
`-
except UTF-8 Mode. */
`
``
2130
`+
/* Reset the configuration before reading again the configuration,
`
``
2131
`+
just keep UTF-8 Mode value. */
`
2140
2132
`int new_utf8_mode = config->utf8_mode;
`
2141
2133
`if (_PyCoreConfig_Copy(config, &save_config) < 0) {
`
2142
2134
`pymain->err = _Py_INIT_NO_MEMORY();
`
`@@ -2157,7 +2149,6 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
`
2157
2149
`setlocale(LC_ALL, oldloc);
`
2158
2150
`PyMem_RawFree(oldloc);
`
2159
2151
` }
`
2160
``
-
2161
2152
`return res;
`
2162
2153
`}
`
2163
2154
``
`@@ -2201,8 +2192,19 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
`
2201
2192
``
2202
2193
`_PyCoreConfig_GetGlobalConfig(config);
`
2203
2194
``
2204
``
`-
assert(config->ignore_environment >= 0);
`
2205
``
`-
if (!config->ignore_environment) {
`
``
2195
`+
if (config->isolated > 0) {
`
``
2196
`+
config->use_environment = 0;
`
``
2197
`+
config->user_site_directory = 0;
`
``
2198
`+
}
`
``
2199
+
``
2200
`+
#ifdef MS_WINDOWS
`
``
2201
`+
if (config->legacy_windows_fs_encoding) {
`
``
2202
`+
config->utf8_mode = 0;
`
``
2203
`+
}
`
``
2204
`+
#endif
`
``
2205
+
``
2206
`+
assert(config->use_environment >= 0);
`
``
2207
`+
if (config->use_environment) {
`
2206
2208
`err = config_read_env_vars(config);
`
2207
2209
`if (_Py_INIT_FAILED(err)) {
`
2208
2210
`return err;
`
`@@ -2222,14 +2224,18 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
`
2222
2224
`return err;
`
2223
2225
` }
`
2224
2226
``
2225
``
`-
err = config_init_utf8_mode(config);
`
2226
``
`-
if (_Py_INIT_FAILED(err)) {
`
2227
``
`-
return err;
`
``
2227
`+
if (config->utf8_mode < 0) {
`
``
2228
`+
err = config_init_utf8_mode(config);
`
``
2229
`+
if (_Py_INIT_FAILED(err)) {
`
``
2230
`+
return err;
`
``
2231
`+
}
`
2228
2232
` }
`
2229
2233
``
2230
``
`-
err = config_init_home(config);
`
2231
``
`-
if (_Py_INIT_FAILED(err)) {
`
2232
``
`-
return err;
`
``
2234
`+
if (config->home == NULL) {
`
``
2235
`+
err = config_init_home(config);
`
``
2236
`+
if (_Py_INIT_FAILED(err)) {
`
``
2237
`+
return err;
`
``
2238
`+
}
`
2233
2239
` }
`
2234
2240
``
2235
2241
`if (config->program_name == NULL) {
`
`@@ -2248,7 +2254,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
`
2248
2254
` }
`
2249
2255
` }
`
2250
2256
``
2251
``
`-
/* options side effects */
`
``
2257
`+
/* default values */
`
2252
2258
`if (config->dev_mode) {
`
2253
2259
`if (config->faulthandler < 0) {
`
2254
2260
`config->faulthandler = 1;
`
`@@ -2257,14 +2263,6 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
`
2257
2263
`config->allocator = "debug";
`
2258
2264
` }
`
2259
2265
` }
`
2260
``
-
2261
``
`-
#ifdef MS_WINDOWS
`
2262
``
`-
if (config->legacy_windows_fs_encoding) {
`
2263
``
`-
config->utf8_mode = 0;
`
2264
``
`-
}
`
2265
``
`-
#endif
`
2266
``
-
2267
``
`-
/* default values */
`
2268
2266
`if (config->use_hash_seed < 0) {
`
2269
2267
`config->use_hash_seed = 0;
`
2270
2268
`config->hash_seed = 0;
`
`@@ -2547,12 +2545,6 @@ pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
`
2547
2545
`return -1;
`
2548
2546
` }
`
2549
2547
`orig_argc = pymain->argc;
`
2550
``
-
2551
``
`-
_PyInitError err = config_init_warnoptions(config, cmdline);
`
2552
``
`-
if (_Py_INIT_FAILED(err)) {
`
2553
``
`-
pymain->err = err;
`
2554
``
`-
return -1;
`
2555
``
`-
}
`
2556
2548
`return 0;
`
2557
2549
`}
`
2558
2550
``