[Python-checkins] bpo-32652: Defer pymain_set_global_config() call (#5303)

Victor Stinner webhook-mailer at python.org
Thu Jan 25 03:18:40 EST 2018


https://github.com/python/cpython/commit/2b822a0bb1de2612c85d8f75e3ce89eda2ac9f68
commit: 2b822a0bb1de2612c85d8f75e3ce89eda2ac9f68
branch: master
author: Victor Stinner <victor.stinner at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-01-25T09:18:36+01:00
summary:

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.

files:
M Lib/test/test_utf8_mode.py
M Modules/main.c

diff --git a/Lib/test/test_utf8_mode.py b/Lib/test/test_utf8_mode.py
index 73d1bd424ca..26e2e13ec53 100644
--- a/Lib/test/test_utf8_mode.py
+++ b/Lib/test/test_utf8_mode.py
@@ -223,6 +223,21 @@ def check(utf8_opt, expected, **kw):
             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()
diff --git a/Modules/main.c b/Modules/main.c
index 2e632e292c2..1ce075f98e3 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -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);



More information about the Python-checkins mailing list