[Libguestfs] [libnbd PATCH v3 4/5] python: Support len(nbd.Buffer(n))

Richard W.M. Jones rjones at redhat.com
Thu Jun 9 14:31:39 UTC 2022


On Thu, Jun 09, 2022 at 08:34:46AM -0500, Eric Blake wrote:
> b.size() is atypical, Python programmers generally expect to be able
> to do len(b).  Keep the old spelling for backwards compatibility.
> ---
>  generator/Python.ml         | 4 ++++
>  python/t/580-aio-is-zero.py | 2 ++
>  2 files changed, 6 insertions(+)
> 
> diff --git a/generator/Python.ml b/generator/Python.ml
> index cd7f0e3..975cab4 100644
> --- a/generator/Python.ml
> +++ b/generator/Python.ml
> @@ -745,6 +745,10 @@ let
>          return bytearray(self._o)
> 
>      def size(self):
> +        '''Return the size of an AIO buffer.'''
> +        return len(self)
> +
> +    def __len__(self):
>          '''Return the size of an AIO buffer.'''
>          return len(self._o)
> 
> diff --git a/python/t/580-aio-is-zero.py b/python/t/580-aio-is-zero.py
> index 1a9a7d3..dd57e30 100644
> --- a/python/t/580-aio-is-zero.py
> +++ b/python/t/580-aio-is-zero.py
> @@ -24,6 +24,7 @@ import nbd
>  ba = bytearray(2**20)
>  buf = nbd.Buffer.from_bytearray(ba)
>  assert buf.size() == 2**20
> +assert len(buf) == 2**20
>  assert buf.is_zero()
> 
>  # The above buffer is 2**20 (= 1MB), slices of it should also be zero.
> @@ -74,5 +75,6 @@ assert buf.is_zero(2**21-1, 1)
>  # used with aio_pread; but it will be zeroed if accessed prematurely
>  buf = nbd.Buffer(1024)
>  assert buf.size() == 1024
> +assert len(buf) == 1024
>  assert buf.is_zero()
>  assert nbd.Buffer.from_bytearray(buf.to_bytearray()).is_zero()
> -- 

Reviewed-by: Richard W.M. Jones <rjones at redhat.com>

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
nbdkit - Flexible, fast NBD server with plugins
https://gitlab.com/nbdkit/nbdkit


More information about the Libguestfs mailing list