1*0b57cec5SDimitry Andric //===-- PythonDataObjects.h--------------------------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 99dba64beSDimitry Andric // 109dba64beSDimitry Andric // !! FIXME FIXME FIXME !! 119dba64beSDimitry Andric // 129dba64beSDimitry Andric // Python APIs nearly all can return an exception. They do this 139dba64beSDimitry Andric // by returning NULL, or -1, or some such value and setting 149dba64beSDimitry Andric // the exception state with PyErr_Set*(). Exceptions must be 159dba64beSDimitry Andric // handled before further python API functions are called. Failure 169dba64beSDimitry Andric // to do so will result in asserts on debug builds of python. 179dba64beSDimitry Andric // It will also sometimes, but not usually result in crashes of 189dba64beSDimitry Andric // release builds. 199dba64beSDimitry Andric // 209dba64beSDimitry Andric // Nearly all the code in this header does not handle python exceptions 219dba64beSDimitry Andric // correctly. It should all be converted to return Expected<> or 229dba64beSDimitry Andric // Error types to capture the exception. 239dba64beSDimitry Andric // 249dba64beSDimitry Andric // Everything in this file except functions that return Error or 259dba64beSDimitry Andric // Expected<> is considered deprecated and should not be 269dba64beSDimitry Andric // used in new code. If you need to use it, fix it first. 279dba64beSDimitry Andric // 289dba64beSDimitry Andric // 299dba64beSDimitry Andric // TODOs for this file 309dba64beSDimitry Andric // 319dba64beSDimitry Andric // * Make all methods safe for exceptions. 329dba64beSDimitry Andric // 339dba64beSDimitry Andric // * Eliminate method signatures that must translate exceptions into 349dba64beSDimitry Andric // empty objects or NULLs. Almost everything here should return 359dba64beSDimitry Andric // Expected<>. It should be acceptable for certain operations that 369dba64beSDimitry Andric // can never fail to assert instead, such as the creation of 379dba64beSDimitry Andric // PythonString from a string literal. 389dba64beSDimitry Andric // 395ffd83dbSDimitry Andric // * Eliminate Reset(), and make all non-default constructors private. 409dba64beSDimitry Andric // Python objects should be created with Retain<> or Take<>, and they 419dba64beSDimitry Andric // should be assigned with operator= 429dba64beSDimitry Andric // 439dba64beSDimitry Andric // * Eliminate default constructors, make python objects always 449dba64beSDimitry Andric // nonnull, and use optionals where necessary. 459dba64beSDimitry Andric // 469dba64beSDimitry Andric 479dba64beSDimitry Andric 48*0b57cec5SDimitry Andric #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 49*0b57cec5SDimitry Andric #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 50*0b57cec5SDimitry Andric 51480093f4SDimitry Andric #include "lldb/Host/Config.h" 52480093f4SDimitry Andric 53480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric // LLDB Python header must be included first 56*0b57cec5SDimitry Andric #include "lldb-python.h" 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric #include "lldb/Host/File.h" 59*0b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h" 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric namespace lldb_private { 649dba64beSDimitry Andric namespace python { 65*0b57cec5SDimitry Andric 669dba64beSDimitry Andric class PythonObject; 67*0b57cec5SDimitry Andric class PythonBytes; 68*0b57cec5SDimitry Andric class PythonString; 69*0b57cec5SDimitry Andric class PythonList; 70*0b57cec5SDimitry Andric class PythonDictionary; 71*0b57cec5SDimitry Andric class PythonInteger; 729dba64beSDimitry Andric class PythonException; 73*0b57cec5SDimitry Andric 74349cc55cSDimitry Andric class GIL { 75349cc55cSDimitry Andric public: 76349cc55cSDimitry Andric GIL() { 77349cc55cSDimitry Andric m_state = PyGILState_Ensure(); 78349cc55cSDimitry Andric assert(!PyErr_Occurred()); 79349cc55cSDimitry Andric } 80349cc55cSDimitry Andric ~GIL() { PyGILState_Release(m_state); } 81349cc55cSDimitry Andric 82349cc55cSDimitry Andric protected: 83349cc55cSDimitry Andric PyGILState_STATE m_state; 84349cc55cSDimitry Andric }; 85349cc55cSDimitry Andric 86*0b57cec5SDimitry Andric class StructuredPythonObject : public StructuredData::Generic { 87*0b57cec5SDimitry Andric public: 88*0b57cec5SDimitry Andric StructuredPythonObject() : StructuredData::Generic() {} 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric StructuredPythonObject(void *obj) : StructuredData::Generic(obj) { 91*0b57cec5SDimitry Andric Py_XINCREF(GetValue()); 92*0b57cec5SDimitry Andric } 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric ~StructuredPythonObject() override { 95*0b57cec5SDimitry Andric if (Py_IsInitialized()) 96*0b57cec5SDimitry Andric Py_XDECREF(GetValue()); 97*0b57cec5SDimitry Andric SetValue(nullptr); 98*0b57cec5SDimitry Andric } 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric bool IsValid() const override { return GetValue() && GetValue() != Py_None; } 101*0b57cec5SDimitry Andric 1029dba64beSDimitry Andric void Serialize(llvm::json::OStream &s) const override; 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric private: 1055ffd83dbSDimitry Andric StructuredPythonObject(const StructuredPythonObject &) = delete; 1065ffd83dbSDimitry Andric const StructuredPythonObject & 1075ffd83dbSDimitry Andric operator=(const StructuredPythonObject &) = delete; 108*0b57cec5SDimitry Andric }; 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric enum class PyObjectType { 111*0b57cec5SDimitry Andric Unknown, 112*0b57cec5SDimitry Andric None, 113*0b57cec5SDimitry Andric Boolean, 114*0b57cec5SDimitry Andric Integer, 115*0b57cec5SDimitry Andric Dictionary, 116*0b57cec5SDimitry Andric List, 117*0b57cec5SDimitry Andric String, 118*0b57cec5SDimitry Andric Bytes, 119*0b57cec5SDimitry Andric ByteArray, 120*0b57cec5SDimitry Andric Module, 121*0b57cec5SDimitry Andric Callable, 122*0b57cec5SDimitry Andric Tuple, 123*0b57cec5SDimitry Andric File 124*0b57cec5SDimitry Andric }; 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric enum class PyRefType { 127*0b57cec5SDimitry Andric Borrowed, // We are not given ownership of the incoming PyObject. 128*0b57cec5SDimitry Andric // We cannot safely hold it without calling Py_INCREF. 129*0b57cec5SDimitry Andric Owned // We have ownership of the incoming PyObject. We should 130*0b57cec5SDimitry Andric // not call Py_INCREF. 131*0b57cec5SDimitry Andric }; 132*0b57cec5SDimitry Andric 1339dba64beSDimitry Andric 1349dba64beSDimitry Andric // Take a reference that you already own, and turn it into 1359dba64beSDimitry Andric // a PythonObject. 1369dba64beSDimitry Andric // 1379dba64beSDimitry Andric // Most python API methods will return a +1 reference 1389dba64beSDimitry Andric // if they succeed or NULL if and only if 1399dba64beSDimitry Andric // they set an exception. Use this to collect such return 1409dba64beSDimitry Andric // values, after checking for NULL. 1419dba64beSDimitry Andric // 1429dba64beSDimitry Andric // If T is not just PythonObject, then obj must be already be 1439dba64beSDimitry Andric // checked to be of the correct type. 1449dba64beSDimitry Andric template <typename T> T Take(PyObject *obj) { 1459dba64beSDimitry Andric assert(obj); 1469dba64beSDimitry Andric assert(!PyErr_Occurred()); 1479dba64beSDimitry Andric T thing(PyRefType::Owned, obj); 1489dba64beSDimitry Andric assert(thing.IsValid()); 149480093f4SDimitry Andric return thing; 1509dba64beSDimitry Andric } 1519dba64beSDimitry Andric 1529dba64beSDimitry Andric // Retain a reference you have borrowed, and turn it into 1539dba64beSDimitry Andric // a PythonObject. 1549dba64beSDimitry Andric // 1559dba64beSDimitry Andric // A minority of python APIs return a borrowed reference 1569dba64beSDimitry Andric // instead of a +1. They will also return NULL if and only 1579dba64beSDimitry Andric // if they set an exception. Use this to collect such return 1589dba64beSDimitry Andric // values, after checking for NULL. 1599dba64beSDimitry Andric // 1609dba64beSDimitry Andric // If T is not just PythonObject, then obj must be already be 1619dba64beSDimitry Andric // checked to be of the correct type. 1629dba64beSDimitry Andric template <typename T> T Retain(PyObject *obj) { 1639dba64beSDimitry Andric assert(obj); 1649dba64beSDimitry Andric assert(!PyErr_Occurred()); 1659dba64beSDimitry Andric T thing(PyRefType::Borrowed, obj); 1669dba64beSDimitry Andric assert(thing.IsValid()); 167480093f4SDimitry Andric return thing; 1689dba64beSDimitry Andric } 1699dba64beSDimitry Andric 1709dba64beSDimitry Andric // This class can be used like a utility function to convert from 1719dba64beSDimitry Andric // a llvm-friendly Twine into a null-terminated const char *, 1729dba64beSDimitry Andric // which is the form python C APIs want their strings in. 1739dba64beSDimitry Andric // 1749dba64beSDimitry Andric // Example: 1759dba64beSDimitry Andric // const llvm::Twine &some_twine; 1769dba64beSDimitry Andric // PyFoo_Bar(x, y, z, NullTerminated(some_twine)); 1779dba64beSDimitry Andric // 1789dba64beSDimitry Andric // Why a class instead of a function? If the twine isn't already null 1799dba64beSDimitry Andric // terminated, it will need a temporary buffer to copy the string 1809dba64beSDimitry Andric // into. We need that buffer to stick around for the lifetime of the 1819dba64beSDimitry Andric // statement. 1829dba64beSDimitry Andric class NullTerminated { 1839dba64beSDimitry Andric const char *str; 1849dba64beSDimitry Andric llvm::SmallString<32> storage; 1859dba64beSDimitry Andric 1869dba64beSDimitry Andric public: 1879dba64beSDimitry Andric NullTerminated(const llvm::Twine &twine) { 1889dba64beSDimitry Andric llvm::StringRef ref = twine.toNullTerminatedStringRef(storage); 1899dba64beSDimitry Andric str = ref.begin(); 1909dba64beSDimitry Andric } 1919dba64beSDimitry Andric operator const char *() { return str; } 1929dba64beSDimitry Andric }; 1939dba64beSDimitry Andric 1949dba64beSDimitry Andric inline llvm::Error nullDeref() { 1959dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 1969dba64beSDimitry Andric "A NULL PyObject* was dereferenced"); 1979dba64beSDimitry Andric } 1989dba64beSDimitry Andric 1999dba64beSDimitry Andric inline llvm::Error exception(const char *s = nullptr) { 2009dba64beSDimitry Andric return llvm::make_error<PythonException>(s); 2019dba64beSDimitry Andric } 2029dba64beSDimitry Andric 2039dba64beSDimitry Andric inline llvm::Error keyError() { 2049dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 2059dba64beSDimitry Andric "key not in dict"); 2069dba64beSDimitry Andric } 2079dba64beSDimitry Andric 208480093f4SDimitry Andric #if PY_MAJOR_VERSION < 3 209480093f4SDimitry Andric // The python 2 API declares some arguments as char* that should 210480093f4SDimitry Andric // be const char *, but it doesn't actually modify them. 211480093f4SDimitry Andric inline char *py2_const_cast(const char *s) { return const_cast<char *>(s); } 212480093f4SDimitry Andric #else 213480093f4SDimitry Andric inline const char *py2_const_cast(const char *s) { return s; } 214480093f4SDimitry Andric #endif 215480093f4SDimitry Andric 216*0b57cec5SDimitry Andric enum class PyInitialValue { Invalid, Empty }; 217*0b57cec5SDimitry Andric 2189dba64beSDimitry Andric template <typename T, typename Enable = void> struct PythonFormat; 2199dba64beSDimitry Andric 2209dba64beSDimitry Andric template <> struct PythonFormat<unsigned long long> { 2219dba64beSDimitry Andric static constexpr char format = 'K'; 2229dba64beSDimitry Andric static auto get(unsigned long long value) { return value; } 2239dba64beSDimitry Andric }; 2249dba64beSDimitry Andric 2259dba64beSDimitry Andric template <> struct PythonFormat<long long> { 2269dba64beSDimitry Andric static constexpr char format = 'L'; 2279dba64beSDimitry Andric static auto get(long long value) { return value; } 2289dba64beSDimitry Andric }; 2299dba64beSDimitry Andric 2309dba64beSDimitry Andric template <> struct PythonFormat<PyObject *> { 2319dba64beSDimitry Andric static constexpr char format = 'O'; 2329dba64beSDimitry Andric static auto get(PyObject *value) { return value; } 2339dba64beSDimitry Andric }; 2349dba64beSDimitry Andric 2359dba64beSDimitry Andric template <typename T> 2369dba64beSDimitry Andric struct PythonFormat< 2379dba64beSDimitry Andric T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> { 2389dba64beSDimitry Andric static constexpr char format = 'O'; 2399dba64beSDimitry Andric static auto get(const T &value) { return value.get(); } 2409dba64beSDimitry Andric }; 2419dba64beSDimitry Andric 242*0b57cec5SDimitry Andric class PythonObject { 243*0b57cec5SDimitry Andric public: 244fe6060f1SDimitry Andric PythonObject() = default; 245*0b57cec5SDimitry Andric 2469dba64beSDimitry Andric PythonObject(PyRefType type, PyObject *py_obj) { 247*0b57cec5SDimitry Andric m_py_obj = py_obj; 248*0b57cec5SDimitry Andric // If this is a borrowed reference, we need to convert it to 249*0b57cec5SDimitry Andric // an owned reference by incrementing it. If it is an owned 250*0b57cec5SDimitry Andric // reference (for example the caller allocated it with PyDict_New() 251*0b57cec5SDimitry Andric // then we must *not* increment it. 2529dba64beSDimitry Andric if (m_py_obj && Py_IsInitialized() && type == PyRefType::Borrowed) 253*0b57cec5SDimitry Andric Py_XINCREF(m_py_obj); 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric 2569dba64beSDimitry Andric PythonObject(const PythonObject &rhs) 2579dba64beSDimitry Andric : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {} 2589dba64beSDimitry Andric 2599dba64beSDimitry Andric PythonObject(PythonObject &&rhs) { 2609dba64beSDimitry Andric m_py_obj = rhs.m_py_obj; 2619dba64beSDimitry Andric rhs.m_py_obj = nullptr; 2629dba64beSDimitry Andric } 2639dba64beSDimitry Andric 2649dba64beSDimitry Andric ~PythonObject() { Reset(); } 2659dba64beSDimitry Andric 2669dba64beSDimitry Andric void Reset() { 2679dba64beSDimitry Andric if (m_py_obj && Py_IsInitialized()) 2689dba64beSDimitry Andric Py_DECREF(m_py_obj); 2699dba64beSDimitry Andric m_py_obj = nullptr; 2709dba64beSDimitry Andric } 2719dba64beSDimitry Andric 272*0b57cec5SDimitry Andric void Dump() const { 273*0b57cec5SDimitry Andric if (m_py_obj) 274*0b57cec5SDimitry Andric _PyObject_Dump(m_py_obj); 275*0b57cec5SDimitry Andric else 276*0b57cec5SDimitry Andric puts("NULL"); 277*0b57cec5SDimitry Andric } 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric void Dump(Stream &strm) const; 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric PyObject *get() const { return m_py_obj; } 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric PyObject *release() { 284*0b57cec5SDimitry Andric PyObject *result = m_py_obj; 285*0b57cec5SDimitry Andric m_py_obj = nullptr; 286*0b57cec5SDimitry Andric return result; 287*0b57cec5SDimitry Andric } 288*0b57cec5SDimitry Andric 2899dba64beSDimitry Andric PythonObject &operator=(PythonObject other) { 2909dba64beSDimitry Andric Reset(); 2919dba64beSDimitry Andric m_py_obj = std::exchange(other.m_py_obj, nullptr); 292*0b57cec5SDimitry Andric return *this; 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric 295*0b57cec5SDimitry Andric PyObjectType GetObjectType() const; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric PythonString Repr() const; 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric PythonString Str() const; 300*0b57cec5SDimitry Andric 301*0b57cec5SDimitry Andric static PythonObject ResolveNameWithDictionary(llvm::StringRef name, 302*0b57cec5SDimitry Andric const PythonDictionary &dict); 303*0b57cec5SDimitry Andric 304*0b57cec5SDimitry Andric template <typename T> 305*0b57cec5SDimitry Andric static T ResolveNameWithDictionary(llvm::StringRef name, 306*0b57cec5SDimitry Andric const PythonDictionary &dict) { 307*0b57cec5SDimitry Andric return ResolveNameWithDictionary(name, dict).AsType<T>(); 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric PythonObject ResolveName(llvm::StringRef name) const; 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric template <typename T> T ResolveName(llvm::StringRef name) const { 313*0b57cec5SDimitry Andric return ResolveName(name).AsType<T>(); 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric bool HasAttribute(llvm::StringRef attribute) const; 317*0b57cec5SDimitry Andric 318*0b57cec5SDimitry Andric PythonObject GetAttributeValue(llvm::StringRef attribute) const; 319*0b57cec5SDimitry Andric 3209dba64beSDimitry Andric bool IsNone() const { return m_py_obj == Py_None; } 321*0b57cec5SDimitry Andric 3229dba64beSDimitry Andric bool IsValid() const { return m_py_obj != nullptr; } 323*0b57cec5SDimitry Andric 3249dba64beSDimitry Andric bool IsAllocated() const { return IsValid() && !IsNone(); } 3259dba64beSDimitry Andric 3269dba64beSDimitry Andric explicit operator bool() const { return IsValid() && !IsNone(); } 327*0b57cec5SDimitry Andric 328*0b57cec5SDimitry Andric template <typename T> T AsType() const { 329*0b57cec5SDimitry Andric if (!T::Check(m_py_obj)) 330*0b57cec5SDimitry Andric return T(); 331*0b57cec5SDimitry Andric return T(PyRefType::Borrowed, m_py_obj); 332*0b57cec5SDimitry Andric } 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andric StructuredData::ObjectSP CreateStructuredObject() const; 335*0b57cec5SDimitry Andric 3369dba64beSDimitry Andric template <typename... T> 3379dba64beSDimitry Andric llvm::Expected<PythonObject> CallMethod(const char *name, 3389dba64beSDimitry Andric const T &... t) const { 3399dba64beSDimitry Andric const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; 3409dba64beSDimitry Andric PyObject *obj = 3419dba64beSDimitry Andric PyObject_CallMethod(m_py_obj, py2_const_cast(name), 3429dba64beSDimitry Andric py2_const_cast(format), PythonFormat<T>::get(t)...); 3439dba64beSDimitry Andric if (!obj) 3449dba64beSDimitry Andric return exception(); 3459dba64beSDimitry Andric return python::Take<PythonObject>(obj); 3469dba64beSDimitry Andric } 3479dba64beSDimitry Andric 3489dba64beSDimitry Andric template <typename... T> 3499dba64beSDimitry Andric llvm::Expected<PythonObject> Call(const T &... t) const { 3509dba64beSDimitry Andric const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; 3519dba64beSDimitry Andric PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format), 3529dba64beSDimitry Andric PythonFormat<T>::get(t)...); 3539dba64beSDimitry Andric if (!obj) 3549dba64beSDimitry Andric return exception(); 3559dba64beSDimitry Andric return python::Take<PythonObject>(obj); 3569dba64beSDimitry Andric } 3579dba64beSDimitry Andric 3589dba64beSDimitry Andric llvm::Expected<PythonObject> GetAttribute(const llvm::Twine &name) const { 3599dba64beSDimitry Andric if (!m_py_obj) 3609dba64beSDimitry Andric return nullDeref(); 3619dba64beSDimitry Andric PyObject *obj = PyObject_GetAttrString(m_py_obj, NullTerminated(name)); 3629dba64beSDimitry Andric if (!obj) 3639dba64beSDimitry Andric return exception(); 3649dba64beSDimitry Andric return python::Take<PythonObject>(obj); 3659dba64beSDimitry Andric } 3669dba64beSDimitry Andric 3679dba64beSDimitry Andric llvm::Expected<bool> IsTrue() { 3689dba64beSDimitry Andric if (!m_py_obj) 3699dba64beSDimitry Andric return nullDeref(); 3709dba64beSDimitry Andric int r = PyObject_IsTrue(m_py_obj); 3719dba64beSDimitry Andric if (r < 0) 3729dba64beSDimitry Andric return exception(); 3739dba64beSDimitry Andric return !!r; 3749dba64beSDimitry Andric } 3759dba64beSDimitry Andric 3765ffd83dbSDimitry Andric llvm::Expected<long long> AsLongLong() const; 3775ffd83dbSDimitry Andric 3785ffd83dbSDimitry Andric llvm::Expected<long long> AsUnsignedLongLong() const; 3795ffd83dbSDimitry Andric 3805ffd83dbSDimitry Andric // wraps on overflow, instead of raising an error. 3815ffd83dbSDimitry Andric llvm::Expected<unsigned long long> AsModuloUnsignedLongLong() const; 3829dba64beSDimitry Andric 3839dba64beSDimitry Andric llvm::Expected<bool> IsInstance(const PythonObject &cls) { 3849dba64beSDimitry Andric if (!m_py_obj || !cls.IsValid()) 3859dba64beSDimitry Andric return nullDeref(); 3869dba64beSDimitry Andric int r = PyObject_IsInstance(m_py_obj, cls.get()); 3879dba64beSDimitry Andric if (r < 0) 3889dba64beSDimitry Andric return exception(); 3899dba64beSDimitry Andric return !!r; 3909dba64beSDimitry Andric } 3919dba64beSDimitry Andric 3929dba64beSDimitry Andric protected: 393fe6060f1SDimitry Andric PyObject *m_py_obj = nullptr; 394*0b57cec5SDimitry Andric }; 395*0b57cec5SDimitry Andric 3969dba64beSDimitry Andric 3979dba64beSDimitry Andric // This is why C++ needs monads. 3989dba64beSDimitry Andric template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) { 3999dba64beSDimitry Andric if (!obj) 4009dba64beSDimitry Andric return obj.takeError(); 4019dba64beSDimitry Andric if (!T::Check(obj.get().get())) 4029dba64beSDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(), 4039dba64beSDimitry Andric "type error"); 4049dba64beSDimitry Andric return T(PyRefType::Borrowed, std::move(obj.get().get())); 4059dba64beSDimitry Andric } 4069dba64beSDimitry Andric 4079dba64beSDimitry Andric template <> llvm::Expected<bool> As<bool>(llvm::Expected<PythonObject> &&obj); 4089dba64beSDimitry Andric 4099dba64beSDimitry Andric template <> 4109dba64beSDimitry Andric llvm::Expected<long long> As<long long>(llvm::Expected<PythonObject> &&obj); 4119dba64beSDimitry Andric 4129dba64beSDimitry Andric template <> 4135ffd83dbSDimitry Andric llvm::Expected<unsigned long long> 4145ffd83dbSDimitry Andric As<unsigned long long>(llvm::Expected<PythonObject> &&obj); 4155ffd83dbSDimitry Andric 4165ffd83dbSDimitry Andric template <> 4179dba64beSDimitry Andric llvm::Expected<std::string> As<std::string>(llvm::Expected<PythonObject> &&obj); 4189dba64beSDimitry Andric 4199dba64beSDimitry Andric 4209dba64beSDimitry Andric template <class T> class TypedPythonObject : public PythonObject { 421*0b57cec5SDimitry Andric public: 4229dba64beSDimitry Andric // override to perform implicit type conversions on Reset 4239dba64beSDimitry Andric // This can be eliminated once we drop python 2 support. 4249dba64beSDimitry Andric static void Convert(PyRefType &type, PyObject *&py_obj) {} 4259dba64beSDimitry Andric 4269dba64beSDimitry Andric TypedPythonObject(PyRefType type, PyObject *py_obj) { 4279dba64beSDimitry Andric if (!py_obj) 4289dba64beSDimitry Andric return; 4299dba64beSDimitry Andric T::Convert(type, py_obj); 4309dba64beSDimitry Andric if (T::Check(py_obj)) 4319dba64beSDimitry Andric PythonObject::operator=(PythonObject(type, py_obj)); 4329dba64beSDimitry Andric else if (type == PyRefType::Owned) 4339dba64beSDimitry Andric Py_DECREF(py_obj); 4349dba64beSDimitry Andric } 4359dba64beSDimitry Andric 436fe6060f1SDimitry Andric TypedPythonObject() = default; 4379dba64beSDimitry Andric }; 4389dba64beSDimitry Andric 4399dba64beSDimitry Andric class PythonBytes : public TypedPythonObject<PythonBytes> { 4409dba64beSDimitry Andric public: 4419dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 442*0b57cec5SDimitry Andric explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes); 443*0b57cec5SDimitry Andric PythonBytes(const uint8_t *bytes, size_t length); 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> GetBytes() const; 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric size_t GetSize() const; 450*0b57cec5SDimitry Andric 451*0b57cec5SDimitry Andric void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric StructuredData::StringSP CreateStructuredString() const; 454*0b57cec5SDimitry Andric }; 455*0b57cec5SDimitry Andric 4569dba64beSDimitry Andric class PythonByteArray : public TypedPythonObject<PythonByteArray> { 457*0b57cec5SDimitry Andric public: 4589dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 459*0b57cec5SDimitry Andric explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes); 460*0b57cec5SDimitry Andric PythonByteArray(const uint8_t *bytes, size_t length); 461*0b57cec5SDimitry Andric PythonByteArray(const PythonBytes &object); 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 464*0b57cec5SDimitry Andric 465*0b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> GetBytes() const; 466*0b57cec5SDimitry Andric 467*0b57cec5SDimitry Andric size_t GetSize() const; 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); 470*0b57cec5SDimitry Andric 471*0b57cec5SDimitry Andric StructuredData::StringSP CreateStructuredString() const; 472*0b57cec5SDimitry Andric }; 473*0b57cec5SDimitry Andric 4749dba64beSDimitry Andric class PythonString : public TypedPythonObject<PythonString> { 475*0b57cec5SDimitry Andric public: 4769dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 4779dba64beSDimitry Andric static llvm::Expected<PythonString> FromUTF8(llvm::StringRef string); 478*0b57cec5SDimitry Andric 4799dba64beSDimitry Andric PythonString() : TypedPythonObject() {} // MSVC requires this for some reason 4809dba64beSDimitry Andric 4819dba64beSDimitry Andric explicit PythonString(llvm::StringRef string); // safe, null on error 482*0b57cec5SDimitry Andric 483*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 4849dba64beSDimitry Andric static void Convert(PyRefType &type, PyObject *&py_obj); 485*0b57cec5SDimitry Andric 4869dba64beSDimitry Andric llvm::StringRef GetString() const; // safe, empty string on error 487*0b57cec5SDimitry Andric 4889dba64beSDimitry Andric llvm::Expected<llvm::StringRef> AsUTF8() const; 489*0b57cec5SDimitry Andric 490*0b57cec5SDimitry Andric size_t GetSize() const; 491*0b57cec5SDimitry Andric 4929dba64beSDimitry Andric void SetString(llvm::StringRef string); // safe, null on error 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric StructuredData::StringSP CreateStructuredString() const; 495*0b57cec5SDimitry Andric }; 496*0b57cec5SDimitry Andric 4979dba64beSDimitry Andric class PythonInteger : public TypedPythonObject<PythonInteger> { 498*0b57cec5SDimitry Andric public: 4999dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 500*0b57cec5SDimitry Andric 5019dba64beSDimitry Andric PythonInteger() : TypedPythonObject() {} // MSVC requires this for some reason 5029dba64beSDimitry Andric 5039dba64beSDimitry Andric explicit PythonInteger(int64_t value); 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 5069dba64beSDimitry Andric static void Convert(PyRefType &type, PyObject *&py_obj); 507*0b57cec5SDimitry Andric 508*0b57cec5SDimitry Andric void SetInteger(int64_t value); 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric StructuredData::IntegerSP CreateStructuredInteger() const; 511*0b57cec5SDimitry Andric }; 512*0b57cec5SDimitry Andric 5139dba64beSDimitry Andric class PythonBoolean : public TypedPythonObject<PythonBoolean> { 514*0b57cec5SDimitry Andric public: 5159dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 516*0b57cec5SDimitry Andric 5179dba64beSDimitry Andric explicit PythonBoolean(bool value); 518*0b57cec5SDimitry Andric 519*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 520*0b57cec5SDimitry Andric 521*0b57cec5SDimitry Andric bool GetValue() const; 522*0b57cec5SDimitry Andric 523*0b57cec5SDimitry Andric void SetValue(bool value); 524*0b57cec5SDimitry Andric 525*0b57cec5SDimitry Andric StructuredData::BooleanSP CreateStructuredBoolean() const; 526*0b57cec5SDimitry Andric }; 527*0b57cec5SDimitry Andric 5289dba64beSDimitry Andric class PythonList : public TypedPythonObject<PythonList> { 529*0b57cec5SDimitry Andric public: 5309dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 5319dba64beSDimitry Andric 5329dba64beSDimitry Andric PythonList() : TypedPythonObject() {} // MSVC requires this for some reason 5339dba64beSDimitry Andric 534*0b57cec5SDimitry Andric explicit PythonList(PyInitialValue value); 535*0b57cec5SDimitry Andric explicit PythonList(int list_size); 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 538*0b57cec5SDimitry Andric 539*0b57cec5SDimitry Andric uint32_t GetSize() const; 540*0b57cec5SDimitry Andric 541*0b57cec5SDimitry Andric PythonObject GetItemAtIndex(uint32_t index) const; 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric void SetItemAtIndex(uint32_t index, const PythonObject &object); 544*0b57cec5SDimitry Andric 545*0b57cec5SDimitry Andric void AppendItem(const PythonObject &object); 546*0b57cec5SDimitry Andric 547*0b57cec5SDimitry Andric StructuredData::ArraySP CreateStructuredArray() const; 548*0b57cec5SDimitry Andric }; 549*0b57cec5SDimitry Andric 5509dba64beSDimitry Andric class PythonTuple : public TypedPythonObject<PythonTuple> { 551*0b57cec5SDimitry Andric public: 5529dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 5539dba64beSDimitry Andric 554*0b57cec5SDimitry Andric explicit PythonTuple(PyInitialValue value); 555*0b57cec5SDimitry Andric explicit PythonTuple(int tuple_size); 556*0b57cec5SDimitry Andric PythonTuple(std::initializer_list<PythonObject> objects); 557*0b57cec5SDimitry Andric PythonTuple(std::initializer_list<PyObject *> objects); 558*0b57cec5SDimitry Andric 559*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 560*0b57cec5SDimitry Andric 561*0b57cec5SDimitry Andric uint32_t GetSize() const; 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric PythonObject GetItemAtIndex(uint32_t index) const; 564*0b57cec5SDimitry Andric 565*0b57cec5SDimitry Andric void SetItemAtIndex(uint32_t index, const PythonObject &object); 566*0b57cec5SDimitry Andric 567*0b57cec5SDimitry Andric StructuredData::ArraySP CreateStructuredArray() const; 568*0b57cec5SDimitry Andric }; 569*0b57cec5SDimitry Andric 5709dba64beSDimitry Andric class PythonDictionary : public TypedPythonObject<PythonDictionary> { 571*0b57cec5SDimitry Andric public: 5729dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 573*0b57cec5SDimitry Andric 5749dba64beSDimitry Andric PythonDictionary() : TypedPythonObject() {} // MSVC requires this for some reason 5759dba64beSDimitry Andric 5769dba64beSDimitry Andric explicit PythonDictionary(PyInitialValue value); 577*0b57cec5SDimitry Andric 578*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 579*0b57cec5SDimitry Andric 580*0b57cec5SDimitry Andric uint32_t GetSize() const; 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric PythonList GetKeys() const; 583*0b57cec5SDimitry Andric 5849dba64beSDimitry Andric PythonObject GetItemForKey(const PythonObject &key) const; // DEPRECATED 5859dba64beSDimitry Andric void SetItemForKey(const PythonObject &key, 5869dba64beSDimitry Andric const PythonObject &value); // DEPRECATED 5879dba64beSDimitry Andric 5889dba64beSDimitry Andric llvm::Expected<PythonObject> GetItem(const PythonObject &key) const; 5899dba64beSDimitry Andric llvm::Expected<PythonObject> GetItem(const llvm::Twine &key) const; 5909dba64beSDimitry Andric llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const; 5919dba64beSDimitry Andric llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const; 592*0b57cec5SDimitry Andric 593*0b57cec5SDimitry Andric StructuredData::DictionarySP CreateStructuredDictionary() const; 594*0b57cec5SDimitry Andric }; 595*0b57cec5SDimitry Andric 5969dba64beSDimitry Andric class PythonModule : public TypedPythonObject<PythonModule> { 597*0b57cec5SDimitry Andric public: 5989dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 599*0b57cec5SDimitry Andric 600*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric static PythonModule BuiltinsModule(); 603*0b57cec5SDimitry Andric 604*0b57cec5SDimitry Andric static PythonModule MainModule(); 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric static PythonModule AddModule(llvm::StringRef module); 607*0b57cec5SDimitry Andric 6089dba64beSDimitry Andric // safe, returns invalid on error; 6099dba64beSDimitry Andric static PythonModule ImportModule(llvm::StringRef name) { 6105ffd83dbSDimitry Andric std::string s = std::string(name); 6119dba64beSDimitry Andric auto mod = Import(s.c_str()); 6129dba64beSDimitry Andric if (!mod) { 6139dba64beSDimitry Andric llvm::consumeError(mod.takeError()); 6149dba64beSDimitry Andric return PythonModule(); 6159dba64beSDimitry Andric } 6169dba64beSDimitry Andric return std::move(mod.get()); 6179dba64beSDimitry Andric } 618*0b57cec5SDimitry Andric 6199dba64beSDimitry Andric static llvm::Expected<PythonModule> Import(const llvm::Twine &name); 620*0b57cec5SDimitry Andric 6219dba64beSDimitry Andric llvm::Expected<PythonObject> Get(const llvm::Twine &name); 622*0b57cec5SDimitry Andric 623*0b57cec5SDimitry Andric PythonDictionary GetDictionary() const; 624*0b57cec5SDimitry Andric }; 625*0b57cec5SDimitry Andric 6269dba64beSDimitry Andric class PythonCallable : public TypedPythonObject<PythonCallable> { 627*0b57cec5SDimitry Andric public: 6289dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 6299dba64beSDimitry Andric 630*0b57cec5SDimitry Andric struct ArgInfo { 6319dba64beSDimitry Andric /* the largest number of positional arguments this callable 6329dba64beSDimitry Andric * can accept, or UNBOUNDED, ie UINT_MAX if it's a varargs 6339dba64beSDimitry Andric * function and can accept an arbitrary number */ 6349dba64beSDimitry Andric unsigned max_positional_args; 6359dba64beSDimitry Andric static constexpr unsigned UNBOUNDED = UINT_MAX; // FIXME c++17 inline 636*0b57cec5SDimitry Andric }; 637*0b57cec5SDimitry Andric 638*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 639*0b57cec5SDimitry Andric 6409dba64beSDimitry Andric llvm::Expected<ArgInfo> GetArgInfo() const; 641*0b57cec5SDimitry Andric 642*0b57cec5SDimitry Andric PythonObject operator()(); 643*0b57cec5SDimitry Andric 644*0b57cec5SDimitry Andric PythonObject operator()(std::initializer_list<PyObject *> args); 645*0b57cec5SDimitry Andric 646*0b57cec5SDimitry Andric PythonObject operator()(std::initializer_list<PythonObject> args); 647*0b57cec5SDimitry Andric 648*0b57cec5SDimitry Andric template <typename Arg, typename... Args> 649*0b57cec5SDimitry Andric PythonObject operator()(const Arg &arg, Args... args) { 650*0b57cec5SDimitry Andric return operator()({arg, args...}); 651*0b57cec5SDimitry Andric } 652*0b57cec5SDimitry Andric }; 653*0b57cec5SDimitry Andric 6549dba64beSDimitry Andric class PythonFile : public TypedPythonObject<PythonFile> { 655*0b57cec5SDimitry Andric public: 6569dba64beSDimitry Andric using TypedPythonObject::TypedPythonObject; 657*0b57cec5SDimitry Andric 6589dba64beSDimitry Andric PythonFile() : TypedPythonObject() {} // MSVC requires this for some reason 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric static bool Check(PyObject *py_obj); 661*0b57cec5SDimitry Andric 6629dba64beSDimitry Andric static llvm::Expected<PythonFile> FromFile(File &file, 6639dba64beSDimitry Andric const char *mode = nullptr); 664*0b57cec5SDimitry Andric 6659dba64beSDimitry Andric llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false); 6669dba64beSDimitry Andric llvm::Expected<lldb::FileSP> 6679dba64beSDimitry Andric ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false); 668*0b57cec5SDimitry Andric }; 669*0b57cec5SDimitry Andric 6709dba64beSDimitry Andric class PythonException : public llvm::ErrorInfo<PythonException> { 6719dba64beSDimitry Andric private: 6729dba64beSDimitry Andric PyObject *m_exception_type, *m_exception, *m_traceback; 6739dba64beSDimitry Andric PyObject *m_repr_bytes; 6749dba64beSDimitry Andric 6759dba64beSDimitry Andric public: 6769dba64beSDimitry Andric static char ID; 6779dba64beSDimitry Andric const char *toCString() const; 6789dba64beSDimitry Andric PythonException(const char *caller = nullptr); 6799dba64beSDimitry Andric void Restore(); 6809dba64beSDimitry Andric ~PythonException(); 6819dba64beSDimitry Andric void log(llvm::raw_ostream &OS) const override; 6829dba64beSDimitry Andric std::error_code convertToErrorCode() const override; 6839dba64beSDimitry Andric bool Matches(PyObject *exc) const; 6849dba64beSDimitry Andric std::string ReadBacktrace() const; 6859dba64beSDimitry Andric }; 6869dba64beSDimitry Andric 6879dba64beSDimitry Andric // This extracts the underlying T out of an Expected<T> and returns it. 6889dba64beSDimitry Andric // If the Expected is an Error instead of a T, that error will be converted 6899dba64beSDimitry Andric // into a python exception, and this will return a default-constructed T. 6909dba64beSDimitry Andric // 6919dba64beSDimitry Andric // This is appropriate for use right at the boundary of python calling into 6929dba64beSDimitry Andric // C++, such as in a SWIG typemap. In such a context you should simply 6939dba64beSDimitry Andric // check if the returned T is valid, and if it is, return a NULL back 6949dba64beSDimitry Andric // to python. This will result in the Error being raised as an exception 6959dba64beSDimitry Andric // from python code's point of view. 6969dba64beSDimitry Andric // 6979dba64beSDimitry Andric // For example: 6989dba64beSDimitry Andric // ``` 6999dba64beSDimitry Andric // Expected<Foo *> efoop = some_cpp_function(); 7009dba64beSDimitry Andric // Foo *foop = unwrapOrSetPythonException(efoop); 7019dba64beSDimitry Andric // if (!foop) 7029dba64beSDimitry Andric // return NULL; 7039dba64beSDimitry Andric // do_something(*foop); 7049dba64beSDimitry Andric // 7059dba64beSDimitry Andric // If the Error returned was itself created because a python exception was 7069dba64beSDimitry Andric // raised when C++ code called into python, then the original exception 7079dba64beSDimitry Andric // will be restored. Otherwise a simple string exception will be raised. 7089dba64beSDimitry Andric template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) { 7099dba64beSDimitry Andric if (expected) 7109dba64beSDimitry Andric return expected.get(); 7119dba64beSDimitry Andric llvm::handleAllErrors( 7129dba64beSDimitry Andric expected.takeError(), [](PythonException &E) { E.Restore(); }, 7139dba64beSDimitry Andric [](const llvm::ErrorInfoBase &E) { 7149dba64beSDimitry Andric PyErr_SetString(PyExc_Exception, E.message().c_str()); 7159dba64beSDimitry Andric }); 7169dba64beSDimitry Andric return T(); 7179dba64beSDimitry Andric } 7189dba64beSDimitry Andric 7199dba64beSDimitry Andric // This is only here to help incrementally migrate old, exception-unsafe 7209dba64beSDimitry Andric // code. 7219dba64beSDimitry Andric template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) { 7229dba64beSDimitry Andric if (expected) 7239dba64beSDimitry Andric return std::move(expected.get()); 7249dba64beSDimitry Andric llvm::consumeError(expected.takeError()); 7259dba64beSDimitry Andric return T(); 7269dba64beSDimitry Andric } 7279dba64beSDimitry Andric 7289dba64beSDimitry Andric llvm::Expected<PythonObject> runStringOneLine(const llvm::Twine &string, 7299dba64beSDimitry Andric const PythonDictionary &globals, 7309dba64beSDimitry Andric const PythonDictionary &locals); 7319dba64beSDimitry Andric 7329dba64beSDimitry Andric llvm::Expected<PythonObject> runStringMultiLine(const llvm::Twine &string, 7339dba64beSDimitry Andric const PythonDictionary &globals, 7349dba64beSDimitry Andric const PythonDictionary &locals); 7359dba64beSDimitry Andric 7369dba64beSDimitry Andric // Sometimes the best way to interact with a python interpreter is 7379dba64beSDimitry Andric // to run some python code. You construct a PythonScript with 7389dba64beSDimitry Andric // script string. The script assigns some function to `_function_` 7399dba64beSDimitry Andric // and you get a C++ callable object that calls the python function. 7409dba64beSDimitry Andric // 7419dba64beSDimitry Andric // Example: 7429dba64beSDimitry Andric // 7439dba64beSDimitry Andric // const char script[] = R"( 7449dba64beSDimitry Andric // def main(x, y): 7459dba64beSDimitry Andric // .... 7469dba64beSDimitry Andric // )"; 7479dba64beSDimitry Andric // 7489dba64beSDimitry Andric // Expected<PythonObject> cpp_foo_wrapper(PythonObject x, PythonObject y) { 7499dba64beSDimitry Andric // // no need to synchronize access to this global, we already have the GIL 7509dba64beSDimitry Andric // static PythonScript foo(script) 7519dba64beSDimitry Andric // return foo(x, y); 7529dba64beSDimitry Andric // } 7539dba64beSDimitry Andric class PythonScript { 7549dba64beSDimitry Andric const char *script; 7559dba64beSDimitry Andric PythonCallable function; 7569dba64beSDimitry Andric 7579dba64beSDimitry Andric llvm::Error Init(); 7589dba64beSDimitry Andric 7599dba64beSDimitry Andric public: 7609dba64beSDimitry Andric PythonScript(const char *script) : script(script), function() {} 7619dba64beSDimitry Andric 7629dba64beSDimitry Andric template <typename... Args> 7639dba64beSDimitry Andric llvm::Expected<PythonObject> operator()(Args &&... args) { 7649dba64beSDimitry Andric if (llvm::Error error = Init()) 7659dba64beSDimitry Andric return std::move(error); 7669dba64beSDimitry Andric return function.Call(std::forward<Args>(args)...); 7679dba64beSDimitry Andric } 7689dba64beSDimitry Andric }; 7699dba64beSDimitry Andric 7709dba64beSDimitry Andric } // namespace python 771*0b57cec5SDimitry Andric } // namespace lldb_private 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric #endif 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 776