Skip to content

Commit 91b4f7a

Browse files
authored
bpo-37526: Add support.catch_threading_exception() (GH-14664)
Context manager catching threading.Thread exception using threading.excepthook.
1 parent cf9c41c commit 91b4f7a

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

Doc/library/test.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,39 @@ The :mod:`test.support` module defines the following functions:
10811081
:exc:`PermissionError` is raised.
10821082

10831083

1084+
.. function:: catch_threading_exception()
1085+
1086+
Context manager catching :class:`threading.Thread` exception using
1087+
:func:`threading.excepthook`.
1088+
1089+
Attributes set when an exception is catched:
1090+
1091+
* ``exc_type``
1092+
* ``exc_value``
1093+
* ``exc_traceback``
1094+
* ``thread``
1095+
1096+
See :func:`threading.excepthook` documentation.
1097+
1098+
These attributes are deleted at the context manager exit.
1099+
1100+
Usage::
1101+
1102+
with support.catch_threading_exception() as cm:
1103+
# code spawning a thread which raises an exception
1104+
...
1105+
1106+
# check the thread exception, use cm attributes:
1107+
# exc_type, exc_value, exc_traceback, thread
1108+
...
1109+
1110+
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
1111+
# exists at this point
1112+
# (to avoid reference cycles)
1113+
1114+
.. versionadded:: 3.8
1115+
1116+
10841117
.. function:: catch_unraisable_exception()
10851118

10861119
Context manager catching unraisable exception using

Lib/test/support/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,3 +3159,60 @@ def __enter__(self):
31593159
def __exit__(self, *exc_info):
31603160
sys.unraisablehook = self._old_hook
31613161
del self.unraisable
3162+
3163+
3164+
class catch_threading_exception:
3165+
"""
3166+
Context manager catching threading.Thread exception using
3167+
threading.excepthook.
3168+
3169+
Attributes set when an exception is catched:
3170+
3171+
* exc_type
3172+
* exc_value
3173+
* exc_traceback
3174+
* thread
3175+
3176+
See threading.excepthook() documentation for these attributes.
3177+
3178+
These attributes are deleted at the context manager exit.
3179+
3180+
Usage:
3181+
3182+
with support.catch_threading_exception() as cm:
3183+
# code spawning a thread which raises an exception
3184+
...
3185+
3186+
# check the thread exception, use cm attributes:
3187+
# exc_type, exc_value, exc_traceback, thread
3188+
...
3189+
3190+
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
3191+
# exists at this point
3192+
# (to avoid reference cycles)
3193+
"""
3194+
3195+
def __init__(self):
3196+
self.exc_type = None
3197+
self.exc_value = None
3198+
self.exc_traceback = None
3199+
self.thread = None
3200+
self._old_hook = None
3201+
3202+
def _hook(self, args):
3203+
self.exc_type = args.exc_type
3204+
self.exc_value = args.exc_value
3205+
self.exc_traceback = args.exc_traceback
3206+
self.thread = args.thread
3207+
3208+
def __enter__(self):
3209+
self._old_hook = threading.excepthook
3210+
threading.excepthook = self._hook
3211+
return self
3212+
3213+
def __exit__(self, *exc_info):
3214+
threading.excepthook = self._old_hook
3215+
del self.exc_type
3216+
del self.exc_value
3217+
del self.exc_traceback
3218+
del self.thread
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :func:`test.support.catch_threading_exception`: context manager catching
2+
:class:`threading.Thread` exception using :func:`threading.excepthook`.

0 commit comments

Comments
 (0)