From 51a4c7854d7e3d59609e9c1105f3340edb83378b Mon Sep 17 00:00:00 2001 From: simon987 Date: Sun, 14 Jun 2020 14:05:06 -0400 Subject: [PATCH] Change silent_stdout --- hexlib/misc.py | 27 ++++++++++++++++++--------- setup.py | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/hexlib/misc.py b/hexlib/misc.py index ff79309..8e078b1 100644 --- a/hexlib/misc.py +++ b/hexlib/misc.py @@ -1,10 +1,10 @@ -import time import os import sys +import time +from threading import Lock import atexit import siphash -from threading import Lock last_time_called = dict() @@ -61,10 +61,19 @@ def signed64(i): return -(i & 0x8000000000000000) | (i & 0x7fffffffffffffff) -def silent_stdout(func, *args, **kwargs): - with open(os.devnull, 'w') as null: - stdout = sys.stdout - sys.stdout = null - res = func(*args, **kwargs) - sys.stdout = stdout - return res +class CustomStdOut: + original_stdout = sys.stdout + + def __init__(self, fname): + self.fname = fname + + def __enter__(self): + self.fp = open(self.fname, "w") + sys.stdout = self.fp + + def __exit__(self, exc_type, exc_val, exc_tb): + sys.stdout = CustomStdOut.original_stdout + self.fp.close() + + +silent_stdout = CustomStdOut(os.devnull) diff --git a/setup.py b/setup.py index 45c071b..b0fd5ec 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="hexlib", - version="1.10", + version="1.11", description="Misc utility methods", author="simon987", author_email="me@simon987.net",