mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
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
This commit is contained in:
parent
2725cb01d7
commit
ef9d9b6312
12 changed files with 497 additions and 123 deletions
|
@ -374,6 +374,30 @@ sys_exc_info_impl(PyObject *module)
|
|||
}
|
||||
|
||||
|
||||
/*[clinic input]
|
||||
sys.unraisablehook
|
||||
|
||||
unraisable: object
|
||||
/
|
||||
|
||||
Handle an unraisable exception.
|
||||
|
||||
The unraisable argument has the following attributes:
|
||||
|
||||
* exc_type: Exception type.
|
||||
* exc_value: Exception value.
|
||||
* exc_tb: Exception traceback, can be None.
|
||||
* obj: Object causing the exception, can be None.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
sys_unraisablehook(PyObject *module, PyObject *unraisable)
|
||||
/*[clinic end generated code: output=bb92838b32abaa14 input=fdbdb47fdd0bee06]*/
|
||||
{
|
||||
return _PyErr_WriteUnraisableDefaultHook(unraisable);
|
||||
}
|
||||
|
||||
|
||||
/*[clinic input]
|
||||
sys.exit
|
||||
|
||||
|
@ -1672,6 +1696,7 @@ static PyMethodDef sys_methods[] = {
|
|||
METH_VARARGS | METH_KEYWORDS, set_asyncgen_hooks_doc},
|
||||
SYS_GET_ASYNCGEN_HOOKS_METHODDEF
|
||||
SYS_GETANDROIDAPILEVEL_METHODDEF
|
||||
SYS_UNRAISABLEHOOK_METHODDEF
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
@ -2369,6 +2394,9 @@ _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
|
|||
SET_SYS_FROM_STRING_BORROW(
|
||||
"__breakpointhook__",
|
||||
PyDict_GetItemString(sysdict, "breakpointhook"));
|
||||
SET_SYS_FROM_STRING_BORROW("__unraisablehook__",
|
||||
PyDict_GetItemString(sysdict, "unraisablehook"));
|
||||
|
||||
SET_SYS_FROM_STRING("version",
|
||||
PyUnicode_FromString(Py_GetVersion()));
|
||||
SET_SYS_FROM_STRING("hexversion",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue