MCPcopy Create free account
hub / github.com/ipython/ipython / CapturedIO

Class CapturedIO

IPython/utils/capture.py:62–121  ·  view source on GitHub ↗

Simple object for containing captured stdout/err and rich display StringIO objects Each instance `c` has three attributes: - ``c.stdout`` : standard output as a string - ``c.stderr`` : standard error as a string - ``c.outputs``: a list of rich display outputs Additionally, the

Source from the content-addressed store, hash-verified

60
61
62class CapturedIO(object):
63 """Simple object for containing captured stdout/err and rich display StringIO objects
64
65 Each instance `c` has three attributes:
66
67 - ``c.stdout`` : standard output as a string
68 - ``c.stderr`` : standard error as a string
69 - ``c.outputs``: a list of rich display outputs
70
71 Additionally, there's a ``c.show()`` method which will print all of the
72 above in the same order, and can be invoked simply via ``c()``.
73 """
74
75 def __init__(self, stdout, stderr, outputs=None):
76 self._stdout = stdout
77 self._stderr = stderr
78 if outputs is None:
79 outputs = []
80 self._outputs = outputs
81
82 def __str__(self):
83 return self.stdout
84
85 @property
86 def stdout(self):
87 "Captured standard output"
88 if not self._stdout:
89 return ''
90 return self._stdout.getvalue()
91
92 @property
93 def stderr(self):
94 "Captured standard error"
95 if not self._stderr:
96 return ''
97 return self._stderr.getvalue()
98
99 @property
100 def outputs(self):
101 """A list of the captured rich display outputs, if any.
102
103 If you have a CapturedIO object ``c``, these can be displayed in IPython
104 using::
105
106 from IPython.display import display
107 for o in c.outputs:
108 display(o)
109 """
110 return [ RichOutput(**kargs) for kargs in self._outputs ]
111
112 def show(self):
113 """write my output to sys.stdout/err as appropriate"""
114 sys.stdout.write(self.stdout)
115 sys.stderr.write(self.stderr)
116 sys.stdout.flush()
117 sys.stderr.flush()
118 for kargs in self._outputs:
119 RichOutput(**kargs).display()

Callers 2

__enter__Method · 0.85

Calls

no outgoing calls

Tested by 1