From 16ce566d887ffa326f3f6035c8d83dd4d672a9cf Mon Sep 17 00:00:00 2001 From: Taylor Wilsdon Date: Tue, 17 Mar 2026 10:21:58 -0400 Subject: [PATCH] refac --- main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index aa2910a..02b66e1 100644 --- a/main.py +++ b/main.py @@ -125,9 +125,22 @@ def narrow_permissions_to_services( } -def _restore_stdout(): +def _restore_stdout() -> None: """Restore the real stdout and replay any captured output to stderr.""" - captured = sys.stdout.getvalue() + captured_stdout = sys.stdout + + # Idempotent: if already restored, nothing to do. + if captured_stdout is _original_stdout: + return + + required_stringio_methods = ("getvalue", "write", "flush") + if not all( + callable(getattr(captured_stdout, method_name, None)) + for method_name in required_stringio_methods + ): + return + + captured = captured_stdout.getvalue() sys.stdout = _original_stdout if captured: print(captured, end="", file=sys.stderr)