[Libguestfs] [PATCH nbdkit v2 01/10] python: Use PyObject_CallFunction instead of constructing the tuple.
Nir Soffer
nsoffer at redhat.com
Sat Nov 23 00:14:20 UTC 2019
On Fri, Nov 22, 2019 at 9:54 PM Richard W.M. Jones <rjones at 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
>
More information about the Libguestfs
mailing list