code: def capture_output(self, with_hook=True):
          
          self.hooked = ''
  
          def display_hook(obj):
              # That's some dirty hack
              self.hooked += self.safe_better_repr(obj)
              self.last_obj = obj
  
          stdout, stderr = sys.stdout, sys.stderr
          if with_hook:
              d_hook = sys.displayhook
              sys.displayhook = display_hook
  
          sys.stdout, sys.stderr = StringIO(), StringIO()
          out, err = [], []
          try:
              yield out, err
          finally:
              out.extend(sys.stdout.getvalue().splitlines())
              err.extend(sys.stderr.getvalue().splitlines())
              if with_hook:
                  sys.displayhook = d_hook
  
              sys.stdout, sys.stderr = stdout, stderr
docs: Steal stream output, return them in string, restore them