python: Implement the context manager protocol.
authorJustus Winter <justus@g10code.com>
Tue, 7 Jun 2016 17:31:10 +0000 (19:31 +0200)
committerJustus Winter <justus@g10code.com>
Wed, 8 Jun 2016 11:44:09 +0000 (13:44 +0200)
* lang/python/pyme/core.py (Context.__del__): Make function
idemptotent.
(Context.{__enter__,__exit__}): Implement the context manager
protocol.
(Data.__del__): Make function idemptotent, drop debug print.
(Data.{__enter__,__exit__}): Implement the context manager
protocol.
* lang/python/tests/t-idiomatic.py: Demonstrate this.

Signed-off-by: Justus Winter <justus@g10code.com>
lang/python/pyme/core.py
lang/python/tests/t-idiomatic.py

index 71c6828..4b3e08a 100644 (file)
@@ -147,8 +147,15 @@ class Context(GpgmeWrapper):
         self._free_passcb()
         self._free_progresscb()
         self._free_statuscb()
-        if self.own and pygpgme.gpgme_release:
+        if self.own and self.wrapped and pygpgme.gpgme_release:
             pygpgme.gpgme_release(self.wrapped)
+            self.wrapped = None
+
+    # Implement the context manager protocol.
+    def __enter__(self):
+        return self
+    def __exit__(self, type, value, tb):
+        self.__del__()
 
     def _free_passcb(self):
         if self.last_passcb != None:
@@ -420,10 +427,16 @@ class Data(GpgmeWrapper):
         if self.wrapped != None and pygpgme.gpgme_data_release:
             pygpgme.gpgme_data_release(self.wrapped)
             if self._callback_excinfo:
-                print(self._callback_excinfo)
                 pygpgme.pygpgme_raise_callback_exception(self)
+            self.wrapped = None
         self._free_datacbs()
 
+    # Implement the context manager protocol.
+    def __enter__(self):
+        return self
+    def __exit__(self, type, value, tb):
+        self.__del__()
+
     def _free_datacbs(self):
         if self.data_cbs != None:
             if pygpgme.pygpgme_clear_generic_cb:
index 05a377e..37cfb64 100755 (executable)
@@ -23,7 +23,15 @@ from pyme import core, constants, errors
 import support
 
 support.init_gpgme(constants.PROTOCOL_OpenPGP)
-c = core.Context()
+
+# Both Context and Data can be used as context manager:
+with core.Context() as c, core.Data() as d:
+    c.get_engine_info()
+    d.write(b"Halloechen")
+    leak_c = c
+    leak_d = d
+assert leak_c.wrapped == None
+assert leak_d.wrapped == None
 
 # Demonstrate automatic wrapping of file-like objects with 'fileno'
 # method.
@@ -33,10 +41,11 @@ with tempfile.TemporaryFile() as source, \
     source.write(b"Hallo Leute\n")
     source.seek(0, os.SEEK_SET)
 
-    c.op_sign(source, signed, constants.SIG_MODE_NORMAL)
-    signed.seek(0, os.SEEK_SET)
-    c.op_verify(signed, None, sink)
-    result = c.op_verify_result()
+    with core.Context() as c:
+        c.op_sign(source, signed, constants.SIG_MODE_NORMAL)
+        signed.seek(0, os.SEEK_SET)
+        c.op_verify(signed, None, sink)
+        result = c.op_verify_result()
 
     assert len(result.signatures) == 1, "Unexpected number of signatures"
     sig = result.signatures[0]