summaryrefslogtreecommitdiff
path: root/typewrappers.c
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2013-12-09 14:00:19 +0000
committerDaniel P. Berrange <berrange@redhat.com>2013-12-11 16:14:46 +0000
commita65df7c7a574e99e65223eb29b58b4b5b7cbeb8a (patch)
treef037720d1aa3e25823f686beba2b50ec11fa9742 /typewrappers.c
parentb4d886346905e735827235fb881e3e547350a41a (diff)
downloadlibvirt-python-a65df7c7a574e99e65223eb29b58b4b5b7cbeb8a.tar.gz
typewrappers: PyInt/PyLong merge for Python3
In Python3 the PyInt / PyLong types have merged into a single PyLong type. Conditionalize the use of PyInt to Python 2 only Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'typewrappers.c')
-rw-r--r--typewrappers.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/typewrappers.c b/typewrappers.c
index 64db9b8..9cce9db 100644
--- a/typewrappers.c
+++ b/typewrappers.c
@@ -39,7 +39,11 @@ PyObject *
libvirt_intWrap(int val)
{
PyObject *ret;
+#if PY_MAJOR_VERSION > 2
+ ret = PyLong_FromLong((long) val);
+#else
ret = PyInt_FromLong((long) val);
+#endif
return ret;
}
@@ -47,7 +51,11 @@ PyObject *
libvirt_uintWrap(uint val)
{
PyObject *ret;
+#if PY_MAJOR_VERSION > 2
+ ret = PyLong_FromLong((long) val);
+#else
ret = PyInt_FromLong((long) val);
+#endif
return ret;
}
@@ -55,7 +63,7 @@ PyObject *
libvirt_longWrap(long val)
{
PyObject *ret;
- ret = PyInt_FromLong(val);
+ ret = PyLong_FromLong(val);
return ret;
}
@@ -157,7 +165,11 @@ libvirt_intUnwrap(PyObject *obj, int *val)
* to C long type directly. If it is of PyLong_Type, PyInt_AsLong
* will call PyLong_AsLong() to deal with it automatically.
*/
+#if PY_MAJOR_VERSION > 2
+ long_val = PyLong_AsLong(obj);
+#else
long_val = PyInt_AsLong(obj);
+#endif
if ((long_val == -1) && PyErr_Occurred())
return -1;
@@ -185,7 +197,11 @@ libvirt_uintUnwrap(PyObject *obj, unsigned int *val)
return -1;
}
+#if PY_MAJOR_VERSION > 2
+ long_val = PyLong_AsLong(obj);
+#else
long_val = PyInt_AsLong(obj);
+#endif
if ((long_val == -1) && PyErr_Occurred())
return -1;
@@ -209,7 +225,7 @@ libvirt_longUnwrap(PyObject *obj, long *val)
return -1;
}
- long_val = PyInt_AsLong(obj);
+ long_val = PyLong_AsLong(obj);
if ((long_val == -1) && PyErr_Occurred())
return -1;
@@ -227,7 +243,7 @@ libvirt_ulongUnwrap(PyObject *obj, unsigned long *val)
return -1;
}
- long_val = PyInt_AsLong(obj);
+ long_val = PyLong_AsLong(obj);
if ((long_val == -1) && PyErr_Occurred())
return -1;
@@ -251,10 +267,14 @@ libvirt_longlongUnwrap(PyObject *obj, long long *val)
return -1;
}
+#if PY_MAJOR_VERSION == 2
/* If obj is of PyInt_Type, PyLong_AsLongLong
* will call PyInt_AsLong() to handle it automatically.
*/
if (PyInt_Check(obj) || PyLong_Check(obj))
+#else
+ if (PyLong_Check(obj))
+#endif
llong_val = PyLong_AsLongLong(obj);
else
PyErr_SetString(PyExc_TypeError, "an integer is required");
@@ -270,24 +290,27 @@ int
libvirt_ulonglongUnwrap(PyObject *obj, unsigned long long *val)
{
unsigned long long ullong_val = -1;
- long long llong_val;
if (!obj) {
PyErr_SetString(PyExc_TypeError, "unexpected type");
return -1;
}
+#if PY_MAJOR_VERSION == 2
/* The PyLong_AsUnsignedLongLong doesn't check the type of
* obj, only accept argument of PyLong_Type, so we check it instead.
*/
if (PyInt_Check(obj)) {
- llong_val = PyInt_AsLong(obj);
+ long long llong_val = PyInt_AsLong(obj);
if (llong_val < 0)
PyErr_SetString(PyExc_OverflowError,
"negative Python int cannot be converted to C unsigned long long");
else
ullong_val = llong_val;
} else if (PyLong_Check(obj)) {
+#else
+ if (PyLong_Check(obj)) {
+#endif
ullong_val = PyLong_AsUnsignedLongLong(obj);
} else {
PyErr_SetString(PyExc_TypeError, "an integer is required");