From ae3695f691c6325f1a504ee3df7f22d75c7a0c96 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 29 Apr 2025 07:21:59 -0600 Subject: patman: Move capture_sys_output() into terminal and rename This function is sometimes useful outside tests. Also it can affect how terminal output is done, e.g. whether ANSI characters should be emitted or not. Move it out of the test_util package and into terminal. Signed-off-by: Simon Glass --- tools/u_boot_pylib/terminal.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tools/u_boot_pylib/terminal.py') diff --git a/tools/u_boot_pylib/terminal.py b/tools/u_boot_pylib/terminal.py index 2cd5a54ab52..84531831760 100644 --- a/tools/u_boot_pylib/terminal.py +++ b/tools/u_boot_pylib/terminal.py @@ -7,6 +7,8 @@ This module handles terminal interaction including ANSI color codes. """ +from contextlib import contextmanager +from io import StringIO import os import re import shutil @@ -271,3 +273,17 @@ class Color(object): base = self.BRIGHT_START if bright else self.NORMAL_START start = base % (color + 30) return start + text + self.RESET + + +# Use this to suppress stdout/stderr output: +# with terminal.capture() as (stdout, stderr) +# ...do something... +@contextmanager +def capture(): + capture_out, capture_err = StringIO(), StringIO() + old_out, old_err = sys.stdout, sys.stderr + try: + sys.stdout, sys.stderr = capture_out, capture_err + yield capture_out, capture_err + finally: + sys.stdout, sys.stderr = old_out, old_err -- cgit v1.2.3