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