Skip to content

Commit ab76e1b

Browse files
committed
Refactor
1 parent 4ad090d commit ab76e1b

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__ = [
@@ -2833,61 +2834,59 @@ def is_slot_wrapper(name, value):
28332834
yield name, True
28342835

28352836

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

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

28662881
@functools.wraps(cls.setUp)
28672882
def setUp_wrapper(self, *args, **kwargs):
2868-
import _colorize
2883+
self._original_fn, self._variables = _disable_terminal_color()
28692884

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

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

28932892
cls.setUp = setUp_wrapper

0 commit comments

Comments
 (0)