| 202 | |
| 203 | |
| 204 | class FakeOutput: |
| 205 | def __init__(self, coderunner, on_write, real_fileobj): |
| 206 | """Fakes sys.stdout or sys.stderr |
| 207 | |
| 208 | on_write should always take unicode |
| 209 | |
| 210 | fileno should be the fileno that on_write will |
| 211 | output to (e.g. 1 for standard output). |
| 212 | """ |
| 213 | self.coderunner = coderunner |
| 214 | self.on_write = on_write |
| 215 | self._real_fileobj = real_fileobj |
| 216 | |
| 217 | def write(self, s, *args, **kwargs): |
| 218 | self.on_write(s, *args, **kwargs) |
| 219 | return self.coderunner.request_from_main_context(force_refresh=True) |
| 220 | |
| 221 | # Some applications which use curses require that sys.stdout |
| 222 | # have a method called fileno. One example is pwntools. This |
| 223 | # is not a widespread issue, but is annoying. |
| 224 | def fileno(self): |
| 225 | return self._real_fileobj.fileno() |
| 226 | |
| 227 | def writelines(self, l): |
| 228 | for s in l: |
| 229 | self.write(s) |
| 230 | |
| 231 | def flush(self): |
| 232 | pass |
| 233 | |
| 234 | def isatty(self): |
| 235 | return True |
| 236 | |
| 237 | @property |
| 238 | def encoding(self): |
| 239 | return self._real_fileobj.encoding |
no outgoing calls