Skip to content

Commit 45f420e

Browse files
authored
[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 4c03d37 commit 45f420e

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

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

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,38 @@ 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+
typedef wchar_t *str_type;
102+
static str_type g_python_home = []() -> str_type {
103+
const char *lldb_python_home = LLDB_PYTHON_HOME;
104+
const char *absolute_python_home = nullptr;
105+
llvm::SmallString<64> path;
106+
if (llvm::sys::path::is_absolute(lldb_python_home)) {
107+
absolute_python_home = lldb_python_home;
108+
} else {
109+
FileSpec spec = HostInfo::GetShlibDir();
110+
if (!spec)
111+
return nullptr;
112+
spec.GetPath(path);
113+
llvm::sys::path::append(path, lldb_python_home);
114+
absolute_python_home = path.c_str();
115+
}
116+
size_t size = 0;
117+
return Py_DecodeLocale(absolute_python_home, &size);
118+
}();
119+
if (g_python_home != nullptr) {
120+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
121+
PyConfig_SetBytesString(&config, &config.home, g_python_home);
122+
#else
123+
Py_SetPythonHome(g_python_home);
124+
#endif
125+
}
126+
#endif
96127

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

151+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
152+
config.install_signal_handlers = 0;
153+
Py_InitializeFromConfig(&config);
154+
PyConfig_Clear(&config);
155+
InitializeThreadsPrivate();
156+
#else
120157
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
121158
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
122159
// 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)
160+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2)
124161
Py_InitializeEx(0);
125162
InitializeThreadsPrivate();
126163
#else
127164
InitializeThreadsPrivate();
128165
Py_InitializeEx(0);
166+
#endif
129167
#endif
130168
}
131169

@@ -142,32 +180,6 @@ struct InitializePythonRAII {
142180
}
143181

144182
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-
171183
void InitializeThreadsPrivate() {
172184
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
173185
// so there is no way to determine whether the embedded interpreter

0 commit comments

Comments
 (0)