MCPcopy Index your code
hub / github.com/ipython/ipython / capture_output

Class capture_output

IPython/utils/capture.py:131–177  ·  view source on GitHub ↗

context manager for capturing stdout/err

Source from the content-addressed store, hash-verified

129
130
131class capture_output:
132 """context manager for capturing stdout/err"""
133 stdout = True
134 stderr = True
135 display = True
136
137 def __init__(self, stdout: bool=True, stderr: bool=True, display: bool=True):
138 self.stdout = stdout
139 self.stderr = stderr
140 self.display = display
141 self.shell = None
142
143 def __enter__(self) -> CapturedIO:
144 from IPython.core.getipython import get_ipython
145 from IPython.core.displaypub import CapturingDisplayPublisher
146 from IPython.core.displayhook import CapturingDisplayHook
147
148 self.sys_stdout = sys.stdout
149 self.sys_stderr = sys.stderr
150
151 if self.display:
152 self.shell = get_ipython()
153 if self.shell is None:
154 self.save_display_pub = None
155 self.display = False
156
157 stdout = stderr = outputs = None
158 if self.stdout:
159 stdout = sys.stdout = StringIO()
160 if self.stderr:
161 stderr = sys.stderr = StringIO()
162 if self.display:
163 self.save_display_pub = self.shell.display_pub
164 self.shell.display_pub = CapturingDisplayPublisher()
165 outputs = self.shell.display_pub.outputs
166 self.save_display_hook = sys.displayhook
167 sys.displayhook = CapturingDisplayHook(shell=self.shell,
168 outputs=outputs)
169
170 return CapturedIO(stdout, stderr, outputs)
171
172 def __exit__(self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[TracebackType]):
173 sys.stdout = self.sys_stdout
174 sys.stderr = self.sys_stderr
175 if self.display and self.shell:
176 self.shell.display_pub = self.save_display_pub
177 sys.displayhook = self.save_display_hook

Callers 15

captureMethod · 0.90
_exceptiongroup_commonFunction · 0.90
test_alias_args_errorFunction · 0.90
test_progress_iterFunction · 0.85
test_error_methodFunction · 0.85
test_warn_error_for_typeFunction · 0.85
test_error_pretty_methodFunction · 0.85
test_bad_repr_tracebackFunction · 0.85
test_print_method_boundFunction · 0.85
test_print_method_weirdFunction · 0.85

Calls

no outgoing calls

Tested by 15

_exceptiongroup_commonFunction · 0.72
test_alias_args_errorFunction · 0.72
test_progress_iterFunction · 0.68
test_error_methodFunction · 0.68
test_warn_error_for_typeFunction · 0.68
test_error_pretty_methodFunction · 0.68
test_bad_repr_tracebackFunction · 0.68
test_print_method_boundFunction · 0.68
test_print_method_weirdFunction · 0.68
test_format_configFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…