8000 gh-128595: Default to stdout isatty for colour detection instead of stderr by hugovk · Pull Request #128498 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-128595: Default to stdout isatty for colour detection instead of stderr #128498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 20, 2025
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
Check can_colorize using same output stream as traceback
  • Loading branch information
hugovk committed Jan 13, 2025
commit 2b88c85e729fdc7cbdf5390c95ed4373768d1c69
9 changes: 6 additions & 3 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def get_colors(colorize: bool = False) -> ANSIColors:
return NoColors


def can_colorize() -> bool:
def can_colorize(*, file=None) -> bool:
if file is None:
file = sys.stdout

if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
Expand All @@ -49,7 +52,7 @@ def can_colorize() -> bool:
if os.environ.get("TERM") == "dumb":
return False

if not hasattr(sys.stdout, "fileno"):
if not hasattr(file, "fileno"):
return False

if sys.platform == "win32":
Expand All @@ -62,6 +65,6 @@ def can_colorize() -> bool:
return False

try:
return os.isatty(sys.stdout.fileno())
return os.isatty(file.fileno())
except io.UnsupportedOperation:
return sys.stdout.isatty()
2 changes: 1 addition & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \

def _print_exception_bltin(exc, /):
file = sys.stderr if sys.stderr is not None else sys.__stderr__
colorize = _colorize.can_colorize()
colorize = _colorize.can_colorize(file=file)
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)


Expand Down
0