[PATCH] python: make other APIs share common {get, set}PyVirTypedParameter

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

 



        *libvirt_virDomainBlockStatsFlags
        *libvirt_virDomainGetSchedulerParameters
        *libvirt_virDomainGetSchedulerParametersFlags
        *libvirt_virDomainSetSchedulerParameters
        *libvirt_virDomainSetSchedulerParametersFlags
        *libvirt_virDomainSetBlkioParameters
        *libvirt_virDomainGetBlkioParameters
        *libvirt_virDomainSetMemoryParameters
        *libvirt_virDomainGetMemoryParameters
        *libvirt_virDomainSetBlockIoTune
        *libvirt_virDomainGetBlockIoTune
---
 python/libvirt-override-api.xml |   12 +-
 python/libvirt-override.c       |  885 +++++++++++++--------------------------
 2 files changed, 295 insertions(+), 602 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 8eed0bb..563dd46 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -144,7 +144,7 @@
     </function>
     <function name='virDomainBlockStatsFlags' file='python'>
       <info>Extracts block device statistics parameters of a running domain</info>
-      <return type='virTypedParameterPtr' info='None in case of error, returns a dictionary of params'/>
+      <return type='str *' info='None in case of error, returns a dictionary of params'/>
       <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
       <arg name='path' type='char *' info='the path for the block device'/>
       <arg name='flags' type='int' info='flags (unused; pass 0)'/>
@@ -169,12 +169,12 @@
     </function>
     <function name='virDomainGetSchedulerParameters' file='python'>
       <info>Get the scheduler parameters, the @params array will be filled with the values.</info>
-      <return type='int' info='-1 in case of error, 0 in case of success.'/>
+      <return type='str *' info='None in case of error, returns a dictionary of params.'/>
       <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
     </function>
     <function name='virDomainGetSchedulerParametersFlags' file='python'>
       <info>Get the scheduler parameters</info>
-     <return type='virSchedParameterPtr' info='None in case of error, returns a dictionary of params'/>
+     <return type='str *' info='None in case of error, returns a dictionary of params'/>
      <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
      <arg name='flags' type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
     </function>
@@ -231,7 +231,7 @@
     </function>
     <function name='virDomainGetBlkioParameters' file='python'>
       <info>Get the blkio parameters</info>
-      <return type='virSchedParameterPtr' info='None in case of error, returns a dictionary of params'/>
+      <return type='str *' info='None in case of error, returns a dictionary of params'/>
       <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
       <arg name='flags' type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
     </function>
@@ -244,7 +244,7 @@
     </function>
     <function name='virDomainGetMemoryParameters' file='python'>
       <info>Get the memory parameters</info>
-      <return type='virSchedParameterPtr' info='None in case of error, returns a dictionary of params'/>
+      <return type='str *' info='None in case of error, returns a dictionary of params'/>
       <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
       <arg name='flags' type='int' info='an OR&apos;ed set of virDomainModificationImpact'/>
     </function>
@@ -415,7 +415,7 @@
       <arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
       <arg name='disk' type='const char *' info='disk name'/>
       <arg name='flags' type='unsigned int' info='an OR&apos;ed set of virDomainModificationImpact'/>
-      <return type='virTypedParameterPtr' info='the I/O tunables value or None in case of error'/>
+      <return type='str *' info='the I/O tunables value or None in case of error'/>
     </function>
     <function name='virDomainBlockPeek' file='python'>
       <info>Read the contents of domain's disk device</info>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index e7c2bd5..3ffdb62 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -330,18 +330,20 @@ libvirt_virDomainBlockStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
 
 static PyObject *
 libvirt_virDomainBlockStatsFlags(PyObject *self ATTRIBUTE_UNUSED,
-                                 PyObject *args) {
+                                 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;
     const char *path;
 
     if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainBlockStatsFlags",
                           &pyobj_domain, &path, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -351,67 +353,29 @@ libvirt_virDomainBlockStatsFlags(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 = virDomainBlockStatsFlags(domain, path, 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;
-        }
-
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+    ret = getPyVirTypedParameter(params, nparams);
 
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
     VIR_FREE(params);
-    return(info);
+    return ret;
 }
 
-
 static PyObject *
 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     virDomainPtr domain;
@@ -530,17 +494,19 @@ libvirt_virDomainGetSchedulerType(PyObject *self ATTRIBUTE_UNUSED,
 
 static PyObject *
 libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
-                                        PyObject *args) {
+                                        PyObject *args)
+{
     virDomainPtr domain;
-    PyObject *pyobj_domain, *info;
+    PyObject *pyobj_domain;
+    PyObject *ret = NULL;
     char *c_retval;
     int i_retval;
-    int nparams, i;
+    int nparams = 0;
     virTypedParameterPtr params;
 
     if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
                           &pyobj_domain))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -551,78 +517,45 @@ libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
     VIR_FREE(c_retval);
 
