Skip to content

Commit 0554e91

Browse files
committed
Refactor
1 parent 6daa3c3 commit 0554e91

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

Lib/test/support/__init__.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,23 +2833,27 @@ def is_slot_wrapper(name, value):
28332833
yield name, True
28342834

28352835

2836+
@contextlib.contextmanager
2837+
def no_color():
2838+
import _colorize
2839+
from .os_helper import EnvironmentVarGuard
2840+
2841+
with (
2842+
swap_attr(_colorize, "can_colorize", lambda: False),
2843+
EnvironmentVarGuard() as env,
2844+
):
2845+
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
2846+
env.unset(var)
2847+
env.set("NO_COLOR", "1")
2848+
yield
2849+
2850+
28362851
def force_not_colorized(func):
28372852
"""Force the terminal not to be colorized."""
28382853
@functools.wraps(func)
28392854
def wrapper(*args, **kwargs):
2840-
import _colorize
2841-
from .os_helper import EnvironmentVarGuard
2842-
2843-
with (
2844-
swap_attr(_colorize, "can_colorize", lambda: False),
2845-
EnvironmentVarGuard() as env,
2846-
):
2847-
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
2848-
env.unset(var)
2849-
env.set("NO_COLOR", "1")
2850-
2855+
with no_color():
28512856
return func(*args, **kwargs)
2852-
28532857
return wrapper
28542858

28552859

@@ -2860,19 +2864,8 @@ def force_not_colorized_test_class(cls):
28602864
@classmethod
28612865
@functools.wraps(cls.setUpClass)
28622866
def new_setUpClass(cls):
2863-
import _colorize
2864-
from .os_helper import EnvironmentVarGuard
2865-
2866-
cls.enterClassContext(
2867-
swap_attr(_colorize, "can_colorize", lambda: False)
2868-
)
2869-
env = cls.enterClassContext(EnvironmentVarGuard())
2870-
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
2871-
env.unset(var)
2872-
env.set("NO_COLOR", "1")
2873-
2874-
if original_setUpClass:
2875-
original_setUpClass()
2867+
cls.enterClassContext(no_color())
2868+
original_setUpClass()
28762869

28772870
cls.setUpClass = new_setUpClass
28782871
return cls

0 commit comments

Comments
 (0)