summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-05-22 09:28:22 (GMT)
committerGitHub <noreply@github.com>2019-05-22 09:28:22 (GMT)
commitef9d9b63129a2f243591db70e9a2dd53fab95d86 (patch)
tree3ecd9bb04fba6c9d360b8db5d8b1e78cda50d49b /Modules/_testcapimodule.c
parent2725cb01d7cbf5caecb51cc20d97ba324b09ce96 (diff)
downloadcpython-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 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 2af5539..7945f49 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4982,6 +4982,20 @@ negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
#endif
+static PyObject*
+test_write_unraisable_exc(PyObject *self, PyObject *args)
+{
+ PyObject *exc, *obj;
+ if (!PyArg_ParseTuple(args, "OO", &exc, &obj)) {
+ return NULL;
+ }
+
+ PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
+ PyErr_WriteUnraisable(obj);
+ Py_RETURN_NONE;
+}
+
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
@@ -5221,6 +5235,7 @@ static PyMethodDef TestMethods[] = {
#ifdef Py_REF_DEBUG
{"negative_refcount", negative_refcount, METH_NOARGS},
#endif
+ {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS},
{NULL, NULL} /* sentinel */
};