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