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

[libvirt] [PATCH 2/2] python: make python APIs use these helper functions



    *libvirt_virDomainGetCPUStats
    *setPyVirTypedParameter
---
 python/libvirt-override-api.xml |    4 +-
 python/libvirt-override.c       |   62 +++++---------------------------------
 2 files changed, 11 insertions(+), 55 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 06986ec..0bafd21 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -151,11 +151,11 @@
     </function>
     <function name='virDomainGetCPUStats' file='python'>
       <info>Extracts CPU statistics for a running domain. On success it will
-   return a list of data of dictionary type. If boolean total is False, the
+   return a list of data of dictionary type. If boolean total is False or 0, the
    first element of the list refers to CPU0 on the host, second element is
    CPU1, and so on. The format of data struct is as follows:
    [{cpu_time:xxx}, {cpu_time:xxx}, ...]
-   If it is True, it returns total domain CPU statistics in the format of
+   If it is True or 1, it returns total domain CPU statistics in the format of
    [{cpu_time:xxx, user_time:xxx, system_time:xxx}]</info>
      <return type='str *' info='returns a list of dictionary in case of success, None in case of error'/>
      <arg name='domain' type='virDomainPtr' info='pointer to domain object'/>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index da1bceb..6d2e3f1 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -195,73 +195,38 @@ setPyVirTypedParameter(PyObject *info,
         switch(params[i].type) {
         case VIR_TYPED_PARAM_INT:
         {
-            long long_val = PyInt_AsLong(value);
-            if ((long_val == -1) && PyErr_Occurred())
+            if (libvirt_intUnwrap(value, &temp->value.i) < 0)
                 goto cleanup;
-            if ((int)long_val == long_val) {
-                temp->value.i = long_val;
-            } else {
-                PyErr_Format(PyExc_ValueError,
-                             "The value of "
-                             "attribute \"%s\" is out of int range", keystr);
-                goto cleanup;
-            }
         }
         break;
         case VIR_TYPED_PARAM_UINT:
         {
-            long long_val = PyInt_AsLong(value);
-            if ((long_val == -1) && PyErr_Occurred())
+            if (libvirt_uintUnwrap(value, &temp->value.ui) < 0)
                 goto cleanup;
-            if ((unsigned int)long_val == long_val) {
-                temp->value.ui = long_val;
-            } else {
-                PyErr_Format(PyExc_ValueError,
-                             "The value of "
-                             "attribute \"%s\" is out of int range", keystr);
-                goto cleanup;
-            }
         }
         break;
         case VIR_TYPED_PARAM_LLONG:
         {
-            long long llong_val = PyLong_AsLongLong(value);
-            if ((llong_val == -1) && PyErr_Occurred())
+            if (libvirt_longlongUnwrap(value, &temp->value.l) < 0)
                 goto cleanup;
-            temp->value.l = llong_val;
         }
         break;
         case VIR_TYPED_PARAM_ULLONG:
         {
-            unsigned long long ullong_val = PyLong_AsUnsignedLongLong(value);
-            if ((ullong_val == -1) && PyErr_Occurred())
+            if (libvirt_ulonglongUnwrap(value, &temp->value.ul) < 0)
                 goto cleanup;
-            temp->value.ul = ullong_val;
         }
         break;
         case VIR_TYPED_PARAM_DOUBLE:
         {
-            double double_val = PyFloat_AsDouble(value);
-            if ((double_val == -1) && PyErr_Occurred())
+            if (libvirt_doubleUnwrap(value, &temp->value.d) < 0)
                 goto cleanup;
-            temp->value.d = double_val;
         }
         break;
         case VIR_TYPED_PARAM_BOOLEAN:
         {
-            /* Hack - Python's definition of Py_True breaks strict
-             * aliasing rules, so can't directly compare
-             */
-            if (PyBool_Check(value)) {
-                PyObject *hacktrue = PyBool_FromLong(1);
-                temp->value.b = hacktrue == value ? 1 : 0;
-                Py_DECREF(hacktrue);
-            } else {
-                PyErr_Format(PyExc_TypeError,
-                             "The value type of "
-                             "attribute \"%s\" must be bool", keystr);
+            if (libvirt_boolUnwrap(value, (bool *)&temp->value.b) < 0)
                 goto cleanup;
-            }
         }
         break;
         case VIR_TYPED_PARAM_STRING:
@@ -388,7 +353,8 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
     int ncpus = -1, start_cpu = 0;
     int sumparams = 0, nparams = -1;
     int i, i_retval;
-    unsigned int flags, totalflag;
+    unsigned int flags;
+    bool totalflag;
     virTypedParameterPtr params = NULL, cpuparams;
 
     if (!PyArg_ParseTuple(args, (char *)"OOi:virDomainGetCPUStats",
@@ -396,18 +362,8 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
         return NULL;
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
-    if (!PyBool_Check(totalbool)) {
-        PyErr_Format(PyExc_TypeError,
-                    "The \"total\" attribute must be bool");
+    if (libvirt_boolUnwrap(totalbool, &totalflag) < 0)
         return NULL;
-    } else {
-        /* Hack - Python's definition of Py_True breaks strict
-         * aliasing rules, so can't directly compare
-         */
-        PyObject *hacktrue = PyBool_FromLong(1);
-        totalflag = hacktrue == totalbool ? 1 : 0;
-        Py_DECREF(hacktrue);
-    }
 
     if ((ret = PyList_New(0)) == NULL)
         return NULL;
-- 
1.7.7.5


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