Skip to content

Commit 08c47ef

Browse files
committed
[lldb] Make Python >= 3.8 required for LLDB 21
As decided on https://discourse.llvm.org/t/rfc-lets-document-and-enforce-a-minimum-python-version-for-lldb/82731. LLDB 20 recommended >= 3.8 but did not remove support for anything earlier. Now we are in what will become LLDB 21, so I'm removing that support and making >= 3.8 required. See https://docs.python.org/3/c-api/apiabiversion.html#c.PY_VERSION_HEX for the format of PY_VERSION_HEX.
1 parent 1ac3665 commit 08c47ef

File tree

6 files changed

+8
-93
lines changed

6 files changed

+8
-93
lines changed

lldb/docs/resources/build.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ CMake configuration error.
6262
+-------------------+--------------------------------------------------------------+--------------------------+
6363
| Libxml2 | XML | ``LLDB_ENABLE_LIBXML2`` |
6464
+-------------------+--------------------------------------------------------------+--------------------------+
65-
| Python | Python scripting. >= 3.0 is required, >= 3.8 is recommended. | ``LLDB_ENABLE_PYTHON`` |
65+
| Python | Python scripting. >= 3.8 is required. | ``LLDB_ENABLE_PYTHON`` |
6666
+-------------------+--------------------------------------------------------------+--------------------------+
6767
| Lua | Lua scripting. Lua 5.3 and 5.4 are supported. | ``LLDB_ENABLE_LUA`` |
6868
+-------------------+--------------------------------------------------------------+--------------------------+

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

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
7373
static bool python_is_finalizing() {
7474
#if PY_VERSION_HEX >= 0x030d0000
7575
return Py_IsFinalizing();
76-
#elif PY_VERSION_HEX >= 0x03070000
77-
return _Py_IsFinalizing();
7876
#else
79-
return _Py_Finalizing != nullptr;
77+
return _Py_IsFinalizing();
8078
#endif
8179
}
8280

@@ -810,7 +808,6 @@ bool PythonCallable::Check(PyObject *py_obj) {
810808
return PyCallable_Check(py_obj);
811809
}
812810

813-
#if PY_VERSION_HEX >= 0x03030000
814811
static const char get_arg_info_script[] = R"(
815812
from inspect import signature, Parameter, ismethod
816813
from collections import namedtuple
@@ -832,15 +829,12 @@ def main(f):
832829
raise Exception(f'unknown parameter kind: {kind}')
833830
return ArgInfo(count, varargs)
834831
)";
835-
#endif
836832

837833
Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
838834
ArgInfo result = {};
839835
if (!IsValid())
840836
return nullDeref();
841837

842-
#if PY_VERSION_HEX >= 0x03030000
843-
844838
// no need to synchronize access to this global, we already have the GIL
845839
static PythonScript get_arg_info(get_arg_info_script);
846840
Expected<PythonObject> pyarginfo = get_arg_info(*this);
@@ -852,57 +846,6 @@ Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
852846
cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs")));
853847
result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count;
854848

855-
#else
856-
PyObject *py_func_obj;
857-
bool is_bound_method = false;
858-
bool is_class = false;
859-
860-
if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) {
861-
auto init = GetAttribute("__init__");
862-
if (!init)
863-
return init.takeError();
864-
py_func_obj = init.get().get();
865-
is_class = true;
866-
} else {
867-
py_func_obj = m_py_obj;
868-
}
869-
870-
if (PyMethod_Check(py_func_obj)) {
871-
py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
872-
PythonObject im_self = GetAttributeValue("im_self");
873-
if (im_self.IsValid() && !im_self.IsNone())
874-
is_bound_method = true;
875-
} else {
876-
// see if this is a callable object with an __call__ method
877-
if (!PyFunction_Check(py_func_obj)) {
878-
PythonObject __call__ = GetAttributeValue("__call__");
879-
if (__call__.IsValid()) {
880-
auto __callable__ = __call__.AsType<PythonCallable>();
881-
if (__callable__.IsValid()) {
882-
py_func_obj = PyMethod_GET_FUNCTION(__callable__.get());
883-
PythonObject im_self = __callable__.GetAttributeValue("im_self");
884-
if (im_self.IsValid() && !im_self.IsNone())
885-
is_bound_method = true;
886-
}
887-
}
888-
}
889-
}
890-
891-
if (!py_func_obj)
892-
return result;
893-
894-
PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj);
895-
if (!code)
896-
return result;
897-
898-
auto count = code->co_argcount;
899-
bool has_varargs = !!(code->co_flags & CO_VARARGS);
900-
result.max_positional_args =
901-
has_varargs ? ArgInfo::UNBOUNDED
902-
: (count - (int)is_bound_method) - (int)is_class;
903-
904-
#endif
905-
906849
return result;
907850
}
908851

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ extern "C" PyObject *PyInit__lldb(void);
7070
// Don't mess with the signal handlers on Windows.
7171
#define LLDB_USE_PYTHON_SET_INTERRUPT 0
7272
#else
73-
// PyErr_SetInterrupt was introduced in 3.2.
74-
#define LLDB_USE_PYTHON_SET_INTERRUPT PY_VERSION_HEX >= 0x03020000
73+
#define LLDB_USE_PYTHON_SET_INTERRUPT 1
7574
#endif
7675

