summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2019-11-28 16:46:23 (GMT)
committerGitHub <noreply@github.com>2019-11-28 16:46:23 (GMT)
commitb74a6f14b94d36fb72b1344663e81776bf450847 (patch)
treec02fcc9ccb6ec32da2b9f7adcf776285c405f16e /Python/errors.c
parent18d8edbbb6626ac9cdf1152a720811beb2230b33 (diff)
downloadcpython-b74a6f14b94d36fb72b1344663e81776bf450847.zip
cpython-b74a6f14b94d36fb72b1344663e81776bf450847.tar.gz
cpython-b74a6f14b94d36fb72b1344663e81776bf450847.tar.bz2
bpo-38920: Add audit hooks for when sys.excepthook and sys.unraisablehook are invoked (GH-17392)
Also fixes some potential segfaults in unraisable hook handling.
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c72
1 files changed, 41 insertions, 31 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 8a94afd..197d977 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1367,44 +1367,54 @@ _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
}
}
+ PyObject *hook_args = make_unraisable_hook_args(
+ tstate, exc_type, exc_value, exc_tb, err_msg, obj);
+ if (hook_args == NULL) {
+ err_msg_str = ("Exception ignored on building "
+ "sys.unraisablehook arguments");
+ goto error;
+ }
+
_Py_IDENTIFIER(unraisablehook);
PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook);
- if (hook != NULL && hook != Py_None) {
- PyObject *hook_args;
-
- hook_args = make_unraisable_hook_args(tstate, exc_type, exc_value,
- exc_tb, err_msg, obj);
- if (hook_args != NULL) {
- PyObject *args[1] = {hook_args};
- PyObject *res = _PyObject_FastCall(hook, args, 1);
- Py_DECREF(hook_args);
- if (res != NULL) {
- Py_DECREF(res);
- goto done;
- }
-
- err_msg_str = "Exception ignored in sys.unraisablehook";
- }
- else {
- err_msg_str = ("Exception ignored on building "
- "sys.unraisablehook arguments");
- }
+ if (hook == NULL) {
+ Py_DECREF(hook_args);
+ goto default_hook;
+ }
- Py_XDECREF(err_msg);
- err_msg = PyUnicode_FromString(err_msg_str);
- if (err_msg == NULL) {
- PyErr_Clear();
- }
+ if (PySys_Audit("sys.unraisablehook", "OO", hook, hook_args) < 0) {
+ Py_DECREF(hook_args);
+ err_msg_str = "Exception ignored in audit hook";
+ obj = NULL;
+ goto error;
+ }
- /* sys.unraisablehook failed: log its error using default hook */
- Py_XDECREF(exc_type);
- Py_XDECREF(exc_value);
- Py_XDECREF(exc_tb);
- _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
+ if (hook == Py_None) {
+ Py_DECREF(hook_args);
+ goto default_hook;
+ }
- obj = hook;
+ PyObject *args[1] = {hook_args};
+ PyObject *res = _PyObject_FastCall(hook, args, 1);
+ Py_DECREF(hook_args);
+ if (res != NULL) {
+ Py_DECREF(res);
+ goto done;
}
+ /* sys.unraisablehook failed: log its error using default hook */
+ obj = hook;
+ err_msg_str = NULL;
+
+error:
+ /* err_msg_str and obj have been updated and we have a new exception */
+ Py_XSETREF(err_msg, PyUnicode_FromString(err_msg_str ?
+ err_msg_str : "Exception ignored in sys.unraisablehook"));
+ Py_XDECREF(exc_type);
+ Py_XDECREF(exc_value);
+ Py_XDECREF(exc_tb);
+ _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
+
default_hook:
/* Call the default unraisable hook (ignore failure) */
(void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb,