return (PyObject *)self;
}
-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;
- PyObject* type = NULL;
- PyObject* value = NULL;
- PyObject* traceback = NULL;
-
- // Do nothing if callback isn't set
- if (!callback)
- return;
-
- PyGILState_STATE state = PyGILState_Ensure();
-
- // Translate priority to Python logging priorities
- switch (priority) {
- case LOG_DEBUG:
- priority = 10;
- break;
-
- case LOG_INFO:
- priority = 20;
- break;
-
- case LOG_ERR:
- priority = 40;
- break;
-
- // Drop messages of an unknown priority
- default:
- return;
- }
-
- PyObject* tuple = NULL;
- PyObject* result = NULL;
- char* buffer = NULL;
-
- // Make line
- int r = vasprintf(&buffer, format, args);
- if (r < 0)
- goto ERROR;
-
- // Build a tuple with the priority and the log message
- tuple = Py_BuildValue("(is)", priority, buffer);
- if (!tuple)
- goto ERROR;
-
- // Call the callback
- 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();
-
- // Fetch the exception and destroy it
- PyErr_Fetch(&type, &value, &traceback);
-
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(traceback);
- }
-
- if (buffer)
- free(buffer);
- Py_XDECREF(tuple);
- Py_XDECREF(result);
-
- // Release the GIL
- PyGILState_Release(state);
-}
-
static int Pakfire_confirm_callback(struct pakfire* pakfire, void* data,
const char* message, const char* question) {
PyObject* callback = (PyObject*)data;
FILE* fconf = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOpOO", kwlist,
- &path, &arch, &self->callbacks.log, &offline, &conf, &self->callbacks.confirm))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzpOO", kwlist,
+ &path, &arch, &offline, &conf, &self->callbacks.confirm))
goto ERROR;
- // Check if log callback is callable
- if (self->callbacks.log && !PyCallable_Check(self->callbacks.log)) {
- PyErr_SetString(PyExc_TypeError, "logger must be callable\n");
- goto ERROR;
- }
-
// Check if confirm callback is callable
if (self->callbacks.confirm && !PyCallable_Check(self->callbacks.confirm)) {
PyErr_SetString(PyExc_TypeError, "Confirm callback is not callable");
if (offline)
flags |= PAKFIRE_FLAGS_OFFLINE;
- // Configure callbacks
- if (self->callbacks.log)
- Py_INCREF(self->callbacks.log);
-
Py_BEGIN_ALLOW_THREADS
// Create a new Pakfire instance