python: Add function to raise exceptions from c.
authorJustus Winter <justus@g10code.com>
Wed, 8 Jun 2016 15:54:45 +0000 (17:54 +0200)
committerJustus Winter <justus@g10code.com>
Wed, 8 Jun 2016 15:54:45 +0000 (17:54 +0200)
* lang/python/helpers.c (pygpgme_raise_exception): New function.

Signed-off-by: Justus Winter <justus@g10code.com>
lang/python/helpers.c

index 7e1c1c3..5c620e6 100644 (file)
@@ -1,4 +1,5 @@
 /*
+# Copyright (C) 2016 g10 Code GmbH
 # Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
 # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
 #
@@ -41,6 +42,25 @@ void pygpgme_exception_init(void) {
   }
 }
 
+static PyObject *
+pygpgme_raise_exception(gpgme_error_t err)
+{
+  PyObject *e;
+
+  pygpgme_exception_init();
+  if (GPGMEError == NULL)
+    return PyErr_Format(PyExc_RuntimeError, "Got gpgme_error_t %d", err);
+
+  e = PyObject_CallFunction(GPGMEError, "l", (long) err);
+  if (e == NULL)
+    return NULL;
+
+  PyErr_SetObject(GPGMEError, e);
+  Py_DECREF(e);
+
+  return NULL; /* raise */
+}
+
 gpgme_error_t pygpgme_exception2code(void) {
   gpgme_error_t err_status = gpg_error(GPG_ERR_GENERAL);
   if (GPGMEError && PyErr_ExceptionMatches(GPGMEError)) {