[Python-checkins] bpo-38821: Fix crash in argparse when using gettext (GH-17192)

Miss Islington (bot) webhook-mailer at python.org
Wed Nov 20 08:48:30 EST 2019


https://github.com/python/cpython/commit/836f137f7ae0799de937e5281cb1da2bfdb8a69d
commit: 836f137f7ae0799de937e5281cb1da2bfdb8a69d
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-11-20T05:48:25-08:00
summary:

bpo-38821: Fix crash in argparse when using gettext (GH-17192)

(cherry picked from commit be5c79e0338005d675a64ba6e5b137e850d556d1)

Co-authored-by: Federico Bond <federicobond at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-11-16-23-26-25.bpo-38821.-albNN.rst
M Lib/argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index e590225c8eb8d..fd61bc78c2c3d 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2093,10 +2093,11 @@ def _match_argument(self, action, arg_strings_pattern):
                 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
index 0000000000000..2e7a22f661ac6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-11-16-23-26-25.bpo-38821.-albNN.rst
@@ -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.



More information about the Python-checkins mailing list