[Pkg-gnupg-commit] [gpgme] 86/412: python: Fix type translation.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Thu Sep 22 21:26:21 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 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>
---
 lang/python/examples/simple.py |  6 ++---
 lang/python/gpgme.i            | 51 ++++++++++++++++++++++++++----------------
 lang/python/pyme/core.py       | 11 ++++-----
 3 files changed, 41 insertions(+), 27 deletions(-)

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)

-- 
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