[PATCH] python: remove use of PyArg_ParseTuple() for callback result

Arend van Spriel arend at broadcom.com
Mon Sep 30 04:21:46 EDT 2013


The message receive callback handler in the netlink api processes
the result object from the python callback. It used PyArg_ParseTuple()
to get the value, but this does not work as intended (see ref [1]).
Instead check the type and convert it accordingly.

refs:
[1] http://stackoverflow.com/questions/13636711/what-is-the-proper-usage-of-pyarg-parsetuple

Reported-by: Teto <mattator at gmail.com>
Signed-off-by: Arend van Spriel <arend at broadcom.com>
---
 python/examples/wiphy.py |    2 +-
 python/netlink/capi.i    |   27 ++++++++++++++++-----------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/python/examples/wiphy.py b/python/examples/wiphy.py
index fc22205..73e2d4d 100644
--- a/python/examples/wiphy.py
+++ b/python/examples/wiphy.py
@@ -127,7 +127,7 @@ try:
 	family = genl.genl_ctrl_resolve(s, 'nl80211')
 	m = nl.nlmsg_alloc()
 	genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_WIPHY, 0)
-	nl.nla_put_u32(m, nl80211.NL80211_ATTR_WIPHY, 0)
+	nl.nla_put_u32(m, nl80211.NL80211_ATTR_WIPHY, 7)
 
 	err = nl.nl_send_auto_complete(s, m);
 	if err < 0:
diff --git a/python/netlink/capi.i b/python/netlink/capi.i
index 1e13d46..e5d8a53 100644
--- a/python/netlink/capi.i
+++ b/python/netlink/capi.i
@@ -599,8 +599,10 @@ static int nl_recv_msg_handler(struct nl_msg *msg, void *arg)
 	PyObject *funcobj;
 	int result;
 
-	if (!cbd)
-		return NL_STOP;
+	if (!cbd) {
+		result = NL_STOP;
+		goto done;
+	}
 	msgobj = SWIG_NewPointerObj(SWIG_as_voidptr(msg),
 				    SWIGTYPE_p_nl_msg, 0 |  0 );
 	/* add selfobj if callback is a method */
@@ -618,11 +620,13 @@ static int nl_recv_msg_handler(struct nl_msg *msg, void *arg)
 	}
 	resobj = PyObject_CallObject(funcobj, cbparobj);
 	Py_DECREF(cbparobj);
-	if (resobj == NULL)
-		return NL_STOP;
-	if (!PyArg_ParseTuple(resobj, "i:nl_recv_msg_handler", &result))
+	if (resobj && PyInt_Check(resobj))
+		result = (int)PyInt_AsLong(resobj);
+	else
 		result = NL_STOP;
-	Py_DECREF(resobj);
+	Py_XDECREF(resobj);
+done:
+	pynl_dbg("result=%d\n", result);
 	return result;
 }
 
@@ -652,11 +656,12 @@ static int nl_recv_err_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
 	}
 	resobj = PyObject_CallObject(funcobj, cbparobj);
 	Py_DECREF(cbparobj);
-	if (resobj == NULL)
-		return NL_STOP;
-	result = (int)PyInt_AsLong(resobj);
-	Py_DECREF(resobj);
-	printf("error: err=%d ret=%d\n", err->error, result);
+	if (resobj && PyInt_Check(resobj))
+		result = (int)PyInt_AsLong(resobj);
+	else
+		result = NL_STOP;
+	Py_XDECREF(resobj);
+	pynl_dbg("error: err=%d ret=%d\n", err->error, result);
 	return result;
 }
 
-- 
1.7.10.4





More information about the libnl mailing list