Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Doc/library/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,39 @@ The :mod:`test.support` module defines the following functions:
:exc:`PermissionError` is raised.


.. function:: catch_threading_exception()

Context manager catching :class:`threading.Thread` exception using
:func:`threading.excepthook`.

Attributes set when an exception is catched:

* ``exc_type``
* ``exc_value``
* ``exc_traceback``
* ``thread``

See :func:`threading.excepthook` documentation.

These attributes are deleted at the context manager exit.

Usage::

with support.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...

# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...

# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)

.. versionadded:: 3.8


.. function:: catch_unraisable_exception()

Context manager catching unraisable exception using
Expand Down
57 changes: 57 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3157,3 +3157,60 @@ def __enter__(self):
def __exit__(self, *exc_info):
sys.unraisablehook = self._old_hook
del self.unraisable


class catch_threading_exception:
"""
Context manager catching threading.Thread exception using
threading.excepthook.

Attributes set when an exception is catched:

* exc_type
* exc_value
* exc_traceback
* thread

See threading.excepthook() documentation for these attributes.

These attributes are deleted at the context manager exit.

Usage:

with support.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...

# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...

# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
"""

def __init__(self):
self.exc_type = None
self.exc_value = None
self.exc_traceback = None
self.thread = None
self._old_hook = None

def _hook(self, args):
self.exc_type = args.exc_type
self.exc_value = args.exc_value
self.exc_traceback = args.exc_traceback
self.thread = args.thread

def __enter__(self):
self._old_hook = threading.excepthook
threading.excepthook = self._hook
return self

def __exit__(self, *exc_info):
threading.excepthook = self._old_hook
del self.exc_type
del self.exc_value
del self.exc_traceback
del self.thread
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`test.support.catch_threading_exception`: context manager catching
:class:`threading.Thread` exception using :func:`threading.excepthook`.