"""
try:
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
- except KeyError:
- height, width = struct.unpack(
- "hhhh", ioctl(self.input_fd, TIOCGWINSZ, b"\000" * 8)
- )[0:2]
+ except (KeyError, TypeError, ValueError):
+ try:
+ size = ioctl(self.input_fd, TIOCGWINSZ, b"\000" * 8)
+ except OSError:
+ return 25, 80
+ height, width = struct.unpack("hhhh", size)[0:2]
if not height:
return 25, 80
return height, width
"""
try:
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
- except KeyError:
+ except (KeyError, TypeError, ValueError):
return 25, 80
def forgetinput(self):
import itertools
+import os
import sys
import unittest
from functools import partial
+from test.support import os_helper
from unittest import TestCase
from unittest.mock import MagicMock, call, patch, ANY
)
console.restore()
con.restore()
+
+ def test_getheightwidth_with_invalid_environ(self, _os_write):
+ # gh-128636
+ console = UnixConsole()
+ with os_helper.EnvironmentVarGuard() as env:
+ env["LINES"] = ""
+ self.assertIsInstance(console.getheightwidth(), tuple)
+ env["COLUMNS"] = ""
+ self.assertIsInstance(console.getheightwidth(), tuple)
+ os.environ = []
+ self.assertIsInstance(console.getheightwidth(), tuple)