]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf python: Add more exceptions on error paths
authorIan Rogers <irogers@google.com>
Tue, 19 Aug 2025 01:39:31 +0000 (18:39 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 3 Sep 2025 15:34:54 +0000 (12:34 -0300)
Returning NULL will cause the python interpreter to fail but not
report an error. If none wants to be returned then Py_None needs
returning.

Set the error for the cases returning NULL so that more meaningful
interpreter behavior is had.

Reviewed-by: Howard Chu <howardchu95@gmail.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Collin Funk <collin.funk1@gmail.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Gautam Menghani <gautam@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Xu Yang <xu.yang_2@nxp.com>
Link: https://lore.kernel.org/r/20250819013941.209033-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/python.c

index ea77bea0306fa08dcf9dddb4482e84f22fcc63c6..d47cbc1c2257c298b68ac2f8e680548989539feb 100644 (file)
@@ -485,13 +485,19 @@ static PyObject *pyrf_event__new(const union perf_event *event)
        if ((event->header.type < PERF_RECORD_MMAP ||
             event->header.type > PERF_RECORD_SAMPLE) &&
            !(event->header.type == PERF_RECORD_SWITCH ||
-             event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
+             event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)) {
+               PyErr_Format(PyExc_TypeError, "Unexpected header type %u",
+                            event->header.type);
                return NULL;
+       }
 
        // FIXME this better be dynamic or we need to parse everything
        // before calling perf_mmap__consume(), including tracepoint fields.
-       if (sizeof(pevent->event) < event->header.size)
+       if (sizeof(pevent->event) < event->header.size) {
+               PyErr_Format(PyExc_TypeError, "Unexpected event size: %zd < %u",
+                            sizeof(pevent->event), event->header.size);
                return NULL;
+       }
 
        ptype = pyrf_event__type[event->header.type];
        pevent = PyObject_New(struct pyrf_event, ptype);
@@ -1209,8 +1215,10 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
                return NULL;
 
        md = get_md(evlist, cpu);
-       if (!md)
+       if (!md) {
+               PyErr_Format(PyExc_TypeError, "Unknown CPU '%d'", cpu);
                return NULL;
+       }
 
        if (perf_mmap__read_init(&md->core) < 0)
                goto end;