8000 bpo-45445: Revert "bpo-45445: Fail if an invalid X-option is provided… · python/cpython@f1eda5b · GitHub
[go: up one dir, main page]

Skip to content

Commit f1eda5b

Browse files
pablogsalmiss-islington
authored andcommitted
bpo-45445: Revert "bpo-45445: Fail if an invalid X-option is provided in the command line (GH-28823)" (GH-94745)
(cherry picked from commit aa37ffd) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent c7ac8b6 commit f1eda5b

File tree

6 files changed

+27
-83
lines changed

6 files changed

+27
-83
lines changed

Doc/library/sys.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,13 +1778,13 @@ always available.
17781778

17791779
.. code-block:: shell-session
17801780
1781-
$ ./python -Xpycache_prefix=some_path -Xdev
1781+
$ ./python -Xa=b -Xc
17821782
Python 3.2a3+ (py3k, Oct 16 2010, 20:14:50)
17831783
[GCC 4.4.3] on linux2
17841784
Type "help", "copyright", "credits" or "license" for more information.
17851785
>>> import sys
17861786
>>> sys._xoptions
1787-
{'pycache_prefix': 'some_path', 'dev': True}
1787+
{'a': 'b', 'c': True}
17881788
17891789
.. impl-detail::
17901790

Lib/test/test_audit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AuditTest(unittest.TestCase):
2020
@support.requires_subprocess()
2121
def do_test(self, *args):
2222
with subprocess.Popen(
23-
[sys.executable, "-Xutf8", AUDIT_TESTS_PY, *args],
23+
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
2424
encoding="utf-8",
2525
stdout=subprocess.PIPE,
2626
stderr=subprocess.PIPE,
@@ -35,7 +35,7 @@ def do_test(self, *args):
3535
def run_python(self, *args):
3636
events = []
3737
with subprocess.Popen(
38-
[sys.executable, "-Xutf8", AUDIT_TESTS_PY, *args],
38+
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
3939
encoding="utf-8",
4040
stdout=subprocess.PIPE,
4141
stderr=subprocess.PIPE,

Lib/test/test_cmd_line.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,8 @@ def get_xoptions(*args):
108108
opts = get_xoptions()
109109
self.assertEqual(opts, {})
110110

111-
opts = get_xoptions('-Xno_debug_ranges', '-Xdev=1234')
112-
self.assertEqual(opts, {'no_debug_ranges': True, 'dev': '1234'})
113-
114-
@unittest.skipIf(interpreter_requires_environment(),
115-
'Cannot run -E tests when PYTHON env vars are required.')
116-
def test_unknown_xoptions(self):
117-
rc, out, err = assert_python_failure('-X', 'blech')
118-
self.assertIn(b'Unknown value for option -X', err)
119-
msg = b'Fatal Python error: Unknown value for option -X (see --help-xoptions)'
120-
self.assertEqual(err.splitlines().count(msg), 1)
121-
self.assertEqual(b'', out)
111+
opts = get_xoptions('-Xa', '-Xb=c,d=e')
112+
self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})
122113

123114
def test_showrefcount(self):
124115
def run_python(*args):

Lib/test/test_embed.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_pre_initialization_sys_options(self):
284284
"test_pre_initialization_sys_options", env=env)
285285
expected_output = (
286286
"sys.warnoptions: ['once', 'module', 'default']\n"
287-
"sys._xoptions: {'dev': '2', 'utf8': '1'}\n"
287+
"sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
288288
"warnings.filters[:3]: ['default', 'module', 'once']\n"
289289
)
290290
self.assertIn(expected_output, out)
@@ -842,14 +842,15 @@ def test_init_from_config(self):
842842
'argv': ['-c', 'arg2'],
843843
'orig_argv': ['python3',
844844
'-W', 'cmdline_warnoption',
845-
'-X', 'dev',
845+
'-X', 'cmdline_xoption',
846846
'-c', 'pass',
847847
'arg2'],
848848
'parse_argv': 2,
849849
'xoptions': [
850-
'dev=3',
851-
'utf8',
852-
'dev',
850+
'config_xoption1=3',
851+
'config_xoption2=',
852+
'config_xoption3',
853+
'cmdline_xoption',
853854
],
854855
'warnoptions': [
855856
'cmdline_warnoption',
@@ -1077,8 +1078,9 @@ def test_init_sys_add(self):
10771078
config = {
10781079
'faulthandler': 1,
10791080
'xoptions': [
1080-
'dev',
1081-
'utf8',
1081+
'config_xoption',
1082+
'cmdline_xoption',
1083+
'sysadd_xoption',
10821084
'faulthandler',
10831085
],
10841086
'warnoptions': [
@@ -1088,12 +1090,9 @@ def test_init_sys_add(self):
10881090
],
10891091
'orig_argv': ['python3',
10901092
'-W', 'ignore:::cmdline_warnoption',
1091-
'-X', 'utf8'],
1093+
'-X', 'cmdline_xoption'],
10921094
}
1093-
preconfig = {'utf8_mode': 1}
1094-
self.check_all_configs("test_init_sys_add", config,
1095-
expected_preconfig=preconfig,
1096-
api=API_PYTHON)
1095+
self.check_all_configs("test_init_sys_add", config, api=API_PYTHON)
10971096

