mirror of
https://github.com/xen-project/xen.git
synced 2026-10-28 10:11:27 +01:00
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@invisiblethingslab.com> Acked-by: Wei Liu <wei.liu2@citrix.com>
This commit is contained in:
1 parent
0c8981f035
commit
121d9d4030
2 files changed
+19
-7
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||