Issue 9779: argparse.ArgumentParser not support unicode in print help (original) (raw)

Created on 2010-09-05 07:36 by gkraser, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_ap.py gkraser,2010-09-05 07:36 unit test
argparsebug.py markastern,2016-02-01 19:00 source code illustrating bug
argparsenobug.py markastern,2016-02-01 19:01 Source code using subclass as a workaround
Messages (6)
msg115630 - (view) Author: (gkraser) Date: 2010-09-05 07:36
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
msg116134 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-11 23:50
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.
msg116293 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-09-13 09:08
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 "", line 1, in 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 "", line 1, in 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.
msg120138 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-11-01 16:38
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.
msg259342 - (view) Author: Mark Stern (markastern) Date: 2016-02-01 19:00
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 > python argparsebug.py --help | more give the error. However: python argparsenobug.py --help python argparsenobug.py --help > python argparsenobug.py --help more all work.
msg259343 - (view) Author: Mark Stern (markastern) Date: 2016-02-01 19:01
And here is the source code with the workaround class
History
Date User Action Args
2022-04-11 14:57:06 admin set github: 53988
2016-02-01 19:01:49 markastern set files: + argparsenobug.pymessages: +
2016-02-01 19:00:46 markastern set files: + argparsebug.pynosy: + markasternmessages: +
2010-11-01 16:38:05 bethard set status: open -> closedresolution: not a bugmessages: +
2010-09-13 09:08:54 bethard set messages: +
2010-09-11 23:50:41 eric.araujo set nosy: + eric.araujomessages: +
2010-09-07 20:06:50 r.david.murray set nosy: + bethard
2010-09-05 07:36:09 gkraser create