8000 gh-127873: Only check `sys.flags.ignore_environment` for `PYTHON*` env vars by hugovk · Pull Request #127877 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127873: Only check sys.flags.ignore_environment for PYTHON* env vars #127877

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 15 commits into from
Jan 21, 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
Add test class helper to force no terminal colour
  • Loading branch information
hugovk committed Dec 12, 2024
commit b664cd421590a1ede4b8b5032ec4fa6f1f18bc6e
39 changes: 39 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"skip_on_s390x",
"without_optimizer",
"force_not_colorized",
"force_not_colorized_test_class",
"BrokenIter",
"in_systemd_nspawn_sync_suppressed",
]
Expand Down Expand Up @@ -2855,6 +2856,44 @@ def wrapper(*args, **kwargs):
return wrapper



def force_not_colorized_test_class(cls):
"""Force the terminal not to be colorized."""
original_setup = cls.setUp
original_teardown = cls.tearDown

@functools.wraps(cls.setUp)
def setUp_wrapper(self, *args, **kwargs):
import _colorize

self._original_fn = _colorize.can_colorize
self._variables: dict[str, str | None] = {
"PYTHON_COLORS": None,
"FORCE_COLOR": None,
"NO_COLOR": None,
}
for key in self._variables:
self._variables[key] = os.environ.pop(key, None)
os.environ["NO_COLOR"] = "1"
_colorize.can_colorize = lambda: False
return original_setup(self, *args, **kwargs)

@functools.wraps(cls.tearDown)
def tearDown_wrapper(self, *args, **kwargs):
import _colorize

_colorize.can_colorize = self._original_fn
del os.environ["NO_COLOR"]
for key, value in self._variables.items():
if value is not None:
os.environ[key] = value
return original_teardown(self, *args, **kwargs)

cls.setUp = setUp_wrapper
cls.tearDown = tearDown_wrapper
return cls


def initialized_with_pyrepl():
"""Detect whether PyREPL was used during Python initialization."""
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
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
13 changes: 8 additions & 5 deletions Lib/test/test_unittest/test_result.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import io
import sys
import textwrap

from test.support import warnings_helper, captured_stdout

import traceback
import unittest
from unittest.util import strclass
from test.support import force_not_colorized
from test.support import (
captured_stdout,
force_not_colorized,
force_not_colorized_test_class,
warnings_helper,
)
from test.test_unittest.support import BufferedWriter
from unittest.util import strclass


class MockTraceback(object):
Expand Down Expand Up @@ -772,6 +774,7 @@ def testFoo(self):
runner.run(Test('testFoo'))


@force_not_colorized_test_class
class TestOutputBuffering(unittest.TestCase):

def setUp(self):
Expand Down
0