@@ -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
18631892class PublicAPITests (unittest .TestCase ):
18641893 """Ensures that the correct values are exposed in the public API."""
0 commit comments