Skip to content

Commit b2929be

Browse files
authored
[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
1 parent 8483d18 commit b2929be

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
@@ -179,18 +179,27 @@ struct InitializePythonRAII {
179179
return;
180180
#endif
181181

182+
// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in
183+
// Python 3.13. It has been returning `true` always since Python 3.7.
184+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
182185
if (PyEval_ThreadsInitialized()) {
186+
#endif
183187
Log *log = GetLog(LLDBLog::Script);
184188

185189
m_was_already_initialized = true;
186190
m_gil_state = PyGILState_Ensure();
187191
LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n",
188192
m_gil_state == PyGILState_UNLOCKED ? "un" : "");
193+
194+
// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in
195+
// Python 3.13.
196+
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
189197
return;
190198
}
191199

192200
// InitThreads acquires the GIL if it hasn't been called before.
193201
PyEval_InitThreads();
202+
#endif
194203
}
195204

196205
PyGILState_STATE m_gil_state = PyGILState_UNLOCKED;

0 commit comments

Comments
 (0)