[PATCH python] Rewrite libvirt_charPtrUnwrap to work with Python 3.0->3.2

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

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@xxxxxxxxxx>
---
 typewrappers.c | 19 +++++++++++--------
 1 file 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)
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]