diff options
author | Zachary Turner <zturner@google.com> | 2015-10-16 17:51:49 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2015-10-16 17:51:49 +0000 |
commit | 7d6d218e1219f7df284b83dfb3eaa5e5b70e77e8 (patch) | |
tree | 20841ad8e031d112a1b143bd60e336bac453eaea /lldb/source/Plugins/ScriptInterpreter/Python | |
parent | a6760f98cf628f215cea98cf0769c4294ca91cf3 (diff) | |
download | bcm5719-llvm-7d6d218e1219f7df284b83dfb3eaa5e5b70e77e8.tar.gz bcm5719-llvm-7d6d218e1219f7df284b83dfb3eaa5e5b70e77e8.zip |
Convert SWIG typemap string operations to PythonObjects.
llvm-svn: 250530
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp | 38 | ||||
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h | 11 |
2 files changed, 44 insertions, 5 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp index 6a016e9e6aa..a5f9b8fc48d 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -111,6 +111,20 @@ PythonObject::HasAttribute(llvm::StringRef attr) const return !!PyObject_HasAttr(m_py_obj, py_attr.get()); } +PythonObject +PythonObject::GetAttributeValue(llvm::StringRef attr) const +{ + if (!IsValid()) + return PythonObject(); + + PythonString py_attr(attr); + if (!PyObject_HasAttr(m_py_obj, py_attr.get())) + return PythonObject(); + + return PythonObject(PyRefType::Owned, + PyObject_GetAttr(m_py_obj, py_attr.get())); +} + bool PythonObject::IsNone() const { @@ -191,11 +205,13 @@ PythonString::Check(PyObject *py_obj) if (!py_obj) return false; -#if PY_MAJOR_VERSION >= 3 - return PyUnicode_Check(py_obj); -#else - return PyString_Check(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 @@ -210,7 +226,13 @@ PythonString::Reset(PyRefType type, PyObject *py_obj) PythonObject::Reset(); return; } - +#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)) + result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(result.get())); +#endif // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls // back into the virtual implementation. PythonObject::Reset(PyRefType::Borrowed, result.get()); @@ -271,6 +293,12 @@ PythonString::CreateStructuredString() const // PythonInteger //---------------------------------------------------------------------- +PythonInteger::PythonInteger() + : PythonObject() +{ + +} + PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj) : PythonObject() { diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index 93513473d30..2fc6c1c64e4 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -201,6 +201,9 @@ public: bool HasAttribute(llvm::StringRef attribute) const; + PythonObject + GetAttributeValue(llvm::StringRef attribute) const; + bool IsValid() const; @@ -210,6 +213,14 @@ public: bool IsNone() const; + template<typename T> + T AsType() const + { + if (!T::Check(m_py_obj)) + return T(); + return T(PyRefType::Borrowed, m_py_obj); + } + StructuredData::ObjectSP CreateStructuredObject() const; protected: |