Skip to content

Commit e4acbd0

Browse files
yihong0618picnixz
andcommitted
fix: better way for already colorized
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 02e8f9d commit e4acbd0

6 files changed

Lines changed: 20 additions & 17 deletions

File tree

Lib/_colorize.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,10 @@ def decolor(text: str) -> str:
286286
return text
287287

288288

289-
def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
289+
def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize=False) -> bool:
290+
if already_colorize:
291+
return True
292+
290293
if file is None:
291294
file = sys.stdout
292295

@@ -331,6 +334,7 @@ def get_theme(
331334
tty_file: IO[str] | IO[bytes] | None = None,
332335
force_color: bool = False,
333336
force_no_color: bool = False,
337+
already_colorize: bool = False,
334338
) -> Theme:
335339
"""Returns the currently set theme, potentially in a zero-color variant.
336340
@@ -344,7 +348,8 @@ def get_theme(
344348
environment (including environment variable state and console configuration
345349
on Windows) can also change in the course of the application life cycle.
346350
"""
347-
if force_color or (not force_no_color and can_colorize(file=tty_file)):
351+
if force_color or (not force_no_color and
352+
can_colorize(file=tty_file, already_colorize=already_colorize)):
348353
return _theme
349354
return theme_no_color
350355

Lib/_pyrepl/reader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def calc_screen(self) -> list[str]:
343343
screeninfo.append((0, []))
344344
pos -= line_len + 1
345345
prompt, prompt_len = self.process_prompt(prompt)
346-
chars, char_widths = disp_str(line, colors, offset)
346+
chars, char_widths = disp_str(line, colors, offset, already_colorize=self.can_colorize)
347347
wrapcount = (sum(char_widths) + prompt_len) // self.console.width
348348
if wrapcount == 0 or not char_widths:
349349
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:
491491
prompt = self.ps1
492492

493493
if self.can_colorize:
494-
t = THEME(force_color=self.can_colorize)
494+
t = THEME(already_colorize=self.can_colorize)
495495
prompt = f"{t.prompt}{prompt}{t.reset}"
496496
return prompt
497497

Lib/_pyrepl/unix_console.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def __init__(
159159
self.pollob.register(self.input_fd, select.POLLIN)
160160
self.terminfo = terminfo.TermInfo(term or None)
161161
self.term = term
162-
self.is_mac = platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
162+
self.is_mac = (
163+
platform.system() == "Darwin"
164+
and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
165+
)
163166

164167
@overload
165168
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...

Lib/_pyrepl/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def disp_str(
266266
colors: list[ColorSpan] | None = None,
267267
start_index: int = 0,
268268
force_color: bool = False,
269+
already_colorize: bool = False,
269270
) -> tuple[CharBuffer, CharWidths]:
270271
r"""Decompose the input buffer into a printable variant with applied colors.
271272
@@ -306,7 +307,7 @@ def disp_str(
306307
# move past irrelevant spans
307308
colors.pop(0)
308309

309-
theme = THEME(force_color=force_color)
310+
theme = THEME(force_color=force_color, already_colorize=already_colorize)
310311
pre_color = ""
311312
post_color = ""
312313
if colors and colors[0].span.start < start_index:

Lib/test/support/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2898,7 +2898,7 @@ def force_color(color: bool):
28982898
from .os_helper import EnvironmentVarGuard
28992899

29002900
with (
2901-
swap_attr(_colorize, "can_colorize", lambda file=None: color),
2901+
swap_attr(_colorize, "can_colorize", lambda *, file=None, already_colorize=False: color),
29022902
EnvironmentVarGuard() as env,
29032903
):
29042904
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,11 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
305305
self.assertIsInstance(console.getheightwidth(), tuple)
306306

307307
@unittest.skipUnless(sys.platform == "darwin", "requires OS X")
308-
def test_is_mac_with_invalid_environ(self, _os_write):
308+
@unittest.skipUnless(sys.platform == "darwin", "requires OS X")
309+
def test_restore_with_invalid_environ_on_macOS(self, _os_write):
309310
# gh-128636 for macOS
310311
console = UnixConsole(term="xterm")
311312
with os_helper.EnvironmentVarGuard() as env:
312-
is_mac = os.getenv("TERM_PROGRAM") == "Apple_Terminal"
313-
self.assertEqual(console.is_mac, is_mac)
314313
os.environ = []
315-
console.prepare() # Need to prepare before restore
316-
try:
317-
console.restore() # This should not crash
318-
except (KeyError, AttributeError, TypeError) as e:
319-
self.fail(f"restore() failed with invalid environ: {e}")
320-
321-
self.assertEqual(console.is_mac, is_mac)
314+
console.prepare() # needed to call restore()
315+
console.restore() # this should succeed

0 commit comments

Comments
 (0)