MCPcopy Index your code
hub / github.com/python/cpython / can_colorize

Function can_colorize

Lib/_colorize.py:447–488  ·  view source on GitHub ↗
(*, file: IO[str] | IO[bytes] | None = None)

Source from the content-addressed store, hash-verified

445
446
447def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
448
449 def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
450 """Exception-safe environment retrieval. See gh-128636."""
451 try:
452 return os.environ.get(k, fallback)
453 except Exception:
454 return fallback
455
456 if file is None:
457 file = sys.stdout
458
459 if not sys.flags.ignore_environment:
460 if _safe_getenv("PYTHON_COLORS") == "0":
461 return False
462 if _safe_getenv("PYTHON_COLORS") == "1":
463 return True
464 if _safe_getenv("NO_COLOR"):
465 return False
466 if not COLORIZE:
467 return False
468 if _safe_getenv("FORCE_COLOR"):
469 return True
470 if _safe_getenv("TERM") == "dumb":
471 return False
472
473 if not hasattr(file, "fileno"):
474 return False
475
476 if sys.platform == "win32":
477 try:
478 import nt
479
480 if not nt._supports_virtual_terminal():
481 return False
482 except (ImportError, AttributeError):
483 return False
484
485 try:
486 return os.isatty(file.fileno())
487 except OSError:
488 return hasattr(file, "isatty") and file.isatty()
489
490
491default_theme = Theme()

Callers 13

_failure_headerMethod · 0.90
_set_colorMethod · 0.90
_get_themeMethod · 0.90
unified_diffFunction · 0.90
_exc_info_to_stringMethod · 0.90
setup_testsFunction · 0.90
create_worker_processFunction · 0.90
mainFunction · 0.90
benchmarkFunction · 0.90
print_benchmark_resultsFunction · 0.90
mainFunction · 0.90
get_colorsFunction · 0.70

Calls 3

_safe_getenvFunction · 0.70
isattyMethod · 0.45
filenoMethod · 0.45

Tested by 1

_failure_headerMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…