fix suppress_stdout_stderr not working in Jupyter notebooks#2181
Open
Anai-Guo wants to merge 1 commit intoabetlen:mainfrom
Open
fix suppress_stdout_stderr not working in Jupyter notebooks#2181Anai-Guo wants to merge 1 commit intoabetlen:mainfrom
Anai-Guo wants to merge 1 commit intoabetlen:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
suppress_stdout_stderrdoes not suppress output in Jupyter notebook environments (VS Code notebooks, JupyterLab, etc.) when loading models withverbose=False.This happens because ipykernel replaces
sys.stdout/sys.stderrwithOutStreamobjects that hold their own copy of the original file descriptor in_original_stdstream_copy. Theos.dup2redirect used bysuppress_stdout_stderronly affects the standard fd (1/2), but ipykernel'sOutStreamwrites through its own fd copy, bypassing the redirect entirely.Fix
Before redirecting, check if
sys.stdout/sys.stderrhave the_original_stdstream_copyattribute (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_copywon't exist.Related