From 2ae847c02731994d99e69d3d025ff01f41406452 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 27 May 2016 14:04:28 +0200 Subject: 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 --- lang/python/tests/t-callbacks.py | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'lang/python/tests/t-callbacks.py') diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py index 57975264..3219463c 100755 --- a/lang/python/tests/t-callbacks.py +++ b/lang/python/tests/t-callbacks.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 . +import os from pyme import core, constants import support @@ -181,3 +182,73 @@ except Exception as e: assert e == myException else: assert False, "Expected an error, got none" + + + +# Test the data callbacks. +def read_cb(amount, hook=None): + assert hook == cookie + return 0 +def release_cb(hook=None): + assert hook == cookie +data = core.Data(cbs=(read_cb, None, None, release_cb, cookie)) +try: + data.read() +except Exception as e: + assert type(e) == TypeError +else: + assert False, "Expected an error, got none" + +def read_cb(amount): + raise myException +data = core.Data(cbs=(read_cb, None, None, lambda: None)) +try: + data.read() +except Exception as e: + assert e == myException +else: + assert False, "Expected an error, got none" + + +def write_cb(what, hook=None): + assert hook == cookie + return "wrong type" +data = core.Data(cbs=(None, write_cb, None, release_cb, cookie)) +try: + data.write(b'stuff') +except Exception as e: + assert type(e) == TypeError +else: + assert False, "Expected an error, got none" + +def write_cb(what): + raise myException +data = core.Data(cbs=(None, write_cb, None, lambda: None)) +try: + data.write(b'stuff') +except Exception as e: + assert e == myException +else: + assert False, "Expected an error, got none" + + +def seek_cb(offset, whence, hook=None): + assert hook == cookie + return "wrong type" +data = core.Data(cbs=(None, None, seek_cb, release_cb, cookie)) +try: + data.seek(0, os.SEEK_SET) +except Exception as e: + assert type(e) == TypeError +else: + assert False, "Expected an error, got none" + +def seek_cb(offset, whence): + raise myException +data = core.Data(cbs=(None, None, seek_cb, lambda: None)) +try: + data.seek(0, os.SEEK_SET) +except Exception as e: + assert e == myException +else: + assert False, "Expected an error, got none" -- cgit v1.2.3