From f3618bc615e3eff1f52fb5849cbf0f0b95515a61 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 13 Jun 2016 19:16:30 +0200 Subject: python: Rework callbacks. Simplify how the lifetime of callback arguments is managed. * lang/python/gpgme.i (gpgme_edit_cb_t): Check arguments. (PyObject_p_p, void_p_p): Drop rather dangerous interface. (pygpgme_unwrap_gpgme_ctx_t): New function. * lang/python/helpers.c (pygpgme_clear_generic_cb): Drop dangerous function. (pyPassphraseCb): Assert contract. (pygpgme_set_passphrase_cb): Use Python's calling convention so that we can raise exceptions. Hand in 'self', get the wrapped object, and simply store the hook data as attribute of the wrapper object. (pyProgressCb, pygpgme_set_progress_cb): Likewise. (pygpgme_set_status_cb): Likewise. (pygpgme_data_new_from_cbs): Likewise. * lang/python/helpers.h (pygpgme_clear_generic_cb): Drop prototype. (pygpgme_set_passphrase_cb): Update prototype. (pygpgme_set_progress_cb): Likewise. (pygpgme_set_status_cb): Likewise. (pygpgme_data_new_from_cbs): Likewise. (pygpgme_unwrap_gpgme_ctx_t): New prottotype. * lang/python/pyme/core.py (Context, Data): Update callsites. Signed-off-by: Justus Winter --- lang/python/gpgme.i | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'lang/python/gpgme.i') diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i index 1e4c9ff6..65cd2352 100644 --- a/lang/python/gpgme.i +++ b/lang/python/gpgme.i @@ -302,11 +302,14 @@ // Include mapper for edit callbacks %typemap(in) (gpgme_edit_cb_t fnc, void *fnc_value) { + if (! PyTuple_Check($input)) + return PyErr_Format(PyExc_TypeError, "edit callback must be a tuple"); + if (PyTuple_Size($input) != 2 && PyTuple_Size($input) != 3) + return PyErr_Format(PyExc_TypeError, + "edit callback must be a tuple of size 2 or 3"); + $1 = (gpgme_edit_cb_t) pyEditCb; - if ($input == Py_None) - $2 = NULL; - else - $2 = $input; + $2 = $input; } /* Include the unmodified for cc, and the cleaned-up local @@ -355,8 +358,6 @@ struct _gpgme_sig_notation %pointer_functions(gpgme_error_t, gpgme_error_t_p); %pointer_functions(gpgme_trust_item_t, gpgme_trust_item_t_p); %pointer_functions(gpgme_engine_info_t, gpgme_engine_info_t_p); -%pointer_functions(PyObject *, PyObject_p_p); -%pointer_functions(void *, void_p_p); // Helper functions. @@ -374,6 +375,18 @@ pygpgme_wrap_gpgme_data_t(gpgme_data_t data) { return SWIG_NewPointerObj(data, SWIGTYPE_p_gpgme_data, 0); } + +gpgme_ctx_t +pygpgme_unwrap_gpgme_ctx_t(PyObject *wrapped) +{ + gpgme_ctx_t result; + if (SWIG_ConvertPtr(wrapped, + (void **) &result, + SWIGTYPE_p_gpgme_context, + SWIG_POINTER_EXCEPTION) == -1) + return NULL; + return result; +} %} %include "helpers.h" -- cgit v1.2.3