Skip to content

Commit b9582da

Browse files
committed
Fix PyREPL failure when os.environ is overwritten
1 parent 34e840f commit b9582da

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Lib/_pyrepl/unix_console.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def getheightwidth(self):
449449
"""
450450
try:
451451
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
452-
except KeyError:
452+
except (KeyError, TypeError, ValueError):
453453
height, width = struct.unpack(
454454
"hhhh", ioctl(self.input_fd, TIOCGWINSZ, b"\000" * 8)
455455
)[0:2]
@@ -468,7 +468,7 @@ def getheightwidth(self):
468468
"""
469469
try:
470470
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
471-
except KeyError:
471+
except (KeyError, TypeError, ValueError):
472472
return 25, 80
473473

474474
def forgetinput(self):

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import itertools
2+
import os
23
import sys
34
import unittest
45
from functools import partial
6+
from test.support import os_helper
57
from unittest import TestCase
68
from unittest.mock import MagicMock, call, patch, ANY
79

@@ -312,3 +314,14 @@ def same_console(events):
312314
)
313315
console.restore()
314316
con.restore()
317+
318+
def test_getheightwidth_with_invalid_environ(self, _os_write):
319+
# gh-128636
320+
console = UnixConsole()
321+
with os_helper.EnvironmentVarGuard() as env:
322+
env["LINES"] = ""
323+
self.assertIsInstance(console.getheightwidth(), tuple)
324+
env["COLUMNS"] = ""
325+
self.assertIsInstance(console.getheightwidth(), tuple)
326+
os.environ = []
327+
self.assertIsInstance(console.getheightwidth(), tuple)

0 commit comments

Comments
 (0)