https://github.com/python/cpython/commit/0b90dc8e89465ef02bbea06ae3f237cb5a190e57
commit: 0b90dc8e89465ef02bbea06ae3f237cb5a190e57
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-01-22T16:38:13Z
summary:

[3.13] gh-128636: Fix crash in PyREPL when `os.environ` is overwritten with an 
invalid value (GH-128653) (#129186)

gh-128636: Fix crash in PyREPL when `os.environ` is overwritten with an invalid 
value (GH-128653)
(cherry picked from commit ba9a4b621577b92f36d88388cc9f791c2dc7d7ba)

Co-authored-by: Tomas R <[email protected]>

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 0f3468421bec949..e69c96b11598aaa 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 e3bbabcb0089fb2..15dbf48bcf0f1c4 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 000000000000000..80c9840b5855305
--- /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]

Reply via email to