| 52 | # from https://stackoverflow.com/a/16571630/6416660 |
| 53 | # alternative use redirect_stdout() from contextlib |
| 54 | class Capturing(list): |
| 55 | |
| 56 | def __enter__(self): |
| 57 | self._stdout = sys.stdout |
| 58 | sys.stdout = self._stringio = StringIO() |
| 59 | # Make closing the StringIO a no-op |
| 60 | self._stringio.close = lambda x: 1 |
| 61 | return self |
| 62 | |
| 63 | def __exit__(self, *args): |
| 64 | self.append(self._stringio.getvalue()) |
| 65 | del self._stringio # free up some memory |
| 66 | sys.stdout = self._stdout |
| 67 | |
| 68 | |
| 69 | def only_int_check(val): |