Writes to stdout in real-time AND accumulates to a buffer. Handles both str and bytes from pexpect safely.
| 55 | |
| 56 | |
| 57 | class _LiveCapture: |
| 58 | """Writes to stdout in real-time AND accumulates to a buffer. |
| 59 | Handles both str and bytes from pexpect safely.""" |
| 60 | |
| 61 | def __init__(self): |
| 62 | self._buf = io.StringIO() |
| 63 | |
| 64 | def write(self, data): |
| 65 | if isinstance(data, bytes): |
| 66 | data = data.decode("utf-8", errors="replace") |
| 67 | sys.stdout.write(data) |
| 68 | sys.stdout.flush() |
| 69 | self._buf.write(data) |
| 70 | |
| 71 | def flush(self): |
| 72 | sys.stdout.flush() |
| 73 | |
| 74 | def getvalue(self) -> str: |
| 75 | return self._buf.getvalue() |
| 76 | |
| 77 | |
| 78 | def _check_pexpect() -> bool: |