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