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

Nir Soffer
Sat, 23 Nov 2019 00:14:20 -0000

On Fri, Nov 22, 2019 at 9:54 PM Richard W.M. Jones <rjones@redhat.com>
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(-)
>
> diff --git a/plugins/python/python.c b/plugins/python/python.c
> index 148097f..d65ac45 100644
> --- a/plugins/python/python.c
> +++ b/plugins/python/python.c
> @@ -557,26 +557,21 @@ py_zero (void *handle, uint32_t count, uint64_t
offset, int may_trim)
>  {
>    PyObject *obj = handle;
>    PyObject *fn;
> -  PyObject *args;
>    PyObject *r;
>
>    if (callback_defined ("zero", &fn)) {
>      PyErr_Clear ();
>
>      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);
> +    r = PyObject_CallFunction (fn, "OiLO",
> +                               obj, count, offset,
> +                               may_trim ? Py_True : Py_False, NULL);

Much nicer.

>      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
> +       * and an exception.
> +       */
>        nbdkit_debug ("zero requested falling back to pwrite");
>        Py_XDECREF (r);
>        PyErr_Clear ();
> --
> 2.23.0
>