Skip to content

Commit

Permalink
Add colour.utilities.suppress_stdout context manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Oct 21, 2023
1 parent db933c6 commit 5ec951e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions colour/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
usage_warning,
filter_warnings,
suppress_warnings,
suppress_stdout,
numpy_print_options,
ANCILLARY_COLOUR_SCIENCE_PACKAGES,
ANCILLARY_RUNTIME_PACKAGES,
Expand Down Expand Up @@ -188,6 +189,7 @@
"usage_warning",
"filter_warnings",
"suppress_warnings",
"suppress_stdout",
"numpy_print_options",
"ANCILLARY_COLOUR_SCIENCE_PACKAGES",
"ANCILLARY_RUNTIME_PACKAGES",
Expand Down
15 changes: 15 additions & 0 deletions colour/utilities/tests/test_verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
multiline_repr,
multiline_str,
show_warning,
suppress_stdout,
suppress_warnings,
warning,
)
Expand All @@ -25,6 +26,7 @@
__all__ = [
"TestShowWarning",
"TestSuppressWarnings",
"TestSuppressStdout",
"TestDescribeEnvironment",
"TestMultilineStr",
"TestMultilineRepr",
Expand Down Expand Up @@ -68,6 +70,19 @@ def test_suppress_warnings(self):
warning("This is a suppressed unit test warning!")


class TestSuppressStdout(unittest.TestCase):
"""
Define :func:`colour.utilities.verbose.suppress_stdout` definition unit
tests methods.
"""

def test_suppress_stdout(self):
"""Test :func:`colour.utilities.verbose.suppress_stdout` definition."""

with suppress_stdout():
print("This is a suppressed message!") # noqa: T201


class TestDescribeEnvironment(unittest.TestCase):
"""
Define :func:`colour.utilities.verbose.describe_environment` definition
Expand Down
41 changes: 41 additions & 0 deletions colour/utilities/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import functools
import os
import sys
import traceback
Expand Down Expand Up @@ -51,6 +52,7 @@
"usage_warning",
"filter_warnings",
"suppress_warnings",
"suppress_stdout",
"numpy_print_options",
"ANCILLARY_COLOUR_SCIENCE_PACKAGES",
"ANCILLARY_RUNTIME_PACKAGES",
Expand Down Expand Up @@ -449,6 +451,45 @@ def suppress_warnings(
warnings.showwarning = show_warnings


class suppress_stdout:
"""
Define a context manager and decorator temporarily suppressing standard
output.
Examples
--------
>>> with suppress_stdout():
... print("Hello World!")
...
>>> print("Hello World!")
'Hello World!'
"""

def __enter__(self) -> suppress_stdout:
"""Redirect the standard output upon entering the context manager."""

self._stdout = sys.stdout
sys.stdout = open(os.devnull, "w") # noqa: SIM115

return self

def __exit__(self, *args: Any):
"""Restore the standard output upon exiting the context manager."""

sys.stdout.close()
sys.stdout = self._stdout

def __call__(self, function: Callable) -> Callable:
"""Call the wrapped definition."""

@functools.wraps(function)
def wrapper(*args: Any, **kwargs: Any) -> Callable:
with self:
return function(*args, **kwargs)

return wrapper


@contextmanager
def numpy_print_options(*args: Any, **kwargs: Any) -> Generator:
"""
Expand Down
1 change: 1 addition & 0 deletions docs/colour.utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Verbose
warning
filter_warnings
suppress_warnings
suppress_stdout
numpy_print_options
describe_environment
multiline_str
Expand Down

0 comments on commit 5ec951e

Please sign in to comment.