[git] GPGME - branch, master, updated. gpgme-1.6.0-129-g09803c4

by Justus Winter cvs at cvs.gnupg.org
Tue May 24 13:17:54 CEST 2016


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GnuPG Made Easy".

The branch, master has been updated
       via  09803c4a81b9431fd4c8f30abb1c60c4c735f0cb (commit)
       via  283f0bdc3d32624dac93d02ba3df516e69d8d4ba (commit)
       via  5476ca6813fc9d8833d5224f19d4bb7515380ab5 (commit)
      from  a42d814a65fdc29a3be6efa97433997495696a88 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 09803c4a81b9431fd4c8f30abb1c60c4c735f0cb
Author: Justus Winter <justus at gnupg.org>
Date:   Tue May 24 12:29:32 2016 +0200

    python: Improve support for edit callbacks.
    
    * lang/python/helpers.c (pyEditCb): Stash exceptions.
    * lang/python/pyme/core.py (Context.op_edit): Hand in 'self'.
    * lang/python/tests/Makefile.am (py_tests): Add new test.
    * lang/python/tests/t-callbacks.py: Test edit callbacks.
    * lang/python/tests/t-edit.py: New file.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 0ee24a3..d0c1f3b 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -280,15 +280,18 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
   PyObject *func = NULL, *dataarg = NULL, *pyargs = NULL, *retval = NULL;
   PyObject *pyopaque = (PyObject *) opaque;
   gpgme_error_t err_status = 0;
+  PyObject *self = NULL;
 
   pygpgme_exception_init();
 
