Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Wrap stdout, so that print calls can go to a buffer."""
- import sys
- import io
- _stdout = sys.stdout
- class Wrapper(io.StringIO):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- sys.stdout = self
- def write(self, *args, **kwargs):
- _stdout.write(*args, **kwargs)
- io.StringIO.write(self, *args, **kwargs)
- def __del__(self):
- """Put sys.stdout back in the right place."""
- sys.stdout = _stdout
- w = Wrapper()
- print('You should see this')
- assert w.getvalue() == 'You should see this\n'
- del w
- print("If you see this it totally worked.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement