diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2024-02-17 15:10:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-17 15:10:09 -0800 |
commit | fbce244299524fc3d736cce9d25b4262303b45d5 (patch) | |
tree | 20139d1af99359b352a14facfb1f52f4828f718d /lldb | |
parent | a6df3472feeb618513877ec136db38f4746142fb (diff) | |
download | llvm-fbce244299524fc3d736cce9d25b4262303b45d5.zip llvm-fbce244299524fc3d736cce9d25b4262303b45d5.tar.gz llvm-fbce244299524fc3d736cce9d25b4262303b45d5.tar.bz2 |
[lldb] Call Import_AppendInittab before Py_Initialize (#82095)
The Python documentation [1] says that `PyImport_AppendInittab` should
be called before `Py_Initialize()`. Starting with Python 3.12, this is
enforced with a fatal error:
Fatal Python error: PyImport_AppendInittab: PyImport_AppendInittab()
may not be called after Py_Initialize()
This commit ensures we only modify the table of built-in modules if
Python hasn't been initialized. For Python embedded in LLDB, that means
this happen exactly once, before the first call to `Py_Initialize`,
which becomes a NO-OP after. However, when lldb is imported in an
existing Python interpreter, Python will have already been initialized,
but by definition, the lldb module will already have been loaded, so
it's safe to skip adding it (again).
This fixes #70453.
[1] https://docs.python.org/3.12/c-api/import.html#c.PyImport_AppendInittab
Diffstat (limited to 'lldb')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index dadcde6..a1ad3f5 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -97,24 +97,28 @@ public: InitializePythonRAII() { InitializePythonHome(); + // The table of built-in modules can only be extended before Python is + // initialized. + if (!Py_IsInitialized()) { #ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE - // Python's readline is incompatible with libedit being linked into lldb. - // Provide a patched version local to the embedded interpreter. - bool ReadlinePatched = false; - for (auto *p = PyImport_Inittab; p->name != nullptr; p++) { - if (strcmp(p->name, "readline") == 0) { - p->initfunc = initlldb_readline; - break; + // Python's readline is incompatible with libedit being linked into lldb. + // Provide a patched version local to the embedded interpreter. + bool ReadlinePatched = false; + for (auto *p = PyImport_Inittab; p->name != nullptr; p++) { + if (strcmp(p->name, "readline") == 0) { + p->initfunc = initlldb_readline; + break; + } + } + if (!ReadlinePatched) { + PyImport_AppendInittab("readline", initlldb_readline); + ReadlinePatched = true; } - } - if (!ReadlinePatched) { - PyImport_AppendInittab("readline", initlldb_readline); - ReadlinePatched = true; - } #endif - // Register _lldb as a built-in module. - PyImport_AppendInittab("_lldb", LLDBSwigPyInit); + // Register _lldb as a built-in module. + PyImport_AppendInittab("_lldb", LLDBSwigPyInit); + } // Python < 3.2 and Python >= 3.2 reversed the ordering requirements for // calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you |