]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38821: Fix crash in argparse when using gettext (GH-17192)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 20 Nov 2019 13:48:20 +0000 (05:48 -0800)
committerGitHub <noreply@github.com>
Wed, 20 Nov 2019 13:48:20 +0000 (05:48 -0800)
(cherry picked from commit be5c79e0338005d675a64ba6e5b137e850d556d1)

Co-authored-by: Federico Bond <federicobond@gmail.com>
Lib/argparse.py
Misc/NEWS.d/next/Library/2019-11-16-23-26-25.bpo-38821.-albNN.rst [new file with mode: 0644]

index 24af355b74413018540ee3093cc02aad484d3999..ac424f4914516feb42d373bea3153dfe2406dbe7 100644 (file)
@@ -2080,10 +2080,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
                 OPTIONAL: _('expected at most one argument'),
                 ONE_OR_MORE: _('expected at least one argument'),
             }
-            default = ngettext('expected %s argument',
+            msg = nargs_errors.get(action.nargs)
+            if msg is None:
+                msg = ngettext('expected %s argument',
                                'expected %s arguments',
                                action.nargs) % action.nargs
-            msg = nargs_errors.get(action.nargs, default)
             raise ArgumentError(action, msg)
 
         # return the number of arguments matched
diff --git a/Misc/NEWS.d/next/Library/2019-11-16-23-26-25.bpo-38821.-albNN.rst b/Misc/NEWS.d/next/Library/2019-11-16-23-26-25.bpo-38821.-albNN.rst
new file mode 100644 (file)
index 0000000..2e7a22f
--- /dev/null
@@ -0,0 +1 @@
+Fix unhandled exceptions in :mod:`argparse` when internationalizing error messages for arguments with ``nargs`` set to special (non-integer) values.  Patch by Federico Bond.