]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
python: keep consistent handling of Python integer conversion
authorGuannan Ren <gren@redhat.com>
Fri, 28 Sep 2012 12:29:03 +0000 (20:29 +0800)
committerGuannan Ren <gren@redhat.com>
Mon, 8 Oct 2012 13:54:06 +0000 (21:54 +0800)
libvirt_ulonglongUnwrap requires the integer type of python obj.
But libvirt_longlongUnwrap still could handle python obj of
Pyfloat_type which causes the float value to be rounded up
to an integer.

For example
 >>> dom.setSchedulerParameters({'vcpu_quota': 0.88})
 0
libvirt_longlongUnwrap treats 0.88 as a valid value 0

However
 >>> dom.setSchedulerParameters({'cpu_shares': 1000.22})
libvirt_ulonglongUnwrap will throw out an error
"TypeError: an integer is required"

The patch make this consistent.

python/typewrappers.c

index 75806897b9f22952d82b8f13e65b179a917c983a..d633603c8f4ca7789225162708826a3bc16269b9 100644 (file)
@@ -220,7 +220,7 @@ libvirt_ulongUnwrap(PyObject *obj, unsigned long *val)
 int
 libvirt_longlongUnwrap(PyObject *obj, long long *val)
 {
-    long long llong_val;
+    long long llong_val = -1;
 
     if (!obj) {
         PyErr_SetString(PyExc_TypeError, "unexpected type");
@@ -230,7 +230,11 @@ libvirt_longlongUnwrap(PyObject *obj, long long *val)
     /* If obj is of PyInt_Type, PyLong_AsLongLong
      * will call PyInt_AsLong() to handle it automatically.
      */
-    llong_val = PyLong_AsLongLong(obj);
+    if (PyInt_Check(obj) || PyLong_Check(obj))
+        llong_val = PyLong_AsLongLong(obj);
+    else
+        PyErr_SetString(PyExc_TypeError, "an integer is required");
+
     if ((llong_val == -1) && PyErr_Occurred())
         return -1;