Skip to content

fix suppress_stdout_stderr not working in Jupyter notebooks#2181

Open
Anai-Guo wants to merge 1 commit intoabetlen:mainfrom
Anai-Guo:fix-suppress-stdout-stderr-notebooks
Open

fix suppress_stdout_stderr not working in Jupyter notebooks#2181
Anai-Guo wants to merge 1 commit intoabetlen:mainfrom
Anai-Guo:fix-suppress-stdout-stderr-notebooks

Conversation

@Anai-Guo
Copy link
Copy Markdown

Problem

suppress_stdout_stderr does not suppress output in Jupyter notebook environments (VS Code notebooks, JupyterLab, etc.) when loading models with verbose=False.

This happens because ipykernel replaces sys.stdout/sys.stderr with OutStream objects that hold their own copy of the original file descriptor in _original_stdstream_copy. The os.dup2 redirect used by suppress_stdout_stderr only affects the standard fd (1/2), but ipykernel's OutStream writes through its own fd copy, bypassing the redirect entirely.

Fix

Before redirecting, check if sys.stdout/sys.stderr have the _original_stdstream_copy attribute (set by ipykernel). If so, temporarily point it at the standard fd so it gets redirected along with everything else. On exit, restore the original value.

This is a no-op in non-notebook environments since _original_stdstream_copy won't exist.

Related

ipykernel replaces sys.stdout/stderr with OutStream objects that store
their own copy of the original file descriptor in _original_stdstream_copy.
This bypasses the dup2 redirect used by suppress_stdout_stderr, so output
still appears even with verbose=False.

Fix by temporarily pointing _original_stdstream_copy at the real fd before
redirecting, and restoring it on exit.

Fixes abetlen#872
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

suppress_stdout_stderr failing in notebooks

1 participant