Skip to content

Commit 0ce7116

Browse files
hugovkserhiy-storchakavstinner
committed
[3.13] pythongh-128595: Default to stdout isatty for colour detection instead of stderr (pythonGH-128498)
(cherry picked from commit 6f167d7) Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 0351ed5 commit 0ce7116

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

Lib/_colorize.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ class ANSIColors:
2424
setattr(NoColors, attr, "")
2525

2626

27-
def get_colors(colorize: bool = False) -> ANSIColors:
28-
if colorize or can_colorize():
27+
def get_colors(colorize: bool = False, *, file=None) -> ANSIColors:
28+
if colorize or can_colorize(file=file):
2929
return ANSIColors()
3030
else:
3131
return NoColors
3232

3333

34-
def can_colorize() -> bool:
34+
def can_colorize(*, file=None) -> bool:
35+
if file is None:
36+
file = sys.stdout
37+
3538
if not sys.flags.ignore_environment:
3639
if os.environ.get("PYTHON_COLORS") == "0":
3740
return False
@@ -47,7 +50,7 @@ def can_colorize() -> bool:
4750
if os.environ.get("TERM") == "dumb":
4851
return False
4952

50-
if not hasattr(sys.stderr, "fileno"):
53+
if not hasattr(file, "fileno"):
5154
return False
5255

5356
if sys.platform == "win32":
@@ -60,6 +63,6 @@ def can_colorize() -> bool:
6063
return False
6164

6265
try:
63-
return os.isatty(sys.stderr.fileno())
66+
return os.isatty(file.fileno())
6467
except io.UnsupportedOperation:
65-
return sys.stderr.isatty()
68+
return file.isatty()

Lib/doctest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ def out(s):
15581558
save_displayhook = sys.displayhook
15591559
sys.displayhook = sys.__displayhook__
15601560
saved_can_colorize = _colorize.can_colorize
1561-
_colorize.can_colorize = lambda: False
1561+
_colorize.can_colorize = lambda *args, **kwargs: False
15621562
color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
15631563
for key in color_variables:
15641564
color_variables[key] = os.environ.pop(key, None)

Lib/test/support/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,7 @@ def no_color():
27002700
from .os_helper import EnvironmentVarGuard
27012701

27022702
with (
2703-
swap_attr(_colorize, "can_colorize", lambda: False),
2703+
swap_attr(_colorize, "can_colorize", lambda file=None: False),
27042704
EnvironmentVarGuard() as env,
27052705
):
27062706
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:

Lib/traceback.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
135135

136136
def _print_exception_bltin(exc, /):
137137
file = sys.stderr if sys.stderr is not None else sys.__stderr__
138-
colorize = _colorize.can_colorize()
138+
colorize = _colorize.can_colorize(file=file)
139139
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
140140

141141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Default to stdout isatty for color detection instead of stderr. Patch by
2+
Hugo van Kemenade.

0 commit comments

Comments
 (0)