]> granicus.if.org Git - python/commitdiff
bpo-13041: Use shutil.get_terminal_size() in argparse.HelpFormatter (GH-8459)
authorBerker Peksag <berker.peksag@gmail.com>
Wed, 25 Jul 2018 15:23:44 +0000 (18:23 +0300)
committerGitHub <noreply@github.com>
Wed, 25 Jul 2018 15:23:44 +0000 (18:23 +0300)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2018-07-25-12-08-48.bpo-13041.lNmgDz.rst [new file with mode: 0644]

index a0307492476259c51d603bb6b15653f6d3a99d85..83f47e35a73c7416c5fa98911b4fe32994d9b677 100644 (file)
@@ -85,6 +85,7 @@ __all__ = [
 
 import os as _os
 import re as _re
+import shutil as _shutil
 import sys as _sys
 
 from gettext import gettext as _, ngettext
@@ -164,10 +165,7 @@ class HelpFormatter(object):
 
         # 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
index 85449c729902fba30caf5b557c8f062c713477cf..f0802a50973116ca095573d9eadde2491705d703 100644 (file)
@@ -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__)
 
 
@@ -5122,6 +5122,7 @@ class TestImportStar(TestCase):
 class TestWrappingMetavar(TestCase):
 
     def setUp(self):
+        super().setUp()
         self.parser = ErrorRaisingArgumentParser(
             'this_is_spammy_prog_with_a_long_name_sorry_about_the_name'
         )
diff --git a/Misc/NEWS.d/next/Library/2018-07-25-12-08-48.bpo-13041.lNmgDz.rst b/Misc/NEWS.d/next/Library/2018-07-25-12-08-48.bpo-13041.lNmgDz.rst
new file mode 100644 (file)
index 0000000..d0871a8
--- /dev/null
@@ -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.