-  if (PyTuple_Check(pyopaque)) {
-    func = PyTuple_GetItem(pyopaque, 0);
-    dataarg = PyTuple_GetItem(pyopaque, 1);
+  assert (PyTuple_Check(pyopaque));
+  assert (PyTuple_Size(pyopaque) == 2 || PyTuple_Size(pyopaque) == 3);
+  self = PyTuple_GetItem(pyopaque, 0);
+  func = PyTuple_GetItem(pyopaque, 1);
+  if (PyTuple_Size(pyopaque) == 3) {
+    dataarg = PyTuple_GetItem(pyopaque, 2);
     pyargs = PyTuple_New(3);
   } else {
-    func = pyopaque;
     pyargs = PyTuple_New(2);
   }
 
@@ -303,6 +306,7 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
   Py_DECREF(pyargs);
   if (PyErr_Occurred()) {
     err_status = pygpgme_exception2code();
+    pygpgme_stash_callback_exception(self);
   } else {
     if (fd>=0 && retval && PyUnicode_Check(retval)) {
       const char *buffer;
diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index 9e7faf7..1b4e6ae 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -225,8 +225,15 @@ class Context(GpgmeWrapper):
         """Start key editing using supplied callback function"""
         if key == None:
             raise ValueError("op_edit: First argument cannot be None")
-        opaquedata = (func, fnc_value)
-        errorcheck(pygpgme.gpgme_op_edit(self.wrapped, key, opaquedata, out))
+        if fnc_value:
+            opaquedata = (self, func, fnc_value)
+        else:
+            opaquedata = (self, func)
+
+        result = pygpgme.gpgme_op_edit(self.wrapped, key, opaquedata, out)
+        if self._callback_excinfo:
+            pygpgme.pygpgme_raise_callback_exception(self)
+        errorcheck(result)
 
 class Data(GpgmeWrapper):
     """From the GPGME C manual:
diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am
index 32dc432..236354f 100644
--- a/lang/python/tests/Makefile.am
+++ b/lang/python/tests/Makefile.am
@@ -38,7 +38,8 @@ py_tests = t-wrapper.py \
 	t-sign.py \
 	t-signers.py \
 	t-decrypt.py \
-	t-export.py
+	t-export.py \
+	t-edit.py
 
 TESTS = $(top_srcdir)/tests/gpg/initial.test \
 	$(py_tests) \
diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py
index 70f641d..d962dc4 100755
--- a/lang/python/tests/t-callbacks.py
+++ b/lang/python/tests/t-callbacks.py
@@ -113,3 +113,36 @@ except Exception as e:
     assert e == myException
 else:
     assert False, "Expected an error, got none"
+
+
+# Test the edit callback.
+c = core.Context()
+c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
+c.set_passphrase_cb(lambda *args: "abc")
+sink = core.Data()
+alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
+
+cookie = object()
+edit_cb_called = False
+def edit_cb(status, args, hook):
+    global edit_cb_called
+    edit_cb_called = True
+    assert hook == cookie
+    return "quit" if args == "keyedit.prompt" else None
+c.op_edit(alpha, edit_cb, cookie, sink)
+assert edit_cb_called
+
+# Test exceptions.
+c = core.Context()
+c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
+c.set_passphrase_cb(lambda *args: "abc")
+sink = core.Data()
+
+def edit_cb(status, args):
+    raise myException
+try:
+    c.op_edit(alpha, edit_cb, None, sink)
+except Exception as e:
+    assert e == myException
+else:
+    assert False, "Expected an error, got none"
diff --git a/lang/python/tests/t-edit.py b/lang/python/tests/t-edit.py
new file mode 100755
index 0000000..64255c9
--- /dev/null
+++ b/lang/python/tests/t-edit.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2005 Igor Belyi <belyi at users.sourceforge.net>
+# Copyright (C) 2016 g10 Code GmbH
+#
+# This file is part of GPGME.
+#
+# GPGME is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GPGME is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import os
+from pyme import core, constants
+import support
+
+class KeyEditor(object):
+    def __init__(self):
+        self.steps = ["fpr", "expire", "1", "primary", "quit"]
+        self.step = 0
+        self.done = False
+        self.verbose = int(os.environ.get('verbose', 0)) > 1
+
+    def edit_fnc(self, status, args, out):
+        if args == "keyedit.prompt":
+            result = self.steps[self.step]
+            self.step += 1
+        elif args == "keyedit.save.okay":
+            result = "Y"
+            self.done = self.step == len(self.steps)
+        elif args == "keygen.valid":
+            result = "0"
+        else:
+            result = None
+
+        if self.verbose:
+            sys.stderr.write("Code: {}, args: {!r}, Returning: {!r}\n"
+                             .format(status, args, result))
+
+        return result
+
+support.init_gpgme(constants.PROTOCOL_OpenPGP)
+
+c = core.Context()
+c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
+c.set_passphrase_cb(lambda *args: "abc")
+c.set_armor(True)
+sink = core.Data()
+
+editor = KeyEditor()
+c.op_edit(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
+          editor.edit_fnc, sink, sink)
+assert editor.done

commit 283f0bdc3d32624dac93d02ba3df516e69d8d4ba
Author: Justus Winter <justus at gnupg.org>
Date:   Mon May 23 18:23:34 2016 +0200

    python: Fix hook.
    
    * lang/python/helpers.c (pyProgressCb): Fix getting hook data.
    * lang/python/tests/t-callbacks.py: Show that this works.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index e305574..0ee24a3 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -235,7 +235,7 @@ static void pyProgressCb(void *hook, const char *what, int type, int current,
   self = PyTuple_GetItem(pyhook, 0);
   func = PyTuple_GetItem(pyhook, 1);
   if (PyTuple_Size(pyhook) == 3) {
-    dataarg = PyTuple_GetItem(pyhook, 1);
+    dataarg = PyTuple_GetItem(pyhook, 2);
     args = PyTuple_New(5);
   } else {
     args = PyTuple_New(4);
diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py
index 13ce486..70f641d 100755
--- a/lang/python/tests/t-callbacks.py
+++ b/lang/python/tests/t-callbacks.py
@@ -91,12 +91,13 @@ Expire-Date: 2020-12-31
 
 messages = []
 def progress_cb(what, typ, current, total, hook=None):
+    assert hook == messages
     messages.append(
         "PROGRESS UPDATE: what = {}, type = {}, current = {}, total = {}"
         .format(what, typ, current, total))
 
 c = core.Context()
-c.set_progress_cb(progress_cb, None)
+c.set_progress_cb(progress_cb, messages)
 c.op_genkey(parms, None, None)
 assert len(messages) > 0
 

commit 5476ca6813fc9d8833d5224f19d4bb7515380ab5
Author: Justus Winter <justus at gnupg.org>
Date:   Mon May 23 18:09:22 2016 +0200

    python: Move edit callback function.
    
    * lang/python/gpgme.i (pyEditCb): Move...
    * lang/python/helpers.c: ... here.
    * lang/python/helpers.h (pyEditCb): New prototype.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i
index fb882c8..a82efb5 100644
--- a/lang/python/gpgme.i
+++ b/lang/python/gpgme.i
@@ -283,48 +283,3 @@ FILE *fdopen(int fildes, const char *mode);
 #include "helpers.h"
 %}
 %include "helpers.h"
-
-%{
-gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
-		       const char *args, int fd) {
-  PyObject *func = NULL, *dataarg = NULL, *pyargs = NULL, *retval = NULL;
-  PyObject *pyopaque = (PyObject *) opaque;
-  gpgme_error_t err_status = 0;
-
-  pygpgme_exception_init();
-
-  if (PyTuple_Check(pyopaque)) {
-    func = PyTuple_GetItem(pyopaque, 0);
-    dataarg = PyTuple_GetItem(pyopaque, 1);
-    pyargs = PyTuple_New(3);
-  } else {
-    func = pyopaque;
-    pyargs = PyTuple_New(2);
-  }
-
-  PyTuple_SetItem(pyargs, 0, PyLong_FromLong((long) status));
-  PyTuple_SetItem(pyargs, 1, PyUnicode_FromString(args));
-  if (dataarg) {
-    Py_INCREF(dataarg);		/* Because GetItem doesn't give a ref but SetItem taketh away */
-    PyTuple_SetItem(pyargs, 2, dataarg);
-  }
-
-  retval = PyObject_CallObject(func, pyargs);
-  Py_DECREF(pyargs);
-  if (PyErr_Occurred()) {
-    err_status = pygpgme_exception2code();
-  } else {
-    if (fd>=0 && retval && PyUnicode_Check(retval)) {
-      const char *buffer;
-      Py_ssize_t size;
-
-      buffer = PyUnicode_AsUTF8AndSize(retval, &size);
-      write(fd, buffer, size);
-      write(fd, "\n", 1);
-    }
-  }
-
-  Py_XDECREF(retval);
-  return err_status;
-}
-%}
diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 7ced04a..e305574 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -272,3 +272,48 @@ void pygpgme_set_progress_cb(gpgme_ctx_t ctx, PyObject *cb, PyObject **freelater
   *freelater = cb;
   gpgme_set_progress_cb(ctx, (gpgme_progress_cb_t) pyProgressCb, (void *) cb);
 }
+
+

+/* Edit callbacks.  */
+gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
+		       const char *args, int fd) {
+  PyObject *func = NULL, *dataarg = NULL, *pyargs = NULL, *retval = NULL;
+  PyObject *pyopaque = (PyObject *) opaque;
+  gpgme_error_t err_status = 0;
+
+  pygpgme_exception_init();
+
+  if (PyTuple_Check(pyopaque)) {
+    func = PyTuple_GetItem(pyopaque, 0);
+    dataarg = PyTuple_GetItem(pyopaque, 1);
+    pyargs = PyTuple_New(3);
+  } else {
+    func = pyopaque;
+    pyargs = PyTuple_New(2);
+  }
+
+  PyTuple_SetItem(pyargs, 0, PyLong_FromLong((long) status));
+  PyTuple_SetItem(pyargs, 1, PyUnicode_FromString(args));
+  if (dataarg) {
+    Py_INCREF(dataarg);		/* Because GetItem doesn't give a ref but SetItem taketh away */
+    PyTuple_SetItem(pyargs, 2, dataarg);
+  }
+
+  retval = PyObject_CallObject(func, pyargs);
+  Py_DECREF(pyargs);
+  if (PyErr_Occurred()) {
+    err_status = pygpgme_exception2code();
+  } else {
+    if (fd>=0 && retval && PyUnicode_Check(retval)) {
+      const char *buffer;
+      Py_ssize_t size;
+
+      buffer = PyUnicode_AsUTF8AndSize(retval, &size);
+      write(fd, buffer, size);
+      write(fd, "\n", 1);
+    }
+  }
+
+  Py_XDECREF(retval);
+  return err_status;
+}
diff --git a/lang/python/helpers.h b/lang/python/helpers.h
index 81d17ff..4bd8ef8 100644
--- a/lang/python/helpers.h
+++ b/lang/python/helpers.h
@@ -35,3 +35,6 @@ PyObject *pygpgme_raise_callback_exception(PyObject *self);
 void pygpgme_set_passphrase_cb(gpgme_ctx_t ctx, PyObject *cb,
 			       PyObject **freelater);
 void pygpgme_set_progress_cb(gpgme_ctx_t ctx, PyObject *cb, PyObject **freelater);
+
+gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
+		       const char *args, int fd);

-----------------------------------------------------------------------

Summary of changes:
 lang/python/gpgme.i              | 45 -----------------------------
 lang/python/helpers.c            | 51 ++++++++++++++++++++++++++++++++-
 lang/python/helpers.h            |  3 ++
 lang/python/pyme/core.py         | 11 +++++--
 lang/python/tests/Makefile.am    |  3 +-
 lang/python/tests/t-callbacks.py | 36 ++++++++++++++++++++++-
 lang/python/tests/t-edit.py      | 62 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 161 insertions(+), 50 deletions(-)
 create mode 100755 lang/python/tests/t-edit.py


hooks/post-receive
-- 
GnuPG Made Easy
http://git.gnupg.org




More information about the Gnupg-commits mailing list