| 277 | # from https://stackoverflow.com/a/16571630/6416660 |
| 278 | # alternative use redirect_stdout() from contextlib |
| 279 | class Capturing(list): |
| 280 | |
| 281 | def __enter__(self): |
| 282 | self._stdout = sys.stdout |
| 283 | sys.stdout = self._stringio = StringIO() |
| 284 | # Make closing the StringIO a no-op |
| 285 | self._stringio.close = lambda x: 1 |
| 286 | return self |
| 287 | |
| 288 | def __exit__(self, *args): |
| 289 | self.extend(self._stringio.getvalue().splitlines()) |
| 290 | del self._stringio # free up some memory |
| 291 | sys.stdout = self._stdout |
| 292 | |
| 293 | |
| 294 | def run_test(sample, test=None, debug=False): |