mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Issue #13609: Add two functions to query the terminal size:
os.get_terminal_size (low level) and shutil.get_terminal_size (high level). Patch by Zbigniew Jędrzejewski-Szmek.
This commit is contained in:
parent
4195b5caea
commit
bcf2b59fb5
10 changed files with 339 additions and 5 deletions
|
@ -1840,6 +1840,43 @@ class Win32DeprecatedBytesAPI(unittest.TestCase):
|
|||
os.symlink, filename, filename)
|
||||
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size")
|
||||
class TermsizeTests(unittest.TestCase):
|
||||
def test_does_not_crash(self):
|
||||
"""Check if get_terminal_size() returns a meaningful value.
|
||||
|
||||
There's no easy portable way to actually check the size of the
|
||||
terminal, so let's check if it returns something sensible instead.
|
||||
"""
|
||||
try:
|
||||
size = os.get_terminal_size()
|
||||
except OSError as e:
|
||||
if e.errno == errno.EINVAL or sys.platform == "win32":
|
||||
# Under win32 a generic OSError can be thrown if the
|
||||
# handle cannot be retrieved
|
||||
self.skipTest("failed to query terminal size")
|
||||
raise
|
||||
|
||||
self.assertGreater(size.columns, 0)
|
||||
self.assertGreater(size.lines, 0)
|
||||
|
||||
def test_stty_match(self):
|
||||
"""Check if stty returns the same results
|
||||
|
||||
stty actually tests stdin, so get_terminal_size is invoked on
|
||||
stdin explicitly. If stty succeeded, then get_terminal_size()
|
||||
should work too.
|
||||
"""
|
||||
try:
|
||||
size = subprocess.check_output(['stty', 'size']).decode().split()
|
||||
except (FileNotFoundError, subprocess.CalledProcessError):
|
||||
self.skipTest("stty invocation failed")
|
||||
expected = (int(size[1]), int(size[0])) # reversed order
|
||||
|
||||
actual = os.get_terminal_size(sys.__stdin__.fileno())
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
|
||||
@support.reap_threads
|
||||
def test_main():
|
||||
support.run_unittest(
|
||||
|
@ -1866,6 +1903,7 @@ def test_main():
|
|||
ProgramPriorityTests,
|
||||
ExtendedAttributeTests,
|
||||
Win32DeprecatedBytesAPI,
|
||||
TermsizeTests,
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue