[git] GPGME - branch, master, updated. gpgme-1.6.0-339-g70a3be2

by Justus Winter cvs at cvs.gnupg.org
Tue Sep 13 13:32:13 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  70a3be27a509a1b5ea7372bee93d83c5019427ff (commit)
       via  4abff7d750a1abf5b388a4c87ec321fc3e4aed10 (commit)
      from  c0c50318bd8ef6c8119ad9fdc53ad9087ded4c32 (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 70a3be27a509a1b5ea7372bee93d83c5019427ff
Author: Justus Winter <justus at g10code.com>
Date:   Tue Sep 13 10:44:14 2016 +0200

    python: Handle slight differences between Python 2 and 3.
    
    * lang/python/helpers.c (pyDataWriteCb): Handle Python integers being
    returned on Python 2.
    (pyDataSeekCb): Likewise.
    * lang/python/pyme/core.py (Data.__init__): Fix testing for string
    argument.
    (Data.new_from_filepart): Likewise.
    * lang/python/pyme/util.py (is_a_string): New function.
    * lang/python/tests/t-encrypt-large.py (read_cb): Force evaluation of
    generator.
    * lang/python/tests/t-idiomatic.py: Partly skip test on Python 2.
    * lang/python/tests/t-verify.py (check_result): Here, the difference
    between 2 and 3 really matters.  We cannot change the char *
    conversion in Python 2 without breaking all existing applications, and
    using bytestrings in Python 3 would be very inconvenient.
    
    Signed-off-by: Justus Winter <justus at g10code.com>

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 5b13fee..bc8aed4 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -833,17 +833,21 @@ static ssize_t pyDataWriteCb(void *hook, const void *buffer, size_t size)
     goto leave;
   }
 
-  if (! PyLong_Check(retval)) {
+#if PY_MAJOR_VERSION < 3
+  if (PyInt_Check(retval))
+    result = PyInt_AsSsize_t(retval);
+  else
+#endif
+  if (PyLong_Check(retval))
+    result = PyLong_AsSsize_t(retval);
+  else {
     PyErr_Format(PyExc_TypeError,
-                 "expected int from read callback, got %s",
+                 "expected int from write callback, got %s",
                  retval->ob_type->tp_name);
     _pyme_stash_callback_exception(self);
     result = -1;
-    goto leave;
   }
 
-  result = PyLong_AsSsize_t(retval);
-
  leave:
   Py_XDECREF(retval);
   return result;
@@ -894,21 +898,25 @@ static off_t pyDataSeekCb(void *hook, off_t offset, int whence)
     goto leave;
   }
 
-  if (! PyLong_Check(retval)) {
+#if PY_MAJOR_VERSION < 3
+  if (PyInt_Check(retval))
+    result = PyInt_AsLong(retval);
+  else
+#endif
+  if (PyLong_Check(retval))
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
+    result = PyLong_AsLongLong(retval);
+#else
+    result = PyLong_AsLong(retval);
+#endif
+  else {
     PyErr_Format(PyExc_TypeError,
-                 "expected int from read callback, got %s",
+                 "expected int from seek callback, got %s",
                  retval->ob_type->tp_name);
     _pyme_stash_callback_exception(self);
     result = -1;
-    goto leave;
   }
 
-#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
-  result = PyLong_AsLongLong(retval);
-#else
-  result = PyLong_AsLong(retval);
-#endif
-
  leave:
   Py_XDECREF(retval);
   return result;
diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index 4bbbc17..a71426b 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -884,7 +884,7 @@ class Data(GpgmeWrapper):
         elif file != None and offset != None and length != None:
             self.new_from_filepart(file, offset, length)
         elif file != None:
-            if type(file) == type("x"):
+            if util.is_a_string(file):
                 self.new_from_file(file, copy)
             else:
                 self.new_from_fd(file)
@@ -961,7 +961,7 @@ class Data(GpgmeWrapper):
         filename = None
         fp = None
 
-        if type(file) == type("x"):
+        if util.is_a_string(file):
             filename = file
         else:
             fp = gpgme.fdopen(file.fileno(), file.mode)
diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py
index c4c9e18..bf25ccb 100644
--- a/lang/python/pyme/util.py
+++ b/lang/python/pyme/util.py
@@ -16,6 +16,8 @@
 #    License along with this library; if not, write to the Free Software
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
+import sys
+
 def process_constants(prefix, scope):
     """Called by the constant modules to load up the constants from the C
     library starting with PREFIX.  Matching constants will be inserted
@@ -36,3 +38,13 @@ def percent_escape(s):
         '%{0:2x}'.format(ord(c))
         if c == '+' or c == '"' or c == '%' or ord(c) <= 0x20 else c
         for c in s)
+
+# Python2/3 compatibility
+if sys.version_info[0] == 3:
+    # Python3
+    def is_a_string(x):
+        return isinstance(x, str)
+else:
+    # Python2
+    def is_a_string(x):
+        return isinstance(x, basestring)
diff --git a/lang/python/tests/t-encrypt-large.py b/lang/python/tests/t-encrypt-large.py
index 69aed48..29f9de2 100755
--- a/lang/python/tests/t-encrypt-large.py
+++ b/lang/python/tests/t-encrypt-large.py
@@ -37,7 +37,7 @@ def read_cb(amount):
     ntoread -= chunk
     assert ntoread >= 0
     assert chunk >= 0
-    return bytes(random.randrange(256) for i in range(chunk))
+    return bytes(bytearray(random.randrange(256) for i in range(chunk)))
 
 nwritten = 0
 def write_cb(data):
diff --git a/lang/python/tests/t-idiomatic.py b/lang/python/tests/t-idiomatic.py
index 1989c92..726bbb9 100755
--- a/lang/python/tests/t-idiomatic.py
+++ b/lang/python/tests/t-idiomatic.py
@@ -17,6 +17,7 @@
 # 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 io
 import os
 import tempfile
@@ -60,17 +61,21 @@ with tempfile.TemporaryFile() as source, \
 
     sign_and_verify(source, signed, sink)
 
-# XXX: Python's io.BytesIo.truncate does not work as advertised.
-# http://bugs.python.org/issue27261
-bio = io.BytesIO()
-bio.truncate(1)
-if len(bio.getvalue()) != 1:
-    # This version of Python is affected, preallocate buffer.
-    preallocate = 128*b'\x00'
-else:
-    preallocate = b''
+if sys.version_info[0] == 3:
+    # Python2's io.BytesIO does not implement the buffer interface,
+    # hence we cannot use it as sink.
 
-# Demonstrate automatic wrapping of objects implementing the buffer
-# interface, and the use of data objects with the 'with' statement.
-with io.BytesIO(preallocate) as signed, pyme.Data() as sink:
-    sign_and_verify(b"Hallo Leute\n", signed, sink)
+    # XXX: Python's io.BytesIo.truncate does not work as advertised.
+    # http://bugs.python.org/issue27261
+    bio = io.BytesIO()
+    bio.truncate(1)
+    if len(bio.getvalue()) != 1:
+        # This version of Python is affected, preallocate buffer.
+        preallocate = 128*b'\x00'
+    else:
+        preallocate = b''
+
+    # Demonstrate automatic wrapping of objects implementing the buffer
+    # interface, and the use of data objects with the 'with' statement.
+    with io.BytesIO(preallocate) as signed, pyme.Data() as sink:
+        sign_and_verify(b"Hallo Leute\n", signed, sink)
diff --git a/lang/python/tests/t-verify.py b/lang/python/tests/t-verify.py
index b88bd07..ed5a91a 100755
--- a/lang/python/tests/t-verify.py
+++ b/lang/python/tests/t-verify.py
@@ -17,6 +17,7 @@
 # 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
 import pyme
 from pyme import core, constants, errors
@@ -67,8 +68,11 @@ def check_result(result, summary, validity, fpr, status, notation):
 
     if notation:
         expected_notations = {
-            "bar": b"\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f".decode() +
-            " das waren Umlaute und jetzt ein prozent%-Zeichen",
+            "bar": (b"\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f" +
+                    b" das waren Umlaute und jetzt ein prozent%-Zeichen"
+                    if sys.version_info[0] < 3 else
+                    b"\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f".decode() +
+                    " das waren Umlaute und jetzt ein prozent%-Zeichen"),
             "foobar.1":  "this is a notation data with 2 lines",
             None: "http://www.gu.org/policy/",
         }

commit 4abff7d750a1abf5b388a4c87ec321fc3e4aed10
Author: Justus Winter <justus at g10code.com>
Date:   Tue Sep 13 13:25:15 2016 +0200

    python: Fix types and error handling.
    
    * lang/python/helpers.c (_pyme_edit_cb): Drop the const.
    (_pyme_assuan_{data,inquire,status}_cb): Fix error handling.
    
    Signed-off-by: Justus Winter <justus at g10code.com>

diff --git a/lang/python/helpers.c b/lang/python/helpers.c
index 6e63c97..5b13fee 100644
--- a/lang/python/helpers.c
+++ b/lang/python/helpers.c
@@ -692,7 +692,7 @@ gpgme_error_t _pyme_edit_cb(void *opaque, gpgme_status_code_t status,
   } else {
     if (fd>=0 && retval && PyUnicode_Check(retval)) {
       PyObject *encoded = NULL;
-      const char *buffer;
+      char *buffer;
       Py_ssize_t size;
 
       encoded = PyUnicode_AsUTF8String(retval);
@@ -999,7 +999,10 @@ _pyme_assuan_data_cb (void *hook, const void *data, size_t datalen)
 
   py_data = PyBytes_FromStringAndSize(data, datalen);
   if (py_data == NULL)
-    return NULL;	/* raise */
+    {
+      err = _pyme_exception2code();
+      goto leave;
+    }
 
   retval = PyObject_CallFunctionObjArgs(func, py_data, NULL);
   if (PyErr_Occurred())
@@ -1033,23 +1036,29 @@ _pyme_assuan_inquire_cb (void *hook, const char *name, const char *args,
 
   py_name = PyUnicode_FromString(name);
   if (py_name == NULL)
-    return NULL;	/* raise */
+    {
+      err = _pyme_exception2code();
+      goto leave;
+    }
 
   py_args = PyUnicode_FromString(args);
   if (py_args == NULL)
-    return NULL;	/* raise */
+    {
+      err = _pyme_exception2code();
+      goto leave;
+    }
 
   retval = PyObject_CallFunctionObjArgs(func, py_name, py_args, NULL);
   if (PyErr_Occurred())
     err = _pyme_exception2code();
-  Py_DECREF(py_name);
-  Py_DECREF(py_args);
   Py_XDECREF(retval);
 
   /* FIXME: Returning data is not yet implemented.  */
-  r_data = NULL;
+  *r_data = NULL;
 
  leave:
+  Py_XDECREF(py_name);
+  Py_XDECREF(py_args);
   if (err)
     _pyme_stash_callback_exception(self);
   return err;
@@ -1074,20 +1083,26 @@ _pyme_assuan_status_cb (void *hook, const char *status, const char *args)
 
   py_status = PyUnicode_FromString(status);
   if (py_status == NULL)
-    return NULL;	/* raise */
+    {
+      err = _pyme_exception2code();
+      goto leave;
+    }
 
   py_args = PyUnicode_FromString(args);
   if (py_args == NULL)
-    return NULL;	/* raise */
+    {
+      err = _pyme_exception2code();
+      goto leave;
+    }
 
   retval = PyObject_CallFunctionObjArgs(func, py_status, py_args, NULL);
   if (PyErr_Occurred())
     err = _pyme_exception2code();
-  Py_DECREF(py_status);
-  Py_DECREF(py_args);
   Py_XDECREF(retval);
 
  leave:
+  Py_XDECREF(py_status);
+  Py_XDECREF(py_args);
   if (err)
     _pyme_stash_callback_exception(self);
   return err;

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

Summary of changes:
 lang/python/helpers.c                | 73 ++++++++++++++++++++++++------------
 lang/python/pyme/core.py             |  4 +-
 lang/python/pyme/util.py             | 12 ++++++
 lang/python/tests/t-encrypt-large.py |  2 +-
 lang/python/tests/t-idiomatic.py     | 31 ++++++++-------
 lang/python/tests/t-verify.py        |  8 +++-
 6 files changed, 87 insertions(+), 43 deletions(-)


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




More information about the Gnupg-commits mailing list