file like object which redirects output to the repl window.
| 32 | |
| 33 | |
| 34 | class _TestOutput(object): |
| 35 | """file like object which redirects output to the repl window.""" |
| 36 | |
| 37 | errors = "strict" |
| 38 | |
| 39 | def __init__(self, old_out, is_stdout): |
| 40 | self.is_stdout = is_stdout |
| 41 | self.old_out = old_out |
| 42 | if sys.version >= "3." and hasattr(old_out, "buffer"): |
| 43 | self.buffer = _TestOutputBuffer(old_out.buffer, is_stdout) |
| 44 | |
| 45 | def flush(self): |
| 46 | if self.old_out: |
| 47 | self.old_out.flush() |
| 48 | |
| 49 | def writelines(self, lines): |
| 50 | for line in lines: |
| 51 | self.write(line) |
| 52 | |
| 53 | @property |
| 54 | def encoding(self): |
| 55 | return "utf8" |
| 56 | |
| 57 | def write(self, value): |
| 58 | _channel.send_event("stdout" if self.is_stdout else "stderr", content=value) |
| 59 | if self.old_out: |
| 60 | self.old_out.write(value) |
| 61 | # flush immediately, else things go wonky and out of order |
| 62 | self.flush() |
| 63 | |
| 64 | def isatty(self): |
| 65 | return True |
| 66 | |
| 67 | def next(self): |
| 68 | pass |
| 69 | |
| 70 | @property |
| 71 | def name(self): |
| 72 | if self.is_stdout: |
| 73 | return "<stdout>" |
| 74 | else: |
| 75 | return "<stderr>" |
| 76 | |
| 77 | def __getattr__(self, name): |
| 78 | return getattr(self.old_out, name) |
| 79 | |
| 80 | |
| 81 | class _TestOutputBuffer(object): |