Skip to content

Commit

Permalink
[3.7] gh-96848: Fix -X int_max_str_digits option parsing (#96988) (#9…
Browse files Browse the repository at this point in the history
…7576)

Fix command line parsing: reject "-X int_max_str_digits" option with
no value (invalid) when the PYTHONINTMAXSTRDIGITS environment
variable is set to a valid limit.

(cherry picked from commit 4135166)
  • Loading branch information
vstinner committed Oct 5, 2022
1 parent 46796ed commit 98884f5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,8 @@ def test_int_max_str_digits(self):
assert_python_failure('-X', 'int_max_str_digits', '-c', code)
assert_python_failure('-X', 'int_max_str_digits=foo', '-c', code)
assert_python_failure('-X', 'int_max_str_digits=100', '-c', code)
assert_python_failure('-X', 'int_max_str_digits', '-c', code,
PYTHONINTMAXSTRDIGITS='4000')

assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='foo')
assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='100')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix command line parsing: reject :option:`-X int_max_str_digits <-X>` option
with no value (invalid) when the :envvar:`PYTHONINTMAXSTRDIGITS` environment
variable is set to a valid limit. Patch by Victor Stinner.
3 changes: 2 additions & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,10 +1813,10 @@ static _PyInitError
config_init_int_max_str_digits(_PyCoreConfig *config)
{
int maxdigits;
int valid = 0;

const char *env = config_get_env_var("PYTHONINTMAXSTRDIGITS");
if (env) {
int valid = 0;
if (!pymain_str_to_int(env, &maxdigits)) {
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
}
Expand All @@ -1834,6 +1834,7 @@ config_init_int_max_str_digits(_PyCoreConfig *config)
const wchar_t *xoption = config_get_xoption(config, L"int_max_str_digits");
if (xoption) {
const wchar_t *sep = wcschr(xoption, L'=');
int valid = 0;
if (sep) {
if (!pymain_wstr_to_int(sep + 1, &maxdigits)) {
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
Expand Down

0 comments on commit 98884f5

Please sign in to comment.