[Pkg-gnupg-commit] [gpgme] 117/412: python: Improve progress callbacks.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Thu Sep 22 21:26:29 UTC 2016


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gpgme.

commit 0d4e95621e05d50cd454049a424bb9ee098a5db6
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 19 15:53:19 2016 +0200

    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 <justus at gnupg.org>
---
 lang/python/helpers.c            | 18 ++++++++++++++----
 lang/python/pyme/core.py         |  4 ++--
 lang/python/tests/t-callbacks.py | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index c3cf3b3..7ced04a 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);
 }
diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index 8a4c197..9e7faf7 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -185,9 +185,9 @@ class Context(GpgmeWrapper):
         else:
             self.last_progresscb = pygpgme.new_PyObject_p_p()
             if hook == None:
-                hookdata = func
+                hookdata = (self, func)
             else:
-                hookdata = (func, hook)
+                hookdata = (self, func, hook)
         pygpgme.pygpgme_set_progress_cb(self.wrapped, hookdata, self.last_progresscb)
 
     def get_engine_info(self):
diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py
index e89fcb8..bd88d86 100755
--- a/lang/python/tests/t-callbacks.py
+++ b/lang/python/tests/t-callbacks.py
@@ -75,3 +75,41 @@ except Exception as e:
     assert type(e) == TypeError
 else:
     assert False, "Expected an error, got none"
+
+
+
+# Test the progress callback.
+parms = """<GnupgKeyParms format="internal">
+Key-Type: RSA
+Key-Length: 1024
+Name-Real: Joe Tester
+Name-Comment: with stupid passphrase
+Name-Email: joe+pyme at example.org
+Passphrase: Crypt0R0cks
+Expire-Date: 2020-12-31
+</GnupgKeyParms>
+"""
+
+messages = []
+def progress_cb(what, typ, current, total, hook=None):
+    messages.append(
+        "PROGRESS UPDATE: what = {}, type = {}, current = {}, total = {}"
+        .format(what, typ, current, total))
+
+c = core.Context()
+c.set_progress_cb(progress_cb, None)
+c.op_genkey(parms, None, None)
+assert len(messages) > 0
+
+# Test exception handling.
+def progress_cb(what, typ, current, total, hook=None):
+    raise myException
+
+c = core.Context()
+c.set_progress_cb(progress_cb, None)
+try:
+    c.op_genkey(parms, None, None)
+except Exception as e:
+    assert e == myException
+else:
+    assert False, "Expected an error, got none"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gpgme.git



More information about the Pkg-gnupg-commit mailing list