summaryrefslogtreecommitdiff
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-05-22 11:28:22 +0200
committerGitHub <noreply@github.com>2019-05-22 11:28:22 +0200
commitef9d9b63129a2f243591db70e9a2dd53fab95d86 (patch)
tree3ecd9bb04fba6c9d360b8db5d8b1e78cda50d49b /Python/sysmodule.c
parent2725cb01d7cbf5caecb51cc20d97ba324b09ce96 (diff)
downloadcpython-git-ef9d9b63129a2f243591db70e9a2dd53fab95d86.tar.gz
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 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 1290164edb..1735b90b33 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -375,6 +375,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
status: object = NULL
@@ -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",