Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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.

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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"):
Expand Down
3 changes: 1 addition & 2 deletions Lib/_pyrepl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading