aboutsummaryrefslogtreecommitdiffstats
path: root/lang/python/tests/t-data.py
diff options
context:
space:
mode:
authorJustus Winter <[email protected]>2016-05-27 12:04:28 +0000
committerJustus Winter <[email protected]>2016-05-27 12:04:28 +0000
commit2ae847c02731994d99e69d3d025ff01f41406452 (patch)
tree71a4609243a69a10fced3a512715f1a4e915cc6a /lang/python/tests/t-data.py
parentpython: Fix object deallocation. (diff)
downloadgpgme-2ae847c02731994d99e69d3d025ff01f41406452.tar.gz
gpgme-2ae847c02731994d99e69d3d025ff01f41406452.zip
python: Implement data callbacks.
* lang/python/gpgme.i (object_to_gpgme_t): Set exception on error. * lang/python/helpers.c (pyDataReadCb): New function. (pyDataWriteCb): Likewise. (pyDataSeekCb): Likewise. (pyDataReleaseCb): Likewise. (pygpgme_data_new_from_cbs): Likewise. * lang/python/helpers.h (pygpgme_data_new_from_cbs): New prototype. * lang/python/pyme/core.py (Data.__init__): Fix docstring, fix read callbacks. (Data.__del__): Fix read callbacks. (Data._free_readcb): Drop function. (Data._free_datacbs): New function. (Data.new_from_cbs): Fix setting the callbacks. (Data.write): Raise stashed exceptions. (Data.read): Likewise. * lang/python/tests/t-callbacks.py: Test new functionality. * lang/python/tests/t-data.py: Likewise. Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'lang/python/tests/t-data.py')
-rwxr-xr-xlang/python/tests/t-data.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/lang/python/tests/t-data.py b/lang/python/tests/t-data.py
index 6cf10fab..de60c473 100755
--- a/lang/python/tests/t-data.py
+++ b/lang/python/tests/t-data.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 io
import os
import tempfile
from pyme import core
@@ -79,3 +80,43 @@ with tempfile.NamedTemporaryFile() as tmp:
# Open using name, offset, and length.
data = core.Data(file=tmp.name, offset=23, length=42)
assert data.read() == binjunk[23:23+42]
+
+# Test callbacks.
+class DataObject(object):
+ def __init__(self):
+ self.buffer = io.BytesIO()
+ self.released = False
+
+ def read(self, amount, hook=None):
+ assert not self.released
+ return self.buffer.read(amount)
+
+ def write(self, data, hook=None):
+ assert not self.released
+ return self.buffer.write(data)
+
+ def seek(self, offset, whence, hook=None):
+ assert not self.released
+ return self.buffer.seek(offset, whence)
+
+ def release(self, hook=None):
+ assert not self.released
+ self.released = True
+
+do = DataObject()
+cookie = object()
+data = core.Data(cbs=(do.read, do.write, do.seek, do.release, cookie))
+data.write('Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+del data
+assert do.released
+
+# Again, without the cookie.
+do = DataObject()
+data = core.Data(cbs=(do.read, do.write, do.seek, do.release))
+data.write('Hello world!')
+data.seek(0, os.SEEK_SET)
+assert data.read() == b'Hello world!'
+del data
+assert do.released