New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argparse crashes in gettext when processing missing arguments #79966
Comments
When argparse is configured with an option that takes arguments, then the script is invoked with the switch but no arguments, a nonsensical exception is raised during gettext processing. In the 3.7.1 source, the error is at line 2077 of argparse.py, where 'action.nargs' is not an integer as expected by 'ngettext', but one of None, '*' or '?': default = ngettext('expected %s argument',
'expected %s arguments',
action.nargs) % action.nargs
msg = nargs_errors.get(action.nargs, default) Fix should be pretty trivial, swap the two lines and if 'get' produces None, only then compute the default. File "C:\Program Files\Python37\lib\argparse.py", line 1749, in parse_args |
Can you provide a reproducer? I'm having difficulty reproducing with this script: import argparse
p = argparse.ArgumentParser()
p.add_argument('--foo', nargs=None)
args = p.parse_args() $ python3.7 --version
Python 3.7.2
$ python3.7 t.py --foo
usage: t.py [-h] [--foo FOO]
t.py: error: argument --foo: expected one argument |
After a bit more digging, it's a side effect of having the locale set with 'Plural-Forms'. I've attached the resulting .mo file, but since it's a binary, I'm not sure it will work cross-platform, so here's how to recreate it.
Then you can reproduce with some setup in your script: import os
import gettext
import argparse
os.putenv('LANG', 'en_US') # Just to make sure.
gettext.bindtextdomain('foo', '.')
gettext.textdomain('foo')
p = argparse.ArgumentParser()
p.add_argument('--foo', nargs=None)
p.parse_args() |
No crash at Python 3.7.2+ (heads/3.7:47290e7642, Jan 20 2019, 12:22:44) |
Umm looks like I should pass an argument on cmd to reproduce it. I will try to reproduce it later |
Thanks, I installed 3.7.2 on one of our non-production machines and it appears that gettext has been fixed, so I'm closing this.
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: