Backport upstream patch to fix FTBFS (Closes: #997471)
Nilesh Patra
2 years ago
0 | From 31cf758ee9d22dbfa125f38153782fe20ac9dce5 Mon Sep 17 00:00:00 2001 | |
1 | From: Delgan <delgan.py@gmail.com> | |
2 | Date: Sat, 19 Dec 2020 16:29:07 +0100 | |
3 | Subject: [PATCH] Fix failing tests due to new "excepthook" in threads | |
4 | ||
5 | --- | |
6 | tests/test_add_option_enqueue.py | 44 +++++++++++++++++++++++++------- | |
7 | 1 file changed, 35 insertions(+), 9 deletions(-) | |
8 | ||
9 | diff --git a/tests/test_add_option_enqueue.py b/tests/test_add_option_enqueue.py | |
10 | index 50e1843..4b7c891 100644 | |
11 | --- a/tests/test_add_option_enqueue.py | |
12 | +++ b/tests/test_add_option_enqueue.py | |
13 | @@ -4,6 +4,9 @@ | |
14 | import re | |
15 | import sys | |
16 | import pickle | |
17 | +import contextlib | |
18 | +import threading | |
19 | +import traceback | |
20 | ||
21 | ||
22 | class NotPicklable: | |
23 | @@ -29,6 +32,27 @@ def write(self, message): | |
24 | print(message, end="") | |
25 | ||
26 | ||
27 | +@contextlib.contextmanager | |
28 | +def default_threading_excepthook(): | |
29 | + if not hasattr(threading, "excepthook"): | |
30 | + yield | |
31 | + return | |
32 | + | |
33 | + # Pytest added "PytestUnhandledThreadExceptionWarning", we need to | |
34 | + # remove it temporarily for somes tests checking exceptions in threads. | |
35 | + | |
36 | + def excepthook(args): | |
37 | + print("Exception in thread:", file=sys.stderr, flush=True) | |
38 | + traceback.print_exception( | |
39 | + args.exc_type, args.exc_value, args.exc_traceback, file=sys.stderr | |
40 | + ) | |
41 | + | |
42 | + old_excepthook = threading.excepthook | |
43 | + threading.excepthook = excepthook | |
44 | + yield | |
45 | + threading.excepthook = old_excepthook | |
46 | + | |
47 | + | |
48 | def test_enqueue(): | |
49 | x = [] | |
50 | ||
51 | @@ -139,10 +163,11 @@ def test_not_caught_exception_queue_put(writer, capsys): | |
52 | def test_not_caught_exception_queue_get(writer, capsys): | |
53 | logger.add(writer, enqueue=True, catch=False, format="{message}") | |
54 | ||
55 | - logger.info("It's fine") | |
56 | - logger.bind(broken=NotUnpicklable()).info("Bye bye...") | |
57 | - logger.info("It's not fine") | |
58 | - logger.remove() | |
59 | + with default_threading_excepthook(): | |
60 | + logger.info("It's fine") | |
61 | + logger.bind(broken=NotUnpicklable()).info("Bye bye...") | |
62 | + logger.info("It's not fine") | |
63 | + logger.remove() | |
64 | ||
65 | out, err = capsys.readouterr() | |
66 | lines = err.strip().splitlines() | |
67 | @@ -152,13 +177,14 @@ def test_not_caught_exception_queue_get(writer, capsys): | |
68 | assert lines[-1].endswith("UnpicklingError: You shall not de-serialize me!") | |
69 | ||
70 | ||
71 | -def test_not_caught_exception_sink_write(capsys): | |
72 | +def test_not_caught_exception_sink_write(monkeypatch, capsys): | |
73 | logger.add(NotWritable(), enqueue=True, catch=False, format="{message}") | |
74 | ||
75 | - logger.info("It's fine") | |
76 | - logger.bind(fail=True).info("Bye bye...") | |
77 | - logger.info("It's not fine") | |
78 | - logger.remove() | |
79 | + with default_threading_excepthook(): | |
80 | + logger.info("It's fine") | |
81 | + logger.bind(fail=True).info("Bye bye...") | |
82 | + logger.info("It's not fine") | |
83 | + logger.remove() | |
84 | ||
85 | out, err = capsys.readouterr() | |
86 | lines = err.strip().splitlines() |