[nbdkit PATCH] python: Make sure callbacks are actually callable
Rather than catching a Python error at runtime when trying to call an object that wasn't callable, just ignore that object up front when loading the plugin. Signed-off-by: Eric Blake <eblake@redhat.com> --- Various examples on calling Python functions from C recommend doing this filtering check. plugins/python/python.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/python/python.c b/plugins/python/python.c index 0206b80..35e8df2 100644 --- a/plugins/python/python.c +++ b/plugins/python/python.c @@ -94,6 +94,11 @@ callback_defined (const char *name, PyObject **obj_rtn) obj = PyObject_GetAttrString (module, name); if (!obj) return 0; + if (!PyCallable_Check (obj)) { + nbdkit_debug ("object %s isn't callable", name); + Py_DECREF (obj); + return 0; + } if (obj_rtn != NULL) *obj_rtn = obj; -- 2.14.3
On Thu, Apr 05, 2018 at 05:25:55PM -0500, Eric Blake wrote:
Rather than catching a Python error at runtime when trying to call an object that wasn't callable, just ignore that object up front when loading the plugin.
Signed-off-by: Eric Blake <eblake@redhat.com> ---
Various examples on calling Python functions from C recommend doing this filtering check.
plugins/python/python.c | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/plugins/python/python.c b/plugins/python/python.c index 0206b80..35e8df2 100644 --- a/plugins/python/python.c +++ b/plugins/python/python.c @@ -94,6 +94,11 @@ callback_defined (const char *name, PyObject **obj_rtn) obj = PyObject_GetAttrString (module, name); if (!obj) return 0; + if (!PyCallable_Check (obj)) { + nbdkit_debug ("object %s isn't callable", name); + Py_DECREF (obj); + return 0; + }
if (obj_rtn != NULL) *obj_rtn = obj;
ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
participants (2)
-
Eric Blake -
Richard W.M. Jones