aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
authorJonas Devlieghere <jonas@devlieghere.com>2022-04-25 20:14:44 -0700
committerJonas Devlieghere <jonas@devlieghere.com>2022-04-27 08:26:25 -0700
commit90537673302f13e92ffabba84901164c6b974b2d (patch)
treecd66c08919923e7809fa146451a4e8df47f0f847 /lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
parent16baf59c6d0b3bf7392995e3e55fc9e2ba9cb5e7 (diff)
downloadllvm-90537673302f13e92ffabba84901164c6b974b2d.zip
llvm-90537673302f13e92ffabba84901164c6b974b2d.tar.gz
llvm-90537673302f13e92ffabba84901164c6b974b2d.tar.bz2
Remove Python 2 support from the ScriptInterpreter plugin
We dropped downstream support for Python 2 in the previous release. Now that we have branched for the next release the window where this kind of change could introduce conflicts is closing too. Start by getting rid of Python 2 support in the Script Interpreter plugin. Differential revision: https://reviews.llvm.org/D124429
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r--lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp159
1 files changed, 2 insertions, 157 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 7392968..ae61736 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -71,9 +71,7 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
}
static bool python_is_finalizing() {
-#if PY_MAJOR_VERSION == 2
- return false;
-#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
+#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
return _Py_Finalizing != nullptr;
#else
return _Py_IsFinalizing();
@@ -97,12 +95,6 @@ void PythonObject::Reset() {
Expected<long long> PythonObject::AsLongLong() const {
if (!m_py_obj)
return nullDeref();
-#if PY_MAJOR_VERSION < 3
- if (!PyLong_Check(m_py_obj)) {
- PythonInteger i(PyRefType::Borrowed, m_py_obj);
- return i.AsLongLong();
- }
-#endif
assert(!PyErr_Occurred());
long long r = PyLong_AsLongLong(m_py_obj);
if (PyErr_Occurred())
@@ -113,12 +105,6 @@ Expected<long long> PythonObject::AsLongLong() const {
Expected<long long> PythonObject::AsUnsignedLongLong() const {
if (!m_py_obj)
return nullDeref();
-#if PY_MAJOR_VERSION < 3
- if (!PyLong_Check(m_py_obj)) {
- PythonInteger i(PyRefType::Borrowed, m_py_obj);
- return i.AsUnsignedLongLong();
- }
-#endif
assert(!PyErr_Occurred());
long long r = PyLong_AsUnsignedLongLong(m_py_obj);
if (PyErr_Occurred())
@@ -130,12 +116,6 @@ Expected<long long> PythonObject::AsUnsignedLongLong() const {
Expected<unsigned long long> PythonObject::AsModuloUnsignedLongLong() const {
if (!m_py_obj)
return nullDeref();
-#if PY_MAJOR_VERSION < 3
- if (!PyLong_Check(m_py_obj)) {
- PythonInteger i(PyRefType::Borrowed, m_py_obj);
- return i.AsModuloUnsignedLongLong();
- }
-#endif
assert(!PyErr_Occurred());
unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj);
if (PyErr_Occurred())
@@ -183,10 +163,8 @@ PyObjectType PythonObject::GetObjectType() const {
return PyObjectType::Dictionary;
if (PythonString::Check(m_py_obj))
return PyObjectType::String;
-#if PY_MAJOR_VERSION >= 3
if (PythonBytes::Check(m_py_obj))
return PyObjectType::Bytes;
-#endif
if (PythonByteArray::Check(m_py_obj))
return PyObjectType::ByteArray;
if (PythonBoolean::Check(m_py_obj))
@@ -282,9 +260,7 @@ PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const {
}
StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
-#if PY_MAJOR_VERSION >= 3
assert(PyGILState_Check());
-#endif
switch (GetObjectType()) {
case PyObjectType::Dictionary:
return PythonDictionary(PyRefType::Borrowed, m_py_obj)
@@ -398,11 +374,7 @@ StructuredData::StringSP PythonByteArray::CreateStructuredString() const {
// PythonString
Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) {
-#if PY_MAJOR_VERSION >= 3
PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size());
-#else
- PyObject *str = PyString_FromStringAndSize(string.data(), string.size());
-#endif
if (!str)
return llvm::make_error<PythonException>();
return Take<PythonString>(str);
@@ -416,35 +388,9 @@ bool PythonString::Check(PyObject *py_obj) {
if (PyUnicode_Check(py_obj))
return true;
-#if PY_MAJOR_VERSION < 3
- if (PyString_Check(py_obj))
- return true;
-#endif
return false;
}
-void PythonString::Convert(PyRefType &type, PyObject *&py_obj) {
-#if PY_MAJOR_VERSION < 3
- // In Python 2, Don't store PyUnicode objects directly, because we need
- // access to their underlying character buffers which Python 2 doesn't
- // provide.
- if (PyUnicode_Check(py_obj)) {
- PyObject *s = PyUnicode_AsUTF8String(py_obj);
- if (s == nullptr) {
- PyErr_Clear();
- if (type == PyRefType::Owned)
- Py_DECREF(py_obj);
- return;
- }
- if (type == PyRefType::Owned)
- Py_DECREF(py_obj);
- else
- type = PyRefType::Owned;
- py_obj = s;
- }
-#endif
-}
-
llvm::StringRef PythonString::GetString() const {
auto s = AsUTF8();
if (!s) {
@@ -461,15 +407,7 @@ Expected<llvm::StringRef> PythonString::AsUTF8() const {
Py_ssize_t size;
const char *data;
-#if PY_MAJOR_VERSION >= 3
data = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
-#else
- char *c = NULL;
- int r = PyString_AsStringAndSize(m_py_obj, &c, &size);
- if (r < 0)
- c = NULL;
- data = c;
-#endif
if (!data)
return exception();
@@ -479,15 +417,11 @@ Expected<llvm::StringRef> PythonString::AsUTF8() const {
size_t PythonString::GetSize() const {
if (IsValid()) {
-#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 3
return PyUnicode_GetLength(m_py_obj);
#else
return PyUnicode_GetSize(m_py_obj);
#endif
-#else
- return PyString_Size(m_py_obj);
-#endif
}
return 0;
}
@@ -516,41 +450,9 @@ bool PythonInteger::Check(PyObject *py_obj) {
if (!py_obj)
return false;
-#if PY_MAJOR_VERSION >= 3
// Python 3 does not have PyInt_Check. There is only one type of integral
// value, long.
return PyLong_Check(py_obj);
-#else
- return PyLong_Check(py_obj) || PyInt_Check(py_obj);
-#endif
-}
-
-void PythonInteger::Convert(PyRefType &type, PyObject *&py_obj) {
-#if PY_MAJOR_VERSION < 3
- // Always store this as a PyLong, which makes interoperability between Python
- // 2.x and Python 3.x easier. This is only necessary in 2.x, since 3.x
- // doesn't even have a PyInt.
- if (PyInt_Check(py_obj)) {
- // Since we converted the original object to a different type, the new
- // object is an owned object regardless of the ownership semantics
- // requested by the user.
- long long value = PyInt_AsLong(py_obj);
- PyObject *l = nullptr;
- if (!PyErr_Occurred())
- l = PyLong_FromLongLong(value);
- if (l == nullptr) {
- PyErr_Clear();
- if (type == PyRefType::Owned)
- Py_DECREF(py_obj);
- return;
- }
- if (type == PyRefType::Owned)
- Py_DECREF(py_obj);
- else
- type = PyRefType::Owned;
- py_obj = l;
- }
-#endif
}
void PythonInteger::SetInteger(int64_t value) {
@@ -762,13 +664,9 @@ Expected<PythonObject>
PythonDictionary::GetItem(const PythonObject &key) const {
if (!IsValid())
return nullDeref();
-#if PY_MAJOR_VERSION >= 3
PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get());
if (PyErr_Occurred())
return exception();
-#else
- PyObject *o = PyDict_GetItem(m_py_obj, key.get());
-#endif
if (!o)
return keyError();
return Retain<PythonObject>(o);
@@ -826,13 +724,7 @@ PythonDictionary::CreateStructuredDictionary() const {
return result;
}
-PythonModule PythonModule::BuiltinsModule() {
-#if PY_MAJOR_VERSION >= 3
- return AddModule("builtins");
-#else
- return AddModule("__builtin__");
-#endif
-}
+PythonModule PythonModule::BuiltinsModule() { return AddModule("builtins"); }
PythonModule PythonModule::MainModule() { return AddModule("__main__"); }
@@ -1000,9 +892,6 @@ operator()(std::initializer_list<PythonObject> args) {
bool PythonFile::Check(PyObject *py_obj) {
if (!py_obj)
return false;
-#if PY_MAJOR_VERSION < 3
- return PyFile_Check(py_obj);
-#else
// In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a
// first-class object type anymore. `PyFile_FromFd` is just a thin wrapper
// over `io.open()`, which returns some object derived from `io.IOBase`. As a
@@ -1024,7 +913,6 @@ bool PythonFile::Check(PyObject *py_obj) {
return false;
}
return !!r;
-#endif
}
const char *PythonException::toCString() const {
@@ -1121,7 +1009,6 @@ char PythonException::ID = 0;
llvm::Expected<File::OpenOptions>
GetOptionsForPyObject(const PythonObject &obj) {
-#if PY_MAJOR_VERSION >= 3
auto options = File::OpenOptions(0);
auto readable = As<bool>(obj.CallMethod("readable"));
if (!readable)
@@ -1136,10 +1023,6 @@ GetOptionsForPyObject(const PythonObject &obj) {
else if (readable.get())
options |= File::eOpenOptionReadOnly;
return options;
-#else
- PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>();
- return File::GetOptionsFromMode(py_mode.GetString());
-#endif
}
// Base class template for python files. All it knows how to do
@@ -1223,8 +1106,6 @@ public:
char SimplePythonFile::ID = 0;
} // namespace
-#if PY_MAJOR_VERSION >= 3
-
namespace {
class PythonBuffer {
public:
@@ -1414,8 +1295,6 @@ public:
};
} // namespace
-#endif
-
llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) {
if (!IsValid())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -1466,13 +1345,6 @@ PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"invalid PythonFile");
-#if PY_MAJOR_VERSION < 3
-
- return llvm::createStringError(llvm::inconvertibleErrorCode(),
- "not supported on python 2");
-
-#else
-
int fd = PyObject_AsFileDescriptor(m_py_obj);
if (fd < 0) {
PyErr_Clear();
@@ -1522,8 +1394,6 @@ PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) {
"invalid File");
return file_sp;
-
-#endif
}
Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) {
@@ -1533,10 +1403,8 @@ Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) {
if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file))
return Retain<PythonFile>(simple->GetPythonObject());
-#if PY_MAJOR_VERSION >= 3
if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file))
return Retain<PythonFile>(pythonio->GetPythonObject());
-#endif
if (!mode) {
auto m = file.GetOpenMode();
@@ -1546,26 +1414,8 @@ Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) {
}
PyObject *file_obj;
-#if PY_MAJOR_VERSION >= 3
file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr,
"ignore", nullptr, /*closefd=*/0);
-#else
- // I'd like to pass ::fflush here if the file is writable, so that
- // when the python side destructs the file object it will be flushed.
- // However, this would be dangerous. It can cause fflush to be called
- // after fclose if the python program keeps a reference to the file after
- // the original lldb_private::File has been destructed.
- //
- // It's all well and good to ask a python program not to use a closed file
- // but asking a python program to make sure objects get released in a
- // particular order is not safe.
- //
- // The tradeoff here is that if a python 2 program wants to make sure this
- // file gets flushed, they'll have to do it explicitly or wait untill the
- // original lldb File itself gets flushed.
- file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""),
- py2_const_cast(mode), [](FILE *) { return 0; });
-#endif
if (!file_obj)
return exception();
@@ -1612,12 +1462,7 @@ python::runStringOneLine(const llvm::Twine &string,
return exception();
auto code_ref = Take<PythonObject>(code);
-#if PY_MAJOR_VERSION < 3
- PyObject *result =
- PyEval_EvalCode((PyCodeObject *)code, globals.get(), locals.get());
-#else
PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get());
-#endif
if (!result)
return exception();