Skip to content

Commit

Permalink
[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC) (#114112)
Browse files Browse the repository at this point in the history
This fixes the deprecation warning for Py_SetPythonHome, which was
deprecated in Python 3.11. With this patch, when building against Python
3.8 or later, we now use Py_InitializeFromConfig instead.

Fixes #113475
  • Loading branch information
JDevlieghere authored Oct 30, 2024
1 parent 4c03d37 commit 45f420e
Showing 1 changed file with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,38 @@ namespace {
struct InitializePythonRAII {
public:
InitializePythonRAII() {
InitializePythonHome();
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
PyConfig config;
PyConfig_InitPythonConfig(&config);
#endif

#if LLDB_EMBED_PYTHON_HOME
typedef wchar_t *str_type;
static str_type g_python_home = []() -> str_type {
const char *lldb_python_home = LLDB_PYTHON_HOME;
const char *absolute_python_home = nullptr;
llvm::SmallString<64> path;
if (llvm::sys::path::is_absolute(lldb_python_home)) {
absolute_python_home = lldb_python_home;
} else {
FileSpec spec = HostInfo::GetShlibDir();
if (!spec)
return nullptr;
spec.GetPath(path);
llvm::sys::path::append(path, lldb_python_home);
absolute_python_home = path.c_str();
}
size_t size = 0;
return Py_DecodeLocale(absolute_python_home, &size);
}();
if (g_python_home != nullptr) {
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
PyConfig_SetBytesString(&config, &config.home, g_python_home);
#else
Py_SetPythonHome(g_python_home);
#endif
}
#endif

// The table of built-in modules can only be extended before Python is
// initialized.
Expand All @@ -117,15 +148,22 @@ struct InitializePythonRAII {
PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
}

#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
config.install_signal_handlers = 0;
Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
InitializeThreadsPrivate();
#else
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2)
Py_InitializeEx(0);
InitializeThreadsPrivate();
#else
InitializeThreadsPrivate();
Py_InitializeEx(0);
#endif
#endif
}

Expand All @@ -142,32 +180,6 @@ struct InitializePythonRAII {
}

private:
void InitializePythonHome() {
#if LLDB_EMBED_PYTHON_HOME
typedef wchar_t *str_type;
static str_type g_python_home = []() -> str_type {
const char *lldb_python_home = LLDB_PYTHON_HOME;
const char *absolute_python_home = nullptr;
llvm::SmallString<64> path;
if (llvm::sys::path::is_absolute(lldb_python_home)) {
absolute_python_home = lldb_python_home;
} else {
FileSpec spec = HostInfo::GetShlibDir();
if (!spec)
return nullptr;
spec.GetPath(path);
llvm::sys::path::append(path, lldb_python_home);
absolute_python_home = path.c_str();
}
size_t size = 0;
return Py_DecodeLocale(absolute_python_home, &size);
}();
if (g_python_home != nullptr) {
Py_SetPythonHome(g_python_home);
}
#endif
}

void InitializeThreadsPrivate() {
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
// so there is no way to determine whether the embedded interpreter
Expand Down

0 comments on commit 45f420e

Please sign in to comment.