File pytest-6.2-excepthooks.patch of Package python-loguru
From 31cf758ee9d22dbfa125f38153782fe20ac9dce5 Mon Sep 17 00:00:00 2001
From: Delgan <delgan.py@gmail.com>
Date: Sat, 19 Dec 2020 16:29:07 +0100
Subject: [PATCH] Fix failing tests due to new "excepthook" in threads
---
tests/test_add_option_enqueue.py | 44 +++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/tests/test_add_option_enqueue.py b/tests/test_add_option_enqueue.py
index 50e1843..4b7c891 100644
--- a/tests/test_add_option_enqueue.py
+++ b/tests/test_add_option_enqueue.py
@@ -4,6 +4,9 @@
import re
import sys
import pickle
+import contextlib
+import threading
+import traceback
class NotPicklable:
@@ -29,6 +32,27 @@ def write(self, message):
print(message, end="")
+@contextlib.contextmanager
+def default_threading_excepthook():
+ if not hasattr(threading, "excepthook"):
+ yield
+ return
+
+ # Pytest added "PytestUnhandledThreadExceptionWarning", we need to
+ # remove it temporarily for somes tests checking exceptions in threads.
+
+ def excepthook(args):
+ print("Exception in thread:", file=sys.stderr, flush=True)
+ traceback.print_exception(
+ args.exc_type, args.exc_value, args.exc_traceback, file=sys.stderr
+ )
+
+ old_excepthook = threading.excepthook
+ threading.excepthook = excepthook
+ yield
+ threading.excepthook = old_excepthook
+
+
def test_enqueue():
x = []
@@ -139,10 +163,11 @@ def test_not_caught_exception_queue_put(writer, capsys):
def test_not_caught_exception_queue_get(writer, capsys):
logger.add(writer, enqueue=True, catch=False, format="{message}")
- logger.info("It's fine")
- logger.bind(broken=NotUnpicklable()).info("Bye bye...")
- logger.info("It's not fine")
- logger.remove()
+ with default_threading_excepthook():
+ logger.info("It's fine")
+ logger.bind(broken=NotUnpicklable()).info("Bye bye...")
+ logger.info("It's not fine")
+ logger.remove()
out, err = capsys.readouterr()
lines = err.strip().splitlines()
@@ -152,13 +177,14 @@ def test_not_caught_exception_queue_get(writer, capsys):
assert lines[-1].endswith("UnpicklingError: You shall not de-serialize me!")
-def test_not_caught_exception_sink_write(capsys):
+def test_not_caught_exception_sink_write(monkeypatch, capsys):
logger.add(NotWritable(), enqueue=True, catch=False, format="{message}")
- logger.info("It's fine")
- logger.bind(fail=True).info("Bye bye...")
- logger.info("It's not fine")
- logger.remove()
+ with default_threading_excepthook():
+ logger.info("It's fine")
+ logger.bind(fail=True).info("Bye bye...")
+ logger.info("It's not fine")
+ logger.remove()
out, err = capsys.readouterr()
lines = err.strip().splitlines()