Files
llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
T

570 lines
14 KiB
C++
Raw Normal View History

//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifdef LLDB_DISABLE_PYTHON
// Python is disabled in this build
#else
#include "lldb-python.h"
#include "PythonDataObjects.h"
#include "ScriptInterpreterPython.h"
2013-06-03 18:00:07 +00:00
#include "lldb/Core/Stream.h"
#include "lldb/Host/File.h"
2013-01-18 23:41:08 +00:00
#include "lldb/Interpreter/ScriptInterpreter.h"
#include <stdio.h>
using namespace lldb_private;
using namespace lldb;
2015-03-17 20:04:04 +00:00
void
StructuredPythonObject::Dump(Stream &s) const
{
s << "Python Obj: 0x" << GetValue();
}
2013-01-18 23:41:08 +00:00
//----------------------------------------------------------------------
// PythonObject
//----------------------------------------------------------------------
void
2015-10-13 18:16:15 +00:00
PythonObject::Dump(Stream &strm) const
{
if (m_py_obj)
{
FILE *file = ::tmpfile();
if (file)
{
::PyObject_Print (m_py_obj, file, 0);
const long length = ftell (file);
if (length)
{
::rewind(file);
std::vector<char> file_contents (length,'\0');
const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
if (length_read > 0)
strm.Write (file_contents.data(), length_read);
}
::fclose (file);
}
}
else
strm.PutCString ("NULL");
}
2015-03-17 20:04:04 +00:00
PyObjectType
PythonObject::GetObjectType() const
{
2015-10-13 18:16:15 +00:00
if (!IsAllocated())
2015-03-17 20:04:04 +00:00
return PyObjectType::None;
if (PythonList::Check(m_py_obj))
2015-03-17 20:04:04 +00:00
return PyObjectType::List;
if (PythonDictionary::Check(m_py_obj))
2015-03-17 20:04:04 +00:00
return PyObjectType::Dictionary;
if (PythonString::Check(m_py_obj))
2015-10-09 19:45:41 +00:00
return PyObjectType::String;
if (PythonInteger::Check(m_py_obj))
2015-10-09 19:45:41 +00:00
return PyObjectType::Integer;
2015-03-17 20:04:04 +00:00
return PyObjectType::Unknown;
}
PythonString
2015-10-13 18:16:15 +00:00
PythonObject::Repr()
{
if (!m_py_obj)
2015-10-13 18:16:15 +00:00
return PythonString();
PyObject *repr = PyObject_Repr(m_py_obj);
if (!repr)
2015-10-13 18:16:15 +00:00
return PythonString();
return PythonString(PyRefType::Owned, repr);
}
PythonString
2015-10-13 18:16:15 +00:00
PythonObject::Str()
{
if (!m_py_obj)
2015-10-13 18:16:15 +00:00
return PythonString();
PyObject *str = PyObject_Str(m_py_obj);
if (!str)
2015-10-13 18:16:15 +00:00
return PythonString();
return PythonString(PyRefType::Owned, str);
}
2014-02-19 01:45:22 +00:00
bool
2015-10-13 18:16:15 +00:00
PythonObject::IsNone() const
2014-02-19 01:45:22 +00:00
{
2015-10-13 18:16:15 +00:00
return m_py_obj == Py_None;
}
bool
PythonObject::IsValid() const
{
return m_py_obj != nullptr;
}
bool
PythonObject::IsAllocated() const
{
return IsValid() && !IsNone();
2014-02-19 01:45:22 +00:00
}
2015-03-17 20:04:04 +00:00
StructuredData::ObjectSP
PythonObject::CreateStructuredObject() const
{
switch (GetObjectType())
{
case PyObjectType::Dictionary:
2015-10-13 18:16:15 +00:00
return PythonDictionary(PyRefType::Borrowed, m_py_obj).CreateStructuredDictionary();
2015-03-17 20:04:04 +00:00
case PyObjectType::Integer:
2015-10-13 18:16:15 +00:00
return PythonInteger(PyRefType::Borrowed, m_py_obj).CreateStructuredInteger();
2015-03-17 20:04:04 +00:00
case PyObjectType::List:
2015-10-13 18:16:15 +00:00
return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
2015-03-17 20:04:04 +00:00
case PyObjectType::String:
2015-10-13 18:16:15 +00:00
return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
2015-03-17 20:04:04 +00:00
case PyObjectType::None:
return StructuredData::ObjectSP();
default:
return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
}
}
2013-01-18 23:41:08 +00:00
//----------------------------------------------------------------------
// PythonString
//----------------------------------------------------------------------
2015-10-13 18:16:15 +00:00
PythonString::PythonString(PyRefType type, PyObject *py_obj)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
}
2015-10-13 18:16:15 +00:00
PythonString::PythonString(const PythonString &object)
: PythonObject(object)
{
}
2015-10-09 19:45:41 +00:00
PythonString::PythonString(llvm::StringRef string)
: PythonObject()
{
2015-10-09 19:45:41 +00:00
SetString(string);
}
2015-10-09 19:45:41 +00:00
PythonString::PythonString(const char *string)
: PythonObject()
{
2015-10-09 19:45:41 +00:00
SetString(llvm::StringRef(string));
}
2013-01-18 23:41:08 +00:00
2015-10-13 18:16:15 +00:00
PythonString::PythonString()
: PythonObject()
2013-01-18 23:41:08 +00:00
{
}
PythonString::~PythonString ()
{
}
bool
2015-10-09 19:45:41 +00:00
PythonString::Check(PyObject *py_obj)
2013-01-18 23:41:08 +00:00
{
2015-10-09 19:45:41 +00:00
if (!py_obj)
return false;
2015-10-09 19:45:41 +00:00
#if PY_MAJOR_VERSION >= 3
return PyUnicode_Check(py_obj);
#else
return PyString_Check(py_obj);
2015-10-09 19:45:41 +00:00
#endif
}
2015-10-13 18:16:15 +00:00
void
PythonString::Reset(PyRefType type, PyObject *py_obj)
2015-10-09 19:45:41 +00:00
{
2015-10-13 18:16:15 +00:00
// 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);
2015-10-09 19:45:41 +00:00
if (!PythonString::Check(py_obj))
{
2015-10-13 18:16:15 +00:00
PythonObject::Reset();
return;
2015-10-09 19:45:41 +00:00
}
2015-10-13 18:16:15 +00:00
// Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
// back into the virtual implementation.
PythonObject::Reset(PyRefType::Borrowed, result.get());
}
2015-03-17 20:04:04 +00:00
llvm::StringRef
2013-01-18 23:41:08 +00:00
PythonString::GetString() const
{
if (!IsValid())
return llvm::StringRef();
Py_ssize_t size;
char *c;
#if PY_MAJOR_VERSION >= 3
c = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
#else
PyString_AsStringAndSize(m_py_obj, &c, &size);
#endif
return llvm::StringRef(c, size);
}
2013-01-18 23:41:08 +00:00
size_t
PythonString::GetSize() const
{
2015-10-13 18:16:15 +00:00
if (IsValid())
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_GetSize(m_py_obj);
#else
return PyString_Size(m_py_obj);
#endif
}
2013-01-18 23:41:08 +00:00
return 0;
}
2013-01-18 23:41:08 +00:00
void
2015-03-17 20:04:04 +00:00
PythonString::SetString (llvm::StringRef string)
2013-01-18 23:41:08 +00:00
{
2015-10-09 19:45:41 +00:00
#if PY_MAJOR_VERSION >= 3
PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size());
PythonObject::Reset(PyRefType::Owned, unicode);
2015-10-09 19:45:41 +00:00
#else
PyObject *str = PyString_FromStringAndSize(string.data(), string.size());
PythonObject::Reset(PyRefType::Owned, str);
2015-10-09 19:45:41 +00:00
#endif
2015-03-17 20:04:04 +00:00
}
StructuredData::StringSP
PythonString::CreateStructuredString() const
{
StructuredData::StringSP result(new StructuredData::String);
result->SetValue(GetString());
return result;
2013-01-18 23:41:08 +00:00
}
//----------------------------------------------------------------------
// PythonInteger
//----------------------------------------------------------------------
2015-10-13 18:16:15 +00:00
PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type
}
2015-10-13 18:16:15 +00:00
PythonInteger::PythonInteger(const PythonInteger &object)
: PythonObject(object)
{
}
2015-10-13 18:16:15 +00:00
PythonInteger::PythonInteger(int64_t value)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
SetInteger(value);
}
2013-01-18 23:41:08 +00:00
PythonInteger::~PythonInteger ()
{
}
2013-01-18 23:41:08 +00:00
bool
2015-10-09 19:45:41 +00:00
PythonInteger::Check(PyObject *py_obj)
2013-01-18 23:41:08 +00:00
{
2015-10-09 19:45:41 +00:00
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
}
2015-10-13 18:16:15 +00:00
void
PythonInteger::Reset(PyRefType type, PyObject *py_obj)
2015-10-09 19:45:41 +00:00
{
2015-10-13 18:16:15 +00:00
// 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);
2015-10-09 19:45:41 +00:00
if (!PythonInteger::Check(py_obj))
2013-10-17 01:10:23 +00:00
{
2015-10-13 18:16:15 +00:00
PythonObject::Reset();
return;
2013-10-17 01:10:23 +00:00
}
2015-10-09 19:45:41 +00:00
#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))
{
2015-10-13 18:16:15 +00:00
// 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.
result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj)));
2015-10-09 19:45:41 +00:00
}
#endif
2015-10-13 18:16:15 +00:00
assert(PyLong_Check(result.get()) && "Couldn't get a PyLong from this PyObject");
2015-10-09 19:45:41 +00:00
2015-10-13 18:16:15 +00:00
// Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
// back into the virtual implementation.
PythonObject::Reset(PyRefType::Borrowed, result.get());
2013-01-18 23:41:08 +00:00
}
int64_t
2015-03-17 20:04:04 +00:00
PythonInteger::GetInteger() const
{
2013-01-18 23:41:08 +00:00
if (m_py_obj)
2013-10-17 01:10:23 +00:00
{
2015-10-09 19:45:41 +00:00
assert(PyLong_Check(m_py_obj) && "PythonInteger::GetInteger has a PyObject that isn't a PyLong");
return PyLong_AsLongLong(m_py_obj);
2013-10-17 01:10:23 +00:00
}
return UINT64_MAX;
}
void
2015-10-13 18:16:15 +00:00
PythonInteger::SetInteger(int64_t value)
{
2015-10-13 18:16:15 +00:00
PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value));
}
2015-03-17 20:04:04 +00:00
StructuredData::IntegerSP
PythonInteger::CreateStructuredInteger() const
{
StructuredData::IntegerSP result(new StructuredData::Integer);
result->SetValue(GetInteger());
return result;
}
2013-01-18 23:41:08 +00:00
//----------------------------------------------------------------------
// PythonList
//----------------------------------------------------------------------
2015-10-13 18:16:15 +00:00
PythonList::PythonList(PyInitialValue value)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
if (value == PyInitialValue::Empty)
Reset(PyRefType::Owned, PyList_New(0));
}
PythonList::PythonList(int list_size)
: PythonObject()
{
Reset(PyRefType::Owned, PyList_New(list_size));
}
2015-10-13 18:16:15 +00:00
PythonList::PythonList(PyRefType type, PyObject *py_obj)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list
}
2015-10-13 18:16:15 +00:00
PythonList::PythonList(const PythonList &list)
: PythonObject(list)
{
}
2013-01-18 23:41:08 +00:00
PythonList::~PythonList ()
{
}
2013-01-18 23:41:08 +00:00
bool
2015-10-09 19:45:41 +00:00
PythonList::Check(PyObject *py_obj)
{
2015-10-09 19:45:41 +00:00
if (!py_obj)
return false;
return PyList_Check(py_obj);
}
2015-10-13 18:16:15 +00:00
void
PythonList::Reset(PyRefType type, PyObject *py_obj)
2015-10-09 19:45:41 +00:00
{
2015-10-13 18:16:15 +00:00
// 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);
2015-10-09 19:45:41 +00:00
if (!PythonList::Check(py_obj))
{
2015-10-13 18:16:15 +00:00
PythonObject::Reset();
return;
2015-10-09 19:45:41 +00:00
}
2015-10-13 18:16:15 +00:00
// Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
// back into the virtual implementation.
PythonObject::Reset(PyRefType::Borrowed, result.get());
}
uint32_t
2015-03-17 20:04:04 +00:00
PythonList::GetSize() const
{
2015-10-13 18:16:15 +00:00
if (IsValid())
2013-01-18 23:41:08 +00:00
return PyList_GET_SIZE(m_py_obj);
return 0;
}
2013-01-18 23:41:08 +00:00
PythonObject
2015-03-17 20:04:04 +00:00
PythonList::GetItemAtIndex(uint32_t index) const
{
2015-10-13 18:16:15 +00:00
if (IsValid())
return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
return PythonObject();
}
void
2015-10-13 18:16:15 +00:00
PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object)
{
2015-10-13 18:16:15 +00:00
if (IsAllocated() && object.IsValid())
{
// PyList_SetItem is documented to "steal" a reference, so we need to
// convert it to an owned reference by incrementing it.
Py_INCREF(object.get());
PyList_SetItem(m_py_obj, index, object.get());
2015-10-13 18:16:15 +00:00
}
}
void
2015-10-13 18:16:15 +00:00
PythonList::AppendItem(const PythonObject &object)
{
2015-10-13 18:16:15 +00:00
if (IsAllocated() && object.IsValid())
{
// `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
// here like we do with `PyList_SetItem`.
PyList_Append(m_py_obj, object.get());
2015-10-13 18:16:15 +00:00
}
}
2015-03-17 20:04:04 +00:00
StructuredData::ArraySP
PythonList::CreateStructuredArray() const
{
StructuredData::ArraySP result(new StructuredData::Array);
uint32_t count = GetSize();
for (uint32_t i = 0; i < count; ++i)
{
PythonObject obj = GetItemAtIndex(i);
result->AddItem(obj.CreateStructuredObject());
}
return result;
}
2013-01-18 23:41:08 +00:00
//----------------------------------------------------------------------
// PythonDictionary
//----------------------------------------------------------------------
2015-10-13 18:16:15 +00:00
PythonDictionary::PythonDictionary(PyInitialValue value)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
if (value == PyInitialValue::Empty)
Reset(PyRefType::Owned, PyDict_New());
}
2015-10-13 18:16:15 +00:00
PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj)
: PythonObject()
{
2015-10-13 18:16:15 +00:00
Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
}
2015-10-13 18:16:15 +00:00
PythonDictionary::PythonDictionary(const PythonDictionary &object)
: PythonObject(object)
{
}
2013-01-18 23:41:08 +00:00
PythonDictionary::~PythonDictionary ()
{
}
bool
2015-10-09 19:45:41 +00:00
PythonDictionary::Check(PyObject *py_obj)
2013-01-18 23:41:08 +00:00
{
2015-10-09 19:45:41 +00:00
if (!py_obj)
return false;
return PyDict_Check(py_obj);
}
2015-10-13 18:16:15 +00:00
void
PythonDictionary::Reset(PyRefType type, PyObject *py_obj)
2015-10-09 19:45:41 +00:00
{
2015-10-13 18:16:15 +00:00
// 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);
2015-10-09 19:45:41 +00:00
if (!PythonDictionary::Check(py_obj))
{
2015-10-13 18:16:15 +00:00
PythonObject::Reset();
return;
2015-10-09 19:45:41 +00:00
}
2015-10-13 18:16:15 +00:00
// Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
// back into the virtual implementation.
PythonObject::Reset(PyRefType::Borrowed, result.get());
2013-01-18 23:41:08 +00:00
}
uint32_t
2015-03-17 20:04:04 +00:00
PythonDictionary::GetSize() const
{
2015-10-13 18:16:15 +00:00
if (IsValid())
2013-01-18 23:41:08 +00:00
return PyDict_Size(m_py_obj);
return 0;
}
2013-01-18 23:41:08 +00:00
PythonList
2015-10-13 18:16:15 +00:00
PythonDictionary::GetKeys() const
{
2015-10-13 18:16:15 +00:00
if (IsValid())
return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj));
return PythonList(PyInitialValue::Invalid);
}
2013-01-18 23:41:08 +00:00
PythonObject
2015-10-13 18:16:15 +00:00
PythonDictionary::GetItemForKey(const PythonObject &key) const
{
2015-10-13 18:16:15 +00:00
if (IsAllocated() && key.IsValid())
return PythonObject(PyRefType::Borrowed, PyDict_GetItem(m_py_obj, key.get()));
2013-01-18 23:41:08 +00:00
return PythonObject();
}
void
2015-10-13 18:16:15 +00:00
PythonDictionary::SetItemForKey(const PythonObject &key, const PythonObject &value)
{
2015-10-13 18:16:15 +00:00
if (IsAllocated() && key.IsValid() && value.IsValid())
PyDict_SetItem(m_py_obj, key.get(), value.get());
}
2015-03-17 20:04:04 +00:00
StructuredData::DictionarySP
PythonDictionary::CreateStructuredDictionary() const
{
StructuredData::DictionarySP result(new StructuredData::Dictionary);
PythonList keys(GetKeys());
uint32_t num_keys = keys.GetSize();
for (uint32_t i = 0; i < num_keys; ++i)
{
PythonObject key = keys.GetItemAtIndex(i);
PythonObject value = GetItemForKey(key);
StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
2015-10-13 18:16:15 +00:00
result->AddItem(key.Str().GetString(), structured_value);
2015-03-17 20:04:04 +00:00
}
return result;
}
#endif