Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d30829d

Browse files
Issue #26801: shutil.get_terminal_size() now handles the case of stdout is
reopened on Windows. Added tests for fallbacks.
1 parent e37fc18 commit d30829d

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lib/shutil.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,9 @@ def get_terminal_size(fallback=(80, 24)):
10691069
if columns <= 0 or lines <= 0:
10701070
try:
10711071
size = os.get_terminal_size(sys.__stdout__.fileno())
1072-
except (AttributeError, OSError):
1072+
except (AttributeError, ValueError, OSError):
1073+
# stdout is None, closed, detached, or not a terminal, or
1074+
# os.get_terminal_size() is unsupported
10731075
size = os.terminal_size(fallback)
10741076
if columns <= 0:
10751077
columns = size.columns

Lib/test/test_shutil.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,14 +1828,24 @@ def test_os_environ_first(self):
18281828

18291829
with support.EnvironmentVarGuard() as env:
18301830
env['COLUMNS'] = '777'
1831+
del env['LINES']
18311832
size = shutil.get_terminal_size()
18321833
self.assertEqual(size.columns, 777)
18331834

18341835
with support.EnvironmentVarGuard() as env:
1836+
del env['COLUMNS']
18351837
env['LINES'] = '888'
18361838
size = shutil.get_terminal_size()
18371839
self.assertEqual(size.lines, 888)
18381840

1841+
def test_bad_environ(self):
1842+
with support.EnvironmentVarGuard() as env:
1843+
env['COLUMNS'] = 'xxx'
1844+
env['LINES'] = 'yyy'
1845+
size = shutil.get_terminal_size()
1846+
self.assertGreaterEqual(size.columns, 0)
1847+
self.assertGreaterEqual(size.lines, 0)
1848+
18391849
@unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty")
18401850
@unittest.skipUnless(hasattr(os, 'get_terminal_size'),
18411851
'need os.get_terminal_size()')
@@ -1859,6 +1869,25 @@ def test_stty_match(self):
18591869

18601870
self.assertEqual(expected, actual)
18611871

1872+
def test_fallback(self):
1873+
with support.EnvironmentVarGuard() as env:
1874+
del env['LINES']
1875+
del env['COLUMNS']
1876+
1877+
# sys.__stdout__ has no fileno()
1878+
with support.swap_attr(sys, '__stdout__', None):
1879+
size = shutil.get_terminal_size(fallback=(10, 20))
1880+
self.assertEqual(size.columns, 10)
1881+
self.assertEqual(size.lines, 20)
1882+
1883+
# sys.__stdout__ is not a terminal on Unix
1884+
# or fileno() not in (0, 1, 2) on Windows
1885+
with open(os.devnull, 'w') as f, \
1886+
support.swap_attr(sys, '__stdout__', f):
1887+
size = shutil.get_terminal_size(fallback=(30, 40))
1888+
self.assertEqual(size.columns, 30)
1889+
self.assertEqual(size.lines, 40)
1890+
18621891

18631892
class PublicAPITests(unittest.TestCase):
18641893
"""Ensures that the correct values are exposed in the public API."""

0 commit comments

Comments
 (0)