Skip to content

Commit fe34228

Browse files
committed
fix: address commnets better way with safe_get
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 72867e0 commit fe34228

5 files changed

Lines changed: 21 additions & 18 deletions

File tree

Lib/_colorize.py

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

288288

289-
def can_colorize(*, file: IO[str] | IO[bytes] | None = None, already_colorize: bool = False) -> bool:
290-
if already_colorize:
291-
return True
289+
def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
290+
291+
def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
292+
"""Exception-safe environment retrieval. See gh-128636."""
293+
try:
294+
return os.environ.get(k, fallback)
295+
except Exception:
296+
return fallback
292297

293298
if file is None:
294299
file = sys.stdout
295300

296301
if not sys.flags.ignore_environment:
297-
if os.environ.get("PYTHON_COLORS") == "0":
302+
if _safe_getenv("PYTHON_COLORS") == "0":
298303
return False
299-
if os.environ.get("PYTHON_COLORS") == "1":
304+
if _safe_getenv("PYTHON_COLORS") == "1":
300305
return True
301-
if os.environ.get("NO_COLOR"):
306+
if _safe_getenv("NO_COLOR"):
302307
return False
303308
if not COLORIZE:
304309
return False
305-
if os.environ.get("FORCE_COLOR"):
310+
if _safe_getenv("FORCE_COLOR"):
306311
return True
307-
if os.environ.get("TERM") == "dumb":
312+
if _safe_getenv("TERM") == "dumb":
308313
return False
309314

310315
if not hasattr(file, "fileno"):
@@ -334,7 +339,6 @@ def get_theme(
334339
tty_file: IO[str] | IO[bytes] | None = None,
335340
force_color: bool = False,
336341
force_no_color: bool = False,
337-
already_colorize: bool = False,
338342
) -> Theme:
339343
"""Returns the currently set theme, potentially in a zero-color variant.
340344
@@ -349,7 +353,7 @@ def get_theme(
349353
on Windows) can also change in the course of the application life cycle.
350354
"""
351355
if force_color or (not force_no_color and
352-
can_colorize(file=tty_file, already_colorize=already_colorize)):
356+
can_colorize(file=tty_file)):
353357
return _theme
354358
return theme_no_color
355359

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, already_colorize=self.can_colorize)
346+
chars, char_widths = disp_str(line, colors, offset)
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(already_colorize=self.can_colorize)
494+
t = THEME()
495495
prompt = f"{t.prompt}{prompt}{t.reset}"
496496
return prompt
497497

Lib/_pyrepl/unix_console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ 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 = (
162+
self.is_apple_terminal = (
163163
platform.system() == "Darwin"
164164
and os.getenv("TERM_PROGRAM") == "Apple_Terminal"
165165
)
@@ -343,7 +343,7 @@ def prepare(self):
343343
tcsetattr(self.input_fd, termios.TCSADRAIN, raw)
344344

345345
# In macOS terminal we need to deactivate line wrap via ANSI escape code
346-
if self.is_mac:
346+
if self.is_apple_terminal:
347347
os.write(self.output_fd, b"\033[?7l")
348348

349349
self.screen = []
@@ -374,7 +374,7 @@ def restore(self):
374374
self.flushoutput()
375375
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)
376376

377-
if self.is_mac:
377+
if self.is_apple_terminal:
378378
os.write(self.output_fd, b"\033[?7h")
379379

380380
if hasattr(self, "old_sigwinch"):

Lib/_pyrepl/utils.py

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

310-
theme = THEME(force_color=force_color, already_colorize=already_colorize)
309+
theme = THEME(force_color=force_color)
311310
pre_color = ""
312311
post_color = ""
313312
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, already_colorize=False: color),
2901+
swap_attr(_colorize, "can_colorize", lambda *, file=None: color),
29022902
EnvironmentVarGuard() as env,
29032903
):
29042904
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")

0 commit comments

Comments
 (0)