diff mbox series

[2/3] trace-cmd: Use PyMemoryView_FromMemory() for Python 3

Message ID 20190719225030.345100829@goodmis.org (mailing list archive)
State Accepted
Commit dbddc64834a7ab93a9ea47e0d2a8fb97185138ec
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 PyBuffer_FromMemory() and instead has
PyMemoryView_FromMemory(). Add a helper function that uses the latter if
Python 3 is detected. 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 | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Johannes Berg July 26, 2019, 7:30 a.m. UTC | #1
On Fri, 2019-07-19 at 18:46 -0400, Steven Rostedt wrote:
> From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> 
> Python 3 has deprecated PyBuffer_FromMemory() and instead has
> PyMemoryView_FromMemory(). Add a helper function that uses the latter if
> Python 3 is detected. 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

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

johannes
diff mbox series

Patch

diff --git a/python/ctracecmd.i b/python/ctracecmd.i
index 09d1d6414fc1..63e5dcb813f1 100644
--- a/python/ctracecmd.i
+++ b/python/ctracecmd.i
@@ -117,6 +117,15 @@  static PyObject *py_field_get_stack(struct tep_handle *pevent,
 	return list;
 }
 
+static PyObject *fromMemory(void *buf, size_t len)
+{
+#if PY_MAJOR_VERSION >= 3
+		return PyMemoryView_FromMemory(buf, len, PyBUF_READ);
+#else
+		return PyBuffer_FromMemory(buf, len);
+#endif
+}
+
 static PyObject *py_field_get_data(struct tep_format_field *f, struct tep_record *r)
 {
 	if (!strncmp(f->type, "__data_loc ", 11)) {
@@ -137,10 +146,10 @@  static PyObject *py_field_get_data(struct tep_format_field *f, struct tep_record
 		offset = val & 0xffff;
 		len = val >> 16;
 
-		return PyBuffer_FromMemory((char *)r->data + offset, len);
+		return fromMemory(r->data + offset, len);
 	}
 
-	return PyBuffer_FromMemory((char *)r->data + f->offset, f->size);
+	return fromMemory(r->data + f->offset, f->size);
 }
 
 static PyObject *py_field_get_str(struct tep_format_field *f, struct tep_record *r)