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