Empty proofofwork.LogOutput on windows

based on AttributeError in __init__
This commit is contained in:
Dmitri Bogomolov 2021-08-13 19:59:35 +03:00
parent f4f78a1011
commit ed7f973b1c
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -37,18 +37,27 @@ class LogOutput(object): # pylint: disable=too-few-public-methods
def __init__(self, prefix='PoW'): def __init__(self, prefix='PoW'):
self.prefix = prefix self.prefix = prefix
try:
sys.stdout.flush() sys.stdout.flush()
self._stdout = sys.stdout self._stdout = sys.stdout
self._stdout_fno = os.dup(sys.stdout.fileno()) self._stdout_fno = os.dup(sys.stdout.fileno())
except AttributeError:
# NullWriter instance has no attribute 'fileno' on Windows
self._stdout = None
else:
self._dst, self._filepath = tempfile.mkstemp() self._dst, self._filepath = tempfile.mkstemp()
def __enter__(self): def __enter__(self):
if not self._stdout:
return
stdout = os.dup(1) stdout = os.dup(1)
os.dup2(self._dst, 1) os.dup2(self._dst, 1)
os.close(self._dst) os.close(self._dst)
sys.stdout = os.fdopen(stdout, 'w') sys.stdout = os.fdopen(stdout, 'w')
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
if not self._stdout:
return
sys.stdout.close() sys.stdout.close()
sys.stdout = self._stdout sys.stdout = self._stdout
sys.stdout.flush() sys.stdout.flush()