diff options
author | Arend van Spriel <arend@broadcom.com> | 2013-09-05 14:11:27 +0200 |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2013-09-05 17:46:00 +0200 |
commit | 522cf98fc2a5865ab738e308eb4c83270b6a5df7 (patch) | |
tree | d83163c07d5cc752115df5ce7f1b5c4bdd473ebe /python | |
parent | 2d55872fb1f735565fa0e34dbe7889dd31f09f00 (diff) | |
download | libnl-522cf98fc2a5865ab738e308eb4c83270b6a5df7.tar.gz |
python: allow callback function to be a class method
The callback functionality only worked for regular Python
functions. With this patch it also allows the callback
to be a class method (bounded or unbounded) as show in
example below.
class test_class(object):
def my_callback(self, msg, arg):
print('handling %s' % str(msg))
s = netlink.core.Socket()
testobj = test_class()
netlink.capi.py_nl_cb_set(cb, netlink.capi.NL_CB_VALID,
netlink.capi.NL_CB_CUSTOM,
test_class.my_callback, testobj)
netlink.capi.py_nl_cb_err(cb, netlink.capi.NL_CB_CUSTOM,
test_class.my_callback, s)
Signed-off-by: Arend van Spriel <arend@broadcom.com>
Signed-off-by: Thomas Graf <tgraf@suug.ch>
Diffstat (limited to 'python')
-rw-r--r-- | python/netlink/capi.i | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/python/netlink/capi.i b/python/netlink/capi.i index 785e0a68..507c220a 100644 --- a/python/netlink/capi.i +++ b/python/netlink/capi.i @@ -9,6 +9,7 @@ #include <netlink/attr.h> #include <net/if.h> +#define DEBUG #include "utils.h" %} @@ -613,14 +614,27 @@ static int nl_recv_msg_handler(struct nl_msg *msg, void *arg) PyObject *msgobj; PyObject *cbparobj; PyObject *resobj; + PyObject *funcobj; int result; if (!cbd) return NL_STOP; msgobj = SWIG_NewPointerObj(SWIG_as_voidptr(msg), SWIGTYPE_p_nl_msg, 0 | 0 ); - cbparobj = Py_BuildValue("(OO)", msgobj, cbd->cba); - resobj = PyObject_CallObject(cbd->cbf, cbparobj); + /* add selfobj if callback is a method */ + if (cbd->cbf && PyMethod_Check(cbd->cbf)) { + PyObject *selfobj = PyMethod_Self(cbd->cbf); + cbparobj = Py_BuildValue("(OOO)", selfobj ? selfobj : cbd->cba, + msgobj, cbd->cba); + funcobj = PyMethod_Function(cbd->cbf); + pynl_dbg("callback %sbounded instance method %p\n", + selfobj ? "" : "un", funcobj); + } else { + cbparobj = Py_BuildValue("(OO)", msgobj, cbd->cba); + funcobj = cbd->cbf; + pynl_dbg("callback function %p\n", funcobj); + } + resobj = PyObject_CallObject(funcobj, cbparobj); Py_DECREF(cbparobj); if (resobj == NULL) return NL_STOP; @@ -637,14 +651,24 @@ static int nl_recv_err_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, PyObject *errobj; PyObject *cbparobj; PyObject *resobj; + PyObject *funcobj; int result; if (!cbd) return NL_STOP; errobj = SWIG_NewPointerObj(SWIG_as_voidptr(err), SWIGTYPE_p_nlmsgerr, 0 | 0 ); - cbparobj = Py_BuildValue("(OO)", errobj, cbd->cba); - resobj = PyObject_CallObject(cbd->cbf, cbparobj); + /* add selfobj if callback is a method */ + if (cbd->cbf && PyMethod_Check(cbd->cbf)) { + PyObject *selfobj = PyMethod_Self(cbd->cbf); + cbparobj = Py_BuildValue("(OOO)", selfobj ? selfobj : cbd->cba, + errobj, cbd->cba); + funcobj = PyMethod_Function(cbd->cbf); + } else { + cbparobj = Py_BuildValue("(OO)", errobj, cbd->cba); + funcobj = cbd->cbf; + } + resobj = PyObject_CallObject(funcobj, cbparobj); Py_DECREF(cbparobj); if (resobj == NULL) return NL_STOP; |