Skip to content
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

Closed
gkraser mannequin opened this issue Sep 5, 2010 · 6 comments
Closed

argparse.ArgumentParser not support unicode in print help #53988

gkraser mannequin opened this issue Sep 5, 2010 · 6 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gkraser
Copy link
Mannequin

gkraser mannequin commented Sep 5, 2010

BPO 9779
Nosy @merwok
Files
  • test_ap.py: unit test
  • argparsebug.py: source code illustrating bug
  • argparsenobug.py: Source code using subclass as a workaround
  • 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:

    assignee = None
    closed_at = <Date 2010-11-01.16:38:05.379>
    created_at = <Date 2010-09-05.07:36:09.098>
    labels = ['invalid', 'type-bug', 'library']
    title = 'argparse.ArgumentParser not support unicode in print help'
    updated_at = <Date 2016-02-01.19:01:49.799>
    user = 'https://bugs.python.org/gkraser'

    bugs.python.org fields:

    activity = <Date 2016-02-01.19:01:49.799>
    actor = 'markastern'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-01.16:38:05.379>
    closer = 'bethard'
    components = ['Library (Lib)']
    creation = <Date 2010-09-05.07:36:09.098>
    creator = 'gkraser'
    dependencies = []
    files = ['18757', '41774', '41775']
    hgrepos = []
    issue_num = 9779
    keywords = []
    message_count = 6.0
    messages = ['115630', '116134', '116293', '120138', '259342', '259343']
    nosy_count = 4.0
    nosy_names = ['bethard', 'eric.araujo', 'gkraser', 'markastern']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9779'
    versions = ['Python 2.7']

    @gkraser
    Copy link
    Mannequin Author

    gkraser mannequin commented Sep 5, 2010

    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

    @gkraser gkraser mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 5, 2010
    @merwok
    Copy link
    Member

    merwok commented Sep 11, 2010

    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.

    @bethard
    Copy link
    Mannequin

    bethard mannequin commented Sep 13, 2010

    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.

    @bethard
    Copy link
    Mannequin

    bethard mannequin commented Nov 1, 2010

    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.

    @bethard bethard mannequin closed this as completed Nov 1, 2010
    @bethard bethard mannequin added the invalid label Nov 1, 2010
    @markastern
    Copy link
    Mannequin

    markastern mannequin commented Feb 1, 2016

    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.

    @markastern
    Copy link
    Mannequin

    markastern mannequin commented Feb 1, 2016

    And here is the source code with the workaround class

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant