bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter (… · python/cpython@74102c9

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -85,6 +85,7 @@

8585
8686

import os as _os

8787

import re as _re

88+

import shutil as _shutil

8889

import sys as _sys

8990
9091

from gettext import gettext as _, ngettext

@@ -164,10 +165,7 @@ def __init__(self,

164165
165166

# default setting for width

166167

if width is None:

167-

try:

168-

width = int(_os.environ['COLUMNS'])

169-

except (KeyError, ValueError):

170-

width = 80

168+

width = _shutil.get_terminal_size().columns

171169

width -= 2

172170
173171

self._prog = prog

Original file line numberDiff line numberDiff line change

@@ -23,9 +23,9 @@ class TestCase(unittest.TestCase):

2323

def setUp(self):

2424

# The tests assume that line wrapping occurs at 80 columns, but this

2525

# behaviour can be overridden by setting the COLUMNS environment

26-

# variable. To ensure that this assumption is true, unset COLUMNS.

26+

# variable. To ensure that this width is used, set COLUMNS to 80.

2727

env = support.EnvironmentVarGuard()

28-

env.unset("COLUMNS")

28+

env['COLUMNS'] = '80'

2929

self.addCleanup(env.__exit__)

3030
3131

@@ -5122,6 +5122,7 @@ def test_all_exports_everything_but_modules(self):

51225122

class TestWrappingMetavar(TestCase):

51235123
51245124

def setUp(self):

5125+

super().setUp()

51255126

self.parser = ErrorRaisingArgumentParser(

51265127

'this_is_spammy_prog_with_a_long_name_sorry_about_the_name'

51275128

)

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,3 @@

1+

Use :func:`shutil.get_terminal_size` to calculate the terminal width

2+

correctly in the ``argparse.HelpFormatter`` class. Initial patch by Zbyszek

3+

Jędrzejewski-Szmek.