From: Michael Tremer Date: Fri, 16 Sep 2022 13:24:42 +0000 (+0000) Subject: python: Hack to not crash when the log function raises an exception X-Git-Tag: 0.9.28~305 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=904e2db4457ab93c5783771c80413d59b37631e8;p=pakfire.git python: Hack to not crash when the log function raises an exception If the log callback raises a Python exception, we cannot handle this in the C code since there are not return values checked. Instead, we will check now if an exception has occured and print it to the console. Not great, but at least this is useful for debugging. Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index c078572e8..73e8418aa 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -60,6 +60,7 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) static void Pakfire_log_callback(void* data, int priority, const char* file, int line, const char* fn, const char* format, va_list args) { PyObject* callback = (PyObject*)data; + PyObject* exception = NULL; // Do nothing if callback isn't set if (!callback) @@ -102,6 +103,17 @@ static void Pakfire_log_callback(void* data, int priority, const char* file, int result = PyObject_CallObject(callback, tuple); ERROR: + /* + We cannot really catch any Python errors here, since we cannot send + any error codes up the chain. + + So, in order to debug the problem, We will check if an exception has + occurred and if so, print it to the console. + */ + exception = PyErr_Occurred(); + if (exception) + PyErr_Print(); + if (buffer) free(buffer); Py_XDECREF(tuple);