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 <eblake@redhat.com> --- 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