[Lldb-commits] [lldb] [lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC) (PR #114112)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Wed Oct 30 07:00:45 PDT 2024
https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/114112
>From c58438160c016841c6a72e53cd5c4985ebd1e521 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 29 Oct 2024 11:19:33 -0700
Subject: [PATCH 1/3] [lldb] Use Py_InitializeFromConfig with Python >= 3.8
(NFC)
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
---
.../Python/ScriptInterpreterPython.cpp | 67 +++++++++++--------
1 file changed, 40 insertions(+), 27 deletions(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 7cc38da6a6a94b..d7734c5e199058 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -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)
+ 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.
@@ -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
}
@@ -142,31 +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,
>From 35d76ac118a4b900e28b33c1d958d44c7b22c21c Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 29 Oct 2024 11:36:42 -0700
Subject: [PATCH 2/3] Fix formatting
---
.../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index d7734c5e199058..cde1e707cd1cec 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -149,10 +149,10 @@ struct InitializePythonRAII {
}
#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();
+ 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
@@ -180,7 +180,6 @@ struct InitializePythonRAII {
}
private:
-
void InitializeThreadsPrivate() {
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
// so there is no way to determine whether the embedded interpreter
>From 97b6b53d57b7f1d812aee2255f3c361be749e1f4 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Wed, 30 Oct 2024 07:00:28 -0700
Subject: [PATCH 3/3] Add missing PY_MAJOR_VERSION > 3 in
LLDB_EMBED_PYTHON_HOME
---
.../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index cde1e707cd1cec..6158083a982801 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -117,7 +117,7 @@ struct InitializePythonRAII {
return Py_DecodeLocale(absolute_python_home, &size);
}();
if (g_python_home != nullptr) {
-#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8)
+#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);
More information about the lldb-commits
mailing list