Skip to content

Commit 59edbec

Browse files
acmelnamhyung
authored andcommitted
perf python: Stop using deprecated PyUnicode_AsString()
As noticed while building for Fedora 43: GEN /tmp/build/perf/python/perf.cpython-314-x86_64-linux-gnu.so /git/perf-6.16.0-rc3/tools/perf/util/python.c: In function ‘get_tracepoint_field’: /git/perf-6.16.0-rc3/tools/perf/util/python.c:340:9: error: ‘_PyUnicode_AsString’ is deprecated [-Werror=deprecated-declarations] 340 | const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); | ^~~~~ In file included from /usr/include/python3.14/unicodeobject.h:1022, from /usr/include/python3.14/Python.h:89, from /git/perf-6.16.0-rc3/tools/perf/util/python.c:2: /usr/include/python3.14/cpython/unicodeobject.h:648:1: note: declared here 648 | _PyUnicode_AsString(PyObject *unicode) | ^~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors error: command '/usr/bin/gcc' failed with exit code 1 Use PyUnicode_AsUTF8() instead and also check if PyObject_Str() fails before doing so. Signed-off-by: Arnaldo Carvalho de Melo <[email protected]> Link: https://lore.kernel.org/r/aIofXNK8QLtLIaI3@x1 Signed-off-by: Namhyung Kim <[email protected]>
1 parent b91a9ab commit 59edbec

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

tools/perf/util/python.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,25 @@ tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field)
337337
static PyObject*
338338
get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
339339
{
340-
const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
341340
struct evsel *evsel = pevent->evsel;
342341
struct tep_event *tp_format = evsel__tp_format(evsel);
343342
struct tep_format_field *field;
344343

345344
if (IS_ERR_OR_NULL(tp_format))
346345
return NULL;
347346

347+
PyObject *obj = PyObject_Str(attr_name);
348+
if (obj == NULL)
349+
return NULL;
350+
351+
const char *str = PyUnicode_AsUTF8(obj);
352+
if (str == NULL) {
353+
Py_DECREF(obj);
354+
return NULL;
355+
}
356+
348357
field = tep_find_any_field(tp_format, str);
358+
Py_DECREF(obj);
349359
return field ? tracepoint_field(pevent, field) : NULL;
350360
}
351361
#endif /* HAVE_LIBTRACEEVENT */

0 commit comments

Comments
 (0)