10000 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
Default to stdout isatty for colour detection instead of stderr
  • Loading branch information
hugovk committed Jan 4, 2025
commit bb90f8924934ccdfecf5573fae0251d22f26a2e7
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
Loading
0