Skip to content

Commit 0202db8

Browse files
tuliommedismailben
authored andcommitted
[lldb] Adapt code to Python 3.13 (llvm#70445)
1. Remove usage of PyEval_ThreadsInitialized and PyEval_InitThreads Both of these functions were removed in Python 3.13 [1] after being deprecated since Python 3.9. According to "What's new in Python 3.13" document [1]: Since Python 3.7, Py_Initialize() always creates the GIL: calling PyEval_InitThreads() did nothing and PyEval_ThreadsInitialized() always returned non-zero. 2. Replace _Py_IsFinalizing() with Py_IsFinalizing(). [1] https://docs.python.org/3.13/whatsnew/3.13.html (cherry picked from commit b2929be)
1 parent 286ccd3 commit 0202db8

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
7272
}
7373

7474
static bool python_is_finalizing() {
75-
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
75+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 13) || (PY_MAJOR_VERSION > 3)
76+
return Py_IsFinalizing();
77+
#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
7678
return _Py_Finalizing != nullptr;
7779
#else
7880
return _Py_IsFinalizing();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,27 @@ struct InitializePythonRAII {
176176
return;
177177
#endif
178178

179+
// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in
180+
// Python 3.13. It has been returning `true` always since Python 3.7.
181+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
179182
if (PyEval_ThreadsInitialized()) {
183+
#endif
180184
Log *log = GetLog(LLDBLog::Script);
181185

182186
m_was_already_initialized = true;
183187
m_gil_state = PyGILState_Ensure();
184188
LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n",
185189
m_gil_state == PyGILState_UNLOCKED ? "un" : "");
190+
191+
// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in
192+
// Python 3.13.
193+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
186194
return;
187195
}
188196

189197
// InitThreads acquires the GIL if it hasn't been called before.
190198
PyEval_InitThreads();
199+
#endif
191200
}
192201

193202
PyGILState_STATE m_gil_state = PyGILState_UNLOCKED;

0 commit comments

Comments
 (0)