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