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
14 changes: 11 additions & 3 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1514,13 +1514,21 @@ always available.
* *err_msg*: Error message, can be ``None``.
* *object*: Object causing the exception, can be ``None``.

:func:`sys.unraisablehook` can be overridden to control how unraisable
exceptions are handled.

The default hook formats *err_msg* and *object* as:
``f'{err_msg}: {object!r}'``; use "Exception ignored in" error message
if *err_msg* is ``None``.

:func:`sys.unraisablehook` can be overridden to control how unraisable
exceptions are handled.

Storing *exc_value* using a custom hook can create a reference cycle. It
should be cleared explicitly to break the reference cycle when the
exception is no longer needed.

Storing *object* using a custom hook can resurrect it if it is set to an
object which is being finalized. Avoid storing *object* after the custom
hook completes to avoid resurrecting objects.

See also :func:`excepthook` which handles uncaught exceptions.

.. versionadded:: 3.8
Expand Down
10 changes: 3 additions & 7 deletions Doc/library/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1086,17 +1086,13 @@ The :mod:`test.support` module defines the following functions:
Context manager catching unraisable exception using
:func:`sys.unraisablehook`.

If the *object* attribute of the unraisable hook is set and the object is
being finalized, the object is resurrected because the context manager
stores a strong reference to it (``cm.unraisable.object``).

Storing the exception value (``cm.unraisable.exc_value``) creates a
reference cycle. The reference cycle is broken explicitly when the context
manager exits.

Exiting the context manager clears the stored unraisable exception. It can
trigger a new unraisable exception (ex: the resurrected object is finalized
again and raises the same exception): it is silently ignored in this case.
Storing the object (``cm.unraisable.object``) can resurrect it if it is set
to an object which is being finalized. Exiting the context manager clears
the stored object.

Usage::

Expand Down
8 changes: 8 additions & 0 deletions Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ This module defines the following functions:
:func:`threading.excepthook` can be overridden to control how uncaught
exceptions raised by :func:`Thread.run` are handled.

Storing *exc_value* using a custom hook can create a reference cycle. It
should be cleared explicitly to break the reference cycle when the
exception is no longer needed.

Storing *object* using a custom hook can resurrect it if it is set to an
object which is being finalized. Avoid storing *object* after the custom
hook completes to avoid resurrecting objects.

.. seealso::
:func:`sys.excepthook` handles uncaught exceptions.

Expand Down
15 changes: 3 additions & 12 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3040,17 +3040,13 @@ class catch_unraisable_exception:
"""
Context manager catching unraisable exception using sys.unraisablehook.

If the *object* attribute of the unraisable hook is set and the object is
being finalized, the object is resurrected because the context manager
stores a strong reference to it (cm.unraisable.object).

Storing the exception value (cm.unraisable.exc_value) creates a reference
cycle. The reference cycle is broken explicitly when the context manager
exits.

Exiting the context manager clears the stored unraisable exception. It can
trigger a new unraisable exception (ex: the resurrected object is finalized
again and raises the same exception): it is silently ignored in this case.
Storing the object (cm.unraisable.object) can resurrect it if it is set to
an object which is being finalized. Exiting the context manager clears the
stored object.

Usage:

Expand Down Expand Up @@ -3080,10 +3076,5 @@ def __enter__(self):
return self

def __exit__(self, *exc_info):
# Clear the unraisable exception to explicitly break a reference cycle.
# It can call _hook() again: ignore the new unraisable exception in
# this case.
self.unraisable = None

sys.unraisablehook = self._old_hook
del self.unraisable
6 changes: 5 additions & 1 deletion Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2072,8 +2072,12 @@ def writer_close():
writer.close = lambda: None
writer = None

# Ignore BufferedWriter (of the BufferedRWPair) unraisable exception
with support.catch_unraisable_exception():
pair = None
# Ignore BufferedRWPair unraisable exception
with support.catch_unraisable_exception():
pair = None
support.gc_collect()
support.gc_collect()

def test_reader_writer_close_error_on_close(self):
Expand Down