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:
Victor Stinner 2018-01-25 09:18:36 +01:00 committed by GitHub
parent cab0b2b053
commit 2b822a0bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 14 deletions

View File

@ -223,6 +223,21 @@ class UTF8ModeTests(unittest.TestCase):
c_arg = arg_ascii
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__":
unittest.main()

View File

@ -1909,22 +1909,24 @@ pymain_read_conf_impl(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
return -1;
}
/* Global configuration variables should be set to read the core
configuration, and then get again to get updated values.
_PyPathConfig_Init() tests !Py_FrozenFlag to avoid some warnings.
Moreover, on Windows, it modifies Py_IsolatedFlag and Py_NoSiteFlag
variables if a "._pth" file is found. */
pymain_set_global_config(pymain, cmdline);
/* On Windows, _PyPathConfig_Init() modifies Py_IsolatedFlag and
Py_NoSiteFlag variables if a "._pth" file is found. */
int init_isolated = Py_IsolatedFlag;
int init_no_site = Py_NoSiteFlag;
Py_IsolatedFlag = cmdline->isolated;
Py_NoSiteFlag = cmdline->no_site_import;
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)) {
pymain->err = err;
return -1;
}
Py_UTF8Mode = pymain->config.utf8_mode;
pymain_get_global_config(pymain, cmdline);
return 0;
}
@ -1948,8 +1950,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
int locale_coerced = 0;
int loops = 0;
int init_ignore_env = pymain->config.ignore_environment;
int init_isolated = cmdline->isolated;
int init_no_site = cmdline->no_site_import;
while (1) {
int utf8_mode = pymain->config.utf8_mode;
@ -2011,8 +2011,6 @@ pymain_read_conf(_PyMain *pymain, _Py_CommandLineDetails *cmdline)
modified by _PyCoreConfig_Read(). */
Py_UTF8Mode = pymain->config.utf8_mode;
Py_IgnoreEnvironmentFlag = init_ignore_env;
Py_IsolatedFlag = init_isolated;
Py_NoSiteFlag = init_no_site;
_PyCoreConfig_Clear(&pymain->config);
pymain_clear_cmdline(pymain, cmdline);
pymain_get_global_config(pymain, cmdline);