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