diff options
author | Justus Winter <[email protected]> | 2016-06-14 15:33:12 +0000 |
---|---|---|
committer | Justus Winter <[email protected]> | 2016-06-16 12:19:17 +0000 |
commit | a324d0cffe93cab955698c2c065b2f2227e379e4 (patch) | |
tree | 81d04341068c2541f6fb77c31b7dc3303e2899bd /lang/python/helpers.c | |
parent | python: Avoid creating SWIG proxy classes. (diff) | |
download | gpgme-a324d0cffe93cab955698c2c065b2f2227e379e4.tar.gz gpgme-a324d0cffe93cab955698c2c065b2f2227e379e4.zip |
python: Make result objects more robust.
Results returned by the GPGME are fragile, i.e. they are only valid
until the next operation is performed in the context.
We cannot arbitrarily constrain the lifetime of Python objects, we
therefore create deep copies of the results.
* lang/python/gpgme.i (gpgme_tofu_info_t): Turn these into a list.
(gpgme_*_result_t): Create deep copies of these objects.
* lang/python/helpers.c (pygpgme_wrap_fragile_result): New function.
* lang/python/helpers.h (pygpgme_wrap_fragile_result): New prototype.
* lang/python/pyme/results.py: New file.
Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'lang/python/helpers.c')
-rw-r--r-- | lang/python/helpers.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lang/python/helpers.c b/lang/python/helpers.c index 6de2b8de..1b661466 100644 --- a/lang/python/helpers.c +++ b/lang/python/helpers.c @@ -272,6 +272,38 @@ object_to_gpgme_data_t(PyObject *input, int argnum, gpgme_data_t *wrapper, +PyObject * +pygpgme_wrap_fragile_result(PyObject *fragile, const char *classname) +{ + static PyObject *results; + PyObject *class; + PyObject *replacement; + + if (results == NULL) + { + PyObject *from_list = PyList_New(0); + if (from_list == NULL) + return NULL; + + results = PyImport_ImportModuleLevel("results", PyEval_GetGlobals(), + PyEval_GetLocals(), from_list, 1); + Py_DECREF(from_list); + + if (results == NULL) + return NULL; + } + + class = PyMapping_GetItemString(PyModule_GetDict(results), classname); + if (class == NULL) + return NULL; + + replacement = PyObject_CallFunctionObjArgs(class, fragile, NULL); + Py_DECREF(class); + return replacement; +} + + + /* Callback support. */ static gpgme_error_t pyPassphraseCb(void *hook, const char *uid_hint, |