]> git.ipfire.org Git - pakfire.git/commitdiff
python: Hack to not crash when the log function raises an exception
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Sep 2022 13:24:42 +0000 (13:24 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Sep 2022 13:24:42 +0000 (13:24 +0000)
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 <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c

index c078572e880d8513aaf7f8fd495c9a26f155cb62..73e8418aab051c25b5027c279c54ef33aa3b0fd0 100644 (file)
@@ -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);