MCPcopy Index your code
hub / github.com/RustPython/RustPython / can_colorize

Function can_colorize

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

Source from the content-addressed store, hash-verified

272
273
274def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
275
276 def _safe_getenv(k: str, fallback: str | None = None) -> str | None:
277 """Exception-safe environment retrieval. See gh-128636."""
278 try:
279 return os.environ.get(k, fallback)
280 except Exception:
281 return fallback
282
283 if file is None:
284 file = sys.stdout
285
286 if not sys.flags.ignore_environment:
287 if _safe_getenv("PYTHON_COLORS") == "0":
288 return False
289 if _safe_getenv("PYTHON_COLORS") == "1":
290 return True
291 if _safe_getenv("NO_COLOR"):
292 return False
293 if not COLORIZE:
294 return False
295 if _safe_getenv("FORCE_COLOR"):
296 return True
297 if _safe_getenv("TERM") == "dumb":
298 return False
299
300 if not hasattr(file, "fileno"):
301 return False
302
303 if sys.platform == "win32":
304 try:
305 import nt
306
307 if not nt._supports_virtual_terminal():
308 return False
309 except (ImportError, AttributeError):
310 return False
311
312 try:
313 return os.isatty(file.fileno())
314 except OSError:
315 return hasattr(file, "isatty") and file.isatty()
316
317
318default_theme = Theme()

Callers 9

_failure_headerMethod · 0.90
_set_colorMethod · 0.90
_exc_info_to_stringMethod · 0.90
setup_testsFunction · 0.90
create_worker_processFunction · 0.90
__post_init__Method · 0.90
mainFunction · 0.90
get_colorsFunction · 0.85
get_themeFunction · 0.85

Calls 4

_safe_getenvFunction · 0.85
hasattrFunction · 0.85
isattyMethod · 0.45
filenoMethod · 0.45

Tested by 1

_failure_headerMethod · 0.72