[Libguestfs] [libnbd PATCH 3/4] python: Let flags parameter be optional

Eric Blake eblake at redhat.com
Tue May 28 02:01:00 UTC 2019


No need to require the user to pass in 0 for flags when the language
has the ability for a default parameter.

This approach works because we never have more than one optional
argument per function, but should we need more optional arguments,
we'd probably need to teach the generator to split type call's args
into mandatory and optional args, as python's PyArg_ParseTuple wants
only a single | separating mandatory from optional arguments.
---
 generator/generator        | 89 +++++++++++++++++++++++++++-----------
 python/t/400-pread.py      |  2 +-
 python/t/410-pwrite.py     |  2 +-
 python/t/500-aio-pread.py  |  2 +-
 python/t/510-aio-pwrite.py |  2 +-
 5 files changed, 67 insertions(+), 30 deletions(-)

diff --git a/generator/generator b/generator/generator
index 8e81d8f..919341c 100755
--- a/generator/generator
+++ b/generator/generator
@@ -823,6 +823,7 @@ and arg =
 | Int of string            (* small int *)
 | Int64 of string          (* 64 bit signed int *)
 | Opaque of string         (* opaque object, void* in C *)
+| OptUInt32 of string      (* optional 32 bit unsigned int, default 0 *)
 | Path of string           (* filename or path *)
 | SockAddrAndLen of string * string (* struct sockaddr * + socklen_t *)
 | String of string         (* string *)
@@ -1219,7 +1220,7 @@ with the server.";

   "pread", {
     default_call with
-    args = [ BytesOut ("buf", "count"); UInt64 "offset"; UInt32 "flags" ];
+    args = [ BytesOut ("buf", "count"); UInt64 "offset"; OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "read from the NBD server";
     longdesc = "\
@@ -1235,7 +1236,7 @@ protocol extensions).";

   "pwrite", {
     default_call with
-    args = [ BytesIn ("buf", "count"); UInt64 "offset"; UInt32 "flags" ];
+    args = [ BytesIn ("buf", "count"); UInt64 "offset"; OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "write to the NBD server";
     longdesc = "\
@@ -1270,7 +1271,7 @@ C<nbd_aio_disconnect>.";

   "flush", {
     default_call with
-    args = [ UInt32 "flags" ]; ret = RErr;
+    args = [ OptUInt32 "flags" ]; ret = RErr;
     shortdesc = "flushing pending write requests";
     longdesc = "\
 Issue the flush command to the NBD server.  The function should
@@ -1284,7 +1285,7 @@ protocol extensions).";

   "trim", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "send trim to the NBD server";
     longdesc = "\
@@ -1303,7 +1304,7 @@ C<nbd_can_fua>).";

   "cache", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "send cache (prefetch) to the NBD server";
     longdesc = "\
@@ -1320,7 +1321,7 @@ protocol extensions).";

   "zero", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "send write zeroes to the NBD server";
     longdesc = "\
@@ -1347,7 +1348,7 @@ punching a hole.";
                                   UInt64 "offset";
                                   ArrayAndLen (UInt32 "entries",
                                                "nr_entries")]);
-             UInt32 "flags" ];
+             OptUInt32 "flags" ];
     ret = RErr;
     shortdesc = "read the block status of the given range";
     longdesc = "\
@@ -1477,7 +1478,7 @@ on the connection.";
   "aio_pread", {
     default_call with
     args = [ BytesPersistOut ("buf", "count"); UInt64 "offset";
-             UInt32 "flags" ];
+             OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "read from the NBD server";
     longdesc = "\
@@ -1491,7 +1492,8 @@ parameters behave as documented in C<nbd_pread>.";

   "aio_pwrite", {
     default_call with
-    args = [ BytesPersistIn ("buf", "count"); UInt64 "offset"; UInt32 "flags" ];
+    args = [ BytesPersistIn ("buf", "count"); UInt64 "offset";
+             OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "write to the NBD server";
     longdesc = "\
@@ -1505,7 +1507,7 @@ parameters behave as documented in C<nbd_pwrite>.";

   "aio_disconnect", {
     default_call with
-    args = [ UInt32 "flags" ]; ret = RErr;
+    args = [ OptUInt32 "flags" ]; ret = RErr;
     shortdesc = "disconnect from the NBD server";
     longdesc = "\
 Issue the disconnect command to the NBD server.  This is
@@ -1520,7 +1522,7 @@ however, C<nbd_shutdown> will call this function if appropriate.";

   "aio_flush", {
     default_call with
-    args = [ UInt32 "flags" ]; ret = RInt64;
+    args = [ OptUInt32 "flags" ]; ret = RInt64;
     shortdesc = "send flush command to the NBD server";
     longdesc = "\
 Issue the flush command to the NBD server.  This returns the
@@ -1532,7 +1534,7 @@ in C<nbd_flush>.";

   "aio_trim", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "send trim to the NBD server";
     longdesc = "\
@@ -1545,7 +1547,7 @@ in C<nbd_trim>.";

   "aio_cache", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "send cache (prefetch) to the NBD server";
     longdesc = "\
@@ -1558,7 +1560,7 @@ in C<nbd_cache>.";

   "aio_zero", {
     default_call with
-    args = [ UInt64 "count"; UInt64 "offset"; UInt32 "flags" ];
+    args = [ UInt64 "count"; UInt64 "offset"; OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "send write zeroes to the NBD server";
     longdesc = "\
@@ -1577,7 +1579,7 @@ in C<nbd_zero>.";
                                   UInt64 "offset";
                                   ArrayAndLen (UInt32 "entries",
                                                "nr_entries")]);
-             UInt32 "flags" ];
+             OptUInt32 "flags" ];
     ret = RInt64;
     shortdesc = "send block status to the NBD server";
     longdesc = "\
@@ -2514,6 +2516,7 @@ let rec name_of_arg = function
 | Int n -> [n]
 | Int64 n -> [n]
 | Opaque n -> [n]
+| OptUInt32 n -> [n]
 | Path n -> [n]
 | SockAddrAndLen (n, len) -> [n; len]
 | String n -> [n]
@@ -2545,6 +2548,7 @@ let rec print_c_arg_list ?(handle = false) args =
       | Int n -> pr "int %s" n
       | Int64 n -> pr "int64_t %s" n
       | Opaque n -> pr "void *%s" n
+      | OptUInt32 n -> pr "uint32_t %s" n
       | Path n
       | String n -> pr "const char *%s" n
       | StringList n -> pr "char **%s" n
@@ -2945,8 +2949,8 @@ let print_python_binding name { args; ret } =
          (* The following not yet implemented for callbacks XXX *)
          | ArrayAndLen _ | Bool _ | BytesIn _ | BytesOut _
          | BytesPersistIn _ | BytesPersistOut _ | Callback _
-         | Int _ | Int64 _ | Path _ | SockAddrAndLen _ | StringList _
-         | UInt _ | UInt32 _ -> pr "  abort ();\n"
+         | Int _ | Int64 _ | OptUInt32 _ | Path _ | SockAddrAndLen _
+         | StringList _ | UInt _ | UInt32 _ -> pr "  abort ();\n"
        ) args;
        pr "\n";

@@ -2960,8 +2964,8 @@ let print_python_binding name { args; ret } =
          (* The following not yet implemented for callbacks XXX *)
          | ArrayAndLen _ | Bool _ | BytesIn _ | BytesOut _
          | BytesPersistIn _ | BytesPersistOut _ | Callback _
-         | Int _ | Int64 _ | Path _ | SockAddrAndLen _ | StringList _
-         | UInt _ | UInt32 _ -> pr "  abort ();\n"
+         | Int _ | Int64 _ | OptUInt32 _ | Path _ | SockAddrAndLen _
+         | StringList _ | UInt _ | UInt32 _ -> pr "  abort ();\n"
        ) args;
        pr " \")\"";
        List.iter (
@@ -2973,8 +2977,8 @@ let print_python_binding name { args; ret } =
          (* The following not yet implemented for callbacks XXX *)
          | ArrayAndLen _ | Bool _ | BytesIn _ | BytesOut _
          | BytesPersistIn _ | BytesPersistOut _ | Callback _
-         | Int _ | Int64 _ | Path _ | SockAddrAndLen _ | StringList _
-         | UInt _ | UInt32 _ -> pr "  abort ();\n"
+         | Int _ | Int64 _ | OptUInt32 _ | Path _ | SockAddrAndLen _
+         | StringList _ | UInt _ | UInt32 _ -> pr "  abort ();\n"
        ) args;
        pr ");\n";
        pr "  Py_INCREF (py_args);\n";
@@ -3004,8 +3008,8 @@ let print_python_binding name { args; ret } =
          (* The following not yet implemented for callbacks XXX *)
          | ArrayAndLen _ | Bool _ | BytesIn _ | BytesOut _
          | BytesPersistIn _ | BytesPersistOut _ | Callback _
-         | Int _ | Int64 _ | Path _ | SockAddrAndLen _ | StringList _
-         | UInt _ | UInt32 _ -> ()
+         | Int _ | Int64 _ | OptUInt32 _ | Path _ | SockAddrAndLen _
+         | StringList _ | UInt _ | UInt32 _ -> ()
        ) args;
        pr "}\n";
        pr "\n"
@@ -3054,6 +3058,9 @@ let print_python_binding name { args; ret } =
        pr "  long long %s; /* really int64_t */\n" n
     | Opaque n ->
        pr "  PyObject *%s;\n" n
+    | OptUInt32 n ->
+       pr "  uint32_t %s_u32;\n" n;
+       pr "  unsigned int %s = 0; /* really uint32_t */\n" n
     | Path n -> pr "  char *%s = NULL;\n" n
     | SockAddrAndLen (n, _) ->
        pr "  /* XXX Complicated - Python uses a tuple of different\n";
@@ -3088,6 +3095,7 @@ let print_python_binding name { args; ret } =
     | Int n -> pr " \"i\""
     | Int64 n -> pr " \"L\""
     | Opaque _ -> pr " \"O\""
+    | OptUInt32 n -> pr " \"|I\""
     | Path n -> pr " \"O&\""
     | SockAddrAndLen (n, _) -> pr " \"O\""
     | String n -> pr " \"s\""
@@ -3111,6 +3119,7 @@ let print_python_binding name { args; ret } =
     | Int n -> pr ", &%s" n
     | Int64 n -> pr ", &%s" n
     | Opaque n -> pr ", &%s" n
+    | OptUInt32 n -> pr ", &%s" n
     | Path n -> pr ", PyUnicode_FSConverter, &%s" n
     | SockAddrAndLen (n, _) -> pr ", &%s" n
     | String n -> pr ", &%s" n
@@ -3160,6 +3169,7 @@ let print_python_binding name { args; ret } =
     | Int64 n -> pr "  %s_i64 = %s;\n" n n
     | Opaque n ->
        pr "  callback_data.data = %s;\n" n
+    | OptUInt32 n -> pr "  %s_u32 = %s;\n" n n
     | Path _ -> ()
     | SockAddrAndLen _ ->
        pr "  abort (); /* XXX SockAddrAndLen not implemented */\n";
@@ -3187,6 +3197,7 @@ let print_python_binding name { args; ret } =
     | Int n -> pr ", %s" n
     | Int64 n -> pr ", %s_i64" n
     | Opaque _ -> pr ", &callback_data"
+    | OptUInt32 n -> pr ", %s_u32" n
     | Path n -> pr ", %s" n
     | SockAddrAndLen (n, _) -> pr ", /* XXX */ (void *) %s, 0" n
     | String n -> pr ", %s" n
@@ -3220,6 +3231,7 @@ let print_python_binding name { args; ret } =
     | Int _
     | Int64 _
     | Opaque _
+    | OptUInt32 _
     | Path _
     | SockAddrAndLen _
     | String _
@@ -3261,6 +3273,7 @@ let print_python_binding name { args; ret } =
     | Int _ -> ()
     | Int64 _ -> ()
     | Opaque _ -> ()
+    | OptUInt32 _ -> ()
     | Path n -> pr "  free (%s);\n" n
     | SockAddrAndLen _ -> ()
     | String n -> ()
