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

[Xen-devel] [PATCH 5/8] python: use PyBytes/PyUnicode instead of PyString



In Python2 PyBytes is the same as PyString, but in Python3 PyString is
gone and 'str' is really PyUnicode in C-API.
When handling arbitrary data, use PyBytes - which is the right thing to
do in Python3, and pose no API change in Python2. When handling
xenstore paths and transaction ids, which have well defined format, use
PyUnicode - to ease API usage - no need to prefix all xenstore paths
with 'b' when migrating scripts to Python3.

Signed-off-by: Marek Marczykowski-Górecki <marmarek@xxxxxxxxxxxxxxxxxxxxxx>
---
 tools/python/xen/lowlevel/xc/xc.c |  6 +++---
 tools/python/xen/lowlevel/xs/xs.c | 20 ++++++++++++++++----
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/tools/python/xen/lowlevel/xc/xc.c 
b/tools/python/xen/lowlevel/xc/xc.c
index bcbb7b0..ce87afb 100644
--- a/tools/python/xen/lowlevel/xc/xc.c
+++ b/tools/python/xen/lowlevel/xc/xc.c
@@ -678,7 +678,7 @@ static void pyxc_dom_extract_cpuid(PyObject *config,
 
     for ( i = 0; i < 4; i++ )
         if ( (obj = PyDict_GetItemString(config, regs_extract[i])) != NULL )
-            regs[i] = PyString_AS_STRING(obj);
+            regs[i] = PyBytes_AS_STRING(obj);
 }
 
 static PyObject *pyxc_create_cpuid_dict(char **regs)
@@ -693,7 +693,7 @@ static PyObject *pyxc_create_cpuid_dict(char **regs)
        if ( regs[i] == NULL )
            continue;
        PyDict_SetItemString(dict, regs_extract[i],
-                            PyString_FromString(regs[i]));
+                            PyBytes_FromString(regs[i]));
        free(regs[i]);
        regs[i] = NULL;
    }
@@ -940,7 +940,7 @@ static PyObject *pyxc_readconsolering(XcObject *self,
         str = ptr;
     }
 
-    obj = PyString_FromStringAndSize(str, count);
+    obj = PyBytes_FromStringAndSize(str, count);
     free(str);
     return obj;
 }
diff --git a/tools/python/xen/lowlevel/xs/xs.c 
b/tools/python/xen/lowlevel/xs/xs.c
index 66ab08d..c2b4d87 100644
--- a/tools/python/xen/lowlevel/xs/xs.c
+++ b/tools/python/xen/lowlevel/xs/xs.c
@@ -103,7 +103,7 @@ static PyObject *xspy_read(XsHandle *self, PyObject *args)
     xsval = xs_read(xh, th, path, &xsval_n);
     Py_END_ALLOW_THREADS
     if (xsval) {
-        PyObject *val = PyString_FromStringAndSize(xsval, xsval_n);
+        PyObject *val = PyBytes_FromStringAndSize(xsval, xsval_n);
         free(xsval);
         return val;
     }
@@ -179,7 +179,11 @@ static PyObject *xspy_ls(XsHandle *self, PyObject *args)
         int i;
         PyObject *val = PyList_New(xsval_n);
         for (i = 0; i < xsval_n; i++)
-            PyList_SetItem(val, i, PyString_FromString(xsval[i]));
+#if PY_MAJOR_VERSION >= 3
+            PyList_SetItem(val, i, PyUnicode_FromString(xsval[i]));
+#else
+            PyList_SetItem(val, i, PyBytes_FromString(xsval[i]));
+#endif
         free(xsval);
         return val;
     }
@@ -550,7 +554,11 @@ static PyObject *xspy_transaction_start(XsHandle *self)
     }
 
     snprintf(thstr, sizeof(thstr), "%lX", (unsigned long)th);
-    return PyString_FromString(thstr);
+#if PY_MAJOR_VERSION >= 3
+    return PyUnicode_FromString(thstr);
+#else
+    return PyBytes_FromString(thstr);
+#endif
 }
 
 #define xspy_transaction_end_doc "\n"                                  \
@@ -773,7 +781,11 @@ static PyObject *xspy_get_domain_path(XsHandle *self, 
PyObject *args)
     Py_END_ALLOW_THREADS
 
     if (xsval) {
-        PyObject *val = PyString_FromString(xsval);
+#if PY_MAJOR_VERSION >= 3
+        PyObject *val = PyUnicode_FromString(xsval);
+#else
+        PyObject *val = PyBytes_FromString(xsval);
+#endif
         free(xsval);
         return val;
     }
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.