47#ifndef LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
48#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
55#include "llvm/ADT/ArrayRef.h"
72 assert(!PyErr_Occurred());
114template <
typename T> T
Take(PyObject *obj) {
116 assert(!PyErr_Occurred());
118 assert(thing.IsValid());
132template <
typename T> T
Retain(PyObject *obj) {
134 assert(!PyErr_Occurred());
136 assert(thing.IsValid());
158 llvm::StringRef ref = twine.toNullTerminatedStringRef(
storage);
161 operator const char *() {
return str; }
165 return llvm::createStringError(llvm::inconvertibleErrorCode(),
166 "A NULL PyObject* was dereferenced");
170 return llvm::make_error<PythonException>(s);
174 return llvm::createStringError(llvm::inconvertibleErrorCode(),
187 static constexpr T
get(T t) {
return t; }
216 T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> {
218 static auto get(
const T &value) {
return value.get(); }
240 rhs.m_py_obj =
nullptr;
272 template <
typename T>
304 template <
typename... T>
306 const T &... t)
const {
316 template <
typename... T>
317 llvm::Expected<PythonObject>
Call(
const T &... t)
const {
326 llvm::Expected<PythonObject>
GetAttribute(
const llvm::Twine &name)
const {
335 llvm::Expected<PythonObject>
GetType()
const {
338 PyObject *obj = PyObject_Type(
m_py_obj);
375template <
typename T> llvm::Expected<T>
As(llvm::Expected<PythonObject> &&obj) {
377 return obj.takeError();
378 if (!T::Check(obj.get().get()))
379 return llvm::createStringError(llvm::inconvertibleErrorCode(),
384template <> llvm::Expected<bool>
As<bool>(llvm::Expected<PythonObject> &&obj);
387llvm::Expected<long long>
As<long long>(llvm::Expected<PythonObject> &&obj);
390llvm::Expected<unsigned long long>
394llvm::Expected<std::string>
As<std::string>(llvm::Expected<PythonObject> &&obj);
402 if (T::Check(py_obj))
414 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
417 static bool Check(PyObject *py_obj);
419 llvm::ArrayRef<uint8_t>
GetBytes()
const;
423 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
435 static bool Check(PyObject *py_obj);
437 llvm::ArrayRef<uint8_t>
GetBytes()
const;
441 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
449 static llvm::Expected<PythonString>
FromUTF8(llvm::StringRef
string);
455 static bool Check(PyObject *py_obj);
459 llvm::Expected<llvm::StringRef>
AsUTF8()
const;
476 static bool Check(PyObject *py_obj);
493 static bool Check(PyObject *py_obj);
511 static bool Check(PyObject *py_obj);
530 PythonTuple(std::initializer_list<PythonObject> objects);
531 PythonTuple(std::initializer_list<PyObject *> objects);
533 static bool Check(PyObject *py_obj);
552 static bool Check(PyObject *py_obj);
554 bool HasKey(
const llvm::Twine &key)
const;
565 llvm::Expected<PythonObject>
GetItem(
const llvm::Twine &key)
const;
576 static bool Check(PyObject *py_obj);
586 std::string s = std::string(name);
587 auto mod =
Import(s.c_str());
589 llvm::consumeError(mod.takeError());
592 return std::move(mod.get());
595 static llvm::Expected<PythonModule>
Import(
const llvm::Twine &name);
597 llvm::Expected<PythonObject>
Get(
const llvm::Twine &name);
614 static bool Check(PyObject *py_obj);
624 template <
typename Arg,
typename...
Args>
636 static bool Check(PyObject *py_obj);
639 const char *mode =
nullptr);
641 llvm::Expected<lldb::FileSP>
ConvertToFile(
bool borrowed =
false);
642 llvm::Expected<lldb::FileSP>
657 void log(llvm::raw_ostream &
OS)
const override;
659 bool Matches(PyObject *exc)
const;
686 return expected.get();
687 llvm::handleAllErrors(
689 [](
const llvm::ErrorInfoBase &E) {
690 PyErr_SetString(PyExc_Exception, E.message().c_str());
699 return std::move(expected.get());
700 llvm::consumeError(expected.takeError());
705 const PythonDictionary &globals,
706 const PythonDictionary &locals);
709 const PythonDictionary &globals,
710 const PythonDictionary &locals);
738 template <
typename...
Args>
741 return std::move(
error);
742 return function.Call(std::forward<Args>(args)...);
762 void Serialize(llvm::json::OStream &s)
const override;
770PyObject *
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