[3.13] gh-128636: Fix crash in PyREPL when os.environ is overwritten with an invalid value for macOS (GH-138089) (GH-138942)

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

(cherry picked from commit 8ef7735c53)
This commit is contained in:
Łukasz Langa 2025-09-15 23:53:51 +01:00 committed by GitHub
parent d8b3a83cf2
commit 3669efb890
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 31 additions and 8 deletions

View file

@ -154,6 +154,10 @@ def __init__(
self.input_buffer_pos = 0
curses.setupterm(term or None, self.output_fd)
self.term = term
self.is_apple_terminal = (
platform.system() == "Darwin"
and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
)
@overload
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
@ -348,7 +352,7 @@ def prepare(self):
tcsetattr(self.input_fd, termios.TCSADRAIN, raw)
# In macOS terminal we need to deactivate line wrap via ANSI escape code
if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_apple_terminal:
os.write(self.output_fd, b"\033[?7l")
self.screen = []
@ -379,7 +383,7 @@ def restore(self):
self.flushoutput()
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)
if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_apple_terminal:
os.write(self.output_fd, b"\033[?7h")
if hasattr(self, "old_sigwinch"):