| 327 | # from https://stackoverflow.com/a/16571630/6416660 |
| 328 | # alternative use redirect_stdout() from contextlib |
| 329 | class Capturing(list): |
| 330 | |
| 331 | def __enter__(self): |
| 332 | self._stdout = sys.stdout |
| 333 | sys.stdout = self._stringio = StringIO() |
| 334 | # Make closing the StringIO a no-op |
| 335 | self._stringio.close = lambda x: 1 |
| 336 | return self |
| 337 | |
| 338 | def __exit__(self, *args): |
| 339 | self.extend(self._stringio.getvalue().splitlines()) |
| 340 | del self._stringio # free up some memory |
| 341 | sys.stdout = self._stdout |
| 342 | |
| 343 | |
| 344 | def run_test(sample, test=None, debug=False): |