From f526d0e22e8b881ccbca66b46a0e1b68bbc4cd6b Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 13 Oct 2016 13:13:23 +0200 Subject: python: Make 'get_key' more idiomatic. * lang/python/pyme/core.py (Context.get_key): Raise errors.KeyNotFound if the key is not found. This error is both a KeyError for idiomatic error handling as well as a GPGMEError so we don't break existing code. * lang/python/pyme/errors.py (KeyNotFound): New class. * lang/python/tests/support.py (no_such_key): New variable. * lang/python/tests/t-keylist.py: Test the new behavior. Signed-off-by: Justus Winter --- lang/python/pyme/core.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lang/python/pyme/core.py') diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py index cd5217f3..f0eab436 100644 --- a/lang/python/pyme/core.py +++ b/lang/python/pyme/core.py @@ -680,11 +680,19 @@ class Context(GpgmeWrapper): -- the matching key Raises: + KeyError -- if the key was not found GPGMEError -- as signaled by the underlying library """ ptr = gpgme.new_gpgme_key_t_p() - errorcheck(gpgme.gpgme_get_key(self.wrapped, fpr, ptr, secret)) + + try: + errorcheck(gpgme.gpgme_get_key(self.wrapped, fpr, ptr, secret)) + except errors.GPGMEError as e: + if e.getcode() == errors.EOF: + raise errors.KeyNotFound(fpr) + raise e + key = gpgme.gpgme_key_t_p_value(ptr) gpgme.delete_gpgme_key_t_p(ptr) assert key -- cgit v1.2.3