|
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__ = [
|
@@ -2832,61 +2833,59 @@ def is_slot_wrapper(name, value):
|
2832 | 2833 | yield name, True
|
2833 | 2834 |
|
2834 | 2835 |
|
| 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 | + |
2835 | 2864 | def force_not_colorized(func):
|
2836 | 2865 | """Force the terminal not to be colorized."""
|
2837 | 2866 | @functools.wraps(func)
|
2838 | 2867 | 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 |
| - } |
2844 | 2868 | 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() |
2849 | 2870 | return func(*args, **kwargs)
|
2850 | 2871 | 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) |
2856 | 2873 | return wrapper
|
2857 | 2874 |
|
2858 |
| - |
2859 |
| - |
2860 | 2875 | def force_not_colorized_test_class(cls):
|
2861 | 2876 | """Force the terminal not to be colorized."""
|
2862 | 2877 | original_setup = cls.setUp
|
2863 | 2878 | original_teardown = cls.tearDown
|
2864 | 2879 |
|
2865 | 2880 | @functools.wraps(cls.setUp)
|
2866 | 2881 | def setUp_wrapper(self, *args, **kwargs):
|
2867 |
| - import _colorize |
| 2882 | + self._original_fn, self._variables = _disable_terminal_color() |
2868 | 2883 |
|
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 |
2879 | 2884 | return original_setup(self, *args, **kwargs)
|
2880 | 2885 |
|
2881 | 2886 | @functools.wraps(cls.tearDown)
|
2882 | 2887 | 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) |
2890 | 2889 | return original_teardown(self, *args, **kwargs)
|
2891 | 2890 |
|
2892 | 2891 | cls.setUp = setUp_wrapper
|
|
0 commit comments