Re: [Libguestfs] [PATCH nbdkit v2 02/10] python: Add various constants to the API.

Nir Soffer
Fri, 22 Nov 2019 20:05:15 -0000

On Fri, Nov 22, 2019 at 9:54 PM Richard W.M. Jones <rjones@redhat.com>
wrote:
>
> These are accessible from the plugin by:
>
>   import nbdkit
>
>   if flags & nbdkit.FLAG_MAY_TRIM:
>   &c.

Nice way to expose the flags!

> Many (all?) of these are not yet useful for plugins, some will never
> be useful, but they only consume a tiny bit of memory and it's nice to
> have the complete set available for future use.
> ---
>  plugins/python/python.c | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
>
> diff --git a/plugins/python/python.c b/plugins/python/python.c
> index d65ac45..69cf4e9 100644
> --- a/plugins/python/python.c
> +++ b/plugins/python/python.c
> @@ -231,6 +231,33 @@ create_nbdkit_module (void)
>      nbdkit_error ("could not create the nbdkit API module");
>      exit (EXIT_FAILURE);
>    }
> +
> +  /* Constants corresponding to various flags. */
> +  PyModule_AddIntConstant (m,
"THREAD_MODEL_SERIALIZE_CONNECTIONS",
> +                           NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS);

This can fail and needs cleanup. Very unlikely and a lot of code in
the standard library have
this issue.

In sanlock we do:

    if (PyModule_AddIntConstant(m, "LSFLAG_ADD", SANLK_LSF_ADD))
        return -1;

And the caller decref the module object and return NULL.

See
https://github.com/nirs/sanlock/blob/53f7f0284c084ac2e4542fd1f71d0406075adb5d/python/sanlock.c#L1846
https://github.com/nirs/sanlock/blob/53f7f0284c084ac2e4542fd1f71d0406075adb5d/python/sanlock.c#L1763

> +  PyModule_AddIntConstant (m,
"THREAD_MODEL_SERIALIZE_ALL_REQUESTS",
> +                           NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS);
> +  PyModule_AddIntConstant (m, "THREAD_MODEL_SERIALIZE_REQUESTS",
> +                           NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS);
> +  PyModule_AddIntConstant (m, "THREAD_MODEL_PARALLEL",
> +                           NBDKIT_THREAD_MODEL_PARALLEL);
> +
> +  PyModule_AddIntConstant (m, "FLAG_MAY_TRIM",
NBDKIT_FLAG_MAY_TRIM);
> +  PyModule_AddIntConstant (m, "FLAG_FUA",
NBDKIT_FLAG_FUA);
> +  PyModule_AddIntConstant (m, "FLAG_REQ_ONE",
NBDKIT_FLAG_REQ_ONE);
> +  PyModule_AddIntConstant (m, "FLAG_FAST_ZERO",
NBDKIT_FLAG_FAST_ZERO);
> +
> +  PyModule_AddIntConstant (m, "FUA_NONE",
NBDKIT_FUA_NONE);
> +  PyModule_AddIntConstant (m, "FUA_EMULATE",
NBDKIT_FUA_EMULATE);
> +  PyModule_AddIntConstant (m, "FUA_NATIVE",
NBDKIT_FUA_NATIVE);
> +
> +  PyModule_AddIntConstant (m, "CACHE_NONE",
NBDKIT_CACHE_NONE);
> +  PyModule_AddIntConstant (m, "CACHE_EMULATE",
NBDKIT_CACHE_EMULATE);
> +  PyModule_AddIntConstant (m, "CACHE_NATIVE",
NBDKIT_CACHE_NATIVE);
> +
> +  PyModule_AddIntConstant (m, "EXTENT_HOLE",
NBDKIT_EXTENT_HOLE);
> +  PyModule_AddIntConstant (m, "EXTENT_ZERO",
NBDKIT_EXTENT_ZERO);
> +
>    return m;
>  }
>
> --
> 2.23.0
>