7776
static ScriptInterpreterPythonImpl *GetPythonInterpreter(Debugger &debugger) {
@@ -91,10 +90,8 @@ namespace {
9190
struct InitializePythonRAII {
9291
public:
9392
InitializePythonRAII() {
94-
#if PY_VERSION_HEX >= 0x03080000
9593
PyConfig config;
9694
PyConfig_InitPythonConfig(&config);
97-
#endif
9895

9996
#if LLDB_EMBED_PYTHON_HOME
10097
static std::string g_python_home = []() -> std::string {
@@ -108,14 +105,7 @@ struct InitializePythonRAII {
108105
return spec.GetPath();
109106
}();
110107
if (!g_python_home.empty()) {
111-
#if PY_VERSION_HEX >= 0x03080000
112108
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
113-
#else
114-
size_t size = 0;
115-
wchar_t *python_home_w = Py_DecodeLocale(g_python_home.c_str(), &size);
116-
Py_SetPythonHome(python_home_w);
117-
PyMem_RawFree(python_home_w);
118-
#endif
119109
}
120110
#endif
121111

@@ -142,23 +132,10 @@ struct InitializePythonRAII {
142132
PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
143133
}
144134

145-
#if PY_VERSION_HEX >= 0x03080000
146135
config.install_signal_handlers = 0;
147136
Py_InitializeFromConfig(&config);
148137
PyConfig_Clear(&config);
149138
InitializeThreadsPrivate();
150-
#else
151-
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
152-
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
153-
// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
154-
#if PY_VERSION_HEX >= 0x03020000
155-
Py_InitializeEx(0);
156-
InitializeThreadsPrivate();
157-
#else
158-
InitializeThreadsPrivate();
159-
Py_InitializeEx(0);
160-
#endif
161-
#endif
162139
}
163140

164141
~InitializePythonRAII() {
@@ -181,11 +158,9 @@ struct InitializePythonRAII {
181158
// would always return `true` and `PyGILState_Ensure/Release` flow would be
182159
// executed instead of unlocking GIL with `PyEval_SaveThread`. When
183160
// an another thread calls `PyGILState_Ensure` it would get stuck in deadlock.
184-
#if PY_VERSION_HEX >= 0x03070000
185161
// The only case we should go further and acquire the GIL: it is unlocked.
186162
if (PyGILState_Check())
187163
return;
188-
#endif
189164

190165
// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in
191166
// Python 3.13. It has been returning `true` always since Python 3.7.

lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ static llvm::Expected<bool> *g_fcxx_modules_workaround [[maybe_unused]];
5050

5151
// Provide a meaningful diagnostic error if someone tries to compile this file
5252
// with a version of Python we don't support.
53-
static_assert(PY_VERSION_HEX >= 0x03000000,
54-
"LLDB requires at least Python 3.0");
53+
static_assert(PY_VERSION_HEX >= 0x03080000,
54+
"LLDB requires at least Python 3.8");
5555
#endif
5656

5757
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H

lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,6 @@ class NewStyle(object):
760760
EXPECT_EQ(arginfo.get().max_positional_args, 3u);
761761
}
762762

763-
#if PY_VERSION_HEX >= 0x03030000
764-
765-
// the old implementation of GetArgInfo just doesn't work on builtins.
766-
767763
{
768764
auto builtins = PythonModule::BuiltinsModule();
769765
auto hex = As<PythonCallable>(builtins.GetAttribute("hex"));
@@ -772,8 +768,6 @@ class NewStyle(object):
772768
ASSERT_THAT_EXPECTED(arginfo, llvm::Succeeded());
773769
EXPECT_EQ(arginfo.get().max_positional_args, 1u);
774770
}
775-
776-
#endif
777771
}
778772

779773
TEST_F(PythonDataObjectsTest, TestScript) {

llvm/docs/ReleaseNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ Changes to the LLVM tools
131131
Changes to LLDB
132132
---------------------------------
133133

134+
* When building LLDB with Python support, the minimum version of Python is now
135+
3.8.
136+
134137
Changes to BOLT
135138
---------------------------------
136139

0 commit comments

Comments
 (0)