[libvirt] [PATCH python 09/14] typewrappers: Replace use of PyString class

Daniel P. Berrange berrange at redhat.com
Mon Dec 9 15:15:44 UTC 2013


From: "Daniel P. Berrange" <berrange at redhat.com>

Replace use of PyString with either PyBytes or PyUnicode.
The former is used for buffers with explicit sizes, which
are used by APIs processing raw bytes.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 typewrappers.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/typewrappers.c b/typewrappers.c
index c230e0f..532fe13 100644
--- a/typewrappers.c
+++ b/typewrappers.c
@@ -92,7 +92,11 @@ libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
         Py_INCREF(Py_None);
         return Py_None;
     }
+#if PY_MAJOR_VERSION > 2
+    ret = PyBytes_FromStringAndSize(str, size);
+#else
     ret = PyString_FromStringAndSize(str, size);
+#endif
     VIR_FREE(str);
     return ret;
 }
@@ -106,7 +110,11 @@ libvirt_charPtrWrap(char *str)
         Py_INCREF(Py_None);
         return Py_None;
     }
+#if PY_MAJOR_VERSION > 2
+    ret = PyUnicode_FromString(str);
+#else
     ret = PyString_FromString(str);
+#endif
     VIR_FREE(str);
     return ret;
 }
@@ -120,7 +128,11 @@ libvirt_constcharPtrWrap(const char *str)
         Py_INCREF(Py_None);
         return Py_None;
     }
+#if PY_MAJOR_VERSION > 2
+    ret = PyUnicode_FromString(str);
+#else
     ret = PyString_FromString(str);
+#endif
     return ret;
 }
 
@@ -328,17 +340,24 @@ libvirt_boolUnwrap(PyObject *obj, bool *val)
 int
 libvirt_charPtrUnwrap(PyObject *obj, char **str)
 {
+#if PY_MAJOR_VERSION < 3
     const char *ret;
+#endif
     *str = NULL;
     if (!obj) {
         PyErr_SetString(PyExc_TypeError, "unexpected type");
         return -1;
     }
 
+#if PY_MAJOR_VERSION > 2
+    if (!(*str = PyUnicode_AsUTF8(obj)))
+        return -1;
+#else
     ret = PyString_AsString(obj);
     if (ret &&
         !(*str = strdup(ret)))
         return -1;
+#endif
 
     return 0;
 }
-- 
1.8.3.1




More information about the libvir-list mailing list