In Python3,  the result of PyModule_Create (called from
scripts/python/Perf-Trace-Util/Context.c) is not automatically added to
sys.modules.  See: https://bugs.python.org/issue4592

Below is the observed behavior without the fix:

# ldd /usr/bin/perf | grep -i python
        libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 
(0x00007f8e1dfb2000)

# perf record /bin/false
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.015 MB perf.data (17 samples) ]

# perf script -g python | cat
generated Python script: perf-script.py

# perf script -s ./perf-script.py
Traceback (most recent call last):
  File "./perf-script.py", line 18, in <module>
    from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context'
Error running python script ./perf-script.py

Signed-off-By: Tony Jones <to...@suse.de>
Cc: Jaroslav Škarvada <jskar...@redhat.com>
Fixes: 66dfdff03d196e51322c6a85c0d8db8bb2bdd655
---
 tools/perf/util/scripting-engines/trace-event-python.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/scripting-engines/trace-event-python.c 
b/tools/perf/util/scripting-engines/trace-event-python.c
index 87ef16a1b17e..40300d8e80a7 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -1494,6 +1494,7 @@ static void _free_command_line(wchar_t **command_line, 
int num)
 static int python_start_script(const char *script, int argc, const char **argv)
 {
        struct tables *tables = &tables_global;
+       PyMODINIT_FUNC (*initfunc)(void);
 #if PY_MAJOR_VERSION < 3
        const char **command_line;
 #else
@@ -1504,24 +1505,25 @@ static int python_start_script(const char *script, int 
argc, const char **argv)
        FILE *fp;
 
 #if PY_MAJOR_VERSION < 3
+       initfunc = initperf_trace_context;
        command_line = malloc((argc + 1) * sizeof(const char *));
        command_line[0] = script;
        for (i = 1; i < argc + 1; i++)
                command_line[i] = argv[i - 1];
 #else
+       initfunc = PyInit_perf_trace_context;
        command_line = malloc((argc + 1) * sizeof(wchar_t *));
        command_line[0] = Py_DecodeLocale(script, NULL);
        for (i = 1; i < argc + 1; i++)
                command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
 #endif
 
+       PyImport_AppendInittab("perf_trace_context", initfunc);
        Py_Initialize();
 
 #if PY_MAJOR_VERSION < 3
-       initperf_trace_context();
        PySys_SetArgv(argc + 1, (char **)command_line);
 #else
-       PyInit_perf_trace_context();
        PySys_SetArgv(argc + 1, command_line);
 #endif
 
-- 
2.20.1

Reply via email to