[git] GPGME - branch, justus/pyme3, updated. gpgme-1.6.0-46-g11314f0

by Justus Winter cvs at cvs.gnupg.org
Tue May 17 10:48:16 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  11314f0db6e57597e3f56351a86fdb36a7a17dd7 (commit)
       via  c5d118b2a76e9528df780d11da9566ff7c22e4f5 (commit)
       via  f7094d8358e933f3ce074eade7a40b2a7d291180 (commit)
       via  e64bffe0307d14204b00a177a472cd4f99c07561 (commit)
       via  ed0ce84fbd2904bf59ac66ae7422716db3624efa (commit)
       via  e3d3d366bd1a1aea8a38ae5dcbf71ea3c784e920 (commit)
       via  af9371eb63664c92fb67e8e7e03cc984e7d38a7f (commit)
      from  ce5121ad53b0e17fbf9150b354c80da73f7fe190 (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 11314f0db6e57597e3f56351a86fdb36a7a17dd7
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 18:29:04 2016 +0200

    python: Share generated methods between objects.
    
    * lang/python/pyme/util.py (GpgmeWrapper.__getattr__): Monkey-patch
    the class.
    * lang/python/tests/t-wrapper.py: Demonstrate the sharing.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py
index a856099..a9fa19d 100644
--- a/lang/python/pyme/util.py
+++ b/lang/python/pyme/util.py
@@ -74,19 +74,25 @@ class GpgmeWrapper(object):
         if key[0] == '_' or self._getnameprepend() == None:
             return None
         name = self._getnameprepend() + key
+        func = getattr(pygpgme, name)
+
         if self._errorcheck(name):
-            def _funcwrap(*args, **kwargs):
-                args = [self.wrapped] + list(args)
-                return errorcheck(getattr(pygpgme, name)(*args, **kwargs),
+            def _funcwrap(slf, *args, **kwargs):
+                return errorcheck(func(slf.wrapped, *args, **kwargs),
                                   "Invocation of " + name)
         else:
-            def _funcwrap(*args, **kwargs):
-                args = [self.wrapped] + list(args)
-                return getattr(pygpgme, name)(*args, **kwargs)
+            def _funcwrap(slf, *args, **kwargs):
+                return func(slf.wrapped, *args, **kwargs)
+
+        _funcwrap.__doc__ = getattr(func, "__doc__")
+
+        # Monkey-patch the class.
+        setattr(self.__class__, key, _funcwrap)
 
-        _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__")
+        # Bind the method to 'self'.
+        def wrapper(*args, **kwargs):
+            return _funcwrap(self, *args, **kwargs)
+        _funcwrap.__doc__ = getattr(func, "__doc__")
 
-        # Cache the wrapper function.
-        setattr(self, key, _funcwrap)
-        return _funcwrap
+        return wrapper
 
diff --git a/lang/python/tests/t-wrapper.py b/lang/python/tests/t-wrapper.py
index acc2ecf..fab0d81 100755
--- a/lang/python/tests/t-wrapper.py
+++ b/lang/python/tests/t-wrapper.py
@@ -20,4 +20,6 @@
 from pyme import core
 
 d0 = core.Data()
+d0.seek # trigger on-demand-wrapping
 assert d0.seek == d0.seek, "Generated wrapper functions are not cached"
+assert hasattr(core.Data, 'seek'), "Generated wrapper functions are not shared"

commit c5d118b2a76e9528df780d11da9566ff7c22e4f5
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 18:00:16 2016 +0200

    python: Raise exceptions on write errors.
    
    * lang/python/pyme/core.py (Data.write): Handle errors.
    * lang/python/pyme/errors.py (GPGMEError.fromSyserror): New function.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index dafbd9b..1d6e384 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -394,7 +394,10 @@ class Data(GpgmeWrapper):
         """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)
+        written = pygpgme.gpgme_data_write(self.wrapped, buffer)
+        if written < 0:
+            raise GPGMEError.fromSyserror()
+        return written
 
     def read(self, size = -1):
         """Read at most size bytes, returned as bytes.
diff --git a/lang/python/pyme/errors.py b/lang/python/pyme/errors.py
index 37d0fe6..5c8f922 100644
--- a/lang/python/pyme/errors.py
+++ b/lang/python/pyme/errors.py
@@ -23,6 +23,10 @@ class GPGMEError(Exception):
         self.error = error
         self.message = message
 
+    @classmethod
+    def fromSyserror(cls):
+        return cls(pygpgme.gpgme_err_code_from_syserror())
+
     def getstring(self):
         message = "%s: %s" % (pygpgme.gpgme_strsource(self.error),
                               pygpgme.gpgme_strerror(self.error))

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>

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

commit e64bffe0307d14204b00a177a472cd4f99c07561
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 15:27:50 2016 +0200

    python: Add a test suite.
    
    * configure.ac: Add new Makefile.
    * lang/python/Makefile.am: Add subdirectory.
    * lang/python/tests/Makefile.am: New file.
    * lang/python/tests/t-wrapper.py: Likewise.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/configure.ac b/configure.ac
index 96df059..e2b4029 100644
--- a/configure.ac
+++ b/configure.ac
@@ -659,7 +659,7 @@ AC_CONFIG_FILES(Makefile src/Makefile
                 src/gpgme.h)
 AC_CONFIG_FILES(src/gpgme-config, chmod +x src/gpgme-config)
 AC_CONFIG_FILES([lang/Makefile lang/cl/Makefile lang/cl/gpgme.asd
-                 lang/python/Makefile])
+                 lang/python/Makefile lang/python/tests/Makefile])
 AC_OUTPUT
 
 echo "
