diff options
author | Justus Winter <[email protected]> | 2016-05-19 13:53:19 +0000 |
---|---|---|
committer | Justus Winter <[email protected]> | 2016-05-19 14:08:33 +0000 |
commit | 0d4e95621e05d50cd454049a424bb9ee098a5db6 (patch) | |
tree | 83b6922cbfde4ca8080b41fe166e8bcd84925f95 /lang/python/helpers.c | |
parent | python: Robust exception handling in callbacks. (diff) | |
download | gpgme-0d4e95621e05d50cd454049a424bb9ee098a5db6.tar.gz gpgme-0d4e95621e05d50cd454049a424bb9ee098a5db6.zip |
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 <[email protected]>
Diffstat (limited to 'lang/python/helpers.c')
-rw-r--r-- | lang/python/helpers.c | 18 |
1 files changed, 14 insertions, 4 deletions
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); } |