From 443b6035bad070d70d72474fc594500991277e87 Mon Sep 17 00:00:00 2001 From: David Perl Date: Mon, 13 Jul 2026 10:52:52 +0200 Subject: [PATCH] feat: add context manager to suppress print from e.g. macros --- src/aarecommon/config/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/aarecommon/config/util.py diff --git a/src/aarecommon/config/util.py b/src/aarecommon/config/util.py new file mode 100644 index 0000000..8bd4b36 --- /dev/null +++ b/src/aarecommon/config/util.py @@ -0,0 +1,12 @@ +import os +from contextlib import contextmanager, redirect_stderr, redirect_stdout + + +@contextmanager +def suppress_output(): + """Suppress both stdout and stderr by redirecting them to os.devnull.""" + # Note that this shouldn't catch logs as well: https://github.com/python/cpython/issues/123481 + # it is intended for suppressing print output from macros called by the DAQ server + with open(os.devnull, "w") as fnull: + with redirect_stdout(fnull), redirect_stderr(fnull): + yield