[Pkg-gnupg-commit] [gpgme] 93/412: python: Fix writing to data buffers.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Thu Sep 22 21:26:23 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 f7094d8358e933f3ce074eade7a40b2a7d291180
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 17:44:54 2016 +0200

    python: Fix writing to data buffers.
    
    * lang/python/gpgme.i: Add typemap for buffers.
    * lang/python/pyme/core.py (Data.write): Fix function.
    * lang/python/tests/Makefile.am: Add new test.
    * lang/python/tests/t-data.py: New file.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>
---
 lang/python/gpgme.i                          | 17 ++++++++++++++
 lang/python/pyme/core.py                     |  6 +++--
 lang/python/tests/Makefile.am                |  2 +-
 lang/python/tests/{Makefile.am => t-data.py} | 35 ++++++++++++++++++++++++----
 4 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i
index 7c11943..c5b5bc9 100644
--- a/lang/python/gpgme.i
+++ b/lang/python/gpgme.i
@@ -173,6 +173,23 @@ PyObject* object_to_gpgme_t(PyObject* input, const char* objtype, int argnum) {
   free($1);
 }
 
+/* For gpgme_data_write, but should be universal.  */
+%typemap(in) (const void *buffer, size_t size) {
+  if ($input == Py_None)
+    $1 = NULL, $2 = 0;
+  else if (PyUnicode_Check($input))
+    $1 = PyUnicode_AsUTF8AndSize($input, (size_t *) &$2);
+  else if (PyBytes_Check($input))
+    PyBytes_AsStringAndSize($input, (char **) &$1, (size_t *) &$2);
+  else {
+    PyErr_Format(PyExc_TypeError,
+                 "arg %d: expected str, bytes, or None, got %s",
+		 $argnum, $input->ob_type->tp_name);
+    return NULL;
+  }
+}
+%typemap(freearg) (const void *buffer, size_t size) "";
+
 // Make types containing 'next' field to be lists
 %ignore next;
 %typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t, gpgme_key_sig_t,
diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index e822704..dafbd9b 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -391,8 +391,10 @@ 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)))
+        """Write buffer given as string or bytes.
+
+        If a string is given, it is implicitly encoded using UTF-8."""
+        return pygpgme.gpgme_data_write(self.wrapped, buffer)
 
     def read(self, size = -1):
         """Read at most size bytes, returned as bytes.
diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am
index 56ff74c..50c78e3 100644
--- a/lang/python/tests/Makefile.am
+++ b/lang/python/tests/Makefile.am
@@ -19,4 +19,4 @@
 TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
 	PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
 
-TESTS = t-wrapper.py
+TESTS = t-wrapper.py t-data.py
diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/t-data.py
old mode 100644
new mode 100755
similarity index 51%
copy from lang/python/tests/Makefile.am
copy to lang/python/tests/t-data.py
index 56ff74c..af2eb98
--- a/lang/python/tests/Makefile.am
+++ b/lang/python/tests/t-data.py
@@ -1,4 +1,5 @@
-# Makefile.am for the tests of the Python bindings.
+#!/usr/bin/env python3
+
 # Copyright (C) 2016 g10 Code GmbH
 #
 # This file is part of GPGME.
@@ -16,7 +17,33 @@
 # 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/>.
 
-TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
-	PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
+import os
+
+from pyme import core
+
+data = core.Data('Hello world!')
+assert data.read() == b'Hello world!'
+assert data.read() == b''
+
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+assert data.read() == b''
+
+data = core.Data(b'Hello world!')
+assert data.read() == b'Hello world!'
+
+data = core.Data()
+data.write('Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+
+data = core.Data()
+data.write(b'Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
 
-TESTS = t-wrapper.py
+binjunk = bytes(range(256))
+data = core.Data()
+data.write(binjunk)
+data.seek(0, os.SEEK_SET)
+assert data.read() == binjunk

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