[Libguestfs] [PATCH nbdkit] python: Allow extents to return any iterable (which includes lists).

Eric Blake eblake at redhat.com
Mon Aug 10 18:32:34 UTC 2020


On 8/10/20 12:38 PM, Richard W.M. Jones wrote:
> Thanks: Nir Soffer.
> Enhances: commit c12e3cb150259f0058727a50341a2d14bb0015a3
> ---
>   plugins/python/nbdkit-python-plugin.pod |  3 +-
>   plugins/python/python.c                 | 39 +++++++++++++++----------
>   2 files changed, 25 insertions(+), 17 deletions(-)
> 

>   
> -    for (i = 0; i < size; ++i) {
> -      PyObject *t, *py_offset, *py_length, *py_type;
> +    size = 0;
> +    while ((t = PyIter_Next (iter)) != NULL) {
> +      PyObject *py_offset, *py_length, *py_type;
>         uint64_t extent_offset, extent_length;
>         uint32_t extent_type;
>   
> -      t = PyList_GetItem (r, i);
> +      size++;
> +
>         if (!PyTuple_Check (t) || PyTuple_Size (t) != 3) {
> -        nbdkit_error ("extents method did not return a list of 3-tuples");
> +        nbdkit_error ("extents method did not return an iterable of 3-tuples");
> +        Py_DECREF (iter);
>           Py_DECREF (r);
>           return -1;
>         }
> @@ -1078,16 +1075,26 @@ py_extents (void *handle, uint32_t count, uint64_t offset,
>         extent_length = PyLong_AsUnsignedLongLong (py_length);
>         extent_type = PyLong_AsUnsignedLong (py_type);
>         if (check_python_failure ("PyLong") == -1) {
> +        Py_DECREF (iter);
>           Py_DECREF (r);
>           return -1;
>         }
>         if (nbdkit_add_extent (extents,
>                                extent_offset, extent_length, extent_type) == -1) {

Do we want to check that the user is making progress? 
nbdkit_add_extent() ignores 0-length extents, but an iterable that could 
generate an infinite number of them would never progress.  Similarly, we 
could track extent_offset, and if it exceeds offset+length, we could 
quit iterating early, rather than consuming ever more CPUs on an 
interable that is generating code at random (nbdkit_add_extent already 
ignores any further extents beyond a maximum reply length, but that does 
not stop the caller from the wasted CPU cycles).  But those seem like 
optimizations, and what you have seems correct even if we don't try to 
optimize.

> +        Py_DECREF (iter);
>           Py_DECREF (r);
>           return -1;
>         }
>       }
>   
> +    if (size < 1) {
> +      nbdkit_error ("extents method cannot return an empty list");

s/list/iterable/ ?

> +      Py_DECREF (iter);
> +      Py_DECREF (r);
> +      return -1;
> +    }
> +
> +    Py_DECREF (iter);
>       Py_DECREF (r);
>     }
>     else {
> 

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




More information about the Libguestfs mailing list