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

Skip to content

Commit 119ebb7

Browse files
committed
Fix shutil.get_terminal_size() error handling
Issue #26801: Fix error handling in shutil.get_terminal_size(), catch AttributeError instead of NameError. Patch written by Emanuel Barry. test_shutil: skip the functional test using "stty size" command if os.get_terminal_size() is missing.
1 parent ded4c49 commit 119ebb7

4 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lib/shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ 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 (NameError, OSError):
1072+
except (AttributeError, OSError):
10731073
size = os.terminal_size(fallback)
10741074
if columns <= 0:
10751075
columns = size.columns

Lib/test/test_shutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,8 @@ def test_os_environ_first(self):
18371837
self.assertEqual(size.lines, 888)
18381838

18391839
@unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty")
1840+
@unittest.skipUnless(hasattr(os, 'get_terminal_size'),
1841+
'need os.get_terminal_size()')
18401842
def test_stty_match(self):
18411843
"""Check if stty returns the same results ignoring env
18421844

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Matthew Barnett
9090
Richard Barran
9191
Cesar Eduardo Barros
9292
Des Barry
93+
Emanuel Barry
9394
Ulf Bartelt
9495
Campbell Barton
9596
Don Bashford

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ Core and Builtins
107107
Library
108108
-------
109109

110+
- Issue #26801: Fix error handling in :func:`shutil.get_terminal_size`, catch
111+
:exc:`AttributeError` instead of :exc:`NameError`. Patch written by Emanuel
112+
Barry.
113+
110114
- Issue #24838: tarfile's ustar and gnu formats now correctly calculate name
111115
and link field limits for multibyte character encodings like utf-8.
112116

0 commit comments

Comments
 (0)