[PATCHv3] python: Fix problems of virDomain{Set, Get}BlkioParameters bindings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Alex Jia <ajia@xxxxxxxxxx>

The parameter 'device_weight' is a string, however, the 'VIR_TYPED_PARAM_STRING'
type condition is missed by libvirt_virDomain{Set, Get}BlkioParameters bindings,
the result is we can't get or change 'device_weight' value.

The latest python binding codes are refactored and added related 'VIR_TYPED_PARAM_STRING'
type argument process, the v3 patch follows latest codes change to resolve the above issue.

The v2 patch added missed 'VIR_TYPED_PARAM_STRING' condition into
libvirt_virDomain{Set, Get}BlkioParameters bindings and free allocated memory.
https://www.redhat.com/archives/libvir-list/2011-December/msg01122.html

RHBZ:https://bugzilla.redhat.com/show_bug.cgi?id=770795

Signed-off-by: Alex Jia <ajia@xxxxxxxxxx>
---
 python/libvirt-override.c |  142 +++++++++++++--------------------------------
 1 files changed, 40 insertions(+), 102 deletions(-)

diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index e7c2bd5..203ffa1 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -889,10 +889,11 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
                                      PyObject *args) {
     virDomainPtr domain;
     PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
     int i_retval;
-    int nparams = 0, i;
+    int nparams = 0, size = 0;
     unsigned int flags;
-    virTypedParameterPtr params;
+    virTypedParameterPtr params, new_params;
 
     if (!PyArg_ParseTuple(args,
                           (char *)"OOi:virDomainSetBlkioParameters",
@@ -900,6 +901,9 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
         return(NULL);
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
@@ -907,85 +911,54 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (i_retval < 0)
         return VIR_PY_INT_FAIL;
 
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Domain has no settable attributes");
+        return NULL;
+    }
+
     if (VIR_ALLOC_N(params, nparams) < 0)
-        return VIR_PY_INT_FAIL;
+        return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
     if (i_retval < 0) {
-        VIR_FREE(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto cleanup;
     }
 
-    /* convert to a Python tuple of long objects */
-    for (i = 0; i < nparams; i++) {
-        PyObject *key, *val;
-        key = libvirt_constcharPtrWrap(params[i].field);
-        val = PyDict_GetItem(info, key);
-        Py_DECREF(key);
-
-        if (val == NULL)
-            continue;
-
-        switch (params[i].type) {
-        case VIR_TYPED_PARAM_INT:
-            params[i].value.i = (int)PyInt_AS_LONG(val);
-            break;
-
-        case VIR_TYPED_PARAM_UINT:
-            params[i].value.ui = (unsigned int)PyInt_AS_LONG(val);
-            break;
-
-        case VIR_TYPED_PARAM_LLONG:
-            params[i].value.l = (long long)PyLong_AsLongLong(val);
-            break;
-
-        case VIR_TYPED_PARAM_ULLONG:
-            params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val);
-            break;
-
-        case VIR_TYPED_PARAM_DOUBLE:
-            params[i].value.d = (double)PyFloat_AsDouble(val);
-            break;
-
-        case VIR_TYPED_PARAM_BOOLEAN:
-            {
-                /* Hack - Python's definition of Py_True breaks strict
-                 * aliasing rules, so can't directly compare :-(
-                 */
-                PyObject *hacktrue = PyBool_FromLong(1);
-                params[i].value.b = hacktrue == val ? 1: 0;
-                Py_DECREF(hacktrue);
-            }
-            break;
-
-        default:
-            VIR_FREE(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    new_params = setPyVirTypedParameter(info, params, nparams);
+    if (!new_params)
+        goto cleanup;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainSetBlkioParameters(domain, params, nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (i_retval < 0) {
-        VIR_FREE(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto cleanup;
     }
 
+    ret = VIR_PY_INT_SUCCESS;
+
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
     VIR_FREE(params);
-    return VIR_PY_INT_SUCCESS;
+    VIR_FREE(new_params);
+    return ret;
 }
 
 static PyObject *
 libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
                                      PyObject *args) {
     virDomainPtr domain;
-    PyObject *pyobj_domain, *info;
+    PyObject *pyobj_domain;
+    PyObject *ret = NULL;
     int i_retval;
-    int nparams = 0, i;
+    int nparams = 0;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -1001,62 +974,27 @@ libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (i_retval < 0)
         return VIR_PY_NONE;
 
+    if (!nparams)
+        return PyDict_New();
+
     if (VIR_ALLOC_N(params, nparams) < 0)
-        return VIR_PY_NONE;
+        return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
     if (i_retval < 0) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_NONE;
+        goto cleanup;
     }
-    for (i = 0 ; i < nparams ; i++) {
-        PyObject *key, *val;
 
-        switch (params[i].type) {
-        case VIR_TYPED_PARAM_INT:
-            val = PyInt_FromLong((long)params[i].value.i);
-            break;
-
-        case VIR_TYPED_PARAM_UINT:
-            val = PyInt_FromLong((long)params[i].value.ui);
-            break;
-
-        case VIR_TYPED_PARAM_LLONG:
-            val = PyLong_FromLongLong((long long)params[i].value.l);
-            break;
-
-        case VIR_TYPED_PARAM_ULLONG:
-            val = PyLong_FromLongLong((long long)params[i].value.ul);
-            break;
-
-        case VIR_TYPED_PARAM_DOUBLE:
-            val = PyFloat_FromDouble((double)params[i].value.d);
-            break;
-
-        case VIR_TYPED_PARAM_BOOLEAN:
-            val = PyBool_FromLong((long)params[i].value.b);
-            break;
-
-        default:
-            VIR_FREE(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
+    ret = getPyVirTypedParameter(params, nparams);
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
     VIR_FREE(params);
-    return(info);
+    return ret;
 }
 
 static PyObject *
-- 
1.7.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]