]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-80448: argparse: Fix IndexError on store_true action (GH-15656)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Nov 2022 02:30:38 +0000 (18:30 -0800)
committerGitHub <noreply@github.com>
Sat, 12 Nov 2022 02:30:38 +0000 (18:30 -0800)
(cherry picked from commit e02cc6d42aee1f0a9ee629f76576eee478224d9d)

Co-authored-by: Hai Shi <shihai1992@gmail.com>
Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2019-09-03-15-45-19.bpo-36267.z42Ex7.rst [new file with mode: 0644]

index 1c5520c4b41bd19623a10553b368dc0d13df581e..77619088614e024672db2b562f94af744cd82887 100644 (file)
@@ -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]
index 25ac03cf14810f08d3e90aa57222995eae146217..1acecbb8abaab82434debdf147b9ddcbfc04af0d 100644 (file)
@@ -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 (file)
index 0000000..7c9b592
--- /dev/null
@@ -0,0 +1 @@
+Fix IndexError in :class:`argparse.ArgumentParser` when a ``store_true`` action is given an explicit argument.