Skip to content

Commit adae1cb

Browse files
committed
Refactor
1 parent b664cd4 commit adae1cb

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

Lib/test/support/__init__.py

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import types
1818
import unittest
1919
import warnings
20+
from collections.abc import Callable
2021

2122

2223
__all__ = [
@@ -2832,61 +2833,59 @@ def is_slot_wrapper(name, value):
28322833
yield name, True
28332834

28342835

2836+
def _disable_terminal_color() -> Callable[[], bool]:
2837+
import _colorize
2838+
2839+
original_fn = _colorize.can_colorize
2840+
variables: dict[str, str | None] = {
2841+
"PYTHON_COLORS": None,
2842+
"FORCE_COLOR": None,
2843+
"NO_COLOR": None,
2844+
}
2845+
for key in variables:
2846+
variables[key] = os.environ.pop(key, None)
2847+
os.environ["NO_COLOR"] = "1"
2848+
_colorize.can_colorize = lambda: False
2849+
return original_fn, variables
2850+
2851+
2852+
def _re_enable_terminal_color(
2853+
original_fn: Callable[[], bool], variables: dict[str, str | None]
2854+
):
2855+
import _colorize
2856+
2857+
_colorize.can_colorize = original_fn
2858+
del os.environ["NO_COLOR"]
2859+
for key, value in variables.items():
2860+
if value is not None:
2861+
os.environ[key] = value
2862+
2863+
28352864
def force_not_colorized(func):
28362865
"""Force the terminal not to be colorized."""
28372866
@functools.wraps(func)
28382867
def wrapper(*args, **kwargs):
2839-
import _colorize
2840-
original_fn = _colorize.can_colorize
2841-
variables: dict[str, str | None] = {
2842-
"PYTHON_COLORS": None, "FORCE_COLOR": None, "NO_COLOR": None
2843-
}
28442868
try:
2845-
for key in variables:
2846-
variables[key] = os.environ.pop(key, None)
2847-
os.environ["NO_COLOR"] = "1"
2848-
_colorize.can_colorize = lambda: False
2869+
original_fn, variables = _disable_terminal_color()
28492870
return func(*args, **kwargs)
28502871
finally:
2851-
_colorize.can_colorize = original_fn
2852-
del os.environ["NO_COLOR"]
2853-
for key, value in variables.items():
2854-
if value is not None:
2855-
os.environ[key] = value
2872+
_re_enable_terminal_color(original_fn, variables)
28562873
return wrapper
28572874

2858-
2859-
28602875
def force_not_colorized_test_class(cls):
28612876
"""Force the terminal not to be colorized."""
28622877
original_setup = cls.setUp
28632878
original_teardown = cls.tearDown
28642879

28652880
@functools.wraps(cls.setUp)
28662881
def setUp_wrapper(self, *args, **kwargs):
2867-
import _colorize
2882+
self._original_fn, self._variables = _disable_terminal_color()
28682883

2869-
self._original_fn = _colorize.can_colorize
2870-
self._variables: dict[str, str | None] = {
2871-
"PYTHON_COLORS": None,
2872-
"FORCE_COLOR": None,
2873-
"NO_COLOR": None,
2874-
}
2875-
for key in self._variables:
2876-
self._variables[key] = os.environ.pop(key, None)
2877-
os.environ["NO_COLOR"] = "1"
2878-
_colorize.can_colorize = lambda: False
28792884
return original_setup(self, *args, **kwargs)
28802885

28812886
@functools.wraps(cls.tearDown)
28822887
def tearDown_wrapper(self, *args, **kwargs):
2883-
import _colorize
2884-
2885-
_colorize.can_colorize = self._original_fn
2886-
del os.environ["NO_COLOR"]
2887-
for key, value in self._variables.items():
2888-
if value is not None:
2889-
os.environ[key] = value
2888+
_re_enable_terminal_color(self._original_fn, self._variables)
28902889
return original_teardown(self, *args, **kwargs)
28912890

28922891
cls.setUp = setUp_wrapper

0 commit comments

Comments
 (0)