From ed7f973b1c001e8e86bb7a9928e0137eaa84a7ff Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 13 Aug 2021 19:59:35 +0300 Subject: [PATCH] Empty proofofwork.LogOutput on windows based on AttributeError in __init__ --- src/proofofwork.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/proofofwork.py b/src/proofofwork.py index f7722509..634db7e6 100644 --- a/src/proofofwork.py +++ b/src/proofofwork.py @@ -37,18 +37,27 @@ class LogOutput(object): # pylint: disable=too-few-public-methods def __init__(self, prefix='PoW'): self.prefix = prefix - sys.stdout.flush() - self._stdout = sys.stdout - self._stdout_fno = os.dup(sys.stdout.fileno()) - self._dst, self._filepath = tempfile.mkstemp() + try: + sys.stdout.flush() + self._stdout = sys.stdout + 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() def __enter__(self): + if not self._stdout: + return stdout = os.dup(1) os.dup2(self._dst, 1) os.close(self._dst) sys.stdout = os.fdopen(stdout, 'w') def __exit__(self, exc_type, exc_val, exc_tb): + if not self._stdout: + return sys.stdout.close() sys.stdout = self._stdout sys.stdout.flush()