You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2016/02/25 00:07:32 UTC

[05/20] lucy-clownfish git commit: Re-raise Python exceptions from Clownfish.

Re-raise Python exceptions from Clownfish.

Add a function which takes the Python `sys.exc_info`, wraps it in a
Clownfish Err, and THROWs it.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/9162199c
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/9162199c
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/9162199c

Branch: refs/heads/master
Commit: 9162199c26ab869bff1133d517388899468b4170
Parents: eccf1b6
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Fri Jan 22 16:51:01 2016 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Tue Feb 23 18:22:02 2016 -0800

----------------------------------------------------------------------
 runtime/python/cfext/CFBind.c | 36 ++++++++++++++++++++++++++++++++++++
 runtime/python/cfext/CFBind.h | 13 +++++++++++++
 2 files changed, 49 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/9162199c/runtime/python/cfext/CFBind.c
----------------------------------------------------------------------
diff --git a/runtime/python/cfext/CFBind.c b/runtime/python/cfext/CFBind.c
index 3c4bb4b..95ae70d 100644
--- a/runtime/python/cfext/CFBind.c
+++ b/runtime/python/cfext/CFBind.c
@@ -40,6 +40,42 @@
 
 static bool Err_initialized;
 
+/**** Utility **************************************************************/
+
+void
+CFBind_reraise_pyerr(cfish_Class *err_klass, cfish_String *mess) {
+    PyObject *type, *value, *traceback;
+    PyObject *type_pystr = NULL;
+    PyObject *value_pystr = NULL;
+    PyObject *traceback_pystr = NULL;
+    char *type_str = "";
+    char *value_str = "";
+    char *traceback_str = "";
+    PyErr_GetExcInfo(&type, &value, &traceback);
+    if (type != NULL) {
+        PyObject *type_pystr = PyObject_Str(type);
+        type_str = PyUnicode_AsUTF8(type_pystr);
+    }
+    if (value != NULL) {
+        PyObject *value_pystr = PyObject_Str(value);
+        value_str = PyUnicode_AsUTF8(value_pystr);
+    }
+    if (traceback != NULL) {
+        PyObject *traceback_pystr = PyObject_Str(traceback);
+        traceback_str = PyUnicode_AsUTF8(traceback_pystr);
+    }
+    cfish_String *new_mess = cfish_Str_newf("%o... %s: %s %s", mess, type_str,
+                                            value_str, traceback_str);
+    Py_XDECREF(type);
+    Py_XDECREF(value);
+    Py_XDECREF(traceback);
+    Py_XDECREF(type_pystr);
+    Py_XDECREF(value_pystr);
+    Py_XDECREF(traceback_pystr);
+    CFISH_DECREF(mess);
+    cfish_Err_throw_mess(err_klass, new_mess);
+}
+
 /**** refcounting **********************************************************/
 
 uint32_t

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/9162199c/runtime/python/cfext/CFBind.h
----------------------------------------------------------------------
diff --git a/runtime/python/cfext/CFBind.h b/runtime/python/cfext/CFBind.h
index 810f201..d6a362e 100644
--- a/runtime/python/cfext/CFBind.h
+++ b/runtime/python/cfext/CFBind.h
@@ -24,6 +24,19 @@ extern "C" {
 #include "cfish_platform.h"
 #include "Python.h"
 
+struct cfish_Class;
+struct cfish_String;
+
+/** Wrap the current state of Python's sys.exc_info in a Clownfish Err and
+  * throw it.
+  *
+  * One refcount of `mess` will be consumed by this function.
+  *
+  * TODO: Leave the original exception intact.
+  */
+void
+CFBind_reraise_pyerr(struct cfish_Class *err_klass, struct cfish_String *mess);
+
 #ifdef __cplusplus
 }
 #endif