[PATCH v10 02/19] perf python: Add missed explicit dependencies

From: Ian Rogers

Date: Fri Jun 05 2026 - 03:50:18 EST


Fix missing #include of pmus.h found while cleaning the evsel/evlist
header files.

Also define PY_SSIZE_T_CLEAN before including Python.h to comply with
modern Python 3 C-API requirements, and introduce CHECK_INITIALIZED
and CHECK_INITIALIZED_INT safety macros to be used by subsequent patches.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/python.c | 41 +++++++++++++++++++++++++++++++---------
1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index cc1019d29a5d..98a8dd9b673d 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1,30 +1,37 @@
// SPDX-License-Identifier: GPL-2.0
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
-#include <structmember.h>
+
#include <inttypes.h>
-#include <poll.h>
+
#include <linux/err.h>
+#include <poll.h>
+
+#include <internal/lib.h>
#include <perf/cpumap.h>
-#ifdef HAVE_LIBTRACEEVENT
-#include <event-parse.h>
-#endif
#include <perf/mmap.h>
+#include <structmember.h>
+
#include "callchain.h"
#include "counts.h"
+#include "event.h"
#include "evlist.h"
#include "evsel.h"
-#include "event.h"
#include "expr.h"
+#include "metricgroup.h"
+#include "mmap.h"
+#include "pmus.h"
#include "print_binary.h"
#include "record.h"
#include "strbuf.h"
#include "thread_map.h"
#include "tp_pmu.h"
#include "trace-event.h"
-#include "metricgroup.h"
-#include "mmap.h"
#include "util/sample.h"
-#include <internal/lib.h>
+
+#ifdef HAVE_LIBTRACEEVENT
+#include <event-parse.h>
+#endif

PyMODINIT_FUNC PyInit_perf(void);

@@ -38,6 +45,22 @@ PyMODINIT_FUNC PyInit_perf(void);
offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
0, help }

+#define CHECK_INITIALIZED(ptr, msg) \
+ do { \
+ if (!(ptr)) { \
+ PyErr_SetString(PyExc_ValueError, msg " not initialized"); \
+ return NULL; \
+ } \
+ } while (0)
+
+#define CHECK_INITIALIZED_INT(ptr, msg) \
+ do { \
+ if (!(ptr)) { \
+ PyErr_SetString(PyExc_ValueError, msg " not initialized"); \
+ return -1; \
+ } \
+ } while (0)
+
struct pyrf_event {
PyObject_HEAD
struct evsel *evsel;
--
2.54.0.1032.g2f8565e1d1-goog