From de69fa496c09386d5e99747670d6887cf52dd09e Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 28 Jul 2016 12:40:54 +0200 Subject: python: Support the Assuan engine. * lang/python/gpgme.i: Add typemaps for the Assuan protocol callbacks. * lang/python/helpers.c (_pyme_assuan_{data,inquire,status}_cb): New functions. * lang/python/private.h (_pyme_assuan_{data,inquire,status}_cb): New prototypes. * lang/python/pyme/core.py (Context.assuan_transact): New method. * lang/python/pyme/util.py (percent_escape): New function. * lang/python/tests/Makefile.am (py_tests): Add new test. * lang/python/tests/t-protocol-assuan.py: New file. Signed-off-by: Justus Winter --- lang/python/helpers.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'lang/python/helpers.c') diff --git a/lang/python/helpers.c b/lang/python/helpers.c index 2b381729..90173e4e 100644 --- a/lang/python/helpers.c +++ b/lang/python/helpers.c @@ -937,3 +937,119 @@ pygpgme_data_new_from_cbs(PyObject *self, Py_INCREF(Py_None); return Py_None; } + + + +/* The assuan callbacks. */ + +gpgme_error_t +_pyme_assuan_data_cb (void *hook, const void *data, size_t datalen) +{ + gpgme_error_t err = 0; + PyObject *pyhook = (PyObject *) hook; + PyObject *self = NULL; + PyObject *func = NULL; + PyObject *py_data = NULL; + PyObject *retval = NULL; + + assert (PyTuple_Check(pyhook)); + assert (PyTuple_Size(pyhook) == 2); + self = PyTuple_GetItem(pyhook, 0); + func = PyTuple_GetItem(pyhook, 1); + assert (PyCallable_Check(func)); + + py_data = PyBytes_FromStringAndSize(data, datalen); + if (py_data == NULL) + return NULL; /* raise */ + + retval = PyObject_CallFunctionObjArgs(func, py_data, NULL); + if (PyErr_Occurred()) + err = pygpgme_exception2code(); + Py_DECREF(py_data); + Py_XDECREF(retval); + + leave: + if (err) + pygpgme_stash_callback_exception(self); + return err; +} + +gpgme_error_t +_pyme_assuan_inquire_cb (void *hook, const char *name, const char *args, + gpgme_data_t *r_data) +{ + gpgme_error_t err = 0; + PyObject *pyhook = (PyObject *) hook; + PyObject *self = NULL; + PyObject *func = NULL; + PyObject *py_name = NULL; + PyObject *py_args = NULL; + PyObject *retval = NULL; + + assert (PyTuple_Check(pyhook)); + assert (PyTuple_Size(pyhook) == 2); + self = PyTuple_GetItem(pyhook, 0); + func = PyTuple_GetItem(pyhook, 1); + assert (PyCallable_Check(func)); + + py_name = PyUnicode_FromString(name); + if (py_name == NULL) + return NULL; /* raise */ + + py_args = PyUnicode_FromString(args); + if (py_args == NULL) + return NULL; /* raise */ + + retval = PyObject_CallFunctionObjArgs(func, py_name, py_args, NULL); + if (PyErr_Occurred()) + err = pygpgme_exception2code(); + Py_DECREF(py_name); + Py_DECREF(py_args); + Py_XDECREF(retval); + + /* FIXME: Returning data is not yet implemented. */ + r_data = NULL; + + leave: + if (err) + pygpgme_stash_callback_exception(self); + return err; +} + +gpgme_error_t +_pyme_assuan_status_cb (void *hook, const char *status, const char *args) +{ + gpgme_error_t err = 0; + PyObject *pyhook = (PyObject *) hook; + PyObject *self = NULL; + PyObject *func = NULL; + PyObject *py_status = NULL; + PyObject *py_args = NULL; + PyObject *retval = NULL; + + assert (PyTuple_Check(pyhook)); + assert (PyTuple_Size(pyhook) == 2); + self = PyTuple_GetItem(pyhook, 0); + func = PyTuple_GetItem(pyhook, 1); + assert (PyCallable_Check(func)); + + py_status = PyUnicode_FromString(status); + if (py_status == NULL) + return NULL; /* raise */ + + py_args = PyUnicode_FromString(args); + if (py_args == NULL) + return NULL; /* raise */ + + retval = PyObject_CallFunctionObjArgs(func, py_status, py_args, NULL); + if (PyErr_Occurred()) + err = pygpgme_exception2code(); + Py_DECREF(py_status); + Py_DECREF(py_args); + Py_XDECREF(retval); + + leave: + if (err) + pygpgme_stash_callback_exception(self); + return err; +} -- cgit v1.2.3