This class works as a replacement for stdio and stderr. It is a buffer and when its contents are requested, it will erase what it has so far so that the next return will not return the same contents again.
| 113 | |
| 114 | |
| 115 | class IOBuf: |
| 116 | """This class works as a replacement for stdio and stderr. |
| 117 | It is a buffer and when its contents are requested, it will erase what |
| 118 | it has so far so that the next return will not return the same contents again. |
| 119 | """ |
| 120 | |
| 121 | def __init__(self): |
| 122 | self.buflist = [] |
| 123 | import os |
| 124 | |
| 125 | self.encoding = os.environ.get("PYTHONIOENCODING", "utf-8") |
| 126 | |
| 127 | def getvalue(self): |
| 128 | b = self.buflist |
| 129 | self.buflist = [] # clear it |
| 130 | return "".join(b) # bytes on py2, str on py3. |
| 131 | |
| 132 | def write(self, s): |
| 133 | if isinstance(s, bytes): |
| 134 | s = s.decode(self.encoding, errors="replace") |
| 135 | self.buflist.append(s) |
| 136 | |
| 137 | def isatty(self): |
| 138 | return False |
| 139 | |
| 140 | def flush(self): |
| 141 | pass |
| 142 | |
| 143 | def empty(self): |
| 144 | return len(self.buflist) == 0 |
| 145 | |
| 146 | |
| 147 | class _RedirectInfo(object): |
no outgoing calls
no test coverage detected