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

bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter #8459

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

import os as _os
import re as _re
import shutil as _shutil
import sys as _sys

from gettext import gettext as _, ngettext
Expand Down Expand Up @@ -164,10 +165,7 @@ def __init__(self,

# default setting for width
if width is None:
try:
width = int(_os.environ['COLUMNS'])
except (KeyError, ValueError):
width = 80
width = _shutil.get_terminal_size().columns
width -= 2

self._prog = prog
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class TestCase(unittest.TestCase):
def setUp(self):
# The tests assume that line wrapping occurs at 80 columns, but this
# behaviour can be overridden by setting the COLUMNS environment
# variable. To ensure that this assumption is true, unset COLUMNS.
# variable. To ensure that this width is used, set COLUMNS to 80.
env = support.EnvironmentVarGuard()
env.unset("COLUMNS")
env['COLUMNS'] = '80'
self.addCleanup(env.__exit__)


Expand Down Expand Up @@ -5122,6 +5122,7 @@ def test_all_exports_everything_but_modules(self):
class TestWrappingMetavar(TestCase):

def setUp(self):
super().setUp()
self.parser = ErrorRaisingArgumentParser(
'this_is_spammy_prog_with_a_long_name_sorry_about_the_name'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use :func:`shutil.get_terminal_size` to calculate the terminal width
correctly in the ``argparse.HelpFormatter`` class. Initial patch by Zbyszek
Jędrzejewski-Szmek.