[Libguestfs] [PATCH nbdkit v2 01/10] python: Use PyObject_CallFunction instead of constructing the tuple.

Eric Blake eblake at redhat.com
Fri Nov 22 20:30:56 UTC 2019


On 11/22/19 1:53 PM, Richard W.M. Jones wrote:
> It is unclear why we were constructing this by hand, but using the
> following tip we can use PyObject_CallFunction:
> https://stackoverflow.com/a/21221335
> ---
>   plugins/python/python.c | 17 ++++++-----------
>   1 file changed, 6 insertions(+), 11 deletions(-)
> 

>       last_error = 0;
> -    args = PyTuple_New (4);
> -    Py_INCREF (obj); /* decremented by Py_DECREF (args) */
> -    PyTuple_SetItem (args, 0, obj);
> -    PyTuple_SetItem (args, 1, PyLong_FromUnsignedLongLong (count));
> -    PyTuple_SetItem (args, 2, PyLong_FromUnsignedLongLong (offset));
> -    PyTuple_SetItem (args, 3, PyBool_FromLong (may_trim));
> -    r = PyObject_CallObject (fn, args);

Our other two uses of PyObject_CallObject pass NULL args, so they are okay.

> +    r = PyObject_CallFunction (fn, "OiLO",
> +                               obj, count, offset,
> +                               may_trim ? Py_True : Py_False, NULL);

ACK.  And definitely easier to read :)

>       Py_DECREF (fn);
> -    Py_DECREF (args);
>       if (last_error == EOPNOTSUPP || last_error == ENOTSUP) {
>         /* When user requests this particular error, we want to
> -         gracefully fall back, and to accomodate both a normal return
> -         and an exception. */
> +       * gracefully fall back, and to accomodate both a normal return

While we're at it, s/accomodate/accommodate/

> +       * and an exception.
> +       */
>         nbdkit_debug ("zero requested falling back to pwrite");
>         Py_XDECREF (r);
>         PyErr_Clear ();
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list