+    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 = virDomainGetSchedulerParameters(domain, params, &nparams);
     LIBVIRT_END_ALLOW_THREADS;
 
     if (i_retval < 0) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_NONE;
+        goto cleanup;
     }
 
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
-    }
-    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 *
 libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
-                                        PyObject *args) {
+                                        PyObject *args)
+{
     virDomainPtr domain;
-    PyObject *pyobj_domain, *info;
+    PyObject *pyobj_domain;
+    PyObject *ret = NULL;
     char *c_retval;
     int i_retval;
-    int nparams, i;
+    int nparams = 0;
     unsigned int flags;
     virTypedParameterPtr params;
 
     if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetScedulerParametersFlags",
                           &pyobj_domain, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -633,79 +566,55 @@ libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
     VIR_FREE(c_retval);
 
+    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 = virDomainGetSchedulerParametersFlags(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 *
 libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
-                                        PyObject *args) {
+                                        PyObject *args)
+{
     virDomainPtr domain;
     PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
     char *c_retval;
     int i_retval;
-    int nparams, i;
-    virTypedParameterPtr params;
+    int nparams = 0, size = 0;
+    virTypedParameterPtr params, new_params;
 
     if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
                           &pyobj_domain, &info))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Need non-empty dictionary to set attributes");
+        return NULL;
+    }
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
     c_retval = virDomainGetSchedulerType(domain, &nparams);
     LIBVIRT_END_ALLOW_THREADS;
@@ -714,95 +623,74 @@ libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_INT_FAIL;
     VIR_FREE(c_retval);
 
