Skip to content

Commit d84f0f9

Browse files
committed
fix suppress_stdout_stderr not working in Jupyter notebooks
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 #872
1 parent 1bcc5bc commit d84f0f9

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

llama_cpp/_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def __enter__(self):
3434
self.old_stdout = self.sys.stdout
3535
self.old_stderr = self.sys.stderr
3636

37+
# In Jupyter notebooks, ipykernel replaces sys.stdout/stderr with
38+
# OutStream objects that hold their own copy of the original fd in
39+
# _original_stdstream_copy. This bypasses our dup2 redirect, so we
40+
# need to point that copy at the real fd temporarily.
41+
self._saved_stdout_copy = getattr(
42+
self.sys.stdout, "_original_stdstream_copy", None
43+
)
44+
self._saved_stderr_copy = getattr(
45+
self.sys.stderr, "_original_stdstream_copy", None
46+
)
47+
if self._saved_stdout_copy is not None:
48+
self.sys.stdout._original_stdstream_copy = self.old_stdout_fileno_undup
49+
if self._saved_stderr_copy is not None:
50+
self.sys.stderr._original_stdstream_copy = self.old_stderr_fileno_undup
51+
3752
self.os.dup2(outnull_file.fileno(), self.old_stdout_fileno_undup)
3853
self.os.dup2(errnull_file.fileno(), self.old_stderr_fileno_undup)
3954

@@ -45,7 +60,6 @@ def __exit__(self, *_):
4560
if self.disable:
4661
return
4762

48-
# Check if sys.stdout and sys.stderr have fileno method
4963
self.sys.stdout = self.old_stdout
5064
self.sys.stderr = self.old_stderr
5165

@@ -55,6 +69,12 @@ def __exit__(self, *_):
5569
self.os.close(self.old_stdout_fileno)
5670
self.os.close(self.old_stderr_fileno)
5771

72+
# Restore ipykernel's OutStream fd copies
73+
if self._saved_stdout_copy is not None:
74+
self.sys.stdout._original_stdstream_copy = self._saved_stdout_copy
75+
if self._saved_stderr_copy is not None:
76+
self.sys.stderr._original_stdstream_copy = self._saved_stderr_copy
77+
5878

5979
class MetaSingleton(type):
6080
"""

0 commit comments

Comments
 (0)