[PATCH 2/2 v3] Python: modify other functions to use {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 |   10 +-
 python/libvirt-override.c       |  789 ++++++++++----------------------------
 2 files changed, 213 insertions(+), 586 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 748aa17..934a832 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='int' info='returns a dictionary of params in case of success, -1 in case of error'/>
       <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)'/>
@@ -174,7 +174,7 @@
     </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='int' info='returns a dictionary of params in case of success, -1 in case of error'/>
      <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='int' info='returns a dictionary of params in case of success, -1 in case of error'/>
       <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='int' info='returns a dictionary of params in case of success, -1 in case of error'/>
       <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='int' info='returns a dictionary of params in case of success, -1 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 5f9d83e..5ef2d4a 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -304,11 +304,13 @@ 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 *ret = NULL;
     int i_retval;
-    int nparams = 0, i;
+    int nparams = 0;
     unsigned int flags;
     virTypedParameterPtr params;
     const char *path;
@@ -323,69 +325,33 @@ libvirt_virDomainBlockStatsFlags(PyObject *self ATTRIBUTE_UNUSED,
     LIBVIRT_END_ALLOW_THREADS;
 
     if (i_retval < 0)
-        return VIR_PY_NONE;
+        return VIR_PY_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        free(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    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:
-            free(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
-
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
 
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(info);
+    return info;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return ret;
 }
 
-
 static PyObject *
 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     virDomainPtr domain;
@@ -498,12 +464,14 @@ 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 *ret = NULL;
     char *c_retval;
     int i_retval;
-    int nparams, i;
+    int nparams = 0;
     virTypedParameterPtr params;
 
     if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
@@ -516,75 +484,44 @@ libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
     LIBVIRT_END_ALLOW_THREADS;
 
     if (c_retval == NULL)
-        return VIR_PY_NONE;
+        return VIR_PY_INT_FAIL;
     free(c_retval);
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        free(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
-    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:
-            free(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(info);
+    return info;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return ret;
 }
 
 static PyObject *
 libvirt_virDomainGetSchedulerParametersFlags(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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -598,75 +535,44 @@ libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
     LIBVIRT_END_ALLOW_THREADS;
 
     if (c_retval == NULL)
-        return VIR_PY_NONE;
+        return VIR_PY_INT_FAIL;
     free(c_retval);
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        free(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
-    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:
-            free(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(info);
+    return info;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    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;
+    int nparams = 0;
     virTypedParameterPtr params;
 
     if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
@@ -683,85 +589,48 @@ libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
     free(c_retval);
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    /* 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:
-            free(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainSetSchedulerParameters(domain, params, nparams);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (i_retval < 0) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
+    virTypedParameterArrayClear(params, nparams);
     free(params);
     return VIR_PY_INT_SUCCESS;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -780,85 +649,47 @@ libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
     free(c_retval);
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    /* 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:
-            free(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
-    i_retval = virDomainSetSchedulerParametersFlags(domain, params, nparams, flags);
+    i_retval = virDomainSetSchedulerParametersFlags(domain, params, nparams, 0);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (i_retval < 0) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
+    virTypedParameterArrayClear(params, nparams);
     free(params);
     return VIR_PY_INT_SUCCESS;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -876,84 +707,48 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    /* 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:
-            free(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainSetBlkioParameters(domain, params, nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (i_retval < 0) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
+    /* The string generated by PyString_AsString
+     * must not be deallocated */
     free(params);
     return VIR_PY_INT_SUCCESS;
+fail:
+    /*same as above*/
+    free(params);
+    return ret;
 }
 
 static PyObject *
 libvirt_virDomainGetBlkioParameters(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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -966,74 +761,43 @@ libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
     i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (i_retval < 0)
-        return VIR_PY_NONE;
-
-    if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        return VIR_PY_NONE;
-
-    LIBVIRT_BEGIN_ALLOW_THREADS;
-    i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
-    LIBVIRT_END_ALLOW_THREADS;
-
-    if (i_retval < 0) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        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;
+    if (i_retval < 0)
+        return VIR_PY_INT_FAIL;
 
-        default:
-            free(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
+    if ((params = malloc(sizeof(*params)*nparams)) == NULL)
+        return PyErr_NoMemory();
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
+
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
+
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(info);
+    return info;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -1051,84 +815,47 @@ libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    /* 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:
-            free(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     i_retval = virDomainSetMemoryParameters(domain, params, nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (i_retval < 0) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
+    virTypedParameterArrayClear(params, nparams);
     free(params);
     return VIR_PY_INT_SUCCESS;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return ret;
 }
 
 static PyObject *
 libvirt_virDomainGetMemoryParameters(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;
     unsigned int flags;
     virTypedParameterPtr params;
 
@@ -1142,64 +869,32 @@ libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
     LIBVIRT_END_ALLOW_THREADS;
 
     if (i_retval < 0)
-        return VIR_PY_NONE;
+        return VIR_PY_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((info = PyDict_New()) == NULL) {
-        free(params);
-        return VIR_PY_NONE;
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
-    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;
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
 
-        default:
-            free(params);
-            Py_DECREF(info);
-            return VIR_PY_NONE;
-        }
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return info;
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(info, key, val);
-    }
+fail:
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(info);
+    return ret;
 }
 
 static PyObject *
@@ -3625,93 +3320,56 @@ libvirt_virDomainSetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
                                 PyObject *args)
 {
     virDomainPtr domain;
-    PyObject *pyobj_domain, *pyinfo;
+    PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
     const char *disk;
+    int i_retval;
+    int nparams = 0;
     unsigned int flags;
     virTypedParameterPtr params;
-    int nparams = 0, i;
-    int c_ret;
 
     if (!PyArg_ParseTuple(args, (char *)"OzOi:virDomainSetBlockIoTune",
-                          &pyobj_domain, &disk, &pyinfo, &flags))
+                          &pyobj_domain, &disk, &info, &flags))
         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_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
 
-    /* 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:
-            free(params);
-            return VIR_PY_INT_FAIL;
-        }
-    }
+    if (!setPyVirTypedParameter(info, params, nparams))
+        goto fail;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
-    c_ret = virDomainSetBlockIoTune(domain, disk, params, nparams, flags);
+    i_retval = virDomainSetBlockIoTune(domain, disk, params, nparams, flags);
     LIBVIRT_END_ALLOW_THREADS;
 
-    if (c_ret < 0) {
-        free(params);
-        return VIR_PY_INT_FAIL;
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
-
+    virTypedParameterArrayClear(params, nparams);
     free(params);
     return VIR_PY_INT_SUCCESS;
+fail:
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return ret;
 }
 
 static PyObject *
@@ -3719,12 +3377,13 @@ libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
                                 PyObject *args)
 {
     virDomainPtr domain;
-    PyObject *pyobj_domain, *pyreply;
+    PyObject *pyobj_domain, *info;
+    PyObject *ret = NULL;
     const char *disk;
-    int nparams = 0, i;
+    int i_retval;
+    int nparams = 0;
     unsigned int flags;
     virTypedParameterPtr params;
-    int c_ret;
 
     if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetBlockIoTune",
                           &pyobj_domain, &disk, &flags))
@@ -3732,68 +3391,36 @@ libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
     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)
-        return VIR_PY_NONE;
+    if (i_retval < 0)
+        return VIR_PY_INT_FAIL;
 
     if ((params = malloc(sizeof(*params)*nparams)) == NULL)
-        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) {
-        free(params);
-        return VIR_PY_NONE;
-    }
-
-    /* convert to a Python tuple of long objects */
-    if ((pyreply = PyDict_New()) == NULL) {
-        free(params);
-        return VIR_PY_NONE;
+    if (i_retval < 0) {
+        ret = VIR_PY_INT_FAIL;
+        goto fail;
     }
-    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;
+    info = getPyVirTypedParameter(params, nparams);
+    if (!info)
+        goto fail;
 
-        default:
-            free(params);
-            Py_DECREF(pyreply);
-            return VIR_PY_NONE;
-        }
+    virTypedParameterArrayClear(params, nparams);
+    free(params);
+    return info;
 
-        key = libvirt_constcharPtrWrap(params[i].field);
-        PyDict_SetItem(pyreply, key, val);
-    }
+fail:
+    virTypedParameterArrayClear(params, nparams);
     free(params);
-    return(pyreply);
+    return ret;
 }
 
 /*******************************************
-- 
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]