+    if (nparams == 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 = virDomainGetSchedulerParameters(domain, params, &nparams);
     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 = virDomainSetSchedulerParameters(domain, params, nparams);
+    i_retval = virDomainSetSchedulerParameters(domain, new_params, size);
     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_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
-                                             PyObject *args) {
+                                             PyObject *args)
+{
     virDomainPtr domain;
     PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
     char *c_retval;
     int i_retval;
-    int nparams, i;
+    int nparams = 0, size = 0;
     unsigned int flags;
-    virTypedParameterPtr params;
+    virTypedParameterPtr params, new_params;
 
     if (!PyArg_ParseTuple(args,
                           (char *)"OOi:virDomainSetScedulerParametersFlags",
                           &pyobj_domain, &info, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Need non-empty dictionary to set attributes");
+        return NULL;
+    }
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
     c_retval = virDomainGetSchedulerType(domain, &nparams);
     LIBVIRT_END_ALLOW_THREADS;
@@ -811,95 +699,73 @@ libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_INT_FAIL;
     VIR_FREE(c_retval);
 
+    if (nparams == 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 = virDomainGetSchedulerParametersFlags(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 = virDomainSetSchedulerParametersFlags(domain, params, nparams, flags);
+    i_retval = virDomainSetSchedulerParametersFlags(domain, new_params, size, 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_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
-                                     PyObject *args) {
+                                    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",
                           &pyobj_domain, &info, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Need non-empty dictionary to set attributes");
+        return NULL;
+    }
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
@@ -907,91 +773,61 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (i_retval < 0)
         return VIR_PY_INT_FAIL;
 
+    if (nparams == 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);
+    i_retval = virDomainSetBlkioParameters(domain, new_params, size, 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) {
+                                    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;
 
     if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetBlkioParameters",
                           &pyobj_domain, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1001,80 +837,56 @@ 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 *
 libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
-                                     PyObject *args) {
+                                     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:virDomainSetMemoryParameters",
                           &pyobj_domain, &info, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Need non-empty dictionary to set attributes");
+        return NULL;
+    }
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainGetMemoryParameters(domain, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
@@ -1082,91 +894,61 @@ libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (i_retval < 0)
         return VIR_PY_INT_FAIL;
 
+    if (nparams == 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 = virDomainGetMemoryParameters(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 = virDomainSetMemoryParameters(domain, params, nparams, flags);
+    i_retval = virDomainSetMemoryParameters(domain, new_params, size, 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_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
-                                     PyObject *args) {
+                                     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;
 
     if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetMemoryParameters",
                           &pyobj_domain, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1176,62 +958,27 @@ libvirt_virDomainGetMemoryParameters(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 = virDomainGetMemoryParameters(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 *
@@ -3650,93 +3397,73 @@ libvirt_virDomainSetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
                                 PyObject *args)
 {
     virDomainPtr domain;
-    PyObject *pyobj_domain, *pyinfo;
+    PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
+    int i_retval;
+    int nparams = 0, size = 0;
     const char *disk;
     unsigned int flags;
-    virTypedParameterPtr params;
-    int nparams = 0, i;
-    int c_ret;
+    virTypedParameterPtr params, new_params;
 
     if (!PyArg_ParseTuple(args, (char *)"OzOi:virDomainSetBlockIoTune",
-                          &pyobj_domain, &disk, &pyinfo, &flags))
-        return(NULL);
+                          &pyobj_domain, &disk, &info, &flags))
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
+    if ((size = PyDict_Size(info)) < 0)
+        return NULL;
+
+    if (size == 0) {
+        PyErr_Format(PyExc_LookupError,
+                     "Need non-empty dictionary to set attributes");
+        return NULL;
+    }
+
     LIBVIRT_BEGIN_ALLOW_THREADS;
-    c_ret = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
+    i_retval = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0)
+    if (i_retval < 0)
         return VIR_PY_INT_FAIL;
 
+    if (nparams == 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;
-    c_ret = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
+    i_retval = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0) {
-        VIR_FREE(params);
-        return VIR_PY_INT_FAIL;
+    if (i_retval < 0) {
+        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(pyinfo, 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:
-            {
-                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;
-    c_ret = virDomainSetBlockIoTune(domain, disk, params, nparams, flags);
+    i_retval = virDomainSetBlockIoTune(domain, disk, new_params, size, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0) {
-        VIR_FREE(params);
-        return VIR_PY_INT_FAIL;
+    if (i_retval < 0) {
+        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 *
@@ -3744,81 +3471,47 @@ libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
                                 PyObject *args)
 {
     virDomainPtr domain;
-    PyObject *pyobj_domain, *pyreply;
+    PyObject *pyobj_domain;
+    PyObject *ret = NULL;
+    int i_retval;
+    int nparams = 0;
     const char *disk;
-    int nparams = 0, i;
     unsigned int flags;
     virTypedParameterPtr params;
-    int c_ret;
 
     if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetBlockIoTune",
                           &pyobj_domain, &disk, &flags))
-        return(NULL);
+        return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
-    c_ret = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
+    i_retval = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0)
+    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;
-    c_ret = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
+    i_retval = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((pyreply = PyDict_New()) == NULL) {
-        VIR_FREE(params);
-        return VIR_PY_NONE;
+    if (i_retval < 0) {
+        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((unsigned 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(pyreply);
-            return VIR_PY_NONE;
-        }
+    ret = getPyVirTypedParameter(params, nparams);
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(pyreply, key, val);
-    }
+cleanup:
+    virTypedParameterArrayClear(params, nparams);
     VIR_FREE(params);
-    return(pyreply);
+    return ret;
 }
 
 static PyObject *
-- 
1.7.7.5

--
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]