From: "Daniel P. Berrange" <berrange(a)redhat.com>
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(a)redhat.com>
---
typewrappers.c | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/typewrappers.c b/typewrappers.c
index 532fe13..7331cbd 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;
}
@@ -159,7 +167,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;
@@ -187,7 +199,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;
@@ -211,7 +227,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;
@@ -229,7 +245,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;
@@ -253,10 +269,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");
@@ -272,24 +292,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");
--
1.8.3.1