diff options
author | Guannan Ren <gren@redhat.com> | 2012-09-28 20:29:03 +0800 |
---|---|---|
committer | Guannan Ren <gren@redhat.com> | 2012-10-08 21:54:06 +0800 |
commit | 49873a855f0ede1388cea7ef37bf861e88950d61 (patch) | |
tree | c89cf3145815f048b3a4db98512470709b66a3a6 /typewrappers.c | |
parent | f0b144406d83a37e6029896a6b5ec5ee4023b39d (diff) | |
download | libvirt-python-v6-49873a855f0ede1388cea7ef37bf861e88950d61.tar.gz libvirt-python-v6-49873a855f0ede1388cea7ef37bf861e88950d61.tar.xz libvirt-python-v6-49873a855f0ede1388cea7ef37bf861e88950d61.zip |
python: keep consistent handling of Python integer conversion
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.
Diffstat (limited to 'typewrappers.c')
-rw-r--r-- | typewrappers.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/typewrappers.c b/typewrappers.c index 7580689..d633603 100644 --- a/typewrappers.c +++ b/typewrappers.c @@ -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; |