From 777486a5be33abc1329f373099063371db3f59a5 Mon Sep 17 00:00:00 2001 From: shortcutme Date: Wed, 27 Nov 2019 03:03:22 +0100 Subject: [PATCH] Try new way to avoid pytest io errors --- src/Test/conftest.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Test/conftest.py b/src/Test/conftest.py index 5d0b6dc0..ebf8cea5 100644 --- a/src/Test/conftest.py +++ b/src/Test/conftest.py @@ -413,8 +413,36 @@ def crypt_bitcoin_lib(request, monkeypatch): CryptBitcoin.loadLib(request.param) return CryptBitcoin -# Workaround for pytest>=0.4.1 bug when logging in atexit handlers (I/O operation on closed file) -@pytest.fixture(scope='session', autouse=True) -def disableLog(): + + +def workaroundPytestLogError(): + # Workaround for pytest bug when logging in atexit/post-fixture handlers (I/O operation on closed file) + + import _pytest.capture + write_original = _pytest.capture.EncodedFile.write + + def write_patched(obj, *args, **kwargs): + try: + write_original(obj, *args, **kwargs) + except ValueError as err: + if str(err) == "I/O operation on closed file": + pass + else: + raise err + + def flush_patched(obj, *args, **kwargs): + try: + obj.buffer.flush(*args, **kwargs) + except ValueError as err: + if str(err).startswith("I/O operation on closed file"): + pass + else: + raise err + + _pytest.capture.EncodedFile.write = write_patched + _pytest.capture.EncodedFile.flush = flush_patched + + +workaroundPytestLogError() yield None # Wait until all test done logging.getLogger('').setLevel(logging.getLevelName(logging.CRITICAL))