[Libguestfs] [PATCH] api: Add a special type for the flags argument.

Eric Blake eblake at redhat.com
Tue May 28 14:58:45 UTC 2019


On 5/28/19 7:57 AM, Richard W.M. Jones wrote:
> By using a special type we can more naturally express flags in
> different programming languages.  For example OCaml will prefer an
> optional argument containing a list of flags, defaulting to the empty
> list:
> 
>   pread [...] ?(flags = [])
> ---
>  generator/generator          | 66 +++++++++++++++++++++++++-----------
>  python/t/400-pread.py        |  2 +-
>  python/t/410-pwrite.py       |  2 +-
>  python/t/460-block-status.py |  4 +--
>  python/t/500-aio-pread.py    |  2 +-
>  python/t/510-aio-pwrite.py   |  2 +-
>  6 files changed, 53 insertions(+), 25 deletions(-)

Definitely nicer than my poor attempt at OCaml hacking :)


> @@ -2494,6 +2495,21 @@ let generate_lib_states_c () =
>  
>  (* Generate C API. *)
>  
> +(* Check the API definition. *)
> +let () =
> +  (* Flags must only appear once in the final argument position. *)
> +  List.iter (
> +    fun (name, { args }) ->
> +      let args = List.rev args in
> +      match args with
> +      | [] -> ()
> +      | Flags _ :: xs
> +      | xs ->
> +         if List.exists (function Flags _ -> true | _ -> false) xs then
> +           failwithf "%s: Flags must appear in final argument position only"
> +                     name
> +  ) handle_calls
> +

And this part is nice (even if I'm having to read up on quite a bit of
documentation to understand how it works)


> +++ b/python/t/400-pread.py
> @@ -20,7 +20,7 @@ import nbd
>  h = nbd.NBD ()
>  h.connect_command (["nbdkit", "-s", "--exit-with-parent", "-v",
>                      "pattern", "size=512"])
> -buf = h.pread (512, 0, 0)
> +buf = h.pread (512, 0)

At any rate, you achieved the same goal I had in mind for omitting a 0
flags argument in Python.

>  
>  print ("%r" % buf)
>  
> diff --git a/python/t/410-pwrite.py b/python/t/410-pwrite.py
> index 9152ba2..811f233 100644
> --- a/python/t/410-pwrite.py
> +++ b/python/t/410-pwrite.py
> @@ -33,7 +33,7 @@ h = nbd.NBD ()
>  h.connect_command (["nbdkit", "-s", "--exit-with-parent", "-v",
>                      "file", datafile])
>  h.pwrite (buf1, 0, nbd.CMD_FLAG_FUA)

There's still the question of how to pass two flags at once; if I
understand your patch (and after confirming it with nbdsh), we have to
write:

h.zero (64*1024, 0, nbd.CMD_FLAG_FUA | nbd.CMD_FLAG_NO_HOLE)

[side note - requires a server that supports flush; to my surprise,
nbkdit 'null' does but 'memory' did not; I'll probably fix that]

I don't know if we want to somehow allow:

h.zero (64*1024, 0, nbd.CMD_FLAG_FUA, nbd.CMD_FLAG_NO_HOLE)

instead (two arguments, each with one flag, which the glue code then
combines into a single integer; compared to the existing approach of
letting Python do the bitwise math and pass in a single integer up front).

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20190528/5ef538c1/attachment.sig>


More information about the Libguestfs mailing list