From 883b24563b5f9bf406fcde5edb19caabbc812ac2 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 12 Dec 2006 19:44:41 +0000 Subject: [PATCH] Add assertion macros and supporting functions --- _dbus_bindings/dbus_bindings-internal.h | 28 ++++++++++++++++++++++-- _dbus_bindings/debug.c | 38 +++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/_dbus_bindings/dbus_bindings-internal.h b/_dbus_bindings/dbus_bindings-internal.h index fead45d..38f158d 100644 --- a/_dbus_bindings/dbus_bindings-internal.h +++ b/_dbus_bindings/dbus_bindings-internal.h @@ -33,7 +33,7 @@ /* on/off switch for debugging support (see below) */ #undef USING_DBG -/* #define USING_DBG */ +/* #define USING_DBG 1 */ #define DEFINE_CHECK(type) \ static inline int type##_Check (PyObject *o) \ @@ -142,19 +142,43 @@ dbus_bool_t dbus_py_validate_object_path(const char *path); #define dbus_py_validate_error_name dbus_py_validate_interface_name /* debugging support */ +void _dbus_py_assertion_failed(const char *); +#define DBUS_PY_RAISE_VIA_NULL_IF_FAIL(assertion) \ + do { if (!(assertion)) { \ + _dbus_py_assertion_failed(#assertion); \ + return NULL; \ + } \ + } while (0) + +#define DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(assertion, label) \ + do { if (!(assertion)) { \ + _dbus_py_assertion_failed(#assertion); \ + goto label; \ + } \ + } while (0) + +#define DBUS_PY_RAISE_VIA_RETURN_IF_FAIL(assertion, value) \ + do { if (!(assertion)) { \ + _dbus_py_assertion_failed(#assertion); \ + return value; \ + } \ + } while (0) + +/* verbose debugging support */ #ifdef USING_DBG # include # include void _dbus_py_dbg_exc(void); +void _dbus_py_whereami(void); void _dbus_py_dbg_dump_message(DBusMessage *); # define DBG(format, ...) fprintf(stderr, "DEBUG: " format "\n",\ __VA_ARGS__) # define DBG_EXC(format, ...) do {DBG(format, __VA_ARGS__); \ _dbus_py_dbg_exc();} while (0) -# define DBG_DUMP_MESSAGE(x) _dbg_dump_message(x) +# define DBG_DUMP_MESSAGE(x) _dbus_py_dbg_dump_message(x) #else /* !defined(USING_DBG) */ diff --git a/_dbus_bindings/debug.c b/_dbus_bindings/debug.c index 5dbe356..16138e9 100644 --- a/_dbus_bindings/debug.c +++ b/_dbus_bindings/debug.c @@ -22,8 +22,42 @@ * */ +#include "dbus_bindings-internal.h" +#include + +void +_dbus_py_assertion_failed(const char *assertion) +{ + PyErr_SetString(PyExc_AssertionError, assertion); +#if 1 || defined(USING_DBG) || defined(FATAL_ASSERTIONS) + /* print the Python stack, and dump core so we can see the C stack too */ + PyErr_Print(); + abort(); +#endif +} + #ifdef USING_DBG -void _dbus_py_dbg_exc(void) +void +_dbus_py_whereami(void) +{ + PyObject *c, *v, *t; + /* This is a little mad. We want to get the traceback without + clearing the error indicator, if any. */ + PyErr_Fetch(&c, &v, &t); /* 3 new refs */ + Py_XINCREF(c); Py_XINCREF(v); Py_XINCREF(t); /* now we own 6 refs */ + PyErr_Restore(c, v, t); /* steals 3 refs */ + + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_AssertionError, + "No error, but plz provide traceback kthx"); + } + PyErr_Print(); + + PyErr_Restore(c, v, t); /* steals another 3 refs */ +} + +void +_dbus_py_dbg_exc(void) { PyObject *c, *v, *t; /* This is a little mad. We want to get the traceback without @@ -35,7 +69,7 @@ void _dbus_py_dbg_exc(void) PyErr_Restore(c, v, t); /* steals another 3 refs */ } -static void +void _dbus_py_dbg_dump_message(DBusMessage *message) { const char *s; -- 2.11.4.GIT