@@ -92,7 +92,33 @@ namespace {
92
92
struct InitializePythonRAII {
93
93
public:
94
94
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
96
122
97
123
// The table of built-in modules can only be extended before Python is
98
124
// initialized.
@@ -117,15 +143,22 @@ struct InitializePythonRAII {
117
143
PyImport_AppendInittab (" _lldb" , LLDBSwigPyInit);
118
144
}
119
145
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
120
152
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
121
153
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
122
154
// 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)
124
156
Py_InitializeEx (0 );
125
157
InitializeThreadsPrivate ();
126
158
#else
127
159
InitializeThreadsPrivate ();
128
160
Py_InitializeEx (0 );
161
+ #endif
129
162
#endif
130
163
}
131
164
@@ -142,32 +175,6 @@ struct InitializePythonRAII {
142
175
}
143
176
144
177
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
-
171
178
void InitializeThreadsPrivate () {
172
179
// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
173
180
// so there is no way to determine whether the embedded interpreter
0 commit comments