@@ -3304,7 +3317,7 @@ Python bindings for libnbd
 import nbd
 h = nbd.NBD ()
 h.connect_tcp (\"localhost\", \"nbd\")
-buf = h.pread (512, 0, 0)
+buf = h.pread (512, 0)

 Read the libnbd(3) man page to find out how to use the API.
 '''
@@ -3344,6 +3357,29 @@ class NBD (object):

   List.iter (
     fun (name, { args; shortdesc; longdesc }) ->
+      let params = List.map (
+                     function
+                     | ArrayAndLen (UInt32 n, _) -> n
+                     | ArrayAndLen _ -> assert false
+                     | Bool n -> n
+                     | BytesIn (n, _) | BytesPersistIn (n, _) -> n
+                     | BytesPersistOut (n, _) -> n
+                     | BytesOut (_, count) -> count
+                     | Callback (n, _) -> n
+                     | Int n -> n
+                     | Int64 n -> n
+                     | Opaque n -> n
+                     | OptUInt32 n -> n
+                     | Path n -> n
+                     | SockAddrAndLen (n, _) -> n
+                     | String n -> n
+                     | StringList n -> n
+                     | UInt n -> n
+                     | UInt32 n -> n
+                     | UInt64 n -> n
+                   ) args in
+      let params = List.map ((^) ", ") params in
+      let params = String.concat "" params in
       let args = List.map (
                      function
                      | ArrayAndLen (UInt32 n, _) -> n
@@ -3356,6 +3392,7 @@ class NBD (object):
                      | Int n -> n
                      | Int64 n -> n
                      | Opaque n -> n
+                     | OptUInt32 n -> n ^ " = 0"
                      | Path n -> n
                      | SockAddrAndLen (n, _) -> n
                      | String n -> n
@@ -3371,7 +3408,7 @@ class NBD (object):
       let longdesc = pod2text longdesc in
       pr "    def %s (self%s):\n" name args;
       pr "        '''▶ %s\n\n%s'''\n" shortdesc (String.concat "\n" longdesc);
-      pr "        return libnbdmod.%s (self._o%s)\n" name args;
+      pr "        return libnbdmod.%s (self._o%s)\n" name params;
       pr "\n"
   ) handle_calls;

@@ -3405,7 +3442,7 @@ is an open NBD handle called ‘h’.

 h.connect_tcp (\"remote\", \"10809\")   # Connect to a remote server.
 h.get_size ()                       # Get size of the remote disk.
-buf = h.pread (512, 0, 0)           # Read the first sector.
+buf = h.pread (512, 0)              # Read the first sector.
 exit() or Ctrl-D                    # Quit the shell
 help (nbd)                          # Display documentation
 '''
diff --git a/python/t/400-pread.py b/python/t/400-pread.py
index da4799b..4d64166 100644
--- a/python/t/400-pread.py
+++ 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)

 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)
-buf2 = h.pread (512, 0, 0)
+buf2 = h.pread (512, 0)

 assert buf1 == buf2

diff --git a/python/t/500-aio-pread.py b/python/t/500-aio-pread.py
index 82199ef..85342f5 100644
--- a/python/t/500-aio-pread.py
+++ b/python/t/500-aio-pread.py
@@ -21,7 +21,7 @@ h = nbd.NBD ()
 h.connect_command (["nbdkit", "-s", "--exit-with-parent", "-v",
                     "pattern", "size=512"])
 buf = nbd.aio_buffer (512)
-id = h.aio_pread (buf, 0, 0)
+id = h.aio_pread (buf, 0)
 while not (h.aio_command_completed (id)):
     h.poll (-1)

diff --git a/python/t/510-aio-pwrite.py b/python/t/510-aio-pwrite.py
index b73464b..4770b83 100644
--- a/python/t/510-aio-pwrite.py
+++ b/python/t/510-aio-pwrite.py
@@ -39,7 +39,7 @@ while not (h.aio_command_completed (id)):
     h.poll (-1)

 buf2 = nbd.aio_buffer (512)
-id = h.aio_pread (buf2, 0, 0)
+id = h.aio_pread (buf2, 0)
 while not (h.aio_command_completed (id)):
     h.poll (-1)

-- 
2.20.1




More information about the Libguestfs mailing list