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.ArgumentParser not support unicode in print help #53988
Comments
argparse.ArgumentParser not support unicode in print help. Example: # -*- coding: utf-8 -*-
import argparse
import unittest
class Test1(unittest.TestCase):
def test_unicode_desc(self):
h = u'Rus Рус' # unicode
print h # ok
parser = argparse.ArgumentParser(description=h)
parser.print_help() # error |
Do the docs say this should be supported? I’m a die-hard unicode user, but this treads the line between fix and feature, so if the docs don’t restrict help to str, this could be fixed, otherwise we’re out of luck for the 2.x series. |
Are you sure this is an argparse issue, and not a terminal issue? Here's what I see: >>> parser = argparse.ArgumentParser(description=u'Rus Рус')
>>> print(parser.description)
Rus Рус
>>> sys.stderr.write(parser.description)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128)
>>> parser.format_help()
u'usage: [-h]\n\nRus \u0420\u0443\u0441\n\noptional arguments:\n -h, --help show this help message and exit\n'
>>> sys.stderr.write(parser.format_help())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-19: ordinal not in range(128) To me, it looks like sys.stderr doesn't like the unicode, even though at the interactive prompt I can print things okay. |
Closing as invalid, as to me this looks like a classic terminal encoding issue and not an argparse issue, and there was no response from the user who filed the issue. If someone still thinks this is an argparse issue, please provide a test and reopen the issue. |
This bit me also. For anybody else reading this, argparsenobug.py includes a class that provides a workaround. python argparsebug.py --help works, but: python argparsebug.py --help > <some file> give the error. However: python argparsenobug.py --help all work. |
And here is the source code with the workaround class |
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: