Skip to content

Commit

Permalink
Default to stdout isatty for colour detection instead of stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 4, 2025
1 parent ab76e1b commit bb90f89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def can_colorize() -> bool:
if os.environ.get("TERM") == "dumb":
return False

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

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

try:
return os.isatty(sys.stderr.fileno())
return os.isatty(sys.stdout.fileno())
except io.UnsupportedOperation:
return sys.stderr.isatty()
return sys.stdout.isatty()
4 changes: 2 additions & 2 deletions Lib/test/test_code_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from textwrap import dedent
from contextlib import ExitStack
from unittest import mock
from test.support import import_helper

from test.support import force_not_colorized_test_class, import_helper

code = import_helper.import_module('code')

Expand All @@ -30,6 +29,7 @@ def mock_sys(self):
del self.sysmod.ps2


@force_not_colorized_test_class
class TestInteractiveConsole(unittest.TestCase, MockSys):
maxDiff = None

Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from test.support.os_helper import TESTFN, unlink
from test.support.script_helper import assert_python_ok, assert_python_failure
from test.support.import_helper import forget
from test.support import force_not_colorized
from test.support import force_not_colorized, force_not_colorized_test_class

import json
import textwrap
Expand Down Expand Up @@ -1712,6 +1712,7 @@ def f():


@requires_debug_ranges()
@force_not_colorized_test_class
class PurePythonTracebackErrorCaretTests(
PurePythonExceptionFormattingMixin,
TracebackErrorLocationCaretTestBase,
Expand All @@ -1725,6 +1726,7 @@ class PurePythonTracebackErrorCaretTests(

@cpython_only
@requires_debug_ranges()
@force_not_colorized_test_class
class CPythonTracebackErrorCaretTests(
CAPIExceptionFormattingMixin,
TracebackErrorLocationCaretTestBase,
Expand All @@ -1736,6 +1738,7 @@ class CPythonTracebackErrorCaretTests(

@cpython_only
@requires_debug_ranges()
@force_not_colorized_test_class
class CPythonTracebackLegacyErrorCaretTests(
CAPIExceptionFormattingLegacyMixin,
TracebackErrorLocationCaretTestBase,
Expand Down Expand Up @@ -2149,10 +2152,12 @@ def test_print_exception_bad_type_python(self):
boundaries = re.compile(
'(%s|%s)' % (re.escape(cause_message), re.escape(context_message)))

@force_not_colorized_test_class
class TestTracebackFormat(unittest.TestCase, TracebackFormatMixin):
pass

@cpython_only
@force_not_colorized_test_class
class TestFallbackTracebackFormat(unittest.TestCase, TracebackFormatMixin):
DEBUG_RANGES = False
def setUp(self) -> None:
Expand Down Expand Up @@ -2940,6 +2945,7 @@ def f():
self.assertEqual(report, expected)


@force_not_colorized_test_class
class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
#
# This checks reporting through the 'traceback' module, with both
Expand All @@ -2956,6 +2962,7 @@ def get_report(self, e):
return s


@force_not_colorized_test_class
class CExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
#
# This checks built-in reporting by the interpreter.
Expand Down

0 comments on commit bb90f89

Please sign in to comment.