diff --git a/lang/python/Makefile.am b/lang/python/Makefile.am
index a1a2bc9..308fcd0 100644
--- a/lang/python/Makefile.am
+++ b/lang/python/Makefile.am
@@ -17,6 +17,7 @@
 # License along with this program; if not, see <http://www.gnu.org/licenses/>.
 
 EXTRA_DIST = README.rst
+SUBDIRS = tests
 
 # Cleanup gpgme.h from deprecated functions and typedefs.
 gpgme.h: ../../src/gpgme.h
diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am
new file mode 100644
index 0000000..56ff74c
--- /dev/null
+++ b/lang/python/tests/Makefile.am
@@ -0,0 +1,22 @@
+# Makefile.am for the tests of the Python bindings.
+# 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/>.
+
+TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
+	PYTHONPATH=`echo $(abs_builddir)/../build/lib.*`
+
+TESTS = t-wrapper.py
diff --git a/lang/python/tests/t-wrapper.py b/lang/python/tests/t-wrapper.py
new file mode 100755
index 0000000..acc2ecf
--- /dev/null
+++ b/lang/python/tests/t-wrapper.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+# 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/>.
+
+from pyme import core
+
+d0 = core.Data()
+assert d0.seek == d0.seek, "Generated wrapper functions are not cached"

commit ed0ce84fbd2904bf59ac66ae7422716db3624efa
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 14:57:42 2016 +0200

    python: Cache generated wrapper functions.
    
    * lang/python/util.py (GpgmeWrap.__getattr__): Cache generated wrapper
    functions.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py
index 19bbb7f..a856099 100644
--- a/lang/python/pyme/util.py
+++ b/lang/python/pyme/util.py
@@ -69,11 +69,11 @@ class GpgmeWrapper(object):
         returning gpgme_error_t."""
         raise NotImplementedError()
 
-    def __getattr__(self, name):
+    def __getattr__(self, key):
         """On-the-fly function generation."""
-        if name[0] == '_' or self._getnameprepend() == None:
+        if key[0] == '_' or self._getnameprepend() == None:
             return None
-        name = self._getnameprepend() + name
+        name = self._getnameprepend() + key
         if self._errorcheck(name):
             def _funcwrap(*args, **kwargs):
                 args = [self.wrapped] + list(args)
@@ -85,5 +85,8 @@ class GpgmeWrapper(object):
                 return getattr(pygpgme, name)(*args, **kwargs)
 
         _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__")
+
+        # Cache the wrapper function.
+        setattr(self, key, _funcwrap)
         return _funcwrap
 

commit e3d3d366bd1a1aea8a38ae5dcbf71ea3c784e920
Author: Justus Winter <justus at gnupg.org>
Date:   Thu May 12 12:54:15 2016 +0200

    python: Fix function invocation.
    
    * lang/python/pyme/core.py (Data.new_from_fd): Fix function
    invocation.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py
index 2a37ba3..e822704 100644
--- a/lang/python/pyme/core.py
+++ b/lang/python/pyme/core.py
@@ -380,7 +380,7 @@ class Data(GpgmeWrapper):
         if fp == None:
             raise ValueError("Failed to open file from %s arg %s" % \
                   (str(type(file)), str(file)))
-        errorcheck(gpgme_data_new_from_fd(tmp, fp))
+        errorcheck(pygpgme.gpgme_data_new_from_fd(tmp, fp))
         self.wrapped = pygpgme.gpgme_data_t_p_value(tmp)
         pygpgme.delete_gpgme_data_t_p(tmp)
 

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

    python: Fix name of exception, make slot methods explicit.
    
    * lang/python/pyme/util.py (GpgmeWrapper._getctype): Fix exception,
    add docstring.
    (GpgmeWrapper._getnameprepend): New function.
    (GpgmeWrapper._errorcheck): Likewise.
    
    Signed-off-by: Justus Winter <justus at gnupg.org>

diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py
index 32e2f55..19bbb7f 100644
--- a/lang/python/pyme/util.py
+++ b/lang/python/pyme/util.py
@@ -50,7 +50,24 @@ class GpgmeWrapper(object):
             return repr(self.wrapped) == repr(other.wrapped)
 
     def _getctype(self):
-        raise NotImplementedException
+        """Must be implemented by child classes.
+
+        Must return the name of the c type."""
+        raise NotImplementedError()
+
+    def _getnameprepend(self):
+        """Must be implemented by child classes.
+
+        Must return the prefix of all c functions mapped to methods of
+        this class."""
+        raise NotImplementedError()
+
+    def _errorcheck(self, name):
+        """Must be implemented by child classes.
+
+        This function must return a trueish value for all c functions
+        returning gpgme_error_t."""
+        raise NotImplementedError()
 
     def __getattr__(self, name):
         """On-the-fly function generation."""

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

Summary of changes:
 configure.ac                   |  2 +-
 lang/python/Makefile.am        |  1 +
 lang/python/gpgme.i            | 17 ++++++++++++++
 lang/python/pyme/core.py       | 11 +++++++---
 lang/python/pyme/errors.py     |  4 ++++
 lang/python/pyme/util.py       | 50 ++++++++++++++++++++++++++++++++----------
 lang/python/tests/Makefile.am  | 22 +++++++++++++++++++
 lang/python/tests/t-data.py    | 49 +++++++++++++++++++++++++++++++++++++++++
 lang/python/tests/t-wrapper.py | 25 +++++++++++++++++++++
 9 files changed, 165 insertions(+), 16 deletions(-)
 create mode 100644 lang/python/tests/Makefile.am
 create mode 100755 lang/python/tests/t-data.py
 create mode 100755 lang/python/tests/t-wrapper.py


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




More information about the Gnupg-commits mailing list