https://github.com/python/cpython/commit/ba9a4b621577b92f36d88388cc9f791c2dc7d7ba
commit: ba9a4b621577b92f36d88388cc9f791c2dc7d7ba
branch: main
author: Tomas R. <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-01-22T16:15:23Z
summary:
gh-128636: Fix crash in PyREPL when `os.environ` is overwritten with an invalid
value (#128653)
files:
A Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst
M Lib/_pyrepl/unix_console.py
M Lib/test/test_pyrepl/test_unix_console.py
diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index add31d52f78651..96379bc20f3357 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -449,10 +449,12 @@ def getheightwidth(self):
"""
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
@@ -468,7 +470,7 @@ def getheightwidth(self):
"""
try:
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
- except KeyError:
+ except (KeyError, TypeError, ValueError):
return 25, 80
def forgetinput(self):
diff --git a/Lib/test/test_pyrepl/test_unix_console.py
b/Lib/test/test_pyrepl/test_unix_console.py
index e3bbabcb0089fb..15dbf48bcf0f1c 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -1,7 +1,9 @@
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
@@ -312,3 +314,14 @@ def same_console(events):
)
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)
diff --git
a/Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst
b/Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst
new file mode 100644
index 00000000000000..80c9840b585530
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-08-22-30-38.gh-issue-128636.jQfWXj.rst
@@ -0,0 +1,2 @@
+Fix PyREPL failure when :data:`os.environ` is overwritten with an invalid
+value.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]