Skip to content

Commit 7a56153

Browse files
committed
align comments between threadexception and unraisableexception plugins
1 parent faf769c commit 7a56153

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/_pytest/threadexception.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ def collect_thread_exception(config: Config) -> None:
7474
try:
7575
warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))
7676
except pytest.PytestUnhandledThreadExceptionWarning as e:
77-
# This except happens when the warning is treated as an error
77+
# This except happens when the warning is treated as an error (e.g. `-Werror`).
7878
if meta.exc_value is not None:
79-
# Exceptions have a better way to show the traceback
79+
# Exceptions have a better way to show the traceback, but
80+
# warnings do not, so hide the traceback from the msg and
81+
# set the cause so the traceback shows up in the right place.
8082
e.args = (meta.cause_msg,)
8183
e.__cause__ = meta.exc_value
8284
errors.append(e)
@@ -109,6 +111,9 @@ def thread_exception_hook(
109111
append: Callable[[ThreadExceptionMeta | BaseException], object],
110112
) -> None:
111113
try:
114+
# we need to compute these strings here as they might change after
115+
# the excepthook finishes and before the metadata object is
116+
# collected by a pytest hook
112117
thread_name = "<unknown>" if args.thread is None else args.thread.name
113118
summary = f"Exception in thread {thread_name}"
114119
traceback_message = "\n\n" + "".join(
@@ -132,6 +137,11 @@ def thread_exception_hook(
132137
)
133138
except BaseException as e:
134139
append(e)
140+
# Raising this will cause the exception to be logged twice, once in our
141+
# collect_thread_exception and once by the unraisablehook plugin
142+
# (exceptions raised from this hook are 'unraisable')
143+
# which is fine - this should never happen anyway and if it does
144+
# it should probably be reported as a pytest bug.
135145
raise
136146

137147

src/_pytest/unraisableexception.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def unraisable_hook(
101101
append: Callable[[UnraisableMeta | BaseException], object],
102102
) -> None:
103103
try:
104+
# we need to compute these strings here as they might change after
105+
# the unraisablehook finishes and before the metadata object is
106+
# collected by a pytest hook
104107
err_msg = (
105108
"Exception ignored in" if unraisable.err_msg is None else unraisable.err_msg
106109
)
@@ -118,9 +121,6 @@ def unraisable_hook(
118121

119122
append(
120123
UnraisableMeta(
121-
# we need to compute these strings here as they might change after
122-
# the unraisablehook finishes and before the unraisable object is
123-
# collected by a hook
124124
msg=msg,
125125
cause_msg=cause_msg,
126126
exc_value=unraisable.exc_value,

0 commit comments

Comments
 (0)