示例#1
0
    class capture_stdout(list):
        """
        Replace sys.stdout with a temporary StringIO
        """
        def __enter__(self):
            self.sys_stdout = sys.stdout
            self.stringio = StringIO()
            sys.stdout = self.stringio
            return self

        def __exit__(self, *args):
            self.append(str(self.stringio.getvalue()))
            del self.stringio
            sys.stdout = self.sys_stdout
示例#2
0
 def __enter__(self):
     self.sys_stdout = sys.stdout
     self.stringio = StringIO()
     sys.stdout = self.stringio
     return self