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