mirror of https://github.com/python/cpython
bpo-32652: Defer pymain_set_global_config() call (#5303)
In Py_Main(), don't call pymain_set_global_config() early: only call it when the whole configuration has been read. Add an unit test to prevent future regression.
This commit is contained in:
parent
cab0b2b053
commit
2b822a0bb1
|
@ -223,6 +223,21 @@ class UTF8ModeTests(unittest.TestCase):
|
||||||
c_arg = arg_ascii
|
c_arg = arg_ascii
|
||||||
check('utf8=0', [c_arg], LC_ALL='C')
|
check('utf8=0', [c_arg], LC_ALL='C')
|
||||||
|
|
||||||
|
def test_optim_level(self):
|
||||||
|
# CPython: check that Py_Main() doesn't increment Py_OptimizeFlag
|
||||||
|
# twice when -X utf8 requires to parse the configuration twice (when
|
||||||
|
# the encoding changes after reading the configuration, the
|
||||||
|
# configuration is read again with the new encoding).
|
||||||
|
code = 'import sys; print(sys.flags.optimize)'
|
||||||
|
out = self.get_output('-X', 'utf8', '-O', '-c', code)
|
||||||
|
self.assertEqual(out, '1')
|
||||||
|
out = self.get_output('-X', 'utf8', '-OO', '-c', code)
|
||||||
|
self.assertEqual(out, '2')
|
||||||
|
|
||||||
|
code = 'import sys; print(sys.flags.ignore_environment)'
|
||||||
|
out = self.get_output('-X', 'utf8', '-E', '-c', code)
|
||||||
|
self.assertEqual(out, '1')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -1909,22 +1909,24 @@ pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Global configuration variables should be set to read the core
|
/* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
|
||||||
configuration, and then get again to get updated values.
|
Py_NoSiteFlag variables if a "._pth" file is found. */
|
||||||
|
int init_isolated = Py_IsolatedFlag;
|
||||||
_PyPathConfig_Init() tests !Py_FrozenFlag to avoid some warnings.
|
int init_no_site = Py_NoSiteFlag;
|
||||||
Moreover, on Windows, it modifies Py_IsolatedFlag and Py_NoSiteFlag
|
Py_IsolatedFlag = cmdline->isolated;
|
||||||
variables if a "._pth" file is found. */
|
Py_NoSiteFlag = cmdline->no_site_import;
|
||||||
pymain_set_global_config(pymain, cmdline);
|
|
||||||
|
|
||||||
err = _PyCoreConfig_Read(config);
|
err = _PyCoreConfig_Read(config);
|
||||||
|
|
||||||
|
cmdline->isolated = Py_IsolatedFlag;
|
||||||
|
cmdline->no_site_import = Py_NoSiteFlag;
|
||||||
|
Py_IsolatedFlag = init_isolated;
|
||||||
|
Py_NoSiteFlag = init_no_site;
|
||||||
|
|
||||||
if (_Py_INIT_FAILED(err)) {
|
if (_Py_INIT_FAILED(err)) {
|
||||||
pymain->err = err;
|
pymain->err = err;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_UTF8Mode = pymain->config.utf8_mode;
|
|
||||||
pymain_get_global_config(pymain, cmdline);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1948,8 +1950,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
|
||||||
int locale_coerced = 0;
|
int locale_coerced = 0;
|
||||||
int loops = 0;
|
int loops = 0;
|
||||||
int init_ignore_env = pymain->config.ignore_environment;
|
int init_ignore_env = pymain->config.ignore_environment;
|
||||||
int init_isolated = cmdline->isolated;
|
|
||||||
int init_no_site = cmdline->no_site_import;
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int utf8_mode = pymain->config.utf8_mode;
|
int utf8_mode = pymain->config.utf8_mode;
|
||||||
|
@ -2011,8 +2011,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
|
||||||
modified by _PyCoreConfig_Read(). */
|
modified by _PyCoreConfig_Read(). */
|
||||||
Py_UTF8Mode = pymain->config.utf8_mode;
|
Py_UTF8Mode = pymain->config.utf8_mode;
|
||||||
Py_IgnoreEnvironmentFlag = init_ignore_env;
|
Py_IgnoreEnvironmentFlag = init_ignore_env;
|
||||||
Py_IsolatedFlag = init_isolated;
|
|
||||||
Py_NoSiteFlag = init_no_site;
|
|
||||||
_PyCoreConfig_Clear(&pymain->config);
|
_PyCoreConfig_Clear(&pymain->config);
|
||||||
pymain_clear_cmdline(pymain, cmdline);
|
pymain_clear_cmdline(pymain, cmdline);
|
||||||
pymain_get_global_config(pymain, cmdline);
|
pymain_get_global_config(pymain, cmdline);
|
||||||
|
|
Loading…
Reference in New Issue