diff options
author | Tatyana Krasnukha <tatyana@synopsys.com> | 2019-02-16 18:39:14 +0000 |
---|---|---|
committer | Tatyana Krasnukha <tatyana@synopsys.com> | 2019-02-16 18:39:14 +0000 |
commit | b81d715cd25178d413431258667861485bb0ff89 (patch) | |
tree | 8fd5225467455bb44deb8da2b6feed41b128dcb8 /lldb/source/Plugins/ScriptInterpreter/Python | |
parent | a532d2cc81b0c67e741137dce6531c51c8506241 (diff) | |
download | bcm5719-llvm-b81d715cd25178d413431258667861485bb0ff89.tar.gz bcm5719-llvm-b81d715cd25178d413431258667861485bb0ff89.zip |
Add PythonBoolean type to the PythonDataObjects
Differential Revision: https://reviews.llvm.org/D57817
llvm-svn: 354206
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp | 54 | ||||
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h | 24 |
2 files changed, 78 insertions, 0 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp index 88759e32042..f82fe612f68 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -77,6 +77,8 @@ PyObjectType PythonObject::GetObjectType() const { #endif if (PythonByteArray::Check(m_py_obj)) return PyObjectType::ByteArray; + if (PythonBoolean::Check(m_py_obj)) + return PyObjectType::Boolean; if (PythonInteger::Check(m_py_obj)) return PyObjectType::Integer; if (PythonFile::Check(m_py_obj)) @@ -178,6 +180,9 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { case PyObjectType::Dictionary: return PythonDictionary(PyRefType::Borrowed, m_py_obj) .CreateStructuredDictionary(); + case PyObjectType::Boolean: + return PythonBoolean(PyRefType::Borrowed, m_py_obj) + .CreateStructuredBoolean(); case PyObjectType::Integer: return PythonInteger(PyRefType::Borrowed, m_py_obj) .CreateStructuredInteger(); @@ -526,6 +531,55 @@ StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const { } //---------------------------------------------------------------------- +// PythonBoolean +//---------------------------------------------------------------------- + +PythonBoolean::PythonBoolean(PyRefType type, PyObject *py_obj) + : PythonObject() { + Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a boolean type +} + +PythonBoolean::PythonBoolean(const PythonBoolean &object) + : PythonObject(object) {} + +PythonBoolean::PythonBoolean(bool value) { + SetValue(value); +} + +bool PythonBoolean::Check(PyObject *py_obj) { + return py_obj ? PyBool_Check(py_obj) : false; +} + +void PythonBoolean::Reset(PyRefType type, PyObject *py_obj) { + // Grab the desired reference type so that if we end up rejecting `py_obj` it + // still gets decremented if necessary. + PythonObject result(type, py_obj); + + if (!PythonBoolean::Check(py_obj)) { + PythonObject::Reset(); + return; + } + + // Calling PythonObject::Reset(const PythonObject&) will lead to stack + // overflow since it calls back into the virtual implementation. + PythonObject::Reset(PyRefType::Borrowed, result.get()); +} + +bool PythonBoolean::GetValue() const { + return m_py_obj ? PyObject_IsTrue(m_py_obj) : false; +} + +void PythonBoolean::SetValue(bool value) { + PythonObject::Reset(PyRefType::Owned, PyBool_FromLong(value)); +} + +StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const { + StructuredData::BooleanSP result(new StructuredData::Boolean); + result->SetValue(GetValue()); + return result; +} + +//---------------------------------------------------------------------- // PythonList //---------------------------------------------------------------------- diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index bc6c3c59e4a..48e47f3e747 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -57,6 +57,7 @@ private: enum class PyObjectType { Unknown, None, + Boolean, Integer, Dictionary, List, @@ -297,6 +298,29 @@ public: StructuredData::IntegerSP CreateStructuredInteger() const; }; +class PythonBoolean : public PythonObject { +public: + PythonBoolean() = default; + explicit PythonBoolean(bool value); + PythonBoolean(PyRefType type, PyObject *o); + PythonBoolean(const PythonBoolean &object); + + ~PythonBoolean() override = default; + + static bool Check(PyObject *py_obj); + + // Bring in the no-argument base class version + using PythonObject::Reset; + + void Reset(PyRefType type, PyObject *py_obj) override; + + bool GetValue() const; + + void SetValue(bool value); + + StructuredData::BooleanSP CreateStructuredBoolean() const; +}; + class PythonList : public PythonObject { public: PythonList() {} |