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
Prev Previous commit
Next Next commit
Add _colorize.get_colors and refactor
  • Loading branch information
hugovk committed Apr 25, 2024
commit a0bf8158e60fc7bfff9e9fa167fd2602a2ceb396
16 changes: 15 additions & 1 deletion Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ class ANSIColors:
YELLOW = "\x1b[33m"


def can_colorize():
def get_colors(colorize: bool = False) -> ANSIColors:
if colorize or can_colorize():
return ANSIColors()

ansi_colors = ANSIColors()

# Set color attributes to empty strings
for attr in dir(ansi_colors):
Comment thread
hugovk marked this conversation as resolved.
Outdated
if not attr.startswith("__"):
setattr(ansi_colors, attr, "")

return ansi_colors


def can_colorize() -> bool:
if sys.platform == "win32":
try:
import nt
Expand Down
21 changes: 7 additions & 14 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,20 +1610,13 @@ def summarize(self, verbose=None):
else:
failed.append((name, (failures, tries, skips)))

if can_colorize():
bold_green = ANSIColors.BOLD_GREEN
bold_red = ANSIColors.BOLD_RED
green = ANSIColors.GREEN
red = ANSIColors.RED
reset = ANSIColors.RESET
yellow = ANSIColors.YELLOW
else:
bold_green = ""
bold_red = ""
green = ""
red = ""
reset = ""
yellow = ""
ansi = _colorize.get_colors()
bold_green = ansi.BOLD_GREEN
bold_red = ansi.BOLD_RED
green = ansi.GREEN
red = ansi.RED
reset = ansi.RESET
yellow = ansi.YELLOW

if verbose:
if notests:
Expand Down