From 90537673302f13e92ffabba84901164c6b974b2d Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Mon, 25 Apr 2022 20:14:44 -0700 Subject: 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 --- .../ScriptInterpreter/Python/PythonDataObjects.cpp | 159 +-------------------- 1 file changed, 2 insertions(+), 157 deletions(-) (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp') 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 python::As(Expected &&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 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 PythonObject::AsLongLong() const { Expected 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 PythonObject::AsUnsignedLongLong() const { Expected 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::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(); return Take(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 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 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 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(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 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 GetOptionsForPyObject(const PythonObject &obj) { -#if PY_MAJOR_VERSION >= 3 auto options = File::OpenOptions(0); auto readable = As(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(); - 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 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::FromFile(File &file, const char *mode) { @@ -1533,10 +1403,8 @@ Expected PythonFile::FromFile(File &file, const char *mode) { if (auto *simple = llvm::dyn_cast(&file)) return Retain(simple->GetPythonObject()); -#if PY_MAJOR_VERSION >= 3 if (auto *pythonio = llvm::dyn_cast(&file)) return Retain(pythonio->GetPythonObject()); -#endif if (!mode) { auto m = file.GetOpenMode(); @@ -1546,26 +1414,8 @@ Expected 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(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(); -- cgit v1.1