diff mbox series

[1/3] trace-cmd: Replace PySting_FromString() with PyUnicode_FromString()

Message ID 20190719225030.187908166@goodmis.org (mailing list archive)
State Accepted
Commit ff3c2189ddbd17fb0a333bc8f9e23a2b8481ee61
Headers show
Series trace-cmd: Update python plugin for Python 3 | expand

Commit Message

Steven Rostedt July 19, 2019, 10:46 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Python 3 has deprecated PyString_FromString(), but both Python 2 and
Python 3 have PyUnicode_FromString() which should be equivalent (at least
according to google). As Python 2 is going to be EOL soon, we need to
support Python 3.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=204231

Reported-by: Troy Engel <troyengel@gmail.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 python/ctracecmd.i | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Johannes Berg July 26, 2019, 7:29 a.m. UTC | #1
On Fri, 2019-07-19 at 18:46 -0400, Steven Rostedt wrote:

> --- a/python/ctracecmd.i
> +++ b/python/ctracecmd.i
> @@ -108,7 +108,7 @@ static PyObject *py_field_get_stack(struct tep_handle *pevent,
>  		    ((int)addr == -1))
>  			break;
>  		func = tep_find_function(event->tep, addr);
> -		if (PyList_Append(list, PyString_FromString(func))) {
> +		if (PyList_Append(list, PyUnicode_FromString(func))) {
>  			Py_DECREF(list);

This assumes that the source code is not using extended identifiers (per
C99, gcc -fextended-identifiers). I think that's probably a reasonable
assumption :-)

In theory, a bit safer would be to use PyBytes_FromString() and let the
python plugin worry about the encoding.

That really applies to all of the instances here, I think, since the
strings eventually come from (macros in the) code.

Reviewed-by: Johannes Berg <johannes@sipsolutions.net>

johannes
diff mbox series

Patch

diff --git a/python/ctracecmd.i b/python/ctracecmd.i
index 65a3d5144b88..09d1d6414fc1 100644
--- a/python/ctracecmd.i
+++ b/python/ctracecmd.i
@@ -108,7 +108,7 @@  static PyObject *py_field_get_stack(struct tep_handle *pevent,
 		    ((int)addr == -1))
 			break;
 		func = tep_find_function(event->tep, addr);
-		if (PyList_Append(list, PyString_FromString(func))) {
+		if (PyList_Append(list, PyUnicode_FromString(func))) {
 			Py_DECREF(list);
 			return NULL;
 		}
@@ -162,10 +162,10 @@  static PyObject *py_field_get_str(struct tep_format_field *f, struct tep_record
 		 */
 		offset = val & 0xffff;
 
-		return PyString_FromString((char *)r->data + offset);
+		return PyUnicode_FromString((char *)r->data + offset);
 	}
 
-	return PyString_FromStringAndSize((char *)r->data + f->offset,
+	return PyUnicode_FromStringAndSize((char *)r->data + f->offset,
 				strnlen((char *)r->data + f->offset, f->size));
 }
 
@@ -177,7 +177,7 @@  static PyObject *py_format_get_keys(struct tep_event *ef)
 	list = PyList_New(0);
 
 	for (f = ef->format.fields; f; f = f->next) {
-		if (PyList_Append(list, PyString_FromString(f->name))) {
+		if (PyList_Append(list, PyUnicode_FromString(f->name))) {
 			Py_DECREF(list);
 			return NULL;
 		}