Created
March 7, 2018 01:11
-
-
Save Alexhuszagh/e49b25aca79d737ad83a4a6fe9bf0e17 to your computer and use it in GitHub Desktop.
Redirect stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
@contextlib.contextmanager | |
def redirect_stderr(): | |
'''Redirect stderr across processes temporarily.''' | |
stderr = None | |
try: | |
stderr = os.dup(sys.stderr.fileno()) | |
with open(os.devnull, 'w') as devnull: | |
os.dup2(devnull.fileno(), sys.stderr.fileno()) | |
yield | |
finally: | |
if stderr is not None: | |
os.dup2(stderr, sys.stderr.fileno()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment