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
Comments
Code snippet from optparse.py: 344 self.help_position = min(max_len + 2, self.max_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:
ValueError: invalid width -15 (must be > 0) A reasonable workaround would be to trim part of the help text instead of causing an exception. |
argparse has some similar code in Lib/argparse.py:489. Can you reproduce the problem with argparse? |
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) |
I think in such case it is reasonable to fail silently, since the information will not be readable anyway. results: >>> import os, argparse; p = argparse.ArgumentParser(prog='PROG')
>>> os.environ['COLUMNS'] = '0'
>>> print(p.format_help())
usage: PROG
[-h] optional arguments:
|
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)? |
Agree. We have two possibilities:
For better look we should use both methods. |
ok. how about argparse_ugly.patch? below some width it simply won't do any wrapping. (I hadn't touch optparse yet) |
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. |
New changeset 779de7b4909b by Serhiy Storchaka in branch '2.7': New changeset c6c30b682e14 by Serhiy Storchaka in branch '3.3': New changeset 48bcd03cd29f by Serhiy Storchaka in branch 'default': |
Thank you Adam for your report. Thank you Elazar for your 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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: