summaryrefslogtreecommitdiffstats
path: root/capture.py
blob: 8c82fef56599d3aeff1ee3c200b521365856aa93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys
from io import StringIO


class Capturing(list):  # To capture output written to the console using print()
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self

    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio  # free up some memory
        sys.stdout = self._stdout