[Libguestfs] [PATCH nbdkit v2 03/10] python: Implement nbdkit API version 2.

Nir Soffer nsoffer at redhat.com
Fri Nov 22 22:30:38 UTC 2019


On Fri, Nov 22, 2019 at 11:53 PM Nir Soffer <nsoffer at redhat.com> wrote:
>
> On Fri, Nov 22, 2019 at 11:35 PM Eric Blake <eblake at redhat.com> wrote:
> >
> > On 11/22/19 3:20 PM, Nir Soffer wrote:
> >
> > >>> +# There are several variants of the API.  nbdkit will call this
> > >>> +# function first to determine which one you want to use.  This is the
> > >>> +# latest version at the time this example was written.
> > >>> +def api_version():
> > >>> +    return 2
> > >>
> > >> Matches the C counterpart of #define NBDKIT_API_VERSION 2 at the top of
> > >> a plugin.
> > >
> > > This is the same thing I was thinking about. This makes it more clear
> > > that the api
> > > version is constant, and not something the plugin should change while
> > > it is being used.
> >
> > Hmm - api_version() really is constant for the entire life of nbdkit. We
> > call it exactly once.  Figuring out how to read a Python variable
> > instead of calling a function would be slightly more in line with the
> > fact that in C code it is a #define constant rather than a function
> > pointer callback.  But it is that much more glue code to figure out how
> > to check for a python global variable, compared to the glue code we
> > already have for calling a python function.
>
> The extra glue code is (without error handling):
>
>     PyObject *d = PyModule_GetDict(PyObject *module);
>     PyObject *v = PyDict_GetItemString(d, "API_VERSION");
>     long api_version = PyLong_AsLong(v);

Or simpler (with error handling):

    PyObject *v = PyObject_GetAttrString(m, "API_VERSION");
    if (v == NULL)
        return 1;

    long value = PyLong_AsLong(v);
    Py_DECREF(m);

    return value;

On error -1 is returned and PyErr_Occurred() will return the exception type.





More information about the Libguestfs mailing list