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