bpo-42260: PyConfig_Read() only parses argv once (GH-23168)
The PyConfig_Read() function now only parses PyConfig.argv arguments
once: PyConfig.parse_argv is set to 2 after arguments are parsed.
Since Python arguments are strippped from PyConfig.argv, parsing
arguments twice would parse the application options as Python
options.
* Rework the PyConfig documentation.
* Fix _testinternalcapi.set_config() error handling.
* SetConfigTests no longer needs parse_argv=0 when restoring the old
configuration.
diff --git a/Lib/test/_test_embed_set_config.py b/Lib/test/_test_embed_set_config.py
index 7c91381..a19f8db 100644
--- a/Lib/test/_test_embed_set_config.py
+++ b/Lib/test/_test_embed_set_config.py
@@ -20,7 +20,7 @@ def setUp(self):
self.sys_copy = dict(sys.__dict__)
def tearDown(self):
- self.set_config(parse_argv=0)
+ _testinternalcapi.set_config(self.old_config)
sys.__dict__.clear()
sys.__dict__.update(self.sys_copy)
@@ -234,6 +234,12 @@ def test_argv(self):
self.assertEqual(sys.argv, ['python_program', 'args'])
self.assertEqual(sys.orig_argv, ['orig', 'orig_args'])
+ self.set_config(parse_argv=0,
+ argv=[],
+ orig_argv=[])
+ self.assertEqual(sys.argv, [''])
+ self.assertEqual(sys.orig_argv, [])
+
def test_pycache_prefix(self):
self.check(pycache_prefix=None)
self.check(pycache_prefix="pycache_prefix")
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index 9182061..a7d9121 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -422,7 +422,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
CONFIG_PYTHON = dict(CONFIG_COMPAT,
_config_init=API_PYTHON,
configure_c_stdio=1,
- parse_argv=1,
+ parse_argv=2,
)
CONFIG_ISOLATED = dict(CONFIG_COMPAT,
_config_init=API_ISOLATED,
@@ -800,7 +800,7 @@ def test_init_from_config(self):
'-X', 'cmdline_xoption',
'-c', 'pass',
'arg2'],
- 'parse_argv': 1,
+ 'parse_argv': 2,
'xoptions': [
'config_xoption1=3',
'config_xoption2=',
@@ -1045,7 +1045,7 @@ def test_init_run_main(self):
'orig_argv': ['python3', '-c', code, 'arg2'],
'program_name': './python3',
'run_command': code + '\n',
- 'parse_argv': 1,
+ 'parse_argv': 2,
}
self.check_all_configs("test_init_run_main", config, api=API_PYTHON)
@@ -1059,7 +1059,7 @@ def test_init_main(self):
'arg2'],
'program_name': './python3',
'run_command': code + '\n',
- 'parse_argv': 1,
+ 'parse_argv': 2,
'_init_main': 0,
}
self.check_all_configs("test_init_main", config,
@@ -1068,7 +1068,7 @@ def test_init_main(self):
def test_init_parse_argv(self):
config = {
- 'parse_argv': 1,
+ 'parse_argv': 2,
'argv': ['-c', 'arg1', '-v', 'arg3'],
'orig_argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
'program_name': './argv0',