mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
14 lines
370 B
Python
14 lines
370 B
Python
|
import sys
|
||
|
from contextlib import contextmanager
|
||
|
from StringIO import StringIO
|
||
|
|
||
|
|
||
|
@contextmanager
|
||
|
def captured_output():
|
||
|
new_out, new_err = StringIO(), StringIO()
|
||
|
old_out, old_err = sys.stdout, sys.stderr
|
||
|
try:
|
||
|
sys.stdout, sys.stderr = new_out, new_err
|
||
|
yield sys.stdout, sys.stderr
|
||
|
finally:
|
||
|
sys.stdout, sys.stderr = old_out, old_err
|