Prepare to trap errors in Python glue code. Add private macro `CFBIND_TRY` and supporting routines, in an effort to wrap Clownfish C routines and trap errors. This will be used inside glue code for Python, setting `sys.exc_info` when something goes wrong.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/91622dbf Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/91622dbf Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/91622dbf Branch: refs/heads/master Commit: 91622dbf97f1afb2a195609fd1bea2b703119211 Parents: bb7e880 Author: Marvin Humphrey <[email protected]> Authored: Thu Feb 18 19:31:30 2016 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Tue Feb 23 18:22:05 2016 -0800 ---------------------------------------------------------------------- compiler/src/CFCPython.c | 9 +++++++++ runtime/python/cfext/CFBind.c | 22 ++++++++++++++++++++++ runtime/python/cfext/CFBind.h | 12 ++++++++++++ 3 files changed, 43 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/91622dbf/compiler/src/CFCPython.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPython.c b/compiler/src/CFCPython.c index b39240d..a2b13a4 100644 --- a/compiler/src/CFCPython.c +++ b/compiler/src/CFCPython.c @@ -149,6 +149,15 @@ S_gen_callbacks(CFCPython *self, CFCParcel *parcel, CFCClass **ordered) { " va_end(args);\n" " return tuple;\n" "}\n" + "#define CFBIND_TRY(routine) \\\n" + " do { \\\n" + " jmp_buf env; \\\n" + " jmp_buf *prev_env = CFBind_swap_env(&env); \\\n" + " if (!setjmp(env)) { \\\n" + " routine; \\\n" + " } \\\n" + " CFBind_swap_env(prev_env); \\\n" + " } while (0)\n" "\n" "static PyObject*\n" "S_call_pymeth(PyObject *self, const char *meth_name, PyObject *args,\n" http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/91622dbf/runtime/python/cfext/CFBind.c ---------------------------------------------------------------------- diff --git a/runtime/python/cfext/CFBind.c b/runtime/python/cfext/CFBind.c index 7e7f617..48743a7 100644 --- a/runtime/python/cfext/CFBind.c +++ b/runtime/python/cfext/CFBind.c @@ -855,6 +855,28 @@ static cfish_Err *current_error; static cfish_Err *thrown_error; static jmp_buf *current_env; +jmp_buf* +CFBind_swap_env(jmp_buf *env) { + jmp_buf *prev_env = current_env; + current_env = env; + return prev_env; +} + +int +CFBind_migrate_cferr() { + if (thrown_error != NULL) { + cfish_Err *err = thrown_error; + thrown_error = NULL; + cfish_String *mess = CFISH_Err_Get_Mess(err); + char *utf8 = CFISH_Str_To_Utf8(mess); + PyErr_SetString(PyExc_RuntimeError, utf8); + CFISH_FREEMEM(utf8); + CFISH_DECREF(err); + return true; + } + return false; +} + void cfish_Err_init_class(void) { Err_initialized = true; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/91622dbf/runtime/python/cfext/CFBind.h ---------------------------------------------------------------------- diff --git a/runtime/python/cfext/CFBind.h b/runtime/python/cfext/CFBind.h index b6e57fd..9279510 100644 --- a/runtime/python/cfext/CFBind.h +++ b/runtime/python/cfext/CFBind.h @@ -21,6 +21,7 @@ extern "C" { #endif +#include <setjmp.h> #include "cfish_platform.h" #include "Python.h" #include "Clownfish/Obj.h" @@ -38,6 +39,17 @@ struct cfish_String; void CFBind_reraise_pyerr(struct cfish_Class *err_klass, struct cfish_String *mess); +/** Set the current_env, return the old value (internal use only). + */ +jmp_buf* +CFBind_swap_env(jmp_buf *env); + +/** If a Clownfish error was thrown, propagate it to Python and return true + * (internal use only). + */ +int +CFBind_migrate_cferr(void); + /** Null-safe invocation of Obj_To_Host. */ static CFISH_INLINE PyObject*
