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

2018-04-09 Thread Eric Blake
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).
> 
> py_zero() is not converted, as it will be changed differently
> to make 'may_trim' an optional callback parameter.
> 
> Signed-off-by: Eric Blake 
> ---
>  plugins/python/python.c | 97 
> -
>  1 file changed, 15 insertions(+), 82 deletions(-)
> 

> @@ -258,17 +257,8 @@ py_config (const char *key, const char *value)
>  /* Other parameters are passed to the Python .config callback. */
>  PyErr_Clear ();
> 
> -args = PyTuple_New (2);

In fact, this patch is a corner-case bug fix.  Here, and in all sorts of
similar places, we are failing to check for args == NULL (unlikely
except on OOM conditions, but still a valid error condition, where we
would presumably have a nice Python error object to report failure with)...

> -#ifdef HAVE_PYSTRING_FROMSTRING
> -PyTuple_SetItem (args, 0, PyString_FromString (key));

...and instead passing NULL on to a function that would crash.  We are
likewise not checking for errors here (although such errors should never
be possible).

> -PyTuple_SetItem (args, 1, PyString_FromString (value));
> -#else
> -PyTuple_SetItem (args, 0, PyUnicode_FromString (key));
> -PyTuple_SetItem (args, 1, PyUnicode_FromString (value));
> -#endif
> -r = PyObject_CallObject (fn, args);
> +r = PyObject_CallFunction (fn, "ss", key, value);

The replacement code is not only shorter, it has fewer places to worry
about corner-case boilerplate error checking.

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



signature.asc
Description: OpenPGP digital signature
___
Libguestfs mailing list
Libguestfs@redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs

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

2018-04-07 Thread Eric Blake
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



signature.asc
Description: OpenPGP digital signature
___
Libguestfs mailing list
Libguestfs@redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs

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

2018-04-06 Thread Eric Blake
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).

py_zero() is not converted, as it will be changed differently
to make 'may_trim' an optional callback parameter.

Signed-off-by: Eric Blake 
---
 plugins/python/python.c | 97 -
 1 file changed, 15 insertions(+), 82 deletions(-)

diff --git a/plugins/python/python.c b/plugins/python/python.c
index c5cc4bd..5ece97a 100644
--- a/plugins/python/python.c
+++ b/plugins/python/python.c
@@ -209,7 +209,6 @@ py_config (const char *key, const char *value)
   FILE *fp;
   PyObject *modname;
   PyObject *fn;
-  PyObject *args;
   PyObject *r;

   if (!script) {
@@ -258,17 +257,8 @@ py_config (const char *key, const char *value)
 /* Other parameters are passed to the Python .config callback. */
 PyErr_Clear ();

-args = PyTuple_New (2);
-#ifdef HAVE_PYSTRING_FROMSTRING
-PyTuple_SetItem (args, 0, PyString_FromString (key));
-PyTuple_SetItem (args, 1, PyString_FromString (value));
-#else
-PyTuple_SetItem (args, 0, PyUnicode_FromString (key));
-PyTuple_SetItem (args, 1, PyUnicode_FromString (value));
-#endif
-r = PyObject_CallObject (fn, args);
+r = PyObject_CallFunction (fn, "ss", key, value);
 Py_DECREF (fn);
-Py_DECREF (args);
 if (check_python_failure ("config") == -1)
   return -1;
 Py_DECREF (r);
@@ -306,7 +296,6 @@ static void *
 py_open (int readonly)
 {
   PyObject *fn;
-  PyObject *args;
   PyObject *handle;

   if (!callback_defined ("open", &fn)) {
@@ -316,11 +305,9 @@ py_open (int readonly)

   PyErr_Clear ();

-  args = PyTuple_New (1);
-  PyTuple_SetItem (args, 0, PyBool_FromLong (readonly));
-  handle = PyObject_CallObject (fn, args);
+  handle = PyObject_CallFunctionObjArgs (fn, readonly ? Py_True : Py_False,
+ NULL);
   Py_DECREF (fn);
-  Py_DECREF (args);
   if (check_python_failure ("open") == -1)
 return NULL;

@@ -332,18 +319,13 @@ py_close (void *handle)
 {
   PyObject *obj = handle;
   PyObject *fn;
-  PyObject *args;
   PyObject *r;

   if (callback_defined ("close", &fn)) {
 PyErr_Clear ();

-args = PyTuple_New (1);
-Py_INCREF (obj); /* decremented by Py_DECREF (args) */
-PyTuple_SetItem (args, 0, obj);
-r = PyObject_CallObject (fn, args);
+r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
 Py_DECREF (fn);
-Py_DECREF (args);
 check_python_failure ("close");
 Py_XDECREF (r);
   }
@@ -356,7 +338,6 @@ py_get_size (void *handle)
 {
   PyObject *obj = handle;
   PyObject *fn;
-  PyObject *args;
   PyObject *r;
   int64_t ret;

@@ -367,12 +348,8 @@ py_get_size (void *handle)

   PyErr_Clear ();

-  args = PyTuple_New (1);
-  Py_INCREF (obj); /* decremented by Py_DECREF (args) */
-  PyTuple_SetItem (args, 0, obj);
-  r = PyObject_CallObject (fn, args);
+  r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
   Py_DECREF (fn);
-  Py_DECREF (args);
   if (check_python_failure ("get_size") == -1)
 return -1;

@@ -390,7 +367,6 @@ py_pread (void *handle, void *buf,
 {
   PyObject *obj = handle;
   PyObject *fn;
-  PyObject *args;
   PyObject *r;

   if (!callback_defined ("pread", &fn)) {
@@ -400,14 +376,8 @@ py_pread (void *handle, void *buf,

   PyErr_Clear ();

-  args = PyTuple_New (3);
-  Py_INCREF (obj); /* decremented by Py_DECREF (args) */
-  PyTuple_SetItem (args, 0, obj);
-  PyTuple_SetItem (args, 1, PyLong_FromLong (count));
-  PyTuple_SetItem (args, 2, PyLong_FromUnsignedLongLong (offset));
-  r = PyObject_CallObject (fn, args);
+  r = PyObject_CallFunction (fn, "OiL", obj, count, offset, NULL);
   Py_DECREF (fn);
-  Py_DECREF (args);
   if (check_python_failure ("pread") == -1)
 return -1;

@@ -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),
+