47#ifndef LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
48#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
56#include "llvm/ADT/ArrayRef.h"
73 assert(!PyErr_Occurred());
115template <
typename T> T
Take(PyObject *obj) {
117 assert(!PyErr_Occurred());
119 assert(thing.IsValid());
133template <
typename T> T
Retain(PyObject *obj) {
135 assert(!PyErr_Occurred());
137 assert(thing.IsValid());
159 llvm::StringRef ref = twine.toNullTerminatedStringRef(
storage);
162 operator const char *() {
return str; }
166 return llvm::createStringError(llvm::inconvertibleErrorCode(),
167 "A NULL PyObject* was dereferenced");
171 return llvm::make_error<PythonException>(s);
175 return llvm::createStringError(llvm::inconvertibleErrorCode(),
188 static constexpr T
get(T t) {
return t; }
217 T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> {
219 static auto get(
const T &value) {
return value.get(); }
241 rhs.m_py_obj =
nullptr;
273 template <
typename T>
305 template <
typename... T>
307 const T &... t)
const {
317 template <
typename... T>
318 llvm::Expected<PythonObject>
Call(
const T &... t)
const {
327 llvm::Expected<PythonObject>
GetAttribute(
const llvm::Twine &name)
const {
336 llvm::Expected<PythonObject>
GetType()
const {
339 PyObject *obj = PyObject_Type(
m_py_obj);
376template <
typename T> llvm::Expected<T>
As(llvm::Expected<PythonObject> &&obj) {
378 return obj.takeError();
379 if (!T::Check(obj.get().get()))
380 return llvm::createStringError(llvm::inconvertibleErrorCode(),
385template <> llvm::Expected<bool>
As<bool>(llvm::Expected<PythonObject> &&obj);
388llvm::Expected<long long>
As<long long>(llvm::Expected<PythonObject> &&obj);
391llvm::Expected<unsigned long long>
395llvm::Expected<std::string>
As<std::string>(llvm::Expected<PythonObject> &&obj);
403 if (T::Check(py_obj))
415 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
418 static bool Check(PyObject *py_obj);
420 llvm::ArrayRef<uint8_t>
GetBytes()
const;
424 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
436 static bool Check(PyObject *py_obj);
438 llvm::ArrayRef<uint8_t>
GetBytes()
const;
442 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
450 static llvm::Expected<PythonString>
FromUTF8(llvm::StringRef
string);
456 static bool Check(PyObject *py_obj);
460 llvm::Expected<llvm::StringRef>
AsUTF8()
const;
477 static bool Check(PyObject *py_obj);
494 static bool Check(PyObject *py_obj);
512 static bool Check(PyObject *py_obj);
531 PythonTuple(std::initializer_list<PythonObject> objects);
532 PythonTuple(std::initializer_list<PyObject *> objects);
534 static bool Check(PyObject *py_obj);
553 static bool Check(PyObject *py_obj);
555 bool HasKey(
const llvm::Twine &key)
const;
566 llvm::Expected<PythonObject>
GetItem(
const llvm::Twine &key)
const;
577 static bool Check(PyObject *py_obj);
587 std::string s = std::string(name);
588 auto mod =
Import(s.c_str());
590 llvm::consumeError(mod.takeError());
593 return std::move(mod.get());
596 static llvm::Expected<PythonModule>
Import(
const llvm::Twine &name);
598 llvm::Expected<PythonObject>
Get(
const llvm::Twine &name);
615 static bool Check(PyObject *py_obj);
625 template <
typename Arg,
typename...
Args>
637 static bool Check(PyObject *py_obj);
640 const char *mode =
nullptr);
642 llvm::Expected<lldb::FileSP>
ConvertToFile(
bool borrowed =
false);
643 llvm::Expected<lldb::FileSP>
658 void log(llvm::raw_ostream &
OS)
const override;
660 bool Matches(PyObject *exc)
const;
687 return expected.get();
688 llvm::handleAllErrors(
690 [](
const llvm::ErrorInfoBase &E) {
691 PyErr_SetString(PyExc_Exception, E.message().c_str());
700 return std::move(expected.get());
701 llvm::consumeError(expected.takeError());
706 const PythonDictionary &globals,
707 const PythonDictionary &locals);
710 const PythonDictionary &globals,
711 const PythonDictionary &locals);
739 template <
typename...
Args>
742 return std::move(
error);
743 return function.Call(std::forward<Args>(args)...);
763 void Serialize(llvm::json::OStream &s)
const override;
771PyObject *
RunString(
const char *str,
int start, PyObject *globals,
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
A stream class that can stream formatted output to a file.
Generic(void *object=nullptr)
A class which can hold structured data.
std::shared_ptr< UnsignedInteger > UnsignedIntegerSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< String > StringSP
std::shared_ptr< Array > ArraySP
std::shared_ptr< Boolean > BooleanSP
std::shared_ptr< SignedInteger > SignedIntegerSP
std::variant< UnsignedIntegerSP, SignedIntegerSP > IntegerSP
NullTerminated(const llvm::Twine &twine)
llvm::SmallString< 32 > storage
StructuredData::BooleanSP CreateStructuredBoolean() const
static bool Check(PyObject *py_obj)
PythonBoolean(bool value)
void SetValue(bool value)
TypedPythonObject(PyRefType type, PyObject *py_obj)
void SetBytes(llvm::ArrayRef< uint8_t > stringbytes)
PythonByteArray(const PythonBytes &object)
StructuredData::StringSP CreateStructuredString() const
llvm::ArrayRef< uint8_t > GetBytes() const
TypedPythonObject(PyRefType type, PyObject *py_obj)
static bool Check(PyObject *py_obj)
PythonByteArray(llvm::ArrayRef< uint8_t > bytes)
static bool Check(PyObject *py_obj)
StructuredData::StringSP CreateStructuredString() const
void SetBytes(llvm::ArrayRef< uint8_t > stringbytes)
PythonBytes(llvm::ArrayRef< uint8_t > bytes)
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::ArrayRef< uint8_t > GetBytes() const
PythonObject operator()(const Arg &arg, Args... args)
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Expected< ArgInfo > GetArgInfo() const
PythonObject operator()()
static bool Check(PyObject *py_obj)
StructuredData::DictionarySP CreateStructuredDictionary() const
llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const
llvm::Expected< PythonObject > GetItem(const PythonObject &key) const
bool HasKey(const llvm::Twine &key) const
static bool Check(PyObject *py_obj)
PythonObject GetItemForKey(const PythonObject &key) const
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const
PythonList GetKeys() const
llvm::Expected< PythonObject > GetItem(const llvm::Twine &key) const
void SetItemForKey(const PythonObject &key, const PythonObject &value)
const char * toCString() const
PyObject * m_exception_type
bool Matches(PyObject *exc) const
std::string ReadBacktrace() const
std::error_code convertToErrorCode() const override
void log(llvm::raw_ostream &OS) const override
~PythonException() override
PythonException(const char *caller=nullptr)
llvm::Expected< lldb::FileSP > ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed=false)
llvm::Expected< lldb::FileSP > ConvertToFile(bool borrowed=false)
static bool Check(PyObject *py_obj)
TypedPythonObject(PyRefType type, PyObject *py_obj)
static llvm::Expected< PythonFile > FromFile(File &file, const char *mode=nullptr)
StructuredData::SignedIntegerSP CreateStructuredSignedInteger() const
static bool Check(PyObject *py_obj)
StructuredData::UnsignedIntegerSP CreateStructuredUnsignedInteger() const
TypedPythonObject(PyRefType type, PyObject *py_obj)
StructuredData::IntegerSP CreateStructuredInteger() const
void SetInteger(int64_t value)
PythonObject GetItemAtIndex(uint32_t index) const
void AppendItem(const PythonObject &object)
static bool Check(PyObject *py_obj)
void SetItemAtIndex(uint32_t index, const PythonObject &object)
TypedPythonObject(PyRefType type, PyObject *py_obj)
StructuredData::ArraySP CreateStructuredArray() const
static PythonModule BuiltinsModule()
static PythonModule AddModule(llvm::StringRef module)
static PythonModule MainModule()
static PythonModule ImportModule(llvm::StringRef name)
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Expected< PythonObject > Get(const llvm::Twine &name)
PythonDictionary GetDictionary() const
static bool Check(PyObject *py_obj)
static llvm::Expected< PythonModule > Import(const llvm::Twine &name)
static T ResolveNameWithDictionary(llvm::StringRef name, const PythonDictionary &dict)
T ResolveName(llvm::StringRef name) const
PythonObject ResolveName(llvm::StringRef name) const
llvm::Expected< PythonObject > GetType() const
PythonString Repr() const
PythonObject & operator=(PythonObject other)
PythonObject(PyRefType type, PyObject *py_obj)
PythonObject(const PythonObject &rhs)
llvm::Expected< long long > AsLongLong() const
llvm::Expected< bool > IsTrue()
StructuredData::ObjectSP CreateStructuredObject() const
llvm::Expected< unsigned long long > AsModuloUnsignedLongLong() const
PythonObject GetAttributeValue(llvm::StringRef attribute) const
static PythonObject ResolveNameWithDictionary(llvm::StringRef name, const PythonDictionary &dict)
llvm::Expected< unsigned long long > AsUnsignedLongLong() const
llvm::Expected< bool > IsInstance(const PythonObject &cls)
llvm::Expected< PythonObject > GetAttribute(const llvm::Twine &name) const
PyObjectType GetObjectType() const
void Dump(Stream &strm) const
PythonObject(PythonObject &&rhs)
bool HasAttribute(llvm::StringRef attribute) const
llvm::Expected< PythonObject > Call(const T &... t) const
llvm::Expected< PythonObject > CallMethod(const char *name, const T &... t) const
PythonScript(const char *script)
llvm::Expected< PythonObject > operator()(Args &&... args)
llvm::Expected< llvm::StringRef > AsUTF8() const
llvm::StringRef GetString() const
void SetString(llvm::StringRef string)
TypedPythonObject(PyRefType type, PyObject *py_obj)
StructuredData::StringSP CreateStructuredString() const
static bool Check(PyObject *py_obj)
static llvm::Expected< PythonString > FromUTF8(llvm::StringRef string)
PythonTuple(PyInitialValue value)
StructuredData::ArraySP CreateStructuredArray() const
TypedPythonObject(PyRefType type, PyObject *py_obj)
void SetItemAtIndex(uint32_t index, const PythonObject &object)
PythonObject GetItemAtIndex(uint32_t index) const
static bool Check(PyObject *py_obj)
StructuredPythonObject(const StructuredPythonObject &)=delete
bool IsValid() const override
~StructuredPythonObject() override
StructuredPythonObject(PythonObject obj)
const StructuredPythonObject & operator=(const StructuredPythonObject &)=delete
void Serialize(llvm::json::OStream &s) const override
TypedPythonObject()=default
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Expected< unsigned long long > As< unsigned long long >(llvm::Expected< PythonObject > &&obj)
llvm::Error exception(const char *s=nullptr)
llvm::Expected< std::string > As< std::string >(llvm::Expected< PythonObject > &&obj)
PyObject * RunString(const char *str, int start, PyObject *globals, PyObject *locals)
const char * py2_const_cast(const char *s)
llvm::Expected< T > As(llvm::Expected< PythonObject > &&obj)
T unwrapOrSetPythonException(llvm::Expected< T > expected)
T unwrapIgnoringErrors(llvm::Expected< T > expected)
llvm::Expected< PythonObject > runStringMultiLine(const llvm::Twine &string, const PythonDictionary &globals, const PythonDictionary &locals)
int RunSimpleString(const char *str)
llvm::Expected< long long > As< long long >(llvm::Expected< PythonObject > &&obj)
llvm::Expected< bool > As< bool >(llvm::Expected< PythonObject > &&obj)
llvm::Expected< PythonObject > runStringOneLine(const llvm::Twine &string, const PythonDictionary &globals, const PythonDictionary &locals)
A class that represents a running process on the host machine.
unsigned max_positional_args
static constexpr unsigned UNBOUNDED