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

Co-authored-by: yihong <zouzou0208@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-09-16 11:10:53 +02:00 committed by GitHub
parent dc4d016e7e
commit 1c901860e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 9 deletions

View file

@ -159,6 +159,10 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
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: ...
@ -339,7 +343,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 = []
@ -370,7 +374,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"):