From ecc80f44c7188bcc62db0036d2a1be6cef009b96 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 4 Sep 2021 10:44:39 +0200 Subject: [PATCH] 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. --- bindings/python/wrapper_top.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 22d20c9a..1d160da4 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -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(); } -- 2.32.0.rc0