[libvirt] [python v2 PATCH] Add dict check for setTime and allow pass one valid parameter

Luyao Huang lhuang at redhat.com
Mon Oct 27 06:58:41 UTC 2014


When pass None to time, it will set guest time to 0.
When pass an empty dictionary, it will report error.
Allow a one-element dictionary which contains 'seconds'
or 'nseconds', setting JUST seconds will do the sane
thing of passing 0 for nseconds, instead of erroring out.

Signed-off-by: Luyao Huang <lhuang at redhat.com>
---
 libvirt-override-virDomain.py |  2 ++
 libvirt-override.c            | 26 +++++++++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/libvirt-override-virDomain.py b/libvirt-override-virDomain.py
index a50ec0d..d788657 100644
--- a/libvirt-override-virDomain.py
+++ b/libvirt-override-virDomain.py
@@ -69,6 +69,8 @@
     def setTime(self, time=None, flags=0):
         """Set guest time to the given value. @time is a dict conatining
         'seconds' field for seconds and 'nseconds' field for nanosecons """
+        if time is None:
+            time = {'nseconds': 0, 'seconds': 0L}
         ret = libvirtmod.virDomainSetTime(self._o, time, flags)
         if ret == -1: raise libvirtError ('virDomainSetTime() failed', dom=self)
         return ret
diff --git a/libvirt-override.c b/libvirt-override.c
index a53b46f..5c016f9 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -7798,8 +7798,28 @@ libvirt_virDomainSetTime(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
     py_dict_size = PyDict_Size(py_dict);
+    
+    if (py_dict_size == 1) {
+        PyObject *pyobj_seconds, *pyobj_nseconds;
+
+        if ((pyobj_seconds = PyDict_GetItemString(py_dict, "seconds")) &&
+            (libvirt_longlongUnwrap(pyobj_seconds, &seconds) < 0)) {
+            PyErr_Format(PyExc_LookupError, "malformed 'seconds'");
+            goto cleanup;
+        }
 
-    if (py_dict_size == 2) {
+        if ((pyobj_nseconds = PyDict_GetItemString(py_dict, "nseconds")) &&
+            (libvirt_uintUnwrap(pyobj_nseconds, &nseconds) < 0)) {
+            PyErr_Format(PyExc_LookupError, "malformed 'nseconds'");
+            goto cleanup;
+        }
+
+        if (!pyobj_nseconds && !pyobj_seconds) {
+            PyErr_Format(PyExc_LookupError, "Dictionary must contain "
+                     "'seconds' or 'nseconds'");
+            goto cleanup;
+        }
+    } else if (py_dict_size == 2) {
         PyObject *pyobj_seconds, *pyobj_nseconds;
 
         if (!(pyobj_seconds = PyDict_GetItemString(py_dict, "seconds")) ||
@@ -7813,9 +7833,9 @@ libvirt_virDomainSetTime(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
             PyErr_Format(PyExc_LookupError, "malformed or missing 'nseconds'");
             goto cleanup;
         }
-    } else if (py_dict_size > 0) {
+    } else if (py_dict_size >= 0) {
         PyErr_Format(PyExc_LookupError, "Dictionary must contain "
-                     "'seconds' and 'nseconds'");
+                     "'seconds' or 'nseconds'");
         goto cleanup;
     }
 
-- 
1.8.3.1




More information about the libvir-list mailing list