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