diff options
author | Simon Glass <sjg@chromium.org> | 2025-04-29 07:22:01 -0600 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2025-05-27 10:07:41 +0100 |
commit | b697480f19cf690a51bfb3c788aa381de43eac79 (patch) | |
tree | 5040b443467798e34cbf92061767ac2670c4479a | |
parent | 84f3deaa995502325a6d84bda6338c4e0a4bf4ea (diff) |
u_boot_pylib: Allow control of capturing
Tests often capture output so they can check it. This means that if the
test fails it is not easy to see what the output actually was.
Add a -N flag which writes out the output after it has been captured.
This is not a perfect solution but it is simple and seems to work well
in practice.
Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r-- | tools/u_boot_pylib/terminal.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/tools/u_boot_pylib/terminal.py b/tools/u_boot_pylib/terminal.py index 84531831760..4b9a907a547 100644 --- a/tools/u_boot_pylib/terminal.py +++ b/tools/u_boot_pylib/terminal.py @@ -28,6 +28,13 @@ last_print_len = None # stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python ansi_escape = re.compile(r'\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') +# True if we are capturing console output +CAPTURING = False + +# Set this to False to disable output-capturing globally +USE_CAPTURE = True + + class PrintLine: """A line of text output @@ -280,10 +287,17 @@ class Color(object): # ...do something... @contextmanager def capture(): + global CAPTURING + capture_out, capture_err = StringIO(), StringIO() old_out, old_err = sys.stdout, sys.stderr try: + CAPTURING = True sys.stdout, sys.stderr = capture_out, capture_err yield capture_out, capture_err finally: sys.stdout, sys.stderr = old_out, old_err + CAPTURING = False + if not USE_CAPTURE: + sys.stdout.write(capture_out.getvalue()) + sys.stderr.write(capture_err.getvalue()) |