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