From anonymous, 2 Months ago, written in Python.
Embed
  1. #!/bin/python
  2.  
  3. import sys
  4. import io
  5. import contextlib
  6.  
  7.  
  8. @contextlib.contextmanager
  9. def patch_stdout():
  10.     sio = io.StringIO()
  11.     sys.stdout = sio
  12.     try:
  13.         yield sio
  14.     finally:
  15.         sys.stdout = sys.__stdout__
  16.  
  17.  
  18. with patch_stdout() as sio:
  19.     print("dumb object")
  20.  
  21.  
  22. print('value: {}'.format(sio.getvalue()))
  23.