Skip to content

Commit 2bc5302

Browse files
authored
Revert "[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC)" (#114290)
Reverts #114112 because this triggers a compile error: ``` no known conversion from 'str_type' (aka 'wchar_t *') to 'const char *' for 3rd argument 221 | PyAPI_FUNC(PyStatus) PyConfig_SetBytesString( | ^ 222 | PyConfig *config, 223 | wchar_t **config_str, 224 | const char *str); | ~~~~~~~~~~~~~~~ 1 error generated. ```
1 parent a518ed2 commit 2bc5302

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

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

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,7 @@ namespace {
9292
struct InitializePythonRAII {
9393
public:
9494
InitializePythonRAII() {
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
95+
InitializePythonHome();
12796

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

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
157120
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
158121
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
159122
// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
160-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2)
123+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
161124
Py_InitializeEx(0);
162125
InitializeThreadsPrivate();
163126
#else
164127
InitializeThreadsPrivate();
165128
Py_InitializeEx(0);
166-
#endif
167129
#endif
168130
}
169131

@@ -180,6 +142,32 @@ struct InitializePythonRAII {
180142
}
181143

182144
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+
183171
void InitializeThreadsPrivate() {
184172
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
185173
// so there is no way to determine whether the embedded interpreter

0 commit comments

Comments
 (0)