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

Text width in optparse.py can become negative #57316

Closed
adambyrtek mannequin opened this issue Oct 5, 2011 · 10 comments
Closed

Text width in optparse.py can become negative #57316

adambyrtek mannequin opened this issue Oct 5, 2011 · 10 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@adambyrtek
Copy link
Mannequin

adambyrtek mannequin commented Oct 5, 2011

BPO 13107
Nosy @mitsuhiko, @ezio-melotti, @serhiy-storchaka, @elazarg
Files
  • fixargparse.patch: patch for argparse
  • argparse_ugly.patch: no wrapping below width=30. +basic test
  • argparse_less_ugly.patch
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2014-01-09.21:27:18.292>
    created_at = <Date 2011-10-05.11:08:22.784>
    labels = ['easy', 'type-bug', 'library']
    title = 'Text width in optparse.py can become negative'
    updated_at = <Date 2014-01-09.21:27:18.291>
    user = 'https://bugs.python.org/adambyrtek'

    bugs.python.org fields:

    activity = <Date 2014-01-09.21:27:18.291>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-01-09.21:27:18.292>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2011-10-05.11:08:22.784>
    creator = 'adambyrtek'
    dependencies = []
    files = ['31552', '31731', '31735']
    hgrepos = []
    issue_num = 13107
    keywords = ['patch', 'easy']
    message_count = 10.0
    messages = ['144947', '145145', '196455', '196740', '196802', '196804', '197502', '197544', '207795', '207797']
    nosy_count = 8.0
    nosy_names = ['bethard', 'aronacher', 'ezio.melotti', 'adambyrtek', 'python-dev', 'serhiy.storchaka', 'elazar', 'dmi.baranov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13107'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @adambyrtek
    Copy link
    Mannequin Author

    adambyrtek mannequin commented Oct 5, 2011

    Code snippet from optparse.py:

    344 self.help_position = min(max_len + 2, self.max_help_position)
    345 self.help_width = self.width - self.help_position

    Where self.width is initialized with the COLUMNS environment variable. On narrow terminals it can happen that self.help_position < self.width, leading to an exception in textwrap.py:

    raise ValueError("invalid width %r (must be \> 0)" % self.width)
    

    ValueError: invalid width -15 (must be > 0)

    A reasonable workaround would be to trim part of the help text instead of causing an exception.

    @adambyrtek adambyrtek mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 5, 2011
    @ezio-melotti
    Copy link
    Member

    argparse has some similar code in Lib/argparse.py:489. Can you reproduce the problem with argparse?
    If you can't and argparse solved the problem already, we might adopt the same solution; if you can, it should be fixed there too.

    @serhiy-storchaka serhiy-storchaka self-assigned this Aug 26, 2013
    @serhiy-storchaka
    Copy link
    Member

    Yes, argparse has same problem.

    >>> import os, argparse
    >>> p = argparse.ArgumentParser(prog='PROG')
    >>> os.environ['COLUMNS'] = '16'
    >>> print(p.format_help())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 2329, in format_help
        return formatter.format_help()
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 276, in format_help
        help = self._root_section.format_help()
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 206, in format_help
        func(*args)
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 206, in format_help
        func(*args)
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 514, in _format_action
        help_lines = self._split_lines(help_text, help_width)
      File "/home/serhiy/py/cpython/Lib/argparse.py", line 614, in _split_lines
        return _textwrap.wrap(text, width)
      File "/home/serhiy/py/cpython/Lib/textwrap.py", line 355, in wrap
        return w.wrap(text)
      File "/home/serhiy/py/cpython/Lib/textwrap.py", line 300, in wrap
        return self._wrap_chunks(chunks)
      File "/home/serhiy/py/cpython/Lib/textwrap.py", line 227, in _wrap_chunks
        raise ValueError("invalid width %r (must be > 0)" % self.width)
    ValueError: invalid width 0 (must be > 0)

    @serhiy-storchaka serhiy-storchaka removed their assignment Aug 29, 2013
    @elazarg
    Copy link
    Mannequin

    elazarg mannequin commented Sep 1, 2013

    I think in such case it is reasonable to fail silently, since the information will not be readable anyway.
    Is a patch like the attached acceptable? (Sorry, I am new here)

    results:

    >>> import os, argparse; p = argparse.ArgumentParser(prog='PROG')
    >>> os.environ['COLUMNS'] = '0'
    >>> print(p.format_help())
    usage: PROG
           
           [-h]

    optional arguments:
    -h, --help

    >>

    @dmibaranov
    Copy link
    Mannequin

    dmibaranov mannequin commented Sep 2, 2013

    I think "ugly look is better than silence" here. Elazar, can you touch a optparse too (with some tests - test.support.EnvironmentVarGuard context manager will be helpful here)?

    @serhiy-storchaka
    Copy link
    Member

    I think "ugly look is better than silence" here.

    Agree. We have two possibilities:

    1. Decrease an indentation. There is a lot of blank spaces below option's names.

    2. Set some minimal width (10 or 20 characters) and let lines wrap out.

    For better look we should use both methods.

    @elazarg
    Copy link
    Mannequin

    elazarg mannequin commented Sep 11, 2013

    ok. how about argparse_ugly.patch? below some width it simply won't do any wrapping.

    (I hadn't touch optparse yet)

    @serhiy-storchaka
    Copy link
    Member

    Here is less ugly patch (for argparse and optparse). Instead of prohibiting wrapping at all for small width, it limits minimal width of formatted text. It try first decrease the indent for help.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 9, 2014

    New changeset 779de7b4909b by Serhiy Storchaka in branch '2.7':
    Issue bpo-13107: argparse and optparse no longer raises an exception when output
    http://hg.python.org/cpython/rev/779de7b4909b

    New changeset c6c30b682e14 by Serhiy Storchaka in branch '3.3':
    Issue bpo-13107: argparse and optparse no longer raises an exception when output
    http://hg.python.org/cpython/rev/c6c30b682e14

    New changeset 48bcd03cd29f by Serhiy Storchaka in branch 'default':
    Issue bpo-13107: argparse and optparse no longer raises an exception when output
    http://hg.python.org/cpython/rev/48bcd03cd29f

    @serhiy-storchaka
    Copy link
    Member

    Thank you Adam for your report. Thank you Elazar for your patch.

    @serhiy-storchaka serhiy-storchaka self-assigned this Jan 9, 2014
    @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
    easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants