code: def swallow_stdout(stream=None):
      
      saved = sys.stdout
      if stream is None:
          stream = StringIO()
      sys.stdout = stream
      try:
          yield
      finally:
          sys.stdout = saved
docs: Divert stdout into the given stream
  
      >>> string = StringIO()
      >>> with swallow_stdout(string):
      ...     print('hello')
      >>> assert string.getvalue().rstrip() == 'hello'