[Libguestfs] [PATCH libnbd v2 1/3] python: Allow Python callbacks to auto-retire by returning an integer.

Eric Blake eblake at redhat.com
Mon Aug 12 13:52:25 UTC 2019


On 8/11/19 5:26 AM, Richard W.M. Jones wrote:
> See equivalent change for OCaml in
> commit d881d160e1cd9c9964782300a7652ffb4e506c27.
> 
> If the Python callback doesn't return something which looks like an
> integer, assume 0 instead of returning an error.
> ---
>  generator/generator | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/generator/generator b/generator/generator
> index 55c4dfc..e5d9aaa 100755
> --- a/generator/generator
> +++ b/generator/generator
> @@ -4234,7 +4234,12 @@ let print_python_binding name { args; optargs; ret; may_set_error } =
>         pr "    Py_DECREF (py_args);\n";
>         pr "\n";
>         pr "    if (py_ret != NULL) {\n";
> -       pr "      Py_DECREF (py_ret); /* return value is discarded */\n";
> +       pr "      if (PyLong_Check (py_ret))\n";
> +       pr "        ret = PyLong_AsLong (py_ret);\n";

If I'm reading the python documentation correctly, this only succeeds
for actual subtypes of PyLong (and not for other python types that have
an __int__ converter available).  But I don't find that too restricting;
'return 1' is easier to type than 'return myobject' where myobject is
not a subtype of PyLong but could otherwise convert to 1.  And even if
the user insists on using some sort of myobject for tracking the logic
on whether auto-retire is needed, they can spell longhand:
  if myobject == 1:
    return 1
  return 0
to get the desired semantics if 'return myobject' doesn't do what they want.

> +       pr "      else\n";
> +       pr "        /* If it's not a long, just assume it's 0. */\n";
> +       pr "        ret = 0;\n";

Makes sense. Thus, only an explicit return of -1 or throwing an
exception will turn into a C return of -1, only an explicit return of 1
will turn into auto-retire, and anything else makes no difference.

ACK.

-- 
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/20190812/d049e950/attachment.sig>


More information about the Libguestfs mailing list