From e02cc6d42aee1f0a9ee629f76576eee478224d9d Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 12 Nov 2022 09:51:38 +0800 Subject: gh-80448: argparse: Fix IndexError on store_true action (#15656) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: RĂ©mi Lapeyre Co-authored-by: Jelle Zijlstra Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> --- Lib/argparse.py | 6 +++++- Lib/test/test_argparse.py | 2 +- Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index d2dcfdf..240625f 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1997,7 +1997,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): # 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 2b7f008..cabb2f8 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -296,7 +296,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 0000000..7c9b592 --- /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. -- cgit v0.12