summaryrefslogtreecommitdiff
path: root/typewrappers.c
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2013-12-12 18:15:05 +0000
committerDaniel P. Berrange <berrange@redhat.com>2013-12-12 18:15:05 +0000
commita06d78b4a1ce0ce2e46fe41be2c373647e79cec7 (patch)
treeaa9770c6160ae6657dcbf4d654ba03a15ef94e36 /typewrappers.c
parentc95c635d9b6b372b0bdb931be8012d40dc73126f (diff)
downloadlibvirt-python-a06d78b4a1ce0ce2e46fe41be2c373647e79cec7.tar.gz
Rewrite libvirt_charPtrUnwrap to work with Python 3.0->3.2
The PyUnicode_AsUTF8 method doesn't exist prior to Python 3.3. It is also somewhat inefficient, so rewrite it to use an intermediate PyBytes object. Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'typewrappers.c')
-rw-r--r--typewrappers.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/typewrappers.c b/typewrappers.c
index a7a42f2..a5b562d 100644
--- a/typewrappers.c
+++ b/typewrappers.c
@@ -361,9 +361,10 @@ libvirt_boolUnwrap(PyObject *obj, bool *val)
int
libvirt_charPtrUnwrap(PyObject *obj, char **str)
{
-#if PY_MAJOR_VERSION < 3
- const char *ret;
+#if PY_MAJOR_VERSION > 2
+ PyObject *bytes;
#endif
+ const char *ret;
*str = NULL;
if (!obj) {
PyErr_SetString(PyExc_TypeError, "unexpected type");
@@ -371,16 +372,18 @@ libvirt_charPtrUnwrap(PyObject *obj, char **str)
}
#if PY_MAJOR_VERSION > 2
- if (!(*str = PyUnicode_AsUTF8(obj)))
+ if (!(bytes = PyUnicode_AsUTF8String(obj)))
return -1;
+ ret = PyBytes_AsString(bytes);
#else
ret = PyString_AsString(obj);
- if (ret &&
- !(*str = strdup(ret)))
- return -1;
#endif
-
- return 0;
+ if (ret)
+ *str = strdup(ret);
+#if PY_MAJOR_VERSION > 2
+ Py_DECREF(bytes);
+#endif
+ return ret && *str ? 0 : -1;
}
int libvirt_charPtrSizeUnwrap(PyObject *obj, char **str, Py_ssize_t *size)