From 0d4e95621e05d50cd454049a424bb9ee098a5db6 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 19 May 2016 15:53:19 +0200 Subject: python: Improve progress callbacks. * lang/python/helpers.c (pyProgressCb): Stash python errors, convert 'what' to Unicode object. * lang/python/pyme/core.py (Context.set_progress_cb): Hand in 'self'. * lang/python/tests/t-callbacks.py: Test progress callbacks. Signed-off-by: Justus Winter --- lang/python/helpers.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'lang/python/helpers.c') diff --git a/lang/python/helpers.c b/lang/python/helpers.c index c3cf3b38..7ced04a4 100644 --- a/lang/python/helpers.c +++ b/lang/python/helpers.c @@ -229,17 +229,25 @@ static void pyProgressCb(void *hook, const char *what, int type, int current, int total) { PyObject *func = NULL, *dataarg = NULL, *args = NULL, *retval = NULL; PyObject *pyhook = (PyObject *) hook; + PyObject *self = NULL; - if (PyTuple_Check(pyhook)) { - func = PyTuple_GetItem(pyhook, 0); + assert (PyTuple_Check(pyhook)); + self = PyTuple_GetItem(pyhook, 0); + func = PyTuple_GetItem(pyhook, 1); + if (PyTuple_Size(pyhook) == 3) { dataarg = PyTuple_GetItem(pyhook, 1); args = PyTuple_New(5); } else { - func = pyhook; args = PyTuple_New(4); } - PyTuple_SetItem(args, 0, PyBytes_FromString(what)); + PyTuple_SetItem(args, 0, PyUnicode_DecodeUTF8(what, strlen (what), + "strict")); + if (PyErr_Occurred()) { + pygpgme_stash_callback_exception(self); + Py_DECREF(args); + return; + } PyTuple_SetItem(args, 1, PyLong_FromLong((long) type)); PyTuple_SetItem(args, 2, PyLong_FromLong((long) current)); PyTuple_SetItem(args, 3, PyLong_FromLong((long) total)); @@ -249,6 +257,8 @@ static void pyProgressCb(void *hook, const char *what, int type, int current, } retval = PyObject_CallObject(func, args); + if (PyErr_Occurred()) + pygpgme_stash_callback_exception(self); Py_DECREF(args); Py_XDECREF(retval); } -- cgit v1.2.3