[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
This commit is contained in:
Jonas Devlieghere
2024-02-17 15:10:09 -08:00
committed by GitHub
parent a6df3472fe
commit fbce244299

View File

@@ -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