Skip to content

Commit d79e98e

Browse files
tuliomarichardson
authored andcommitted
[lldb] Adapt code to Python 3.13 (#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 430ebd2 commit d79e98e

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
@@ -71,7 +71,9 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
7171
}
7272

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,27 @@ struct InitializePythonRAII {
188188
return;
189189
#endif
190190

191+
// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in
192+
// Python 3.13. It has been returning `true` always since Python 3.7.
193+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
191194
if (PyEval_ThreadsInitialized()) {
195+
#endif
192196
Log *log = GetLog(LLDBLog::Script);
193197

194198
m_was_already_initialized = true;
195199
m_gil_state = PyGILState_Ensure();
196200
LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n",
197201
m_gil_state == PyGILState_UNLOCKED ? "un" : "");
202+
203+
// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in
204+
// Python 3.13.
205+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
198206
return;
199207
}
200208

201209
// InitThreads acquires the GIL if it hasn't been called before.
202210
PyEval_InitThreads();
211+
#endif
203212
}
204213

205214
PyGILState_STATE m_gil_state = PyGILState_UNLOCKED;

0 commit comments

Comments
 (0)