git » libfiu » commit d4d466b

python: Add enable_stack_by_name()

author Alberto Bertogli
2012-03-28 20:46:15 UTC
committer Alberto Bertogli
2012-03-28 20:46:15 UTC
parent b17a81b37cf9ba202d1f498cb26f8d68002ff81a

python: Add enable_stack_by_name()

This patch implements a wrapper for fiu_enable_stack_by_name().

We don't add fiu_enable_stack() because it takes a C function pointer, and
it's not clear that it's useful to have that available in Python at this time.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

bindings/python/fiu.py +15 -0
bindings/python/fiu_ll.c +23 -1

diff --git a/bindings/python/fiu.py b/bindings/python/fiu.py
index c61eef9..4ae2c0c 100644
--- a/bindings/python/fiu.py
+++ b/bindings/python/fiu.py
@@ -63,6 +63,21 @@ def enable_external(name, cb, failnum = 1, flags = 0):
 	if r != 0:
 		raise RuntimeError(r)
 
+def enable_stack_by_name(name, func_name,
+		failnum = 1, failinfo = None, flags = 0,
+		pos_in_stack = -1):
+	"""Enables the given point of failure, but only if 'func_name' is in
+	the stack.
+
+	'func_name' is be the name of the C function to look for.
+	"""
+	_fi_table[name] = failinfo
+	r = _ll.enable_stack_by_name(name, failnum, failinfo, flags,
+			func_name, pos_in_stack)
+	if r != 0:
+		del _fi_table[name]
+		raise RuntimeError(r)
+
 def disable(name):
 	"""Disables the given point of failure, undoing the actions of the
 	enable*() functions."""
diff --git a/bindings/python/fiu_ll.c b/bindings/python/fiu_ll.c
index a558f28..e49874b 100644
--- a/bindings/python/fiu_ll.c
+++ b/bindings/python/fiu_ll.c
@@ -158,6 +158,25 @@ static PyObject *enable_external(PyObject *self, PyObject *args)
 				py_external_cb, flags, external_callback));
 }
 
+static PyObject *enable_stack_by_name(PyObject *self, PyObject *args)
+{
+	char *name;
+	int failnum;
+	PyObject *failinfo;
+	unsigned int flags;
+	char *func_name;
+	int pos_in_stack = -1;
+
+	if (!PyArg_ParseTuple(args, "siOIs|i:enable_stack_by_name",
+				&name, &failnum, &failinfo, &flags,
+				&func_name, &pos_in_stack))
+		return NULL;
+
+	return PyLong_FromLong(fiu_enable_stack_by_name(name, failnum,
+				failinfo, flags,
+				func_name, pos_in_stack));
+}
+
 static PyObject *disable(PyObject *self, PyObject *args)
 {
 	char *name;
@@ -183,7 +202,10 @@ static PyMethodDef fiu_methods[] = {
 	{ "failinfo", (PyCFunction) failinfo, METH_VARARGS, NULL },
 	{ "enable", (PyCFunction) enable, METH_VARARGS, NULL },
 	{ "enable_random", (PyCFunction) enable_random, METH_VARARGS, NULL },
-	{ "enable_external", (PyCFunction) enable_external, METH_VARARGS, NULL },
+	{ "enable_external", (PyCFunction) enable_external,
+		METH_VARARGS, NULL },
+	{ "enable_stack_by_name", (PyCFunction) enable_stack_by_name,
+		METH_VARARGS, NULL },
 	{ "disable", (PyCFunction) disable, METH_VARARGS, NULL },
 	{ "rc_fifo", (PyCFunction) rc_fifo, METH_VARARGS, NULL },
 	{ NULL }