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