This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse.ArgumentParser not support unicode in print help
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bethard, eric.araujo, gkraser, markastern
Priority: normal Keywords:

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 "<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.
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 > <some file>
python argparsebug.py --help | more

give the error.

However:

python argparsenobug.py --help
python argparsenobug.py --help > <some file>
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:06adminsetgithub: 53988
2016-02-01 19:01:49markasternsetfiles: + argparsenobug.py

messages: + msg259343
2016-02-01 19:00:46markasternsetfiles: + argparsebug.py
nosy: + markastern
messages: + msg259342

2010-11-01 16:38:05bethardsetstatus: open -> closed
resolution: not a bug
messages: + msg120138
2010-09-13 09:08:54bethardsetmessages: + msg116293
2010-09-11 23:50:41eric.araujosetnosy: + eric.araujo
messages: + msg116134
2010-09-07 20:06:50r.david.murraysetnosy: + bethard
2010-09-05 07:36:09gkrasercreate