[Python-checkins] gh-80448: argparse: Fix IndexError on store_true action (GH-15656)
miss-islington
webhook-mailer at python.org
Fri Nov 11 21:16:47 EST 2022
https://github.com/python/cpython/commit/8d27e6294b73fa2b5c6ecbeee072bb05c30d2685
commit: 8d27e6294b73fa2b5c6ecbeee072bb05c30d2685
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-11-11T18:16:18-08:00
summary:
gh-80448: argparse: Fix IndexError on store_true action (GH-15656)
(cherry picked from commit e02cc6d42aee1f0a9ee629f76576eee478224d9d)
Co-authored-by: Hai Shi <shihai1992 at gmail.com>
Co-authored-by: Rémi Lapeyre <remi.lapeyre at henki.fr>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst
M Lib/argparse.py
M Lib/test/test_argparse.py
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 9be18488abe5..fb042a86b47c 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1962,7 +1962,11 @@ def consume_optional(start_index):
# arguments, try to parse more single-dash options out
# of the tail of the option string
chars = self.prefix_chars
- if arg_count == 0 and option_string[1] not in chars:
+ if (
+ arg_count == 0
+ and option_string[1] not in chars
+ and explicit_arg != ''
+ ):
action_tuples.append((action, [], option_string))
char = option_string[0]
option_string = char + explicit_arg[0]
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 0b237ab5b930..4d43e3310d49 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -295,7 +295,7 @@ class TestOptionalsSingleDashCombined(ParserTestCase):
Sig('-z'),
]
failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
- '-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
+ '-yx', '-yz a', '-yyyx', '-yyyza', '-xyza', '-x=']
successes = [
('', NS(x=False, yyy=None, z=None)),
('-x', NS(x=True, yyy=None, z=None)),
diff --git a/Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst b/Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst
new file mode 100644
index 000000000000..7c9b592d6ecd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst
@@ -0,0 +1 @@
+Fix IndexError in :class:`argparse.ArgumentParser` when a ``store_true`` action is given an explicit argument.
More information about the Python-checkins
mailing list