From 1b8a228730ad129f75f527fde167bacda2467224 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sat, 23 Aug 2025 21:52:17 +0800
Subject: [PATCH 01/14] fix: close issue #128636 for mac make is_mac at init
Signed-off-by: yihong0618
---
Lib/_pyrepl/reader.py | 4 +++-
Lib/_pyrepl/unix_console.py | 5 +++--
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index 0ebd9162eca4bba..308ce52aeb43052 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -274,6 +274,8 @@ def __post_init__(self) -> None:
self.cxy = self.pos2xy()
self.lxy = (self.pos, 0)
self.can_colorize = _colorize.can_colorize()
+ if self.can_colorize:
+ self.theme = THEME()
self.last_refresh_cache.screeninfo = self.screeninfo
self.last_refresh_cache.pos = self.pos
@@ -491,7 +493,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1
if self.can_colorize:
- t = THEME()
+ t = self.theme
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index a7e49923191c073..bbb207318b9fb9a 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -159,6 +159,7 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
self.term = term
+ self.is_mac = platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
@overload
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
@@ -339,7 +340,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_mac:
os.write(self.output_fd, b"\033[?7l")
self.screen = []
@@ -370,7 +371,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_mac:
os.write(self.output_fd, b"\033[?7h")
if hasattr(self, "old_sigwinch"):
From ae9d55fa77cf34389a8e4a120e63baf7477683c9 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sat, 23 Aug 2025 22:08:05 +0800
Subject: [PATCH 02/14] fix: mypy check issue
Signed-off-by: yihong0618
---
Lib/_pyrepl/reader.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index 308ce52aeb43052..f5b238901200996 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -23,6 +23,7 @@
import sys
import _colorize
+from _colorize import Syntax
from contextlib import contextmanager
from dataclasses import dataclass, field, fields
@@ -213,6 +214,7 @@ class Reader:
lxy: tuple[int, int] = field(init=False)
scheduled_commands: list[str] = field(default_factory=list)
can_colorize: bool = False
+ theme: Syntax | None = None
threading_hook: Callback | None = None
## cached metadata to speed up screen refreshes
@@ -494,6 +496,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
if self.can_colorize:
t = self.theme
+ assert t is not None # theme is set when can_colorize is True
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
From b838c04057987850cba90b9298d78a04d375c9dc Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sat, 23 Aug 2025 22:33:11 +0800
Subject: [PATCH 03/14] fix: delete copilot tab useless comments
Signed-off-by: yihong0618
---
Lib/_pyrepl/reader.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index f5b238901200996..228fa84d690a6f0 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -496,7 +496,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
if self.can_colorize:
t = self.theme
- assert t is not None # theme is set when can_colorize is True
+ assert t is not None
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
From 738d41e928b4352281948d8429b34e893af99e2e Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 06:58:30 +0800
Subject: [PATCH 04/14] fix: address comments
Signed-off-by: yihong0618
---
Lib/_pyrepl/reader.py | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index 228fa84d690a6f0..275e5621ee0e8f8 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -23,7 +23,6 @@
import sys
import _colorize
-from _colorize import Syntax
from contextlib import contextmanager
from dataclasses import dataclass, field, fields
@@ -214,7 +213,6 @@ class Reader:
lxy: tuple[int, int] = field(init=False)
scheduled_commands: list[str] = field(default_factory=list)
can_colorize: bool = False
- theme: Syntax | None = None
threading_hook: Callback | None = None
## cached metadata to speed up screen refreshes
@@ -276,8 +274,6 @@ def __post_init__(self) -> None:
self.cxy = self.pos2xy()
self.lxy = (self.pos, 0)
self.can_colorize = _colorize.can_colorize()
- if self.can_colorize:
- self.theme = THEME()
self.last_refresh_cache.screeninfo = self.screeninfo
self.last_refresh_cache.pos = self.pos
@@ -495,8 +491,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1
if self.can_colorize:
- t = self.theme
- assert t is not None
+ t = THEME(force_color=self.can_colorize)
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
From a0a5cf32dd6d2806cc61aaabaa540616221e6c90 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 10:22:45 +0800
Subject: [PATCH 05/14] fix: add tests for the change
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index ab1236768cfb3ef..72c04b299462824 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -303,3 +303,13 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
self.assertIsInstance(console.getheightwidth(), tuple)
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
+
+ def test_mac_with_invalid_environ(self, _os_write):
+ # gh-128636 for macOS
+ if sys.platform == "darwin":
+ console = UnixConsole(term="xterm")
+ with os_helper.EnvironmentVarGuard() as env:
+ env["TERM_PROGRAM"] = "Apple_Terminal"
+ console.is_mac = True
+ os.environ = []
+ self.assertIsInstance(console.getheightwidth(), tuple)
From 2e9faf78d7547432e0ec23a0237c5e164bc8c09e Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 10:32:03 +0800
Subject: [PATCH 06/14] fix: test is wrong
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index 72c04b299462824..6dacb44b3b474fc 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -309,7 +309,9 @@ def test_mac_with_invalid_environ(self, _os_write):
if sys.platform == "darwin":
console = UnixConsole(term="xterm")
with os_helper.EnvironmentVarGuard() as env:
- env["TERM_PROGRAM"] = "Apple_Terminal"
- console.is_mac = True
+ if os.getenv("TERM_PROGRAM") == "Apple_Terminal":
+ self.assertEqual(console.is_mac, True)
+ else:
+ self.assertEqual(console.is_mac, False)
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
From c7cbd228077ce29dc58b39a308a55fe21037223b Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 17:17:39 +0800
Subject: [PATCH 07/14] fix: address review comments
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index 6dacb44b3b474fc..66a1880c5bd55ff 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -304,14 +304,12 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
+ @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
def test_mac_with_invalid_environ(self, _os_write):
# gh-128636 for macOS
- if sys.platform == "darwin":
- console = UnixConsole(term="xterm")
- with os_helper.EnvironmentVarGuard() as env:
- if os.getenv("TERM_PROGRAM") == "Apple_Terminal":
- self.assertEqual(console.is_mac, True)
- else:
- self.assertEqual(console.is_mac, False)
- os.environ = []
- self.assertIsInstance(console.getheightwidth(), tuple)
+ console = UnixConsole(term="xterm")
+ with os_helper.EnvironmentVarGuard() as env:
+ is_mac = os.getenv("TERM_PROGRAM") == "Apple_Terminal"
+ self.assertEqual(console.is_mac, is_mac)
+ os.environ = []
+ self.assertIsInstance(console.getheightwidth(), tuple)
From 02e8f9d81e46229b913aae5c083fbf2150b8761c Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 18:29:48 +0800
Subject: [PATCH 08/14] fix: address comments better test for it
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index 66a1880c5bd55ff..a2251d9769f9e78 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -304,12 +304,18 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
- @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
- def test_mac_with_invalid_environ(self, _os_write):
+ @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
+ def test_is_mac_with_invalid_environ(self, _os_write):
# gh-128636 for macOS
console = UnixConsole(term="xterm")
with os_helper.EnvironmentVarGuard() as env:
is_mac = os.getenv("TERM_PROGRAM") == "Apple_Terminal"
self.assertEqual(console.is_mac, is_mac)
os.environ = []
- self.assertIsInstance(console.getheightwidth(), tuple)
+ console.prepare() # Need to prepare before restore
+ try:
+ console.restore() # This should not crash
+ except (KeyError, AttributeError, TypeError) as e:
+ self.fail(f"restore() failed with invalid environ: {e}")
+
+ self.assertEqual(console.is_mac, is_mac)
From e4acbd0af3c9668da4443b4279e5ddcbf44aa7a2 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 21:49:29 +0800
Subject: [PATCH 09/14] fix: better way for already colorized
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: yihong0618
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
---
Lib/_colorize.py | 9 +++++++--
Lib/_pyrepl/reader.py | 4 ++--
Lib/_pyrepl/unix_console.py | 5 ++++-
Lib/_pyrepl/utils.py | 3 ++-
Lib/test/support/__init__.py | 2 +-
Lib/test/test_pyrepl/test_unix_console.py | 14 ++++----------
6 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/Lib/_colorize.py b/Lib/_colorize.py
index 325efed274aed75..48a698b7e2a3e84 100644
--- a/Lib/_colorize.py
+++ b/Lib/_colorize.py
@@ -286,7 +286,10 @@ def decolor(text: str) -> str:
return text
-def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
+def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize=False) -> bool:
+ if already_colorize:
+ return True
+
if file is None:
file = sys.stdout
@@ -331,6 +334,7 @@ def get_theme(
tty_file: IO[str] | IO[bytes] | None = None,
force_color: bool = False,
force_no_color: bool = False,
+ already_colorize: bool = False,
) -> Theme:
"""Returns the currently set theme, potentially in a zero-color variant.
@@ -344,7 +348,8 @@ def get_theme(
environment (including environment variable state and console configuration
on Windows) can also change in the course of the application life cycle.
"""
- if force_color or (not force_no_color and can_colorize(file=tty_file)):
+ if force_color or (not force_no_color and
+ can_colorize(file=tty_file, already_colorize=already_colorize)):
return _theme
return theme_no_color
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index 275e5621ee0e8f8..b005bbcfeeeb6b6 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -343,7 +343,7 @@ def calc_screen(self) -> list[str]:
screeninfo.append((0, []))
pos -= line_len + 1
prompt, prompt_len = self.process_prompt(prompt)
- chars, char_widths = disp_str(line, colors, offset)
+ chars, char_widths = disp_str(line, colors, offset, already_colorize=self.can_colorize)
wrapcount = (sum(char_widths) + prompt_len) // self.console.width
if wrapcount == 0 or not char_widths:
offset += line_len + 1 # Takes all of the line plus the newline
@@ -491,7 +491,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1
if self.can_colorize:
- t = THEME(force_color=self.can_colorize)
+ t = THEME(already_colorize=self.can_colorize)
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index bbb207318b9fb9a..a385996a39e6a09 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -159,7 +159,10 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
self.term = term
- self.is_mac = platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
+ self.is_mac = (
+ platform.system() == "Darwin"
+ and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
+ )
@overload
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
diff --git a/Lib/_pyrepl/utils.py b/Lib/_pyrepl/utils.py
index fd788c8429e15b2..59faeee70cd7126 100644
--- a/Lib/_pyrepl/utils.py
+++ b/Lib/_pyrepl/utils.py
@@ -266,6 +266,7 @@ def disp_str(
colors: list[ColorSpan] | None = None,
start_index: int = 0,
force_color: bool = False,
+ already_colorize: bool = False,
) -> tuple[CharBuffer, CharWidths]:
r"""Decompose the input buffer into a printable variant with applied colors.
@@ -306,7 +307,7 @@ def disp_str(
# move past irrelevant spans
colors.pop(0)
- theme = THEME(force_color=force_color)
+ theme = THEME(force_color=force_color, already_colorize=already_colorize)
pre_color = ""
post_color = ""
if colors and colors[0].span.start < start_index:
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 4bfd01ed14a0a12..bd6bc27c322c87d 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -2898,7 +2898,7 @@ def force_color(color: bool):
from .os_helper import EnvironmentVarGuard
with (
- swap_attr(_colorize, "can_colorize", lambda file=None: color),
+ swap_attr(_colorize, "can_colorize", lambda *, file=None, already_colorize=False: color),
EnvironmentVarGuard() as env,
):
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index a2251d9769f9e78..4c8b81de896d987 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -305,17 +305,11 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
self.assertIsInstance(console.getheightwidth(), tuple)
@unittest.skipUnless(sys.platform == "darwin", "requires OS X")
- def test_is_mac_with_invalid_environ(self, _os_write):
+ @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
+ def test_restore_with_invalid_environ_on_macOS(self, _os_write):
# gh-128636 for macOS
console = UnixConsole(term="xterm")
with os_helper.EnvironmentVarGuard() as env:
- is_mac = os.getenv("TERM_PROGRAM") == "Apple_Terminal"
- self.assertEqual(console.is_mac, is_mac)
os.environ = []
- console.prepare() # Need to prepare before restore
- try:
- console.restore() # This should not crash
- except (KeyError, AttributeError, TypeError) as e:
- self.fail(f"restore() failed with invalid environ: {e}")
-
- self.assertEqual(console.is_mac, is_mac)
+ console.prepare() # needed to call restore()
+ console.restore() # this should succeed
From aa379965b6b6175ddca286f6ff0f03a97ba5e04c Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 21:52:16 +0800
Subject: [PATCH 10/14] fix: mypy check
Signed-off-by: yihong0618
---
Lib/_colorize.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Lib/_colorize.py b/Lib/_colorize.py
index 48a698b7e2a3e84..3f872e7052082c7 100644
--- a/Lib/_colorize.py
+++ b/Lib/_colorize.py
@@ -286,7 +286,7 @@ def decolor(text: str) -> str:
return text
-def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize=False) -> bool:
+def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize: bool = False) -> bool:
if already_colorize:
return True
From 72867e03725d237e9e719d7007f86c44877e5699 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Sun, 24 Aug 2025 21:56:22 +0800
Subject: [PATCH 11/14] fix: useless code check
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index 4c8b81de896d987..e64837f93cfdf31 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -304,7 +304,6 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
- @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
@unittest.skipUnless(sys.platform == "darwin", "requires OS X")
def test_restore_with_invalid_environ_on_macOS(self, _os_write):
# gh-128636 for macOS
From fe34228d498a22e8f310df7fe0dfbbdf66a93cec Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Mon, 25 Aug 2025 21:48:59 +0800
Subject: [PATCH 12/14] fix: address commnets better way with safe_get
Signed-off-by: yihong0618
---
Lib/_colorize.py | 24 ++++++++++++++----------
Lib/_pyrepl/reader.py | 4 ++--
Lib/_pyrepl/unix_console.py | 6 +++---
Lib/_pyrepl/utils.py | 3 +--
Lib/test/support/__init__.py | 2 +-
5 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/Lib/_colorize.py b/Lib/_colorize.py
index 3f872e7052082c7..c7ebd927b33a77a 100644
--- a/Lib/_colorize.py
+++ b/Lib/_colorize.py
@@ -286,25 +286,30 @@ def decolor(text: str) -> str:
return text
-def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize: bool = False) -> bool:
- if already_colorize:
- return True
+def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
+
+ def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
+ """Exception-safe environment retrieval. See gh-128636."""
+ try:
+ return os.environ.get(k, fallback)
+ except Exception:
+ return fallback
if file is None:
file = sys.stdout
if not sys.flags.ignore_environment:
- if os.environ.get("PYTHON_COLORS") == "0":
+ if _safe_getenv("PYTHON_COLORS") == "0":
return False
- if os.environ.get("PYTHON_COLORS") == "1":
+ if _safe_getenv("PYTHON_COLORS") == "1":
return True
- if os.environ.get("NO_COLOR"):
+ if _safe_getenv("NO_COLOR"):
return False
if not COLORIZE:
return False
- if os.environ.get("FORCE_COLOR"):
+ if _safe_getenv("FORCE_COLOR"):
return True
- if os.environ.get("TERM") == "dumb":
+ if _safe_getenv("TERM") == "dumb":
return False
if not hasattr(file, "fileno"):
@@ -334,7 +339,6 @@ def get_theme(
tty_file: IO[str] | IO[bytes] | None = None,
force_color: bool = False,
force_no_color: bool = False,
- already_colorize: bool = False,
) -> Theme:
"""Returns the currently set theme, potentially in a zero-color variant.
@@ -349,7 +353,7 @@ def get_theme(
on Windows) can also change in the course of the application life cycle.
"""
if force_color or (not force_no_color and
- can_colorize(file=tty_file, already_colorize=already_colorize)):
+ can_colorize(file=tty_file)):
return _theme
return theme_no_color
diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index b005bbcfeeeb6b6..0ebd9162eca4bba 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -343,7 +343,7 @@ def calc_screen(self) -> list[str]:
screeninfo.append((0, []))
pos -= line_len + 1
prompt, prompt_len = self.process_prompt(prompt)
- chars, char_widths = disp_str(line, colors, offset, already_colorize=self.can_colorize)
+ chars, char_widths = disp_str(line, colors, offset)
wrapcount = (sum(char_widths) + prompt_len) // self.console.width
if wrapcount == 0 or not char_widths:
offset += line_len + 1 # Takes all of the line plus the newline
@@ -491,7 +491,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1
if self.can_colorize:
- t = THEME(already_colorize=self.can_colorize)
+ t = THEME()
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt
diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index a385996a39e6a09..9953051bf7c4efb 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -159,7 +159,7 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
self.term = term
- self.is_mac = (
+ self.is_apple_terminal = (
platform.system() == "Darwin"
and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
)
@@ -343,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 self.is_mac:
+ if self.is_apple_terminal:
os.write(self.output_fd, b"\033[?7l")
self.screen = []
@@ -374,7 +374,7 @@ def restore(self):
self.flushoutput()
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)
- if self.is_mac:
+ if self.is_apple_terminal:
os.write(self.output_fd, b"\033[?7h")
if hasattr(self, "old_sigwinch"):
diff --git a/Lib/_pyrepl/utils.py b/Lib/_pyrepl/utils.py
index 59faeee70cd7126..fd788c8429e15b2 100644
--- a/Lib/_pyrepl/utils.py
+++ b/Lib/_pyrepl/utils.py
@@ -266,7 +266,6 @@ def disp_str(
colors: list[ColorSpan] | None = None,
start_index: int = 0,
force_color: bool = False,
- already_colorize: bool = False,
) -> tuple[CharBuffer, CharWidths]:
r"""Decompose the input buffer into a printable variant with applied colors.
@@ -307,7 +306,7 @@ def disp_str(
# move past irrelevant spans
colors.pop(0)
- theme = THEME(force_color=force_color, already_colorize=already_colorize)
+ theme = THEME(force_color=force_color)
pre_color = ""
post_color = ""
if colors and colors[0].span.start < start_index:
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index bd6bc27c322c87d..8d614ab3d42b5a6 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -2898,7 +2898,7 @@ def force_color(color: bool):
from .os_helper import EnvironmentVarGuard
with (
- swap_attr(_colorize, "can_colorize", lambda *, file=None, already_colorize=False: color),
+ swap_attr(_colorize, "can_colorize", lambda *, file=None: color),
EnvironmentVarGuard() as env,
):
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")
From 3177b0aaa21f863427aee855bb2c78e26b449820 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Tue, 26 Aug 2025 04:55:16 +0800
Subject: [PATCH 13/14] fix: address comments again
Signed-off-by: yihong0618
---
Lib/test/test_pyrepl/test_unix_console.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py
index e64837f93cfdf31..6185c7e3c794e37 100644
--- a/Lib/test/test_pyrepl/test_unix_console.py
+++ b/Lib/test/test_pyrepl/test_unix_console.py
@@ -304,11 +304,11 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)
- @unittest.skipUnless(sys.platform == "darwin", "requires OS X")
- def test_restore_with_invalid_environ_on_macOS(self, _os_write):
+ @unittest.skipUnless(sys.platform == "darwin", "requires macOS")
+ def test_restore_with_invalid_environ_on_macos(self, _os_write):
# gh-128636 for macOS
console = UnixConsole(term="xterm")
- with os_helper.EnvironmentVarGuard() as env:
+ with os_helper.EnvironmentVarGuard():
os.environ = []
console.prepare() # needed to call restore()
console.restore() # this should succeed
From 5b6c7b3d943ac8ca5a7d7bc87fcbbb393eae251a Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Wed, 10 Sep 2025 10:03:12 +0800
Subject: [PATCH 14/14] fix: add news for it
Signed-off-by: yihong0618
---
.../next/Library/2025-09-10-10-02-59.gh-issue-128636.ldRKGZ.rst | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 Misc/NEWS.d/next/Library/2025-09-10-10-02-59.gh-issue-128636.ldRKGZ.rst
diff --git a/Misc/NEWS.d/next/Library/2025-09-10-10-02-59.gh-issue-128636.ldRKGZ.rst b/Misc/NEWS.d/next/Library/2025-09-10-10-02-59.gh-issue-128636.ldRKGZ.rst
new file mode 100644
index 000000000000000..54eae0a4601617b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-09-10-10-02-59.gh-issue-128636.ldRKGZ.rst
@@ -0,0 +1,2 @@
+Fix crash in PyREPL when os.environ is overwritten with an invalid value for
+mac