10981097
def test_init_run_main(self):
10991098
code = ('import _testinternalcapi, json; '

Programs/_testembed.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static int test_pre_initialization_sys_options(void)
279279
* relying on the caller to keep the passed in strings alive.
280280
*/
281281
const wchar_t *static_warnoption = L"once";
282-
const wchar_t *static_xoption = L"utf8=1";
282+
const wchar_t *static_xoption = L"also_not_an_option=2";
283283
size_t warnoption_len = wcslen(static_warnoption);
284284
size_t xoption_len = wcslen(static_xoption);
285285
wchar_t *dynamic_once_warnoption = \
@@ -298,7 +298,7 @@ static int test_pre_initialization_sys_options(void)
298298
PySys_AddWarnOption(L"module");
299299
PySys_AddWarnOption(L"default");
300300
_Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
301-
PySys_AddXOption(L"dev=2");
301+
PySys_AddXOption(L"not_an_option=1");
302302
PySys_AddXOption(dynamic_xoption);
303303

304304
/* Delete the dynamic options early */
@@ -591,17 +591,18 @@ static int test_init_from_config(void)
591591
L"-W",
592592
L"cmdline_warnoption",
593593
L"-X",
594-
L"dev",
594+
L"cmdline_xoption",
595595
L"-c",
596596
L"pass",
597597
L"arg2",
598598
};
599599
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
600600
config.parse_argv = 1;
601601

602-
wchar_t* xoptions[2] = {
603-
L"dev=3",
604-
L"utf8",
602+
wchar_t* xoptions[3] = {
603+
L"config_xoption1=3",
604+
L"config_xoption2=",
605+
L"config_xoption3",
605606
};
606607
config_set_wide_string_list(&config, &config.xoptions,
607608
Py_ARRAY_LENGTH(xoptions), xoptions);
@@ -1425,6 +1426,7 @@ static int test_init_read_set(void)
14251426

14261427
static int test_init_sys_add(void)
14271428
{
1429+
PySys_AddXOption(L"sysadd_xoption");
14281430
PySys_AddXOption(L"faulthandler");
14291431
PySys_AddWarnOption(L"ignore:::sysadd_warnoption");
14301432

@@ -1436,14 +1438,14 @@ static int test_init_sys_add(void)
14361438
L"-W",
14371439
L"ignore:::cmdline_warnoption",
14381440
L"-X",
1439-
L"utf8",
1441+
L"cmdline_xoption",
14401442
};
14411443
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
14421444
config.parse_argv = 1;
14431445

14441446
PyStatus status;
14451447
status = PyWideStringList_Append(&config.xoptions,
1446-
L"dev");
1448+
L"config_xoption");
14471449
if (PyStatus_Exception(status)) {
14481450
goto fail;
14491451
}

Python/initconfig.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,49 +2046,6 @@ _PyConfig_InitImportConfig(PyConfig *config)
20462046
return config_init_import(config, 1);
20472047
}
20482048

2049-
// List of known xoptions to validate against the provided ones. Note that all
2050-
// options are listed, even if they are only available if a specific macro is
2051-
// set, like -X showrefcount which requires a debug build. In this case unknown
2052-
// options are silently ignored.
2053-
const wchar_t* known_xoptions[] = {
2054-< 1CF5 /span>
L"faulthandler",
2055-
L"showrefcount",
2056-
L"tracemalloc",
2057-
L"importtime",
2058-
L"dev",
2059-
L"utf8",
2060-
L"pycache_prefix",
2061-
L"warn_default_encoding",
2062-
L"no_debug_ranges",
2063-
L"frozen_modules",
2064-
NULL,
2065-
};
2066-
2067-
static const wchar_t*
2068-
_Py_check_xoptions(const PyWideStringList *xoptions, const wchar_t **names)
2069-
{
2070-
for (Py_ssize_t i=0; i < xoptions->length; i++) {
2071-
const wchar_t *option = xoptions->items[i];
2072-
size_t len;
2073-
wchar_t *sep = wcschr(option, L'=');
2074-
if (sep != NULL) {
2075-
len = (sep - option);
2076-
}
2077-
else {
2078-
len = wcslen(option);
2079-
}
2080-
int found = 0;
2081-
for (const wchar_t** name = names; *name != NULL; name++) {
2082-
if (wcsncmp(option, *name, len) == 0 && (*name)[len] == L'\0') {
2083-
found = 1;
2084-
}
2085-
}
2086-
if (found == 0) {
2087-
return option;
2088-
}
2089-
}
2090-
return NULL;
2091-
}
20922049

20932050
static PyStatus
20942051
config_read(PyConfig *config, int compute_path_config)
@@ -2104,11 +2061,6 @@ config_read(PyConfig *config, int compute_path_config)
21042061
}
21052062

21062063
/* -X options */
2107-
const wchar_t* option = _Py_check_xoptions(&config->xoptions, known_xoptions);
2108-
if (option != NULL) {
2109-
return PyStatus_Error("Unknown value for option -X (see --help-xoptions)");
2110-
}
2111-
21122064
if (config_get_xoption(config, L"showrefcount")) {
21132065
config->show_ref_count = 1;
21142066
}

0 commit comments

Comments
 (0)
0