mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03: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
18
Python/clinic/sysmodule.c.h
generated
18
Python/clinic/sysmodule.c.h
generated
|
|
@ -65,6 +65,22 @@ sys_exc_info(PyObject *module, PyObject *Py_UNUSED(ignored))
|
|||
return sys_exc_info_impl(module);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sys_unraisablehook__doc__,
|
||||
"unraisablehook($module, unraisable, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Handle an unraisable exception.\n"
|
||||
"\n"
|
||||
"The unraisable argument has the following attributes:\n"
|
||||
"\n"
|
||||
"* exc_type: Exception type.\n"
|
||||
"* exc_value: Exception value.\n"
|
||||
"* exc_tb: Exception traceback, can be None.\n"
|
||||
"* obj: Object causing the exception, can be None.");
|
||||
|
||||
#define SYS_UNRAISABLEHOOK_METHODDEF \
|
||||
{"unraisablehook", (PyCFunction)sys_unraisablehook, METH_O, sys_unraisablehook__doc__},
|
||||
|
||||
PyDoc_STRVAR(sys_exit__doc__,
|
||||
"exit($module, status=None, /)\n"
|
||||
"--\n"
|
||||
|
|
@ -1060,4 +1076,4 @@ sys_getandroidapilevel(PyObject *module, PyObject *Py_UNUSED(ignored))
|
|||
#ifndef SYS_GETANDROIDAPILEVEL_METHODDEF
|
||||
#define SYS_GETANDROIDAPILEVEL_METHODDEF
|
||||
#endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */
|
||||
/*[clinic end generated code: output=3ba4c194d00f1866 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=603e4d5a453dc769 input=a9049054013a1b77]*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue