LLDB mainline
PythonDataObjects.h
Go to the documentation of this file.
1//===-- PythonDataObjects.h--------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9//
10// !! FIXME FIXME FIXME !!
11//
12// Python APIs nearly all can return an exception. They do this
13// by returning NULL, or -1, or some such value and setting
14// the exception state with PyErr_Set*(). Exceptions must be
15// handled before further python API functions are called. Failure
16// to do so will result in asserts on debug builds of python.
17// It will also sometimes, but not usually result in crashes of
18// release builds.
19//
20// Nearly all the code in this header does not handle python exceptions
21// correctly. It should all be converted to return Expected<> or
22// Error types to capture the exception.
23//
24// Everything in this file except functions that return Error or
25// Expected<> is considered deprecated and should not be
26// used in new code. If you need to use it, fix it first.
27//
28//
29// TODOs for this file
30//
31// * Make all methods safe for exceptions.
32//
33// * Eliminate method signatures that must translate exceptions into
34// empty objects or NULLs. Almost everything here should return
35// Expected<>. It should be acceptable for certain operations that
36// can never fail to assert instead, such as the creation of
37// PythonString from a string literal.
38//
39// * Eliminate Reset(), and make all non-default constructors private.
40// Python objects should be created with Retain<> or Take<>, and they
41// should be assigned with operator=
42//
43// * Eliminate default constructors, make python objects always
44// nonnull, and use optionals where necessary.
45//
46
47#ifndef LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
48#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
49
50// clang-format off
51// LLDB Python header must be included first
52#include "lldb-python.h"
53//clang-format on
54
55#include "lldb/Host/File.h"
57
58#include "llvm/ADT/ArrayRef.h"
59
60namespace lldb_private {
61namespace python {
62
63class PythonObject;
64class PythonBytes;
65class PythonString;
66class PythonList;
68class PythonInteger;
69class PythonException;
70
71class GIL {
72public:
73 GIL() {
74 m_state = PyGILState_Ensure();
75 assert(!PyErr_Occurred());
76 }
77 ~GIL() { PyGILState_Release(m_state); }
78
79protected:
80 PyGILState_STATE m_state;
81};
82
98
99enum class PyRefType {
100 Borrowed, // We are not given ownership of the incoming PyObject.
101 // We cannot safely hold it without calling Py_INCREF.
102 Owned // We have ownership of the incoming PyObject. We should
103 // not call Py_INCREF.
104};
105
106
107// Take a reference that you already own, and turn it into
108// a PythonObject.
109//
110// Most python API methods will return a +1 reference
111// if they succeed or NULL if and only if
112// they set an exception. Use this to collect such return
113// values, after checking for NULL.
114//
115// If T is not just PythonObject, then obj must be already be
116// checked to be of the correct type.
117template <typename T> T Take(PyObject *obj) {
118 assert(obj);
119 assert(!PyErr_Occurred());
120 T thing(PyRefType::Owned, obj);
121 assert(thing.IsValid());
122 return thing;
123}
124
125// Retain a reference you have borrowed, and turn it into
126// a PythonObject.
127//
128// A minority of python APIs return a borrowed reference
129// instead of a +1. They will also return NULL if and only
130// if they set an exception. Use this to collect such return
131// values, after checking for NULL.
132//
133// If T is not just PythonObject, then obj must be already be
134// checked to be of the correct type.
135template <typename T> T Retain(PyObject *obj) {
136 assert(obj);
137 assert(!PyErr_Occurred());
138 T thing(PyRefType::Borrowed, obj);
139 assert(thing.IsValid());
140 return thing;
141}
142
143// This class can be used like a utility function to convert from
144// a llvm-friendly Twine into a null-terminated const char *,
145// which is the form python C APIs want their strings in.
146//
147// Example:
148// const llvm::Twine &some_twine;
149// PyFoo_Bar(x, y, z, NullTerminated(some_twine));
150//
151// Why a class instead of a function? If the twine isn't already null
152// terminated, it will need a temporary buffer to copy the string
153// into. We need that buffer to stick around for the lifetime of the
154// statement.
156 const char *str;
157 llvm::SmallString<32> storage;
158
159public:
160 NullTerminated(const llvm::Twine &twine) {
161 llvm::StringRef ref = twine.toNullTerminatedStringRef(storage);
162 str = ref.begin();
163 }
164 operator const char *() { return str; }
165};
166
167inline llvm::Error nullDeref() {
168 return llvm::createStringError(llvm::inconvertibleErrorCode(),
169 "A NULL PyObject* was dereferenced");
170}
171
172inline llvm::Error exception(const char *s = nullptr) {
173 return llvm::make_error<PythonException>(s);
174}
175
176inline llvm::Error keyError() {
177 return llvm::createStringError(llvm::inconvertibleErrorCode(),
178 "key not in dict");
179}
180
181inline const char *py2_const_cast(const char *s) { return s; }
182
184
185// DOC: https://docs.python.org/3/c-api/arg.html#building-values
186template <typename T, typename Enable = void> struct PythonFormat;
187
188template <typename T, char F> struct PassthroughFormat {
189 static constexpr char format = F;
190 static constexpr T get(T t) { return t; }
191};
192
193template <> struct PythonFormat<char *> : PassthroughFormat<char *, 's'> {};
194template <>
195struct PythonFormat<const char *> : PassthroughFormat<const char *, 's'> {};
196template <> struct PythonFormat<char> : PassthroughFormat<char, 'b'> {};
197template <>
198struct PythonFormat<unsigned char> : PassthroughFormat<unsigned char, 'B'> {};
199template <> struct PythonFormat<short> : PassthroughFormat<short, 'h'> {};
200template <>
201struct PythonFormat<unsigned short> : PassthroughFormat<unsigned short, 'H'> {};
202template <> struct PythonFormat<int> : PassthroughFormat<int, 'i'> {};
203template <> struct PythonFormat<bool> : PassthroughFormat<bool, 'p'> {};
204template <>
205struct PythonFormat<unsigned int> : PassthroughFormat<unsigned int, 'I'> {};
206template <> struct PythonFormat<long> : PassthroughFormat<long, 'l'> {};
207template <>
208struct PythonFormat<unsigned long> : PassthroughFormat<unsigned long, 'k'> {};
209template <>
210struct PythonFormat<long long> : PassthroughFormat<long long, 'L'> {};
211template <>
212struct PythonFormat<unsigned long long>
213 : PassthroughFormat<unsigned long long, 'K'> {};
214template <>
215struct PythonFormat<PyObject *> : PassthroughFormat<PyObject *, 'O'> {};
216
217template <typename T>
219 T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> {
220 static constexpr char format = 'O';
221 static auto get(const T &value) { return value.get(); }
222};
223
225public:
226 PythonObject() = default;
227
228 PythonObject(PyRefType type, PyObject *py_obj) {
229 m_py_obj = py_obj;
230 // If this is a borrowed reference, we need to convert it to
231 // an owned reference by incrementing it. If it is an owned
232 // reference (for example the caller allocated it with PyDict_New()
233 // then we must *not* increment it.
234 if (m_py_obj && Py_IsInitialized() && type == PyRefType::Borrowed)
235 Py_XINCREF(m_py_obj);
236 }
237
240
242 m_py_obj = rhs.m_py_obj;
243 rhs.m_py_obj = nullptr;
244 }
245
247
248 void Reset();
249
250 void Dump(Stream &strm) const;
251
252 PyObject *get() const { return m_py_obj; }
253
254 PyObject *release() {
255 PyObject *result = m_py_obj;
256 m_py_obj = nullptr;
257 return result;
258 }
259
261 Reset();
262 m_py_obj = std::exchange(other.m_py_obj, nullptr);
263 return *this;
264 }
265
267
268 PythonString Repr() const;
269
270 PythonString Str() const;
271
272 static PythonObject ResolveNameWithDictionary(llvm::StringRef name,
273 const PythonDictionary &dict);
274
275 template <typename T>
276 static T ResolveNameWithDictionary(llvm::StringRef name,
277 const PythonDictionary &dict) {
278 return ResolveNameWithDictionary(name, dict).AsType<T>();
279 }
280
281 PythonObject ResolveName(llvm::StringRef name) const;
282
283 template <typename T> T ResolveName(llvm::StringRef name) const {
284 return ResolveName(name).AsType<T>();
285 }
286
287 bool HasAttribute(llvm::StringRef attribute) const;
288
289 PythonObject GetAttributeValue(llvm::StringRef attribute) const;
290
291 bool IsNone() const { return m_py_obj == Py_None; }
292
293 bool IsValid() const { return m_py_obj != nullptr; }
294
295 bool IsAllocated() const { return IsValid() && !IsNone(); }
296
297 explicit operator bool() const { return IsValid() && !IsNone(); }
298
299 template <typename T> T AsType() const {
300 if (!T::Check(m_py_obj))
301 return T();
302 return T(PyRefType::Borrowed, m_py_obj);
303 }
304
306
307 template <typename... T>
308 llvm::Expected<PythonObject> CallMethod(const char *name,
309 const T &... t) const {
310 const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
311 PyObject *obj =
312 PyObject_CallMethod(m_py_obj, py2_const_cast(name),
313 py2_const_cast(format), PythonFormat<T>::get(t)...);
314 if (!obj)
315 return exception();
316 return python::Take<PythonObject>(obj);
317 }
318
319 template <typename... T>
320 llvm::Expected<PythonObject> Call(const T &... t) const {
321 const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
322 PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324 if (!obj)
325 return exception();
326 return python::Take<PythonObject>(obj);
327 }
328
329 llvm::Expected<PythonObject> GetAttribute(const llvm::Twine &name) const {
330 if (!m_py_obj)
331 return nullDeref();
332 PyObject *obj = PyObject_GetAttrString(m_py_obj, NullTerminated(name));
333 if (!obj)
334 return exception();
335 return python::Take<PythonObject>(obj);
336 }
337
338 llvm::Expected<PythonObject> GetType() const {
339 if (!m_py_obj)
340 return nullDeref();
341 PyObject *obj = PyObject_Type(m_py_obj);
342 if (!obj)
343 return exception();
344 return python::Take<PythonObject>(obj);
345 }
346
347 llvm::Expected<bool> IsTrue() {
348 if (!m_py_obj)
349 return nullDeref();
350 int r = PyObject_IsTrue(m_py_obj);
351 if (r < 0)
352 return exception();
353 return !!r;
354 }
355
356 llvm::Expected<long long> AsLongLong() const;
357
358 llvm::Expected<unsigned long long> AsUnsignedLongLong() const;
359
360 // wraps on overflow, instead of raising an error.
361 llvm::Expected<unsigned long long> AsModuloUnsignedLongLong() const;
362
363 llvm::Expected<bool> IsInstance(const PythonObject &cls) {
364 if (!m_py_obj || !cls.IsValid())
365 return nullDeref();
366 int r = PyObject_IsInstance(m_py_obj, cls.get());
367 if (r < 0)
368 return exception();
369 return !!r;
370 }
371
372protected:
373 PyObject *m_py_obj = nullptr;
374};
375
376
377// This is why C++ needs monads.
378template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) {
379 if (!obj)
380 return obj.takeError();
381 if (!T::Check(obj.get().get()))
382 return llvm::createStringError(llvm::inconvertibleErrorCode(),
383 "type error");
384 return T(PyRefType::Borrowed, std::move(obj.get().get()));
385}
386
387template <> llvm::Expected<bool> As<bool>(llvm::Expected<PythonObject> &&obj);
388
389template <>
390llvm::Expected<long long> As<long long>(llvm::Expected<PythonObject> &&obj);
391
392template <>
393llvm::Expected<unsigned long long>
394As<unsigned long long>(llvm::Expected<PythonObject> &&obj);
395
396template <>
397llvm::Expected<std::string> As<std::string>(llvm::Expected<PythonObject> &&obj);
398
399
400template <class T> class TypedPythonObject : public PythonObject {
401public:
402 TypedPythonObject(PyRefType type, PyObject *py_obj) {
403 if (!py_obj)
404 return;
405 if (T::Check(py_obj))
407 else if (type == PyRefType::Owned)
408 Py_DECREF(py_obj);
409 }
410
411 TypedPythonObject() = default;
412};
413
414class PythonBytes : public TypedPythonObject<PythonBytes> {
415public:
417 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
418 PythonBytes(const uint8_t *bytes, size_t length);
419
420 static bool Check(PyObject *py_obj);
421
422 llvm::ArrayRef<uint8_t> GetBytes() const;
423
424 size_t GetSize() const;
425
426 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
427
429};
430
431class PythonByteArray : public TypedPythonObject<PythonByteArray> {
432public:
434 explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes);
435 PythonByteArray(const uint8_t *bytes, size_t length);
437
438 static bool Check(PyObject *py_obj);
439
440 llvm::ArrayRef<uint8_t> GetBytes() const;
441
442 size_t GetSize() const;
443
444 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
445
447};
448
449class PythonString : public TypedPythonObject<PythonString> {
450public:
452 static llvm::Expected<PythonString> FromUTF8(llvm::StringRef string);
453
454 PythonString() : TypedPythonObject() {} // MSVC requires this for some reason
455
456 explicit PythonString(llvm::StringRef string); // safe, null on error
457
458 static bool Check(PyObject *py_obj);
459
460 llvm::StringRef GetString() const; // safe, empty string on error
461
462 llvm::Expected<llvm::StringRef> AsUTF8() const;
463
464 size_t GetSize() const;
465
466 void SetString(llvm::StringRef string); // safe, null on error
467
469};
470
471class PythonInteger : public TypedPythonObject<PythonInteger> {
472public:
474
475 PythonInteger() : TypedPythonObject() {} // MSVC requires this for some reason
476
477 explicit PythonInteger(int64_t value);
478
479 static bool Check(PyObject *py_obj);
480
481 void SetInteger(int64_t value);
482
484
486
488};
489
490class PythonBoolean : public TypedPythonObject<PythonBoolean> {
491public:
493
494 explicit PythonBoolean(bool value);
495
496 static bool Check(PyObject *py_obj);
497
498 bool GetValue() const;
499
500 void SetValue(bool value);
501
503};
504
505class PythonList : public TypedPythonObject<PythonList> {
506public:
508
509 PythonList() : TypedPythonObject() {} // MSVC requires this for some reason
510
511 explicit PythonList(PyInitialValue value);
512 explicit PythonList(int list_size);
513
514 static bool Check(PyObject *py_obj);
515
516 uint32_t GetSize() const;
517
518 PythonObject GetItemAtIndex(uint32_t index) const;
519
520 void SetItemAtIndex(uint32_t index, const PythonObject &object);
521
522 void AppendItem(const PythonObject &object);
523
525};
526
527class PythonTuple : public TypedPythonObject<PythonTuple> {
528public:
530
531 explicit PythonTuple(PyInitialValue value);
532 explicit PythonTuple(int tuple_size);
533 PythonTuple(std::initializer_list<PythonObject> objects);
534 PythonTuple(std::initializer_list<PyObject *> objects);
535
536 static bool Check(PyObject *py_obj);
537
538 uint32_t GetSize() const;
539
540 PythonObject GetItemAtIndex(uint32_t index) const;
541
542 void SetItemAtIndex(uint32_t index, const PythonObject &object);
543
545};
546
547class PythonDictionary : public TypedPythonObject<PythonDictionary> {
548public:
550
551 PythonDictionary() : TypedPythonObject() {} // MSVC requires this for some reason
552
553 explicit PythonDictionary(PyInitialValue value);
554
555 static bool Check(PyObject *py_obj);
556
557 bool HasKey(const llvm::Twine &key) const;
558
559 uint32_t GetSize() const;
560
561 PythonList GetKeys() const;
562
563 PythonObject GetItemForKey(const PythonObject &key) const; // DEPRECATED
564 void SetItemForKey(const PythonObject &key,
565 const PythonObject &value); // DEPRECATED
566
567 llvm::Expected<PythonObject> GetItem(const PythonObject &key) const;
568 llvm::Expected<PythonObject> GetItem(const llvm::Twine &key) const;
569 llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const;
570 llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const;
571
573};
574
575class PythonModule : public TypedPythonObject<PythonModule> {
576public:
578
579 static bool Check(PyObject *py_obj);
580
582
583 static PythonModule MainModule();
584
585 static PythonModule AddModule(llvm::StringRef module);
586
587 // safe, returns invalid on error;
588 static PythonModule ImportModule(llvm::StringRef name) {
589 std::string s = std::string(name);
590 auto mod = Import(s.c_str());
591 if (!mod) {
592 llvm::consumeError(mod.takeError());
593 return PythonModule();
594 }
595 return std::move(mod.get());
596 }
597
598 static llvm::Expected<PythonModule> Import(const llvm::Twine &name);
599
600 llvm::Expected<PythonObject> Get(const llvm::Twine &name);
601
603};
604
605class PythonCallable : public TypedPythonObject<PythonCallable> {
606public:
608
609 struct ArgInfo {
610 /* the largest number of positional arguments this callable
611 * can accept, or UNBOUNDED, ie UINT_MAX if it's a varargs
612 * function and can accept an arbitrary number */
614 static constexpr unsigned UNBOUNDED = UINT_MAX; // FIXME c++17 inline
615 };
616
617 static bool Check(PyObject *py_obj);
618
619 llvm::Expected<ArgInfo> GetArgInfo() const;
620
622
623 PythonObject operator()(std::initializer_list<PyObject *> args);
624
625 PythonObject operator()(std::initializer_list<PythonObject> args);
626
627 template <typename Arg, typename... Args>
628 PythonObject operator()(const Arg &arg, Args... args) {
629 return operator()({arg, args...});
630 }
631};
632
633class PythonFile : public TypedPythonObject<PythonFile> {
634public:
636
637 PythonFile() : TypedPythonObject() {} // MSVC requires this for some reason
638
639 static bool Check(PyObject *py_obj);
640
641 static llvm::Expected<PythonFile> FromFile(File &file,
642 const char *mode = nullptr);
643
644 llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false);
645 llvm::Expected<lldb::FileSP>
646 ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false);
647};
648
649class PythonException : public llvm::ErrorInfo<PythonException> {
650private:
652 PyObject *m_repr_bytes;
653
654public:
655 static char ID;
656 const char *toCString() const;
657 PythonException(const char *caller = nullptr);
658 void Restore();
659 ~PythonException() override;
660 void log(llvm::raw_ostream &OS) const override;
661 std::error_code convertToErrorCode() const override;
662 bool Matches(PyObject *exc) const;
663 std::string ReadBacktrace() const;
664};
665
666// This extracts the underlying T out of an Expected<T> and returns it.
667// If the Expected is an Error instead of a T, that error will be converted
668// into a python exception, and this will return a default-constructed T.
669//
670// This is appropriate for use right at the boundary of python calling into
671// C++, such as in a SWIG typemap. In such a context you should simply
672// check if the returned T is valid, and if it is, return a NULL back
673// to python. This will result in the Error being raised as an exception
674// from python code's point of view.
675//
676// For example:
677// ```
678// Expected<Foo *> efoop = some_cpp_function();
679// Foo *foop = unwrapOrSetPythonException(efoop);
680// if (!foop)
681// return NULL;
682// do_something(*foop);
683//
684// If the Error returned was itself created because a python exception was
685// raised when C++ code called into python, then the original exception
686// will be restored. Otherwise a simple string exception will be raised.
687template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
688 if (expected)
689 return expected.get();
690 llvm::handleAllErrors(
691 expected.takeError(), [](PythonException &E) { E.Restore(); },
692 [](const llvm::ErrorInfoBase &E) {
693 PyErr_SetString(PyExc_Exception, E.message().c_str());
694 });
695 return T();
696}
697
698// This is only here to help incrementally migrate old, exception-unsafe
699// code.
700template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) {
701 if (expected)
702 return std::move(expected.get());
703 llvm::consumeError(expected.takeError());
704 return T();
705}
706
707llvm::Expected<PythonObject> runStringOneLine(const llvm::Twine &string,
708 const PythonDictionary &globals,
709 const PythonDictionary &locals);
710
711llvm::Expected<PythonObject> runStringMultiLine(const llvm::Twine &string,
712 const PythonDictionary &globals,
713 const PythonDictionary &locals);
714
715// Sometimes the best way to interact with a python interpreter is
716// to run some python code. You construct a PythonScript with
717// script string. The script assigns some function to `_function_`
718// and you get a C++ callable object that calls the python function.
719//
720// Example:
721//
722// const char script[] = R"(
723// def main(x, y):
724// ....
725// )";
726//
727// Expected<PythonObject> cpp_foo_wrapper(PythonObject x, PythonObject y) {
728// // no need to synchronize access to this global, we already have the GIL
729// static PythonScript foo(script)
730// return foo(x, y);
731// }
733 const char *script;
735
736 llvm::Error Init();
737
738public:
740
741 template <typename... Args>
742 llvm::Expected<PythonObject> operator()(Args &&... args) {
743 if (llvm::Error error = Init())
744 return std::move(error);
745 return function.Call(std::forward<Args>(args)...);
746 }
747};
748
750public:
752
753 // Take ownership of the object we received.
756
758 // Hand ownership back to a (temporary) PythonObject instance and let it
759 // take care of releasing it.
760 PythonObject(PyRefType::Owned, static_cast<PyObject *>(GetValue()));
761 }
762
763 bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
764
765 void Serialize(llvm::json::OStream &s) const override;
766
767private:
771};
772
773PyObject *RunString(const char *str, int start, PyObject *globals,
774 PyObject *locals);
775int RunSimpleString(const char *str);
776
777} // namespace python
778} // namespace lldb_private
779
780#endif // LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
Definition Args.h:33
A stream class that can stream formatted output to a file.
Definition Stream.h:28
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)
StructuredData::BooleanSP CreateStructuredBoolean() const
static bool Check(PyObject *py_obj)
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
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
PythonObject GetItemForKey(const PythonObject &key) const
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const
llvm::Expected< PythonObject > GetItem(const llvm::Twine &key) const
void SetItemForKey(const PythonObject &key, const PythonObject &value)
std::error_code convertToErrorCode() const override
void log(llvm::raw_ostream &OS) const 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
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 AddModule(llvm::StringRef module)
static PythonModule ImportModule(llvm::StringRef name)
TypedPythonObject(PyRefType type, PyObject *py_obj)
llvm::Expected< PythonObject > Get(const llvm::Twine &name)
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
PythonObject & operator=(PythonObject other)
PythonObject(PyRefType type, PyObject *py_obj)
PythonObject(const PythonObject &rhs)
llvm::Expected< long long > AsLongLong() const
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
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
llvm::Expected< PythonObject > operator()(Args &&... args)
llvm::Expected< llvm::StringRef > AsUTF8() 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)
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
const StructuredPythonObject & operator=(const StructuredPythonObject &)=delete
void Serialize(llvm::json::OStream &s) const override
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)
T Retain(PyObject *obj)
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.