From b667c8b41df307c06b9df3ab4122e7ffcf20672a Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Tue, 22 May 2007 23:59:55 -0500 Subject: [PATCH] moo_py_set_error() --- moo/moopython/moopython-api.h | 39 +++++++++++++++++++++++++++++++++++ moo/mooutils/moopython.h | 14 ++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/moo/moopython/moopython-api.h b/moo/moopython/moopython-api.h index 151db3a5..17556ff2 100644 --- a/moo/moopython/moopython-api.h +++ b/moo/moopython/moopython-api.h @@ -20,6 +20,7 @@ #include #define NO_IMPORT_PYGOBJECT #include "pygobject.h" +#include static MooPyObject * @@ -232,6 +233,43 @@ moo_python_api_dict_del_item (MooPyObject *dict, } +static void +moo_python_api_set_error (int type, + const char *format, + ...) +{ + PyObject *exception = NULL; + char *msg = NULL; + va_list args; + + va_start (args, format); + g_vasprintf (&msg, format, args); + va_end (args); + + g_return_if_fail (msg != NULL); + + switch (type) + { + case MOO_PY_RUNTIME_ERROR: + exception = PyExc_RuntimeError; + break; + case MOO_PY_TYPE_ERROR: + exception = PyExc_TypeError; + break; + case MOO_PY_VALUE_ERROR: + exception = PyExc_ValueError; + break; + case MOO_PY_NOT_IMPLEMENTED_ERROR: + exception = PyExc_NotImplementedError; + break; + } + + g_return_if_fail (exception != NULL); + + PyErr_SetString (exception, (char*) msg); +} + + static void moo_python_api_py_err_print (void) { @@ -439,6 +477,7 @@ moo_python_api_init (void) moo_python_api_dict_del_item, moo_python_api_import_exec, + moo_python_api_set_error, moo_python_api_py_err_print, moo_python_api_py_object_call_method, moo_python_api_py_object_call_function, diff --git a/moo/mooutils/moopython.h b/moo/mooutils/moopython.h index 34a8d2e2..1047142b 100644 --- a/moo/mooutils/moopython.h +++ b/moo/mooutils/moopython.h @@ -18,7 +18,7 @@ G_BEGIN_DECLS -#define MOO_PY_API_VERSION 93 +#define MOO_PY_API_VERSION 94 typedef struct _MooPyAPI MooPyAPI; typedef struct _MooPyObject MooPyObject; @@ -35,6 +35,13 @@ enum { MOO_PY_METH_STATIC = 1 << 5 }; +enum { + MOO_PY_RUNTIME_ERROR, + MOO_PY_TYPE_ERROR, + MOO_PY_VALUE_ERROR, + MOO_PY_NOT_IMPLEMENTED_ERROR +}; + struct _MooPyMethodDef { const char *ml_name; MooPyCFunction ml_meth; @@ -79,6 +86,9 @@ struct _MooPyAPI { MooPyObject* (*import_exec) (const char *name, const char *string); + void (*set_error) (int type, + const char *format, + ...); void (*py_err_print) (void); MooPyObject* (*py_object_call_method) (MooPyObject *object, const char *method, @@ -128,6 +138,8 @@ void moo_Py_DECREF (MooPyObject *obj); #define moo_py_object_from_gobject moo_py_api->py_object_from_gobject #define moo_gobject_from_py_object moo_py_api->gobject_from_py_object +#define moo_py_set_error moo_py_api->set_error + #define moo_PyErr_Print moo_py_api->py_err_print #define moo_PyObject_CallMethod moo_py_api->py_object_call_method #define moo_PyObject_CallFunction moo_py_api->py_object_call_function