[git] GPGME - branch, justus/pyme3, updated. gpgme-1.6.0-39-gce5121a

by Justus Winter cvs at cvs.gnupg.org
Thu May 12 12:01:03 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, justus/pyme3 has been updated
       via  ce5121ad53b0e17fbf9150b354c80da73f7fe190 (commit)
       via  c89d3a71ad20ff02755539a44f254b1e59054c4a (commit)
       via  d60deb8a127fb35c01acc729f33b014840af0e7b (commit)
      from  bbeee5e1a060f2d1e37a08220eb552cf4673a058 (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 ce5121ad53b0e17fbf9150b354c80da73f7fe190
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 11:53:43 2016 +0200

    python: Handle interpreter shutdown.
    
    * lang/python/pyme/core.py: Avoid races at interpreter shutdown.  This
    silences the most annoying occurrences, however this problem also
    affects the SWIG generated code, which might indicate that the real
    problem is somewhere else.  If so, this change can be easily reverted.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index fd4802e..2a37ba3 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -66,21 +66,29 @@ class Context(GpgmeWrapper):
         self.last_progresscb = None
 
     def __del__(self):
+        if not pygpgme:
+            # At interpreter shutdown, pygpgme is set to NONE.
+            return
+
         self._free_passcb()
         self._free_progresscb()
-        if self.own:
+        if self.own and pygpgme.gpgme_release:
             pygpgme.gpgme_release(self.wrapped)
 
     def _free_passcb(self):
         if self.last_passcb != None:
-            pygpgme.pygpgme_clear_generic_cb(self.last_passcb)
-            pygpgme.delete_PyObject_p_p(self.last_passcb)
+            if pygpgme.pygpgme_clear_generic_cb:
+                pygpgme.pygpgme_clear_generic_cb(self.last_passcb)
+            if pygpgme.delete_PyObject_p_p:
+                pygpgme.delete_PyObject_p_p(self.last_passcb)
             self.last_passcb = None
 
     def _free_progresscb(self):
         if self.last_progresscb != None:
-            pygpgme.pygpgme_clear_generic_cb(self.last_progresscb)
-            pygpgme.delete_PyObject_p_p(self.last_progresscb)
+            if pygpgme.pygpgme_clear_generic_cb:
+                pygpgme.pygpgme_clear_generic_cb(self.last_progresscb)
+            if pygpgme.delete_PyObject_p_p:
+                pygpgme.delete_PyObject_p_p(self.last_progresscb)
             self.last_progresscb = None
 
     def op_keylist_all(self, *args, **kwargs):
@@ -292,14 +300,20 @@ class Data(GpgmeWrapper):
             self.new()
 
     def __del__(self):
-        if self.wrapped != None:
+        if not pygpgme:
+            # At interpreter shutdown, pygpgme is set to NONE.
+            return
+
+        if self.wrapped != None and pygpgme.gpgme_data_release:
             pygpgme.gpgme_data_release(self.wrapped)
         self._free_readcb()
 
     def _free_readcb(self):
         if self.last_readcb != None:
-            pygpgme.pygpgme_clear_generic_cb(self.last_readcb)
-            pygpgme.delete_PyObject_p_p(self.last_readcb)
+            if pygpgme.pygpgme_clear_generic_cb:
+                pygpgme.pygpgme_clear_generic_cb(self.last_readcb)
+            if pygpgme.delete_PyObject_p_p:
+                pygpgme.delete_PyObject_p_p(self.last_readcb)
             self.last_readcb = None
 
     def new(self):

commit c89d3a71ad20ff02755539a44f254b1e59054c4a
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 11:51:21 2016 +0200

    python: Make test case more robust.
    
    * lang/python/examples/t-edit.py: Check if key is found.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py
index 5553190..5e5857a 100644
--- a/lang/python/examples/t-edit.py
+++ b/lang/python/examples/t-edit.py
@@ -16,6 +16,7 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+import sys
 import os
 from pyme import core
 from pyme.core import Data, Context
@@ -53,6 +54,9 @@ else:
     out = Data()
     c.op_keylist_start(b"Alpha", 0)
     key = c.op_keylist_next()
+    if not key:
+        sys.exit("Key Alpha not found.  " +
+                 "Did you point GNUPGHOME to GPGME's tests/gpg dir?")
     c.op_edit(key, KeyEditor().edit_fnc, out, out)
     print("[-- Last response --]")
     out.seek(0,0)

commit d60deb8a127fb35c01acc729f33b014840af0e7b
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 11:21:58 2016 +0200

    python: Fix type translation.
    
    * lang/python/gpgme.i: Adjust to Python3's string type being
    'Unicode', not 'bytes'.  Fix type checking.
    * lang/python/core.py (Data.write): Add docstring mentioning the
    expected type of parameter 'buffer'.
    (Data.read): Adjust read loop.  Also, use a saner chunk size, and join
    all chunks at the end instead of adding them.
    * lang/python/examples/simple.py: Adjust example.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py
index 739291e..4ff6d28 100755
--- a/lang/python/examples/simple.py
+++ b/lang/python/examples/simple.py
@@ -25,7 +25,7 @@ core.check_version(None)
 
 # Set up our input and output buffers.
 
-plain = core.Data(b'This is my message.')
+plain = core.Data('This is my message.')
 cipher = core.Data()
 
 # Initialize our context.
@@ -38,7 +38,7 @@ c.set_armor(1)
 sys.stdout.write("Enter name of your recipient: ")
 sys.stdout.flush()
 name = sys.stdin.readline().strip()
-c.op_keylist_start(name.encode(), 0)
+c.op_keylist_start(name, 0)
 r = c.op_keylist_next()
 
 if r == None:
@@ -48,6 +48,6 @@ else:
     try:
         c.op_encrypt([r], 1, plain, cipher)
         cipher.seek(0,0)
-        print(cipher.read())
+        sys.stdout.buffer.write(cipher.read())
     except errors.GPGMEError as ex:
         print(ex.getstring())
diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i
index 87efce7..7c11943 100644
--- a/lang/python/gpgme.i
+++ b/lang/python/gpgme.i
@@ -24,16 +24,18 @@
 // Generate doc strings for all methods.
 %feature("autodoc", "0");
 
-// Allow use of None for strings.
+/* Allow use of Unicode objects, bytes, and None for strings.  */
 
 %typemap(in) const char * {
   if ($input == Py_None)
     $1 = NULL;
+  else if (PyUnicode_Check($input))
+    $1 = PyUnicode_AsUTF8($input);
   else if (PyBytes_Check($input))
     $1 = PyBytes_AsString($input);
   else {
     PyErr_Format(PyExc_TypeError,
-                 "arg %d: expected string or None, got %s",
+                 "arg %d: expected str, bytes, or None, got %s",
 		 $argnum, $input->ob_type->tp_name);
     return NULL;
   }
@@ -49,20 +51,27 @@
 PyObject* object_to_gpgme_t(PyObject* input, const char* objtype, int argnum) {
   PyObject *pyname = NULL, *pypointer = NULL;
   pyname = PyObject_CallMethod(input, "_getctype", NULL);
-  if (pyname == NULL) {
-    PyErr_Format(PyExc_TypeError,
-		 "arg %d: Expected an instance of type %s, but got %s",
-		 argnum, objtype,
-		 input == Py_None ? "None" : input->ob_type->tp_name);
-    return NULL;
-  }
-  if (strcmp(PyBytes_AsString(pyname), objtype) != 0) {
-    PyErr_Format(PyExc_TypeError,
-		 "arg %d: Expected value of type %s, but got %s",
-		 argnum, objtype, PyBytes_AsString(pyname));
-    Py_DECREF(pyname);
-    return NULL;
-  }
+  if (pyname && PyUnicode_Check(pyname))
+    {
+      if (strcmp(PyUnicode_AsUTF8(pyname), objtype) != 0)
+        {
+          PyErr_Format(PyExc_TypeError,
+                       "arg %d: Expected value of type %s, but got %s",
+                       argnum, objtype, PyUnicode_AsUTF8(pyname));
+          Py_DECREF(pyname);
+          return NULL;
+        }
+    }
+  else
+    {
+      PyErr_Format(PyExc_TypeError,
+                   "Protocol violation: Expected an instance of type str "
+                   "from _getctype, but got %s",
+                   pyname == NULL ? "NULL"
+                   : (pyname == Py_None ? "None" : pyname->ob_type->tp_name));
+      return NULL;
+    }
+
   Py_DECREF(pyname);
   pypointer = PyObject_GetAttrString(input, "wrapped");
   if (pypointer == NULL) {
@@ -243,7 +252,7 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
   }
 
   PyTuple_SetItem(pyargs, 0, PyLong_FromLong((long) status));
-  PyTuple_SetItem(pyargs, 1, PyBytes_FromString(args));
+  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);
@@ -254,8 +263,12 @@ gpgme_error_t pyEditCb(void *opaque, gpgme_status_code_t status,
   if (PyErr_Occurred()) {
     err_status = pygpgme_exception2code();
   } else {
-    if (fd>=0 && retval) {
-      write(fd, PyBytes_AsString(retval), PyBytes_Size(retval));
+    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);
     }
   }
diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index 09f0fa8..fd4802e 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -377,10 +377,11 @@ class Data(GpgmeWrapper):
         self.new_from_fd(file)
 
     def write(self, buffer):
+        """Write buffer given as bytes."""
         errorcheck(pygpgme.gpgme_data_write(self.wrapped, buffer, len(buffer)))
 
     def read(self, size = -1):
-        """Read at most size bytes, returned as a string.
+        """Read at most size bytes, returned as bytes.
 
         If the size argument is negative or omitted, read until EOF is reached.
 
@@ -393,13 +394,13 @@ class Data(GpgmeWrapper):
         if size > 0:
             return pygpgme.gpgme_data_read(self.wrapped, size)
         else:
-            retval = ''
+            chunks = []
             while 1:
-                result = pygpgme.gpgme_data_read(self.wrapped, 10240)
+                result = pygpgme.gpgme_data_read(self.wrapped, 4096)
                 if len(result) == 0:
                     break
-                retval += result
-            return retval
+                chunks.append(result)
+            return b''.join(chunks)
 
 def pubkey_algo_name(algo):
     return pygpgme.gpgme_pubkey_algo_name(algo)

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

Summary of changes:
 lang/python/examples/simple.py |  6 ++---
 lang/python/examples/t-edit.py |  4 ++++
 lang/python/gpgme.i            | 51 ++++++++++++++++++++++++++----------------
 lang/python/pyme/core.py       | 41 ++++++++++++++++++++++-----------
 4 files changed, 67 insertions(+), 35 deletions(-)


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




More information about the Gnupg-commits mailing list