LLDB mainline
PythonDataObjects.cpp
Go to the documentation of this file.
1//===-- PythonDataObjects.cpp ---------------------------------------------===//
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#include "PythonDataObjects.h"
11
12#include "lldb/Host/File.h"
16#include "lldb/Utility/Log.h"
17#include "lldb/Utility/Stream.h"
18
19#include "llvm/ADT/ScopeExit.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/ConvertUTF.h"
22#include "llvm/Support/Errno.h"
23
24#include <cstdio>
25#include <variant>
26
27using namespace lldb_private;
28using namespace lldb;
29using namespace lldb_private::python;
30using llvm::cantFail;
31using llvm::Error;
32using llvm::Expected;
33using llvm::Twine;
34
35template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) {
36 if (!obj)
37 return obj.takeError();
38 return obj.get().IsTrue();
39}
40
41template <>
42Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) {
43 if (!obj)
44 return obj.takeError();
45 return obj->AsLongLong();
46}
47
48template <>
49Expected<unsigned long long>
50python::As<unsigned long long>(Expected<PythonObject> &&obj) {
51 if (!obj)
52 return obj.takeError();
53 return obj->AsUnsignedLongLong();
54}
55
56template <>
57Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
58 if (!obj)
59 return obj.takeError();
60 PyObject *str_obj = PyObject_Str(obj.get().get());
61 if (!str_obj)
62 return llvm::make_error<PythonException>();
63 auto str = Take<PythonString>(str_obj);
64 auto utf8 = str.AsUTF8();
65 if (!utf8)
66 return utf8.takeError();
67 return std::string(utf8.get());
68}
69
71 if (m_py_obj && Py_IsInitialized()) {
72 PyGILState_STATE state = PyGILState_Ensure();
73 Py_DECREF(m_py_obj);
74 PyGILState_Release(state);
75 }
76 m_py_obj = nullptr;
77}
78
79Expected<long long> PythonObject::AsLongLong() const {
80 if (!m_py_obj)
81 return nullDeref();
82 assert(!PyErr_Occurred());
83 long long r = PyLong_AsLongLong(m_py_obj);
84 if (PyErr_Occurred())
85 return exception();
86 return r;
87}
88
89Expected<unsigned long long> PythonObject::AsUnsignedLongLong() const {
90 if (!m_py_obj)
91 return nullDeref();
92 assert(!PyErr_Occurred());
93 long long r = PyLong_AsUnsignedLongLong(m_py_obj);
94 if (PyErr_Occurred())
95 return exception();
96 return r;
97}
98
99// wraps on overflow, instead of raising an error.
100Expected<unsigned long long> PythonObject::AsModuloUnsignedLongLong() const {
101 if (!m_py_obj)
102 return nullDeref();
103 assert(!PyErr_Occurred());
104 unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj);
105 // FIXME: We should fetch the exception message and hoist it.
106 if (PyErr_Occurred())
107 return exception();
108 return r;
109}
110
111void StructuredPythonObject::Serialize(llvm::json::OStream &s) const {
112 s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str());
113}
114
115// PythonObject
116
117void PythonObject::Dump(Stream &strm) const {
118 if (!m_py_obj) {
119 strm << "NULL";
120 return;
121 }
122
123 PyObject *py_str = PyObject_Repr(m_py_obj);
124 if (!py_str)
125 return;
126
127 llvm::scope_exit release_py_str([py_str] { Py_DECREF(py_str); });
128
129 PyObject *py_bytes = PyUnicode_AsEncodedString(py_str, "utf-8", "replace");
130 if (!py_bytes)
131 return;
132
133 llvm::scope_exit release_py_bytes([py_bytes] { Py_DECREF(py_bytes); });
134
135 char *buffer = nullptr;
136 Py_ssize_t length = 0;
137 if (PyBytes_AsStringAndSize(py_bytes, &buffer, &length) == -1)
138 return;
139
140 strm << llvm::StringRef(buffer, length);
141}
142
171
173 if (!m_py_obj)
174 return PythonString();
175 PyObject *repr = PyObject_Repr(m_py_obj);
176 if (!repr)
177 return PythonString();
178 return PythonString(PyRefType::Owned, repr);
179}
180
182 if (!m_py_obj)
183 return PythonString();
184 PyObject *str = PyObject_Str(m_py_obj);
185 if (!str)
186 return PythonString();
187 return PythonString(PyRefType::Owned, str);
188}
189
192 const PythonDictionary &dict) {
193 size_t dot_pos = name.find('.');
194 llvm::StringRef piece = name.substr(0, dot_pos);
195 PythonObject result = dict.GetItemForKey(PythonString(piece));
196 if (dot_pos == llvm::StringRef::npos) {
197 // There was no dot, we're done.
198 return result;
199 }
200
201 // There was a dot. The remaining portion of the name should be looked up in
202 // the context of the object that was found in the dictionary.
203 return result.ResolveName(name.substr(dot_pos + 1));
204}
205
206PythonObject PythonObject::ResolveName(llvm::StringRef name) const {
207 // Resolve the name in the context of the specified object. If, for example,
208 // `this` refers to a PyModule, then this will look for `name` in this
209 // module. If `this` refers to a PyType, then it will resolve `name` as an
210 // attribute of that type. If `this` refers to an instance of an object,
211 // then it will resolve `name` as the value of the specified field.
212 //
213 // This function handles dotted names so that, for example, if `m_py_obj`
214 // refers to the `sys` module, and `name` == "path.append", then it will find
215 // the function `sys.path.append`.
216
217 size_t dot_pos = name.find('.');
218 if (dot_pos == llvm::StringRef::npos) {
219 // No dots in the name, we should be able to find the value immediately as
220 // an attribute of `m_py_obj`.
221 return GetAttributeValue(name);
222 }
223
224 // Look up the first piece of the name, and resolve the rest as a child of
225 // that.
226 PythonObject parent = ResolveName(name.substr(0, dot_pos));
227 if (!parent.IsAllocated())
228 return PythonObject();
229
230 // Tail recursion.. should be optimized by the compiler
231 return parent.ResolveName(name.substr(dot_pos + 1));
232}
233
234bool PythonObject::HasAttribute(llvm::StringRef attr) const {
235 if (!IsValid())
236 return false;
237 PythonString py_attr(attr);
238 return !!PyObject_HasAttr(m_py_obj, py_attr.get());
239}
240
242 if (!IsValid())
243 return PythonObject();
244
245 PythonString py_attr(attr);
246 if (!PyObject_HasAttr(m_py_obj, py_attr.get()))
247 return PythonObject();
248
250 PyObject_GetAttr(m_py_obj, py_attr.get()));
251}
252
254 switch (GetObjectType()) {
264 if (std::holds_alternative<StructuredData::UnsignedIntegerSP>(int_sp))
265 return std::get<StructuredData::UnsignedIntegerSP>(int_sp);
266 if (std::holds_alternative<StructuredData::SignedIntegerSP>(int_sp))
267 return std::get<StructuredData::SignedIntegerSP>(int_sp);
268 return nullptr;
269 };
281 default:
284 }
285}
286
287// PythonString
288
289PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) { SetBytes(bytes); }
290
291PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) {
292 SetBytes(llvm::ArrayRef<uint8_t>(bytes, length));
293}
294
295bool PythonBytes::Check(PyObject *py_obj) {
296 if (!py_obj)
297 return false;
298 return PyBytes_Check(py_obj);
299}
300
301llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const {
302 if (!IsValid())
303 return llvm::ArrayRef<uint8_t>();
304
305 Py_ssize_t size;
306 char *c;
307
308 PyBytes_AsStringAndSize(m_py_obj, &c, &size);
309 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
310}
311
312size_t PythonBytes::GetSize() const {
313 if (!IsValid())
314 return 0;
315 return PyBytes_Size(m_py_obj);
316}
317
318void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) {
319 const char *data = reinterpret_cast<const char *>(bytes.data());
320 *this = Take<PythonBytes>(PyBytes_FromStringAndSize(data, bytes.size()));
321}
322
325 Py_ssize_t size;
326 char *c;
327 PyBytes_AsStringAndSize(m_py_obj, &c, &size);
328 result->SetValue(std::string(c, size));
329 return result;
330}
331
332PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes)
333 : PythonByteArray(bytes.data(), bytes.size()) {}
334
335PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) {
336 const char *str = reinterpret_cast<const char *>(bytes);
337 *this = Take<PythonByteArray>(PyByteArray_FromStringAndSize(str, length));
338}
339
340bool PythonByteArray::Check(PyObject *py_obj) {
341 if (!py_obj)
342 return false;
343 return PyByteArray_Check(py_obj);
344}
345
346llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const {
347 if (!IsValid())
348 return llvm::ArrayRef<uint8_t>();
349
350 char *c = PyByteArray_AsString(m_py_obj);
351 size_t size = GetSize();
352 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
353}
354
356 if (!IsValid())
357 return 0;
358
359 return PyByteArray_Size(m_py_obj);
360}
361
364 llvm::ArrayRef<uint8_t> bytes = GetBytes();
365 const char *str = reinterpret_cast<const char *>(bytes.data());
366 result->SetValue(std::string(str, bytes.size()));
367 return result;
368}
369
370// PythonString
371
372Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) {
373 PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size());
374 if (!str)
375 return llvm::make_error<PythonException>();
376 return Take<PythonString>(str);
377}
378
379PythonString::PythonString(llvm::StringRef string) { SetString(string); }
380
381bool PythonString::Check(PyObject *py_obj) {
382 if (!py_obj)
383 return false;
384
385 if (PyUnicode_Check(py_obj))
386 return true;
387 return false;
388}
389
390llvm::StringRef PythonString::GetString() const {
391 auto s = AsUTF8();
392 if (!s) {
393 llvm::consumeError(s.takeError());
394 return llvm::StringRef("");
395 }
396 return s.get();
397}
398
399Expected<llvm::StringRef> PythonString::AsUTF8() const {
400 if (!IsValid())
401 return nullDeref();
402
403 // PyUnicode_AsUTF8AndSize caches the UTF-8 representation of the string in
404 // the Unicode object, which makes it more efficient and ties the lifetime of
405 // the data to the Python string. However, it was only added to the Stable API
406 // in Python 3.10. Older versions that want to use the Stable API must use
407 // PyUnicode_AsUTF8String in combination with ConstString.
408#if defined(Py_LIMITED_API) && (Py_LIMITED_API < 0x030a0000)
409 PyObject *py_bytes = PyUnicode_AsUTF8String(m_py_obj);
410 if (!py_bytes)
411 return exception();
412 llvm::scope_exit release_py_str([py_bytes] { Py_DECREF(py_bytes); });
413 Py_ssize_t size = PyBytes_Size(py_bytes);
414 const char *str = PyBytes_AsString(py_bytes);
415
416 if (!str)
417 return exception();
418
419 return ConstString(str, size).GetStringRef();
420#else
421 Py_ssize_t size;
422 const char *str = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
423
424 if (!str)
425 return exception();
426
427 return llvm::StringRef(str, size);
428#endif
429}
430
431size_t PythonString::GetSize() const {
432 if (IsValid())
433 return PyUnicode_GetLength(m_py_obj);
434 return 0;
435}
436
437void PythonString::SetString(llvm::StringRef string) {
438 auto s = FromUTF8(string);
439 if (!s) {
440 llvm::consumeError(s.takeError());
441 Reset();
442 } else {
443 *this = std::move(s.get());
444 }
445}
446
449 result->SetValue(GetString());
450 return result;
451}
452
453// PythonInteger
454
455PythonInteger::PythonInteger(int64_t value) { SetInteger(value); }
456
457bool PythonInteger::Check(PyObject *py_obj) {
458 if (!py_obj)
459 return false;
460
461 // Python 3 does not have PyInt_Check. There is only one type of integral
462 // value, long.
463 return PyLong_Check(py_obj);
464}
465
466void PythonInteger::SetInteger(int64_t value) {
467 *this = Take<PythonInteger>(PyLong_FromLongLong(value));
468}
469
475
478 StructuredData::UnsignedIntegerSP result = nullptr;
479 llvm::Expected<unsigned long long> value = AsUnsignedLongLong();
480 if (!value)
481 llvm::consumeError(value.takeError());
482 else
483 result = std::make_shared<StructuredData::UnsignedInteger>(value.get());
484
485 return result;
486}
487
490 StructuredData::SignedIntegerSP result = nullptr;
491 llvm::Expected<long long> value = AsLongLong();
492 if (!value)
493 llvm::consumeError(value.takeError());
494 else
495 result = std::make_shared<StructuredData::SignedInteger>(value.get());
496
497 return result;
498}
499
500// PythonBoolean
501
503
504bool PythonBoolean::Check(PyObject *py_obj) {
505 return py_obj ? PyBool_Check(py_obj) : false;
506}
507
509 return m_py_obj ? PyObject_IsTrue(m_py_obj) : false;
510}
511
512void PythonBoolean::SetValue(bool value) {
513 *this = Take<PythonBoolean>(PyBool_FromLong(value));
514}
515
521
522// PythonList
523
525 if (value == PyInitialValue::Empty)
526 *this = Take<PythonList>(PyList_New(0));
527}
528
530 *this = Take<PythonList>(PyList_New(list_size));
531}
532
533bool PythonList::Check(PyObject *py_obj) {
534 if (!py_obj)
535 return false;
536 return PyList_Check(py_obj);
537}
538
539uint32_t PythonList::GetSize() const {
540 if (IsValid())
541 return PyList_Size(m_py_obj);
542 return 0;
543}
544
546 if (IsValid())
547 return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
548 return PythonObject();
549}
550
551void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) {
552 if (IsAllocated() && object.IsValid()) {
553 // PyList_SetItem is documented to "steal" a reference, so we need to
554 // convert it to an owned reference by incrementing it.
555 Py_INCREF(object.get());
556 PyList_SetItem(m_py_obj, index, object.get());
557 }
558}
559
561 if (IsAllocated() && object.IsValid()) {
562 // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
563 // here like we do with `PyList_SetItem`.
564 PyList_Append(m_py_obj, object.get());
565 }
566}
567
570 uint32_t count = GetSize();
571 for (uint32_t i = 0; i < count; ++i) {
573 result->AddItem(obj.CreateStructuredObject());
574 }
575 return result;
576}
577
578// PythonTuple
579
581 if (value == PyInitialValue::Empty)
582 *this = Take<PythonTuple>(PyTuple_New(0));
583}
584
586 *this = Take<PythonTuple>(PyTuple_New(tuple_size));
587}
588
589PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) {
590 m_py_obj = PyTuple_New(objects.size());
591
592 uint32_t idx = 0;
593 for (auto object : objects) {
594 if (object.IsValid())
595 SetItemAtIndex(idx, object);
596 idx++;
597 }
598}
599
600PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) {
601 m_py_obj = PyTuple_New(objects.size());
602
603 uint32_t idx = 0;
604 for (auto py_object : objects) {
605 PythonObject object(PyRefType::Borrowed, py_object);
606 if (object.IsValid())
607 SetItemAtIndex(idx, object);
608 idx++;
609 }
610}
611
612bool PythonTuple::Check(PyObject *py_obj) {
613 if (!py_obj)
614 return false;
615 return PyTuple_Check(py_obj);
616}
617
618uint32_t PythonTuple::GetSize() const {
619 if (IsValid())
620 return PyTuple_Size(m_py_obj);
621 return 0;
622}
623
625 if (IsValid())
626 return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index));
627 return PythonObject();
628}
629
630void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) {
631 if (IsAllocated() && object.IsValid()) {
632 // PyTuple_SetItem is documented to "steal" a reference, so we need to
633 // convert it to an owned reference by incrementing it.
634 Py_INCREF(object.get());
635 PyTuple_SetItem(m_py_obj, index, object.get());
636 }
637}
638
641 uint32_t count = GetSize();
642 for (uint32_t i = 0; i < count; ++i) {
644 result->AddItem(obj.CreateStructuredObject());
645 }
646 return result;
647}
648
649// PythonDictionary
650
655
656bool PythonDictionary::Check(PyObject *py_obj) {
657 if (!py_obj)
658 return false;
659
660 return PyDict_Check(py_obj);
661}
662
663bool PythonDictionary::HasKey(const llvm::Twine &key) const {
664 if (!IsValid())
665 return false;
666
667 PythonString key_object(key.isSingleStringRef() ? key.getSingleStringRef()
668 : key.str());
669
670 if (int res = PyDict_Contains(m_py_obj, key_object.get()) > 0)
671 return res;
672
673 PyErr_Print();
674 return false;
675}
676
678 if (IsValid())
679 return PyDict_Size(m_py_obj);
680 return 0;
681}
682
688
690 auto item = GetItem(key);
691 if (!item) {
692 llvm::consumeError(item.takeError());
693 return PythonObject();
694 }
695 return std::move(item.get());
696}
697
698Expected<PythonObject>
700 if (!IsValid())
701 return nullDeref();
702 PyObject *o = PyDict_GetItemWithError(m_py_obj, key.get());
703 if (PyErr_Occurred())
704 return exception();
705 if (!o)
706 return keyError();
707 return Retain<PythonObject>(o);
708}
709
710Expected<PythonObject> PythonDictionary::GetItem(const Twine &key) const {
711 if (!IsValid())
712 return nullDeref();
713 PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(key));
714 if (PyErr_Occurred())
715 return exception();
716 if (!o)
717 return keyError();
718 return Retain<PythonObject>(o);
719}
720
722 const PythonObject &value) const {
723 if (!IsValid() || !value.IsValid())
724 return nullDeref();
725 int r = PyDict_SetItem(m_py_obj, key.get(), value.get());
726 if (r < 0)
727 return exception();
728 return Error::success();
729}
730
731Error PythonDictionary::SetItem(const Twine &key,
732 const PythonObject &value) const {
733 if (!IsValid() || !value.IsValid())
734 return nullDeref();
735 int r = PyDict_SetItemString(m_py_obj, NullTerminated(key), value.get());
736 if (r < 0)
737 return exception();
738 return Error::success();
739}
740
742 const PythonObject &value) {
743 Error error = SetItem(key, value);
744 if (error)
745 llvm::consumeError(std::move(error));
746}
747
751 PythonList keys(GetKeys());
752 uint32_t num_keys = keys.GetSize();
753 for (uint32_t i = 0; i < num_keys; ++i) {
754 PythonObject key = keys.GetItemAtIndex(i);
755 PythonObject value = GetItemForKey(key);
756 StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
757 result->AddItem(key.Str().GetString(), structured_value);
758 }
759 return result;
760}
761
763
765
766PythonModule PythonModule::AddModule(llvm::StringRef module) {
767 std::string str = module.str();
768 return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
769}
770
771Expected<PythonModule> PythonModule::Import(const Twine &name) {
772 PyObject *mod = PyImport_ImportModule(NullTerminated(name));
773 if (!mod)
774 return exception();
775 return Take<PythonModule>(mod);
776}
777
778Expected<PythonObject> PythonModule::Get(const Twine &name) {
779 if (!IsValid())
780 return nullDeref();
781 PyObject *dict = PyModule_GetDict(m_py_obj);
782 if (!dict)
783 return exception();
784 PyObject *item = PyDict_GetItemString(dict, NullTerminated(name));
785 if (!item)
786 return exception();
787 return Retain<PythonObject>(item);
788}
789
790bool PythonModule::Check(PyObject *py_obj) {
791 if (!py_obj)
792 return false;
793
794 return PyModule_Check(py_obj);
795}
796
798 if (!IsValid())
799 return PythonDictionary();
800 return Retain<PythonDictionary>(PyModule_GetDict(m_py_obj));
801}
802
803bool PythonCallable::Check(PyObject *py_obj) {
804 if (!py_obj)
805 return false;
806
807 PythonObject python_obj(PyRefType::Borrowed, py_obj);
808
809 // Handle staticmethod/classmethod descriptors by extracting the
810 // `__func__` attribute.
811 if (python_obj.HasAttribute("__func__")) {
812 PythonObject function_obj = python_obj.GetAttributeValue("__func__");
813 if (!function_obj.IsAllocated())
814 return false;
815 return PyCallable_Check(function_obj.release());
816 }
817
818 return PyCallable_Check(py_obj);
819}
820
821static const char get_arg_info_script[] = R"(
822from inspect import signature, Parameter, ismethod
823from collections import namedtuple
824ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs'])
825def main(f):
826 count = 0
827 varargs = False
828 for parameter in signature(f).parameters.values():
829 kind = parameter.kind
830 if kind in (Parameter.POSITIONAL_ONLY,
831 Parameter.POSITIONAL_OR_KEYWORD):
832 count += 1
833 elif kind == Parameter.VAR_POSITIONAL:
834 varargs = True
835 elif kind in (Parameter.KEYWORD_ONLY,
836 Parameter.VAR_KEYWORD):
837 pass
838 else:
839 raise Exception(f'unknown parameter kind: {kind}')
840 return ArgInfo(count, varargs)
841)";
842
843Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
844 ArgInfo result = {};
845 if (!IsValid())
846 return nullDeref();
847
848 // no need to synchronize access to this global, we already have the GIL
849 static PythonScript get_arg_info(get_arg_info_script);
850 Expected<PythonObject> pyarginfo = get_arg_info(*this);
851 if (!pyarginfo)
852 return pyarginfo.takeError();
853 long long count =
854 cantFail(As<long long>(pyarginfo.get().GetAttribute("count")));
855 bool has_varargs =
856 cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs")));
857 result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count;
858
859 return result;
860}
861
862constexpr unsigned
863 PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17
864
866 return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr));
867}
868
870PythonCallable::operator()(std::initializer_list<PyObject *> args) {
871 PythonTuple arg_tuple(args);
873 PyObject_CallObject(m_py_obj, arg_tuple.get()));
874}
875
877PythonCallable::operator()(std::initializer_list<PythonObject> args) {
878 PythonTuple arg_tuple(args);
880 PyObject_CallObject(m_py_obj, arg_tuple.get()));
881}
882
883bool PythonFile::Check(PyObject *py_obj) {
884 if (!py_obj)
885 return false;
886 // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a
887 // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper
888 // over `io.open()`, which returns some object derived from `io.IOBase`. As a
889 // result, the only way to detect a file in Python 3 is to check whether it
890 // inherits from `io.IOBase`.
891 auto io_module = PythonModule::Import("io");
892 if (!io_module) {
893 llvm::consumeError(io_module.takeError());
894 return false;
895 }
896 auto iobase = io_module.get().Get("IOBase");
897 if (!iobase) {
898 llvm::consumeError(iobase.takeError());
899 return false;
900 }
901 int r = PyObject_IsInstance(py_obj, iobase.get().get());
902 if (r < 0) {
903 llvm::consumeError(exception()); // clear the exception and log it.
904 return false;
905 }
906 return !!r;
907}
908
909const char *PythonException::toCString() const {
910 if (!m_repr_bytes)
911 return "unknown exception";
912 return PyBytes_AsString(m_repr_bytes);
913}
914
916 assert(PyErr_Occurred());
918 PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback);
919 PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback);
920 PyErr_Clear();
921 if (m_exception) {
922 PyObject *repr = PyObject_Repr(m_exception);
923 if (repr) {
924 m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr);
925 if (!m_repr_bytes) {
926 PyErr_Clear();
927 }
928 Py_XDECREF(repr);
929 } else {
930 PyErr_Clear();
931 }
932 }
934 if (caller)
935 LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString());
936 else
937 LLDB_LOGF(log, "python exception: %s", toCString());
938}
942 } else {
943 PyErr_SetString(PyExc_Exception, toCString());
944 }
946}
947
949 Py_XDECREF(m_exception_type);
950 Py_XDECREF(m_exception);
951 Py_XDECREF(m_traceback);
952 Py_XDECREF(m_repr_bytes);
953}
954
955void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); }
956
957std::error_code PythonException::convertToErrorCode() const {
958 return llvm::inconvertibleErrorCode();
959}
960
961bool PythonException::Matches(PyObject *exc) const {
962 return PyErr_GivenExceptionMatches(m_exception_type, exc);
963}
964
965const char read_exception_script[] = R"(
966import sys
967from traceback import print_exception
968if sys.version_info.major < 3:
969 from StringIO import StringIO
970else:
971 from io import StringIO
972def main(exc_type, exc_value, tb):
973 f = StringIO()
974 print_exception(exc_type, exc_value, tb, file=f)
975 return f.getvalue()
976)";
977
979
980 if (!m_traceback)
981 return toCString();
982
983 // no need to synchronize access to this global, we already have the GIL
984 static PythonScript read_exception(read_exception_script);
985
986 Expected<std::string> backtrace = As<std::string>(
987 read_exception(m_exception_type, m_exception, m_traceback));
988
989 if (!backtrace) {
990 std::string message =
991 std::string(toCString()) + "\n" +
992 "Traceback unavailable, an error occurred while reading it:\n";
993 return (message + llvm::toString(backtrace.takeError()));
994 }
995
996 return std::move(backtrace.get());
997}
998
999char PythonException::ID = 0;
1000
1001llvm::Expected<File::OpenOptions>
1003 auto options = File::OpenOptions(0);
1004 auto readable = As<bool>(obj.CallMethod("readable"));
1005 if (!readable)
1006 return readable.takeError();
1007 auto writable = As<bool>(obj.CallMethod("writable"));
1008 if (!writable)
1009 return writable.takeError();
1010 if (readable.get() && writable.get())
1011 options |= File::eOpenOptionReadWrite;
1012 else if (writable.get())
1013 options |= File::eOpenOptionWriteOnly;
1014 else if (readable.get())
1015 options |= File::eOpenOptionReadOnly;
1016 return options;
1017}
1018
1019// Base class template for python files. All it knows how to do
1020// is hold a reference to the python object and close or flush it
1021// when the File is closed.
1022namespace {
1023template <typename Base> class OwnedPythonFile : public Base {
1024public:
1025 template <typename... Args>
1026 OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args)
1027 : Base(args...), m_py_obj(file), m_borrowed(borrowed) {
1028 assert(m_py_obj);
1029 }
1030
1031 ~OwnedPythonFile() override {
1032 assert(m_py_obj);
1033 GIL takeGIL;
1034 Close();
1035 // we need to ensure the python object is released while we still
1036 // hold the GIL
1037 m_py_obj.Reset();
1038 }
1039
1040 bool IsPythonSideValid() const {
1041 GIL takeGIL;
1042 auto closed = As<bool>(m_py_obj.GetAttribute("closed"));
1043 if (!closed) {
1044 llvm::consumeError(closed.takeError());
1045 return false;
1046 }
1047 return !closed.get();
1048 }
1049
1050 bool IsValid() const override {
1051 return IsPythonSideValid() && Base::IsValid();
1052 }
1053
1054 Status Close() override {
1055 assert(m_py_obj);
1056 Status py_error, base_error;
1057 GIL takeGIL;
1058 if (!m_borrowed) {
1059 auto r = m_py_obj.CallMethod("close");
1060 if (!r)
1061 py_error = Status::FromError(r.takeError());
1062 }
1063 base_error = Base::Close();
1064 // Cloning since the wrapped exception may still reference the PyThread.
1065 if (py_error.Fail())
1066 return py_error.Clone();
1067 return base_error.Clone();
1068 };
1069
1070 PyObject *GetPythonObject() const {
1071 assert(m_py_obj.IsValid());
1072 return m_py_obj.get();
1073 }
1074
1075 static bool classof(const File *file) = delete;
1076
1077protected:
1078 PythonFile m_py_obj;
1079 bool m_borrowed;
1080};
1081} // namespace
1082
1083// A SimplePythonFile is a OwnedPythonFile that just does all I/O as
1084// a NativeFile
1085namespace {
1086class SimplePythonFile : public OwnedPythonFile<NativeFile> {
1087public:
1088 SimplePythonFile(const PythonFile &file, bool borrowed, int fd,
1089 File::OpenOptions options)
1090 : OwnedPythonFile(file, borrowed, fd, options, false) {}
1091
1092 static char ID;
1093 bool isA(const void *classID) const override {
1094 return classID == &ID || NativeFile::isA(classID);
1095 }
1096 static bool classof(const File *file) { return file->isA(&ID); }
1097};
1098char SimplePythonFile::ID = 0;
1099} // namespace
1100
1101// Shared methods between TextPythonFile and BinaryPythonFile
1102namespace {
1103class PythonIOFile : public OwnedPythonFile<File> {
1104public:
1105 PythonIOFile(const PythonFile &file, bool borrowed)
1106 : OwnedPythonFile(file, borrowed) {}
1107
1108 ~PythonIOFile() override { Close(); }
1109
1110 bool IsValid() const override { return IsPythonSideValid(); }
1111
1112 Status Close() override {
1113 assert(m_py_obj);
1114 GIL takeGIL;
1115 if (m_borrowed)
1116 return Flush();
1117 auto r = m_py_obj.CallMethod("close");
1118 if (!r)
1119 // Cloning since the wrapped exception may still reference the PyThread.
1120 return Status::FromError(r.takeError()).Clone();
1121 return Status();
1122 }
1123
1124 Status Flush() override {
1125 GIL takeGIL;
1126 auto r = m_py_obj.CallMethod("flush");
1127 if (!r)
1128 // Cloning since the wrapped exception may still reference the PyThread.
1129 return Status::FromError(r.takeError()).Clone();
1130 return Status();
1131 }
1132
1133 Expected<File::OpenOptions> GetOptions() const override {
1134 GIL takeGIL;
1135 return GetOptionsForPyObject(m_py_obj);
1136 }
1137
1138 static char ID;
1139 bool isA(const void *classID) const override {
1140 return classID == &ID || File::isA(classID);
1141 }
1142 static bool classof(const File *file) { return file->isA(&ID); }
1143};
1144char PythonIOFile::ID = 0;
1145} // namespace
1146
1147namespace {
1148class BinaryPythonFile : public PythonIOFile {
1149protected:
1150 int m_descriptor;
1151
1152public:
1153 BinaryPythonFile(int fd, const PythonFile &file, bool borrowed)
1154 : PythonIOFile(file, borrowed),
1155 m_descriptor(File::DescriptorIsValid(fd) ? fd
1156 : File::kInvalidDescriptor) {}
1157
1158 int GetDescriptor() const override { return m_descriptor; }
1159
1160 Status Write(const void *buf, size_t &num_bytes) override {
1161 GIL takeGIL;
1162 PyObject *pybuffer_p = PyMemoryView_FromMemory(
1163 const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ);
1164 if (!pybuffer_p)
1165 // Cloning since the wrapped exception may still reference the PyThread.
1166 return Status::FromError(llvm::make_error<PythonException>()).Clone();
1167 auto pybuffer = Take<PythonObject>(pybuffer_p);
1168 num_bytes = 0;
1169 auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer));
1170 if (!bytes_written)
1171 return Status::FromError(bytes_written.takeError());
1172 if (bytes_written.get() < 0)
1173 return Status::FromErrorString(
1174 ".write() method returned a negative number!");
1175 static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1176 num_bytes = bytes_written.get();
1177 return Status();
1178 }
1179
1180 Status Read(void *buf, size_t &num_bytes) override {
1181 GIL takeGIL;
1182 static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1183 auto pybuffer_obj =
1184 m_py_obj.CallMethod("read", (unsigned long long)num_bytes);
1185 if (!pybuffer_obj)
1186 // Cloning since the wrapped exception may still reference the PyThread.
1187 return Status::FromError(pybuffer_obj.takeError()).Clone();
1188 num_bytes = 0;
1189 if (pybuffer_obj.get().IsNone()) {
1190 // EOF
1191 num_bytes = 0;
1192 return Status();
1193 }
1194 PythonBytes pybytes(PyRefType::Borrowed, pybuffer_obj->get());
1195 if (!pybytes)
1196 return Status::FromError(llvm::make_error<PythonException>());
1197 llvm::ArrayRef<uint8_t> bytes = pybytes.GetBytes();
1198 memcpy(buf, bytes.begin(), bytes.size());
1199 num_bytes = bytes.size();
1200 return Status();
1201 }
1202};
1203} // namespace
1204
1205namespace {
1206class TextPythonFile : public PythonIOFile {
1207protected:
1208 int m_descriptor;
1209
1210public:
1211 TextPythonFile(int fd, const PythonFile &file, bool borrowed)
1212 : PythonIOFile(file, borrowed),
1213 m_descriptor(File::DescriptorIsValid(fd) ? fd
1214 : File::kInvalidDescriptor) {}
1215
1216 int GetDescriptor() const override { return m_descriptor; }
1217
1218 Status Write(const void *buf, size_t &num_bytes) override {
1219 GIL takeGIL;
1220 auto pystring =
1221 PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes));
1222 if (!pystring)
1223 return Status::FromError(pystring.takeError());
1224 num_bytes = 0;
1225 auto bytes_written =
1226 As<long long>(m_py_obj.CallMethod("write", pystring.get()));
1227 if (!bytes_written)
1228 // Cloning since the wrapped exception may still reference the PyThread.
1229 return Status::FromError(bytes_written.takeError()).Clone();
1230 if (bytes_written.get() < 0)
1231 return Status::FromErrorString(
1232 ".write() method returned a negative number!");
1233 static_assert(sizeof(long long) >= sizeof(size_t), "overflow");
1234 num_bytes = bytes_written.get();
1235 return Status();
1236 }
1237
1238 Status Read(void *buf, size_t &num_bytes) override {
1239 GIL takeGIL;
1240 size_t num_chars = num_bytes / 6;
1241 size_t orig_num_bytes = num_bytes;
1242 num_bytes = 0;
1243 if (orig_num_bytes < 6) {
1244 return Status::FromErrorString(
1245 "can't read less than 6 bytes from a utf8 text stream");
1246 }
1247 auto pystring = As<PythonString>(
1248 m_py_obj.CallMethod("read", (unsigned long long)num_chars));
1249 if (!pystring)
1250 // Cloning since the wrapped exception may still reference the PyThread.
1251 return Status::FromError(pystring.takeError()).Clone();
1252 if (pystring.get().IsNone()) {
1253 // EOF
1254 return Status();
1255 }
1256 auto stringref = pystring.get().AsUTF8();
1257 if (!stringref)
1258 // Cloning since the wrapped exception may still reference the PyThread.
1259 return Status::FromError(stringref.takeError()).Clone();
1260 num_bytes = stringref.get().size();
1261 memcpy(buf, stringref.get().begin(), num_bytes);
1262 return Status();
1263 }
1264};
1265} // namespace
1266
1267llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) {
1268 if (!IsValid())
1269 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1270 "invalid PythonFile");
1271
1272 int fd = PyObject_AsFileDescriptor(m_py_obj);
1273 if (fd < 0) {
1274 PyErr_Clear();
1276 }
1277 auto options = GetOptionsForPyObject(*this);
1278 if (!options)
1279 return options.takeError();
1280
1285 // LLDB and python will not share I/O buffers. We should probably
1286 // flush the python buffers now.
1287 auto r = CallMethod("flush");
1288 if (!r)
1289 return r.takeError();
1290 }
1291
1292 FileSP file_sp;
1293 if (borrowed) {
1294 // In this case we don't need to retain the python
1295 // object at all.
1296 file_sp = std::make_shared<NativeFile>(fd, options.get(), false);
1297 } else {
1298 file_sp = std::static_pointer_cast<File>(
1299 std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get()));
1300 }
1301 if (!file_sp->IsValid())
1302 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1303 "invalid File");
1304
1305 return file_sp;
1306}
1307
1308llvm::Expected<FileSP>
1310
1311 assert(!PyErr_Occurred());
1312
1313 if (!IsValid())
1314 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1315 "invalid PythonFile");
1316
1317 int fd = PyObject_AsFileDescriptor(m_py_obj);
1318 if (fd < 0) {
1319 PyErr_Clear();
1321 }
1322
1323 auto io_module = PythonModule::Import("io");
1324 if (!io_module)
1325 return io_module.takeError();
1326 auto textIOBase = io_module.get().Get("TextIOBase");
1327 if (!textIOBase)
1328 return textIOBase.takeError();
1329 auto rawIOBase = io_module.get().Get("RawIOBase");
1330 if (!rawIOBase)
1331 return rawIOBase.takeError();
1332 auto bufferedIOBase = io_module.get().Get("BufferedIOBase");
1333 if (!bufferedIOBase)
1334 return bufferedIOBase.takeError();
1335
1336 FileSP file_sp;
1337
1338 auto isTextIO = IsInstance(textIOBase.get());
1339 if (!isTextIO)
1340 return isTextIO.takeError();
1341 if (isTextIO.get())
1342 file_sp = std::static_pointer_cast<File>(
1343 std::make_shared<TextPythonFile>(fd, *this, borrowed));
1344
1345 auto isRawIO = IsInstance(rawIOBase.get());
1346 if (!isRawIO)
1347 return isRawIO.takeError();
1348 auto isBufferedIO = IsInstance(bufferedIOBase.get());
1349 if (!isBufferedIO)
1350 return isBufferedIO.takeError();
1351
1352 if (isRawIO.get() || isBufferedIO.get()) {
1353 file_sp = std::static_pointer_cast<File>(
1354 std::make_shared<BinaryPythonFile>(fd, *this, borrowed));
1355 }
1356
1357 if (!file_sp)
1358 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1359 "python file is neither text nor binary");
1360
1361 if (!file_sp->IsValid())
1362 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1363 "invalid File");
1364
1365 return file_sp;
1366}
1367
1368Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) {
1369 if (!file.IsValid())
1370 return llvm::createStringError(llvm::inconvertibleErrorCode(),
1371 "invalid file");
1372
1373 if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file))
1374 return Retain<PythonFile>(simple->GetPythonObject());
1375 if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file))
1376 return Retain<PythonFile>(pythonio->GetPythonObject());
1377
1378 if (!mode) {
1379 auto m = file.GetOpenMode();
1380 if (!m)
1381 return m.takeError();
1382 mode = m.get();
1383 }
1384
1385 PyObject *file_obj;
1386 file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr,
1387 "ignore", nullptr, /*closefd=*/0);
1388
1389 if (!file_obj)
1390 return exception();
1391
1392 return Take<PythonFile>(file_obj);
1393}
1394
1396 if (function.IsValid())
1397 return Error::success();
1398
1400 auto builtins = PythonModule::BuiltinsModule();
1401 if (Error error = globals.SetItem("__builtins__", builtins))
1402 return error;
1403 PyObject *o = RunString(script, Py_file_input, globals.get(), globals.get());
1404 if (!o)
1405 return exception();
1407 auto f = As<PythonCallable>(globals.GetItem("main"));
1408 if (!f)
1409 return f.takeError();
1410 function = std::move(f.get());
1411
1412 return Error::success();
1413}
1414
1415llvm::Expected<PythonObject>
1416python::runStringOneLine(const llvm::Twine &string,
1417 const PythonDictionary &globals,
1418 const PythonDictionary &locals) {
1419 if (!globals.IsValid() || !locals.IsValid())
1420 return nullDeref();
1421
1422 PyObject *code =
1423 Py_CompileString(NullTerminated(string), "<string>", Py_eval_input);
1424 if (!code) {
1425 PyErr_Clear();
1426 code =
1427 Py_CompileString(NullTerminated(string), "<string>", Py_single_input);
1428 }
1429 if (!code)
1430 return exception();
1431 auto code_ref = Take<PythonObject>(code);
1432
1433 PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get());
1434
1435 if (!result)
1436 return exception();
1437
1438 return Take<PythonObject>(result);
1439}
1440
1441llvm::Expected<PythonObject>
1442python::runStringMultiLine(const llvm::Twine &string,
1443 const PythonDictionary &globals,
1444 const PythonDictionary &locals) {
1445 if (!globals.IsValid() || !locals.IsValid())
1446 return nullDeref();
1447 PyObject *result = RunString(NullTerminated(string), Py_file_input,
1448 globals.get(), locals.get());
1449 if (!result)
1450 return exception();
1451 return Take<PythonObject>(result);
1452}
1453
1454PyObject *lldb_private::python::RunString(const char *str, int start,
1455 PyObject *globals, PyObject *locals) {
1456 const char *filename = "<string>";
1457
1458 // Compile the string into a code object.
1459 PyObject *code = Py_CompileString(str, filename, start);
1460 if (!code)
1461 return nullptr;
1462
1463 // Execute the code object.
1464 PyObject *result = PyEval_EvalCode(code, globals, locals);
1465
1466 // Clean up the code object.
1467 Py_DECREF(code);
1468
1469 return result;
1470}
1471
1473 PyObject *main_module = PyImport_AddModule("__main__");
1474 if (!main_module)
1475 return -1;
1476
1477 PyObject *globals = PyModule_GetDict(main_module);
1478 if (!globals)
1479 return -1;
1480
1481 PyObject *result = RunString(str, Py_file_input, globals, globals);
1482 if (!result)
1483 return -1;
1484
1485 return 0;
1486}
static llvm::raw_ostream & error(Stream &strm)
static char ID
#define LLDB_LOGF(log,...)
Definition Log.h:376
static const char get_arg_info_script[]
llvm::Expected< File::OpenOptions > GetOptionsForPyObject(const PythonObject &obj)
const char read_exception_script[]
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
virtual bool isA(const void *classID) const
Definition File.h:366
static int kInvalidDescriptor
Definition File.h:38
virtual int GetDescriptor() const
Get underlying OS file descriptor for this file, or kInvalidDescriptor.
Definition File.cpp:126
@ eOpenOptionReadOnly
Definition File.h:51
@ eOpenOptionReadWrite
Definition File.h:53
@ eOpenOptionWriteOnly
Definition File.h:52
llvm::Expected< const char * > GetOpenMode() const
Definition File.h:323
bool IsValid() const override
IsValid.
Definition File.cpp:113
bool isA(const void *classID) const override
Definition File.h:416
Status Clone() const
Don't call this function in new code.
Definition Status.h:174
bool Fail() const
Test for error condition.
Definition Status.cpp:294
A stream class that can stream formatted output to a file.
Definition Stream.h:28
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
StructuredData::BooleanSP CreateStructuredBoolean() const
static bool Check(PyObject *py_obj)
StructuredData::StringSP CreateStructuredString() const
llvm::ArrayRef< uint8_t > GetBytes() const
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)
llvm::ArrayRef< uint8_t > GetBytes() const
llvm::Expected< ArgInfo > GetArgInfo() const
static bool Check(PyObject *py_obj)
StructuredData::DictionarySP CreateStructuredDictionary() const
llvm::Expected< PythonObject > GetItem(const PythonObject &key) const
bool HasKey(const llvm::Twine &key) const
PythonObject GetItemForKey(const PythonObject &key) const
llvm::Error SetItem(const PythonObject &key, const PythonObject &value) 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)
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
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)
StructuredData::ArraySP CreateStructuredArray() const
static PythonModule AddModule(llvm::StringRef module)
llvm::Expected< PythonObject > Get(const llvm::Twine &name)
static bool Check(PyObject *py_obj)
static llvm::Expected< PythonModule > Import(const llvm::Twine &name)
PythonObject ResolveName(llvm::StringRef name) const
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)
bool HasAttribute(llvm::StringRef attribute) const
llvm::Expected< PythonObject > CallMethod(const char *name, const T &... t) const
llvm::Expected< llvm::StringRef > AsUTF8() const
void SetString(llvm::StringRef string)
StructuredData::StringSP CreateStructuredString() const
static bool Check(PyObject *py_obj)
static llvm::Expected< PythonString > FromUTF8(llvm::StringRef string)
StructuredData::ArraySP CreateStructuredArray() const
void SetItemAtIndex(uint32_t index, const PythonObject &object)
PythonObject GetItemAtIndex(uint32_t index) const
static bool Check(PyObject *py_obj)
void Serialize(llvm::json::OStream &s) const override
#define PyBUF_READ
Definition lldb-python.h:61
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)
llvm::Expected< T > As(llvm::Expected< PythonObject > &&obj)
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.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::File > FileSP