diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-05-22 09:28:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-22 09:28:22 (GMT) |
commit | ef9d9b63129a2f243591db70e9a2dd53fab95d86 (patch) | |
tree | 3ecd9bb04fba6c9d360b8db5d8b1e78cda50d49b /Doc | |
parent | 2725cb01d7cbf5caecb51cc20d97ba324b09ce96 (diff) | |
download | cpython-ef9d9b63129a2f243591db70e9a2dd53fab95d86.zip cpython-ef9d9b63129a2f243591db70e9a2dd53fab95d86.tar.gz cpython-ef9d9b63129a2f243591db70e9a2dd53fab95d86.tar.bz2 |
bpo-36829: Add sys.unraisablehook() (GH-13187)
Add new sys.unraisablehook() function which can be overridden to
control how "unraisable exceptions" are handled. It is called when an
exception has occurred but there is no way for Python to handle it.
For example, when a destructor raises an exception or during garbage
collection (gc.collect()).
Changes:
* Add an internal UnraisableHookArgs type used to pass arguments to
sys.unraisablehook.
* Add _PyErr_WriteUnraisableDefaultHook().
* The default hook now ignores exception on writing the traceback.
* test_sys now uses unittest.main() to automatically discover tests:
remove test_main().
* Add _PyErr_Init().
* Fix PyErr_WriteUnraisable(): hold a strong reference to sys.stderr
while using it
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/c-api/exceptions.rst | 5 | ||||
-rw-r--r-- | Doc/library/sys.rst | 33 | ||||
-rw-r--r-- | Doc/whatsnew/3.8.rst | 10 |
3 files changed, 44 insertions, 4 deletions
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index 00ef005..18ff697 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -72,6 +72,9 @@ Printing and clearing .. c:function:: void PyErr_WriteUnraisable(PyObject *obj) + Call :func:`sys.unraisablehook` using the current exception and *obj* + argument. + This utility function prints a warning message to ``sys.stderr`` when an exception has been set but it is impossible for the interpreter to actually raise the exception. It is used, for example, when an exception occurs in an @@ -81,6 +84,8 @@ Printing and clearing in which the unraisable exception occurred. If possible, the repr of *obj* will be printed in the warning message. + An exception must be set when calling this function. + Raising exceptions ================== diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 7d27c89..3b754bd 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -248,16 +248,19 @@ always available. before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to ``sys.excepthook``. + See also :func:`unraisablehook` which handles unraisable exceptions. + .. data:: __breakpointhook__ __displayhook__ __excepthook__ + __unraisablehook__ These objects contain the original values of ``breakpointhook``, - ``displayhook``, and ``excepthook`` at the start of the program. They are - saved so that ``breakpointhook``, ``displayhook`` and ``excepthook`` can be - restored in case they happen to get replaced with broken or alternative - objects. + ``displayhook``, ``excepthook``, and ``unraisablehook`` at the start of the + program. They are saved so that ``breakpointhook``, ``displayhook`` and + ``excepthook``, ``unraisablehook`` can be restored in case they happen to + get replaced with broken or alternative objects. .. versionadded:: 3.7 __breakpointhook__ @@ -1487,6 +1490,28 @@ always available. is suppressed and only the exception type and value are printed. +.. function:: unraisablehook(unraisable, /) + + Handle an unraisable exception. + + Called when an exception has occurred but there is no way for Python to + handle it. For example, when a destructor raises an exception or during + garbage collection (:func:`gc.collect`). + + The *unraisable* argument has the following attributes: + + * *exc_type*: Exception type. + * *exc_value*: Exception value, can be ``None``. + * *exc_traceback*: Exception traceback, can be ``None``. + * *object*: Object causing the exception, can be ``None``. + + :func:`sys.unraisablehook` can be overridden to control how unraisable + exceptions are handled. + + See also :func:`excepthook` which handles uncaught exceptions. + + .. versionadded:: 3.8 + .. data:: version A string containing the version number of the Python interpreter plus additional diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 5f8208d..63b8200 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -481,6 +481,16 @@ and manipulating normal distributions of a random variable. [7.672102882379219, 12.000027119750287, 4.647488369766392] +sys +--- + +Add new :func:`sys.unraisablehook` function which can be overridden to control +how "unraisable exceptions" are handled. It is called when an exception has +occurred but there is no way for Python to handle it. For example, when a +destructor raises an exception or during garbage collection +(:func:`gc.collect`). + + tarfile ------- |