Eric Blake
2018-Apr-05 22:25 UTC
[Libguestfs] [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
Richard W.M. Jones
2018-Apr-06 09:23 UTC
Re: [Libguestfs] [nbdkit PATCH] python: Make sure callbacks are actually callable
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
Reasonably Related Threads
- [PATCH nbdkit] python: Try harder to print the full traceback on error.
- Re: [PATCH libnbd v2] generator: Define new Closure type instead of callbacks.
- [libnbd PATCH] python: Plug some memory leaks on error paths
- Re: [PATCH libnbd v2 1/3] generator: Implement OClosure.
- Re: [PATCH libnbd 5/6] generator: Implement OClosure.