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