[Libguestfs] [nbdkit PATCH 2/2] python: Simplify calling into plugin

Eric Blake eblake at redhat.com
Sat Apr 7 11:50:29 UTC 2018


On 04/06/2018 05:24 PM, Eric Blake wrote:
> PyObject_CallObject is powerful, but awkward - we have to wrap
> all arguments into a temporary tuple before using it.
> 
> Let python do more of the work by using PyObject_CallFunction
> anywhere that all arguments can be described by a Py_BuildValue()
> format string, instead of creating one-shot arguments ourselves.
> In fact, for our py_config(), this makes it easier to not worry
> about python 2 String vs. python 3 Unicode.
> 
> Similarly, PyObject_CallFunctionObjArgs is nicer when we
> already have PyObjects for all parameters (including in py_open(),
> where we can't use a Py_BuildValue() string for converting a
> C int into Py_True or Py_False, but can easily avoid the tuple
> wrapper).
> 

> @@ -436,20 +406,15 @@ py_pwrite (void *handle, const void *buf,
>  {
>    PyObject *obj = handle;
>    PyObject *fn;
> -  PyObject *args;
>    PyObject *r;
> 
>    if (callback_defined ("pwrite", &fn)) {
>      PyErr_Clear ();
> 
> -    args = PyTuple_New (3);
> -    Py_INCREF (obj); /* decremented by Py_DECREF (args) */
> -    PyTuple_SetItem (args, 0, obj);
> -    PyTuple_SetItem (args, 1, PyByteArray_FromStringAndSize (buf, count));
> -    PyTuple_SetItem (args, 2, PyLong_FromUnsignedLongLong (offset));
> -    r = PyObject_CallObject (fn, args);
> +    r = PyObject_CallFunction (fn, "OOL", obj,
> +                               PyByteArray_FromStringAndSize (buf, count),
> +                               offset, NULL);

This leaks the PyByteArray object.  Will fix.

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

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


More information about the Libguestfs mailing list