| 
17 | 17 | import types  | 
18 | 18 | import unittest  | 
19 | 19 | import warnings  | 
 | 20 | +from collections.abc import Callable  | 
20 | 21 | 
 
  | 
21 | 22 | 
 
  | 
22 | 23 | __all__ = [  | 
@@ -2833,61 +2834,59 @@ def is_slot_wrapper(name, value):  | 
2833 | 2834 |             yield name, True  | 
2834 | 2835 | 
 
  | 
2835 | 2836 | 
 
  | 
 | 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 | + | 
2836 | 2865 | def force_not_colorized(func):  | 
2837 | 2866 |     """Force the terminal not to be colorized."""  | 
2838 | 2867 |     @functools.wraps(func)  | 
2839 | 2868 |     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 |  | -        }  | 
2845 | 2869 |         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()  | 
2850 | 2871 |             return func(*args, **kwargs)  | 
2851 | 2872 |         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)  | 
2857 | 2874 |     return wrapper  | 
2858 | 2875 | 
 
  | 
2859 |  | - | 
2860 |  | - | 
2861 | 2876 | def force_not_colorized_test_class(cls):  | 
2862 | 2877 |     """Force the terminal not to be colorized."""  | 
2863 | 2878 |     original_setup = cls.setUp  | 
2864 | 2879 |     original_teardown = cls.tearDown  | 
2865 | 2880 | 
 
  | 
2866 | 2881 |     @functools.wraps(cls.setUp)  | 
2867 | 2882 |     def setUp_wrapper(self, *args, **kwargs):  | 
2868 |  | -        import _colorize  | 
 | 2883 | +        self._original_fn, self._variables = _disable_terminal_color()  | 
2869 | 2884 | 
 
  | 
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  | 
2880 | 2885 |         return original_setup(self, *args, **kwargs)  | 
2881 | 2886 | 
 
  | 
2882 | 2887 |     @functools.wraps(cls.tearDown)  | 
2883 | 2888 |     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)  | 
2891 | 2890 |         return original_teardown(self, *args, **kwargs)  | 
2892 | 2891 | 
 
  | 
2893 | 2892 |     cls.setUp = setUp_wrapper  | 
 | 
0 commit comments