Skip to content

Commit de7ad6b

Browse files
committed
[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC) (#114112)
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
1 parent e9b7fe8 commit de7ad6b

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,33 @@ namespace {
9292
struct InitializePythonRAII {
9393
public:
9494
InitializePythonRAII() {
95-
InitializePythonHome();
95+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
96+
PyConfig config;
97+
PyConfig_InitPythonConfig(&config);
98+
#endif
99+
100+
#if LLDB_EMBED_PYTHON_HOME
101+
static std::string g_python_home = []() -> std::string {
102+
if (llvm::sys::path::is_absolute(LLDB_PYTHON_HOME))
103+
return LLDB_PYTHON_HOME;
104+
105+
FileSpec spec = HostInfo::GetShlibDir();
106+
if (!spec)
107+
return {};
108+
spec.AppendPathComponent(LLDB_PYTHON_HOME);
109+
return spec.GetPath();
110+
}();
111+
if (!g_python_home.empty()) {
112+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
113+
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
114+
#else
115+
size_t size = 0;
116+
wchar_t *python_home_w = Py_DecodeLocale(g_python_home.c_str(), &size);
117+
Py_SetPythonHome(python_home_w);
118+
PyMem_RawFree(python_home_w);
119+
#endif
120+
}
121+
#endif
96122

97123
// The table of built-in modules can only be extended before Python is
98124
// initialized.
@@ -117,15 +143,22 @@ struct InitializePythonRAII {
117143
PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
118144
}
119145

146+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
147+
config.install_signal_handlers = 0;
148+
Py_InitializeFromConfig(&config);
149+
PyConfig_Clear(&config);
150+
InitializeThreadsPrivate();
151+
#else
120152
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
121153
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
122154
// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
123-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
155+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2)
124156
Py_InitializeEx(0);
125157
InitializeThreadsPrivate();
126158
#else
127159
InitializeThreadsPrivate();
128160
Py_InitializeEx(0);
161+
#endif
129162
#endif
130163
}
131164

@@ -142,32 +175,6 @@ struct InitializePythonRAII {
142175
}
143176

144177
private:
145-
void InitializePythonHome() {
146-
#if LLDB_EMBED_PYTHON_HOME
147-
typedef wchar_t *str_type;
148-
static str_type g_python_home = []() -> str_type {
149-
const char *lldb_python_home = LLDB_PYTHON_HOME;
150-
const char *absolute_python_home = nullptr;
151-
llvm::SmallString<64> path;
152-
if (llvm::sys::path::is_absolute(lldb_python_home)) {
153-
absolute_python_home = lldb_python_home;
154-
} else {
155-
FileSpec spec = HostInfo::GetShlibDir();
156-
if (!spec)
157-
return nullptr;
158-
spec.GetPath(path);
159-
llvm::sys::path::append(path, lldb_python_home);
160-
absolute_python_home = path.c_str();
161-
}
162-
size_t size = 0;
163-
return Py_DecodeLocale(absolute_python_home, &size);
164-
}();
165-
if (g_python_home != nullptr) {
166-
Py_SetPythonHome(g_python_home);
167-
}
168-
#endif
169-
}
170-
171178
void InitializeThreadsPrivate() {
172179
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
173180
// so there is no way to determine whether the embedded interpreter

0 commit comments

Comments
 (0)