Clear Python error indicator after logging (#56572)

Lasso log using the GLib logging API and the Python binding install a
hook to delegate logging to a Python logger named "lasso".

During the logging call the error indicator can be set to signal an
exception. The indicator will still be set when we return from the Lasso
API call, and is not handled by the Python wrapping of the C functions.
If our function returns a non-NULL value, the Python interpreter will
raise because this situation is forbidden.

To prevent it, if we detect that an exception occurred during logging
calls, we print it to stderr, clear the error indicator and return
immediately.
This commit is contained in:
Benjamin Dauvergne 2021-09-04 10:44:39 +02:00
parent 53b0bd3569
commit 23035115a3
1 changed files with 12 additions and 0 deletions

View File

@ -739,6 +739,13 @@ lasso_python_log(const char *domain, GLogLevelFlags log_level, const gchar *mess
G_GNUC_UNUSED gpointer user_data)
{
PyObject *logger_object = get_logger_object(domain), *result;
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
return;
}
char *method = NULL;
if (! logger_object) {
@ -773,4 +780,9 @@ lasso_python_log(const char *domain, GLogLevelFlags log_level, const gchar *mess
} else {
PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "lasso could not call method %s on its logger", method);
}
/* clear any exception emitted during log call */
if (PyErr_Occurred()) {
PyErr_Print();
}
PyErr_Clear();
}