From: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
To: xen-devel@lists.xen.org
Cc: "Wei Liu" <wei.liu2@citrix.com>,
"Ian Jackson" <ian.jackson@eu.citrix.com>,
"Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
Subject: [PATCH 5/8] python: use PyBytes/PyUnicode instead of PyString
Date: Thu, 23 Feb 2017 11:48:25 +0100 [thread overview]
Message-ID: <1487846908-21462-6-git-send-email-marmarek@invisiblethingslab.com> (raw)
In-Reply-To: <1487846908-21462-1-git-send-email-marmarek@invisiblethingslab.com>
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>
---
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@lists.xen.org
https://lists.xen.org/xen-devel
next prev parent reply other threads:[~2017-02-23 10:48 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-23 10:48 [PATCH 0/8] Python 3 bindings Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 1/8] python: check return value of PyErr_NewException Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 2/8] python: drop tp_getattr implementation Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 3/8] python: use Py_TYPE instead of looking directly into PyObject_HEAD Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 4/8] python: initialize specific fields of PyTypeObject Marek Marczykowski-Górecki
2017-02-23 10:48 ` Marek Marczykowski-Górecki [this message]
2017-02-23 10:48 ` [PATCH 6/8] python: use PyLong_* for constructing 'int' type in Python3 Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 7/8] python: adjust module initalization for Python3 Marek Marczykowski-Górecki
2017-02-23 10:48 ` [PATCH 8/8] python: handle long type in scripts Marek Marczykowski-Górecki
2017-02-23 11:23 ` [PATCH 0/8] Python 3 bindings Wei Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1487846908-21462-6-git-send-email-marmarek@invisiblethingslab.com \
--to=marmarek@invisiblethingslab.com \
--cc=ian.jackson@eu.citrix.com \
--cc=wei.liu2@citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).