Skip to content

Commit 9ce0a61

Browse files
authored
[lldb] Use PY_VERSION_HEX to simplify conditional compilation (NFC) (#114346)
Use PY_VERSION_HEX to simplify conditional compilation depending on the Python version. This also adds a static_assert to lldb-python to error out with a meaningful diagnostic when you try building LLDB with an older Python version in preparation for [1]. [1] https://discourse.llvm.org/t/rfc-lets-document-and-enforce-a-minimum-python-version-for-lldb/82731/15
1 parent 9234ae1 commit 9ce0a61

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ 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 >= 13) || (PY_MAJOR_VERSION > 3)
74+
#if PY_VERSION_HEX >= 0x030d0000
7575
return Py_IsFinalizing();
76-
#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
77-
return _Py_Finalizing != nullptr;
78-
#else
76+
#elif PY_VERSION_HEX >= 0x03070000
7977
return _Py_IsFinalizing();
78+
#else
79+
return _Py_Finalizing != nullptr;
8080
#endif
8181
}
8282

@@ -810,7 +810,7 @@ bool PythonCallable::Check(PyObject *py_obj) {
810810
return PyCallable_Check(py_obj);
811811
}
812812

813-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
813+
#if PY_VERSION_HEX >= 0x03030000
814814
static const char get_arg_info_script[] = R"(
815815
from inspect import signature, Parameter, ismethod
816816
from collections import namedtuple
@@ -839,7 +839,7 @@ Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
839839
if (!IsValid())
840840
return nullDeref();
841841

842-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
842+
#if PY_VERSION_HEX >= 0x03030000
843843

844844
// no need to synchronize access to this global, we already have the GIL
845845
static PythonScript get_arg_info(get_arg_info_script);

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ extern "C" PyObject *PyInit__lldb(void);
7171
#define LLDB_USE_PYTHON_SET_INTERRUPT 0
7272
#else
7373
// PyErr_SetInterrupt was introduced in 3.2.
74-
#define LLDB_USE_PYTHON_SET_INTERRUPT \
75-
(PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
74+
#define LLDB_USE_PYTHON_SET_INTERRUPT PY_VERSION_HEX >= 0x03020000
7675
#endif
7776

7877
static ScriptInterpreterPythonImpl *GetPythonInterpreter(Debugger &debugger) {
@@ -92,7 +91,7 @@ namespace {
9291
struct InitializePythonRAII {
9392
public:
9493
InitializePythonRAII() {
95-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
94+
#if PY_VERSION_HEX >= 0x03080000
9695
PyConfig config;
9796
PyConfig_InitPythonConfig(&config);
9897
#endif
@@ -109,7 +108,7 @@ struct InitializePythonRAII {
109108
return spec.GetPath();
110109
}();
111110
if (!g_python_home.empty()) {
112-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
111+
#if PY_VERSION_HEX >= 0x03080000
113112
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
114113
#else
115114
size_t size = 0;
@@ -143,7 +142,7 @@ struct InitializePythonRAII {
143142
PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
144143
}
145144

146-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) || (PY_MAJOR_VERSION > 3)
145+
#if PY_VERSION_HEX >= 0x03080000
147146
config.install_signal_handlers = 0;
148147
Py_InitializeFromConfig(&config);
149148
PyConfig_Clear(&config);
@@ -152,7 +151,7 @@ struct InitializePythonRAII {
152151
// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
153152
// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
154153
// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
155-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2)
154+
#if PY_VERSION_HEX >= 0x03020000
156155
Py_InitializeEx(0);
157156
InitializeThreadsPrivate();
158157
#else
@@ -182,15 +181,15 @@ struct InitializePythonRAII {
182181
// would always return `true` and `PyGILState_Ensure/Release` flow would be
183182
// executed instead of unlocking GIL with `PyEval_SaveThread`. When
184183
// an another thread calls `PyGILState_Ensure` it would get stuck in deadlock.
185-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || (PY_MAJOR_VERSION > 3)
184+
#if PY_VERSION_HEX >= 0x03070000
186185
// The only case we should go further and acquire the GIL: it is unlocked.
187186
if (PyGILState_Check())
188187
return;
189188
#endif
190189

191190
// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in
192191
// 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)
192+
#if PY_VERSION_HEX < 0x03090000
194193
if (PyEval_ThreadsInitialized()) {
195194
#else
196195
if (true) {
@@ -204,7 +203,7 @@ struct InitializePythonRAII {
204203

205204
// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in
206205
// Python 3.13.
207-
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9) || (PY_MAJOR_VERSION < 3)
206+
#if PY_VERSION_HEX < 0x03090000
208207
return;
209208
}
210209

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ static llvm::Expected<bool> *g_fcxx_modules_workaround [[maybe_unused]];
4747

4848
// Include python for non windows machines
4949
#include <Python.h>
50+
51+
// Provide a meaningful diagnostic error if someone tries to compile this file
52+
// with a version of Python we don't support.
53+
static_assert(PY_VERSION_HEX >= 0x03000000,
54+
"LLDB requires at least Python 3.0");
5055
#endif
5156

5257
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H

lldb/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ class NewStyle(object):
760760
EXPECT_EQ(arginfo.get().max_positional_args, 3u);
761761
}
762762

763-
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
763+
#if PY_VERSION_HEX >= 0x03030000
764764

765765
// the old implementation of GetArgInfo just doesn't work on builtins.
766766

0 commit comments

Comments
 (0)