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