xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (revision 05097246f352eca76207c9ebb08656c88bdf751a)
1 //===-- PythonDataObjects.cpp ------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifdef LLDB_DISABLE_PYTHON
12 
13 // Python is disabled in this build
14 
15 #else
16 
17 #include "PythonDataObjects.h"
18 #include "ScriptInterpreterPython.h"
19 
20 #include "lldb/Host/File.h"
21 #include "lldb/Host/FileSystem.h"
22 #include "lldb/Interpreter/ScriptInterpreter.h"
23 #include "lldb/Utility/Stream.h"
24 
25 #include "llvm/Support/ConvertUTF.h"
26 
27 #include <stdio.h>
28 
29 #include "llvm/ADT/StringSwitch.h"
30 
31 using namespace lldb_private;
32 using namespace lldb;
33 
34 void StructuredPythonObject::Dump(Stream &s, bool pretty_print) const {
35   s << "Python Obj: 0x" << GetValue();
36 }
37 
38 //----------------------------------------------------------------------
39 // PythonObject
40 //----------------------------------------------------------------------
41 
42 void PythonObject::Dump(Stream &strm) const {
43   if (m_py_obj) {
44     FILE *file = ::tmpfile();
45     if (file) {
46       ::PyObject_Print(m_py_obj, file, 0);
47       const long length = ftell(file);
48       if (length) {
49         ::rewind(file);
50         std::vector<char> file_contents(length, '\0');
51         const size_t length_read =
52             ::fread(file_contents.data(), 1, file_contents.size(), file);
53         if (length_read > 0)
54           strm.Write(file_contents.data(), length_read);
55       }
56       ::fclose(file);
57     }
58   } else
59     strm.PutCString("NULL");
60 }
61 
62 PyObjectType PythonObject::GetObjectType() const {
63   if (!IsAllocated())
64     return PyObjectType::None;
65 
66   if (PythonModule::Check(m_py_obj))
67     return PyObjectType::Module;
68   if (PythonList::Check(m_py_obj))
69     return PyObjectType::List;
70   if (PythonTuple::Check(m_py_obj))
71     return PyObjectType::Tuple;
72   if (PythonDictionary::Check(m_py_obj))
73     return PyObjectType::Dictionary;
74   if (PythonString::Check(m_py_obj))
75     return PyObjectType::String;
76 #if PY_MAJOR_VERSION >= 3
77   if (PythonBytes::Check(m_py_obj))
78     return PyObjectType::Bytes;
79 #endif
80   if (PythonByteArray::Check(m_py_obj))
81     return PyObjectType::ByteArray;
82   if (PythonInteger::Check(m_py_obj))
83     return PyObjectType::Integer;
84   if (PythonFile::Check(m_py_obj))
85     return PyObjectType::File;
86   if (PythonCallable::Check(m_py_obj))
87     return PyObjectType::Callable;
88   return PyObjectType::Unknown;
89 }
90 
91 PythonString PythonObject::Repr() const {
92   if (!m_py_obj)
93     return PythonString();
94   PyObject *repr = PyObject_Repr(m_py_obj);
95   if (!repr)
96     return PythonString();
97   return PythonString(PyRefType::Owned, repr);
98 }
99 
100 PythonString PythonObject::Str() const {
101   if (!m_py_obj)
102     return PythonString();
103   PyObject *str = PyObject_Str(m_py_obj);
104   if (!str)
105     return PythonString();
106   return PythonString(PyRefType::Owned, str);
107 }
108 
109 PythonObject
110 PythonObject::ResolveNameWithDictionary(llvm::StringRef name,
111                                         const PythonDictionary &dict) {
112   size_t dot_pos = name.find_first_of('.');
113   llvm::StringRef piece = name.substr(0, dot_pos);
114   PythonObject result = dict.GetItemForKey(PythonString(piece));
115   if (dot_pos == llvm::StringRef::npos) {
116     // There was no dot, we're done.
117     return result;
118   }
119 
120   // There was a dot.  The remaining portion of the name should be looked up in
121   // the context of the object that was found in the dictionary.
122   return result.ResolveName(name.substr(dot_pos + 1));
123 }
124 
125 PythonObject PythonObject::ResolveName(llvm::StringRef name) const {
126   // Resolve the name in the context of the specified object.  If, for example,
127   // `this` refers to a PyModule, then this will look for `name` in this
128   // module.  If `this` refers to a PyType, then it will resolve `name` as an
129   // attribute of that type.  If `this` refers to an instance of an object,
130   // then it will resolve `name` as the value of the specified field.
131   //
132   // This function handles dotted names so that, for example, if `m_py_obj`
133   // refers to the `sys` module, and `name` == "path.append", then it will find
134   // the function `sys.path.append`.
135 
136   size_t dot_pos = name.find_first_of('.');
137   if (dot_pos == llvm::StringRef::npos) {
138     // No dots in the name, we should be able to find the value immediately as
139     // an attribute of `m_py_obj`.
140     return GetAttributeValue(name);
141   }
142 
143   // Look up the first piece of the name, and resolve the rest as a child of
144   // that.
145   PythonObject parent = ResolveName(name.substr(0, dot_pos));
146   if (!parent.IsAllocated())
147     return PythonObject();
148 
149   // Tail recursion.. should be optimized by the compiler
150   return parent.ResolveName(name.substr(dot_pos + 1));
151 }
152 
153 bool PythonObject::HasAttribute(llvm::StringRef attr) const {
154   if (!IsValid())
155     return false;
156   PythonString py_attr(attr);
157   return !!PyObject_HasAttr(m_py_obj, py_attr.get());
158 }
159 
160 PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const {
161   if (!IsValid())
162     return PythonObject();
163 
164   PythonString py_attr(attr);
165   if (!PyObject_HasAttr(m_py_obj, py_attr.get()))
166     return PythonObject();
167 
168   return PythonObject(PyRefType::Owned,
169                       PyObject_GetAttr(m_py_obj, py_attr.get()));
170 }
171 
172 bool PythonObject::IsNone() const { return m_py_obj == Py_None; }
173 
174 bool PythonObject::IsValid() const { return m_py_obj != nullptr; }
175 
176 bool PythonObject::IsAllocated() const { return IsValid() && !IsNone(); }
177 
178 StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
179   switch (GetObjectType()) {
180   case PyObjectType::Dictionary:
181     return PythonDictionary(PyRefType::Borrowed, m_py_obj)
182         .CreateStructuredDictionary();
183   case PyObjectType::Integer:
184     return PythonInteger(PyRefType::Borrowed, m_py_obj)
185         .CreateStructuredInteger();
186   case PyObjectType::List:
187     return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
188   case PyObjectType::String:
189     return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
190   case PyObjectType::Bytes:
191     return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
192   case PyObjectType::ByteArray:
193     return PythonByteArray(PyRefType::Borrowed, m_py_obj)
194         .CreateStructuredString();
195   case PyObjectType::None:
196     return StructuredData::ObjectSP();
197   default:
198     return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
199   }
200 }
201 
202 //----------------------------------------------------------------------
203 // PythonString
204 //----------------------------------------------------------------------
205 PythonBytes::PythonBytes() : PythonObject() {}
206 
207 PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) : PythonObject() {
208   SetBytes(bytes);
209 }
210 
211 PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) : PythonObject() {
212   SetBytes(llvm::ArrayRef<uint8_t>(bytes, length));
213 }
214 
215 PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() {
216   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
217 }
218 
219 PythonBytes::PythonBytes(const PythonBytes &object) : PythonObject(object) {}
220 
221 PythonBytes::~PythonBytes() {}
222 
223 bool PythonBytes::Check(PyObject *py_obj) {
224   if (!py_obj)
225     return false;
226   if (PyBytes_Check(py_obj))
227     return true;
228   return false;
229 }
230 
231 void PythonBytes::Reset(PyRefType type, PyObject *py_obj) {
232   // Grab the desired reference type so that if we end up rejecting `py_obj` it
233   // still gets decremented if necessary.
234   PythonObject result(type, py_obj);
235 
236   if (!PythonBytes::Check(py_obj)) {
237     PythonObject::Reset();
238     return;
239   }
240 
241   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
242   // overflow since it calls back into the virtual implementation.
243   PythonObject::Reset(PyRefType::Borrowed, result.get());
244 }
245 
246 llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const {
247   if (!IsValid())
248     return llvm::ArrayRef<uint8_t>();
249 
250   Py_ssize_t size;
251   char *c;
252 
253   PyBytes_AsStringAndSize(m_py_obj, &c, &size);
254   return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
255 }
256 
257 size_t PythonBytes::GetSize() const {
258   if (!IsValid())
259     return 0;
260   return PyBytes_Size(m_py_obj);
261 }
262 
263 void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) {
264   const char *data = reinterpret_cast<const char *>(bytes.data());
265   PyObject *py_bytes = PyBytes_FromStringAndSize(data, bytes.size());
266   PythonObject::Reset(PyRefType::Owned, py_bytes);
267 }
268 
269 StructuredData::StringSP PythonBytes::CreateStructuredString() const {
270   StructuredData::StringSP result(new StructuredData::String);
271   Py_ssize_t size;
272   char *c;
273   PyBytes_AsStringAndSize(m_py_obj, &c, &size);
274   result->SetValue(std::string(c, size));
275   return result;
276 }
277 
278 PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes)
279     : PythonByteArray(bytes.data(), bytes.size()) {}
280 
281 PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) {
282   const char *str = reinterpret_cast<const char *>(bytes);
283   Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length));
284 }
285 
286 PythonByteArray::PythonByteArray(PyRefType type, PyObject *o) {
287   Reset(type, o);
288 }
289 
290 PythonByteArray::PythonByteArray(const PythonBytes &object)
291     : PythonObject(object) {}
292 
293 PythonByteArray::~PythonByteArray() {}
294 
295 bool PythonByteArray::Check(PyObject *py_obj) {
296   if (!py_obj)
297     return false;
298   if (PyByteArray_Check(py_obj))
299     return true;
300   return false;
301 }
302 
303 void PythonByteArray::Reset(PyRefType type, PyObject *py_obj) {
304   // Grab the desired reference type so that if we end up rejecting `py_obj` it
305   // still gets decremented if necessary.
306   PythonObject result(type, py_obj);
307 
308   if (!PythonByteArray::Check(py_obj)) {
309     PythonObject::Reset();
310     return;
311   }
312 
313   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
314   // overflow since it calls back into the virtual implementation.
315   PythonObject::Reset(PyRefType::Borrowed, result.get());
316 }
317 
318 llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const {
319   if (!IsValid())
320     return llvm::ArrayRef<uint8_t>();
321 
322   char *c = PyByteArray_AsString(m_py_obj);
323   size_t size = GetSize();
324   return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
325 }
326 
327 size_t PythonByteArray::GetSize() const {
328   if (!IsValid())
329     return 0;
330 
331   return PyByteArray_Size(m_py_obj);
332 }
333 
334 StructuredData::StringSP PythonByteArray::CreateStructuredString() const {
335   StructuredData::StringSP result(new StructuredData::String);
336   llvm::ArrayRef<uint8_t> bytes = GetBytes();
337   const char *str = reinterpret_cast<const char *>(bytes.data());
338   result->SetValue(std::string(str, bytes.size()));
339   return result;
340 }
341 
342 //----------------------------------------------------------------------
343 // PythonString
344 //----------------------------------------------------------------------
345 
346 PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() {
347   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
348 }
349 
350 PythonString::PythonString(const PythonString &object) : PythonObject(object) {}
351 
352 PythonString::PythonString(llvm::StringRef string) : PythonObject() {
353   SetString(string);
354 }
355 
356 PythonString::PythonString(const char *string) : PythonObject() {
357   SetString(llvm::StringRef(string));
358 }
359 
360 PythonString::PythonString() : PythonObject() {}
361 
362 PythonString::~PythonString() {}
363 
364 bool PythonString::Check(PyObject *py_obj) {
365   if (!py_obj)
366     return false;
367 
368   if (PyUnicode_Check(py_obj))
369     return true;
370 #if PY_MAJOR_VERSION < 3
371   if (PyString_Check(py_obj))
372     return true;
373 #endif
374   return false;
375 }
376 
377 void PythonString::Reset(PyRefType type, PyObject *py_obj) {
378   // Grab the desired reference type so that if we end up rejecting `py_obj` it
379   // still gets decremented if necessary.
380   PythonObject result(type, py_obj);
381 
382   if (!PythonString::Check(py_obj)) {
383     PythonObject::Reset();
384     return;
385   }
386 #if PY_MAJOR_VERSION < 3
387   // In Python 2, Don't store PyUnicode objects directly, because we need
388   // access to their underlying character buffers which Python 2 doesn't
389   // provide.
390   if (PyUnicode_Check(py_obj))
391     result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(result.get()));
392 #endif
393   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
394   // overflow since it calls back into the virtual implementation.
395   PythonObject::Reset(PyRefType::Borrowed, result.get());
396 }
397 
398 llvm::StringRef PythonString::GetString() const {
399   if (!IsValid())
400     return llvm::StringRef();
401 
402   Py_ssize_t size;
403   char *c;
404 
405 #if PY_MAJOR_VERSION >= 3
406   c = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
407 #else
408   PyString_AsStringAndSize(m_py_obj, &c, &size);
409 #endif
410   return llvm::StringRef(c, size);
411 }
412 
413 size_t PythonString::GetSize() const {
414   if (IsValid()) {
415 #if PY_MAJOR_VERSION >= 3
416     return PyUnicode_GetSize(m_py_obj);
417 #else
418     return PyString_Size(m_py_obj);
419 #endif
420   }
421   return 0;
422 }
423 
424 void PythonString::SetString(llvm::StringRef string) {
425 #if PY_MAJOR_VERSION >= 3
426   PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size());
427   PythonObject::Reset(PyRefType::Owned, unicode);
428 #else
429   PyObject *str = PyString_FromStringAndSize(string.data(), string.size());
430   PythonObject::Reset(PyRefType::Owned, str);
431 #endif
432 }
433 
434 StructuredData::StringSP PythonString::CreateStructuredString() const {
435   StructuredData::StringSP result(new StructuredData::String);
436   result->SetValue(GetString());
437   return result;
438 }
439 
440 //----------------------------------------------------------------------
441 // PythonInteger
442 //----------------------------------------------------------------------
443 
444 PythonInteger::PythonInteger() : PythonObject() {}
445 
446 PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj)
447     : PythonObject() {
448   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type
449 }
450 
451 PythonInteger::PythonInteger(const PythonInteger &object)
452     : PythonObject(object) {}
453 
454 PythonInteger::PythonInteger(int64_t value) : PythonObject() {
455   SetInteger(value);
456 }
457 
458 PythonInteger::~PythonInteger() {}
459 
460 bool PythonInteger::Check(PyObject *py_obj) {
461   if (!py_obj)
462     return false;
463 
464 #if PY_MAJOR_VERSION >= 3
465   // Python 3 does not have PyInt_Check.  There is only one type of integral
466   // value, long.
467   return PyLong_Check(py_obj);
468 #else
469   return PyLong_Check(py_obj) || PyInt_Check(py_obj);
470 #endif
471 }
472 
473 void PythonInteger::Reset(PyRefType type, PyObject *py_obj) {
474   // Grab the desired reference type so that if we end up rejecting `py_obj` it
475   // still gets decremented if necessary.
476   PythonObject result(type, py_obj);
477 
478   if (!PythonInteger::Check(py_obj)) {
479     PythonObject::Reset();
480     return;
481   }
482 
483 #if PY_MAJOR_VERSION < 3
484   // Always store this as a PyLong, which makes interoperability between Python
485   // 2.x and Python 3.x easier.  This is only necessary in 2.x, since 3.x
486   // doesn't even have a PyInt.
487   if (PyInt_Check(py_obj)) {
488     // Since we converted the original object to a different type, the new
489     // object is an owned object regardless of the ownership semantics
490     // requested by the user.
491     result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj)));
492   }
493 #endif
494 
495   assert(PyLong_Check(result.get()) &&
496          "Couldn't get a PyLong from this PyObject");
497 
498   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
499   // overflow since it calls back into the virtual implementation.
500   PythonObject::Reset(PyRefType::Borrowed, result.get());
501 }
502 
503 int64_t PythonInteger::GetInteger() const {
504   if (m_py_obj) {
505     assert(PyLong_Check(m_py_obj) &&
506            "PythonInteger::GetInteger has a PyObject that isn't a PyLong");
507 
508     int overflow = 0;
509     int64_t result = PyLong_AsLongLongAndOverflow(m_py_obj, &overflow);
510     if (overflow != 0) {
511       // We got an integer that overflows, like 18446744072853913392L we can't
512       // use PyLong_AsLongLong() as it will return 0xffffffffffffffff. If we
513       // use the unsigned long long it will work as expected.
514       const uint64_t uval = PyLong_AsUnsignedLongLong(m_py_obj);
515       result = static_cast<int64_t>(uval);
516     }
517     return result;
518   }
519   return UINT64_MAX;
520 }
521 
522 void PythonInteger::SetInteger(int64_t value) {
523   PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value));
524 }
525 
526 StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const {
527   StructuredData::IntegerSP result(new StructuredData::Integer);
528   result->SetValue(GetInteger());
529   return result;
530 }
531 
532 //----------------------------------------------------------------------
533 // PythonList
534 //----------------------------------------------------------------------
535 
536 PythonList::PythonList(PyInitialValue value) : PythonObject() {
537   if (value == PyInitialValue::Empty)
538     Reset(PyRefType::Owned, PyList_New(0));
539 }
540 
541 PythonList::PythonList(int list_size) : PythonObject() {
542   Reset(PyRefType::Owned, PyList_New(list_size));
543 }
544 
545 PythonList::PythonList(PyRefType type, PyObject *py_obj) : PythonObject() {
546   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list
547 }
548 
549 PythonList::PythonList(const PythonList &list) : PythonObject(list) {}
550 
551 PythonList::~PythonList() {}
552 
553 bool PythonList::Check(PyObject *py_obj) {
554   if (!py_obj)
555     return false;
556   return PyList_Check(py_obj);
557 }
558 
559 void PythonList::Reset(PyRefType type, PyObject *py_obj) {
560   // Grab the desired reference type so that if we end up rejecting `py_obj` it
561   // still gets decremented if necessary.
562   PythonObject result(type, py_obj);
563 
564   if (!PythonList::Check(py_obj)) {
565     PythonObject::Reset();
566     return;
567   }
568 
569   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
570   // overflow since it calls back into the virtual implementation.
571   PythonObject::Reset(PyRefType::Borrowed, result.get());
572 }
573 
574 uint32_t PythonList::GetSize() const {
575   if (IsValid())
576     return PyList_GET_SIZE(m_py_obj);
577   return 0;
578 }
579 
580 PythonObject PythonList::GetItemAtIndex(uint32_t index) const {
581   if (IsValid())
582     return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
583   return PythonObject();
584 }
585 
586 void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) {
587   if (IsAllocated() && object.IsValid()) {
588     // PyList_SetItem is documented to "steal" a reference, so we need to
589     // convert it to an owned reference by incrementing it.
590     Py_INCREF(object.get());
591     PyList_SetItem(m_py_obj, index, object.get());
592   }
593 }
594 
595 void PythonList::AppendItem(const PythonObject &object) {
596   if (IsAllocated() && object.IsValid()) {
597     // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
598     // here like we do with `PyList_SetItem`.
599     PyList_Append(m_py_obj, object.get());
600   }
601 }
602 
603 StructuredData::ArraySP PythonList::CreateStructuredArray() const {
604   StructuredData::ArraySP result(new StructuredData::Array);
605   uint32_t count = GetSize();
606   for (uint32_t i = 0; i < count; ++i) {
607     PythonObject obj = GetItemAtIndex(i);
608     result->AddItem(obj.CreateStructuredObject());
609   }
610   return result;
611 }
612 
613 //----------------------------------------------------------------------
614 // PythonTuple
615 //----------------------------------------------------------------------
616 
617 PythonTuple::PythonTuple(PyInitialValue value) : PythonObject() {
618   if (value == PyInitialValue::Empty)
619     Reset(PyRefType::Owned, PyTuple_New(0));
620 }
621 
622 PythonTuple::PythonTuple(int tuple_size) : PythonObject() {
623   Reset(PyRefType::Owned, PyTuple_New(tuple_size));
624 }
625 
626 PythonTuple::PythonTuple(PyRefType type, PyObject *py_obj) : PythonObject() {
627   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a tuple
628 }
629 
630 PythonTuple::PythonTuple(const PythonTuple &tuple) : PythonObject(tuple) {}
631 
632 PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) {
633   m_py_obj = PyTuple_New(objects.size());
634 
635   uint32_t idx = 0;
636   for (auto object : objects) {
637     if (object.IsValid())
638       SetItemAtIndex(idx, object);
639     idx++;
640   }
641 }
642 
643 PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) {
644   m_py_obj = PyTuple_New(objects.size());
645 
646   uint32_t idx = 0;
647   for (auto py_object : objects) {
648     PythonObject object(PyRefType::Borrowed, py_object);
649     if (object.IsValid())
650       SetItemAtIndex(idx, object);
651     idx++;
652   }
653 }
654 
655 PythonTuple::~PythonTuple() {}
656 
657 bool PythonTuple::Check(PyObject *py_obj) {
658   if (!py_obj)
659     return false;
660   return PyTuple_Check(py_obj);
661 }
662 
663 void PythonTuple::Reset(PyRefType type, PyObject *py_obj) {
664   // Grab the desired reference type so that if we end up rejecting `py_obj` it
665   // still gets decremented if necessary.
666   PythonObject result(type, py_obj);
667 
668   if (!PythonTuple::Check(py_obj)) {
669     PythonObject::Reset();
670     return;
671   }
672 
673   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
674   // overflow since it calls back into the virtual implementation.
675   PythonObject::Reset(PyRefType::Borrowed, result.get());
676 }
677 
678 uint32_t PythonTuple::GetSize() const {
679   if (IsValid())
680     return PyTuple_GET_SIZE(m_py_obj);
681   return 0;
682 }
683 
684 PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const {
685   if (IsValid())
686     return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index));
687   return PythonObject();
688 }
689 
690 void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) {
691   if (IsAllocated() && object.IsValid()) {
692     // PyTuple_SetItem is documented to "steal" a reference, so we need to
693     // convert it to an owned reference by incrementing it.
694     Py_INCREF(object.get());
695     PyTuple_SetItem(m_py_obj, index, object.get());
696   }
697 }
698 
699 StructuredData::ArraySP PythonTuple::CreateStructuredArray() const {
700   StructuredData::ArraySP result(new StructuredData::Array);
701   uint32_t count = GetSize();
702   for (uint32_t i = 0; i < count; ++i) {
703     PythonObject obj = GetItemAtIndex(i);
704     result->AddItem(obj.CreateStructuredObject());
705   }
706   return result;
707 }
708 
709 //----------------------------------------------------------------------
710 // PythonDictionary
711 //----------------------------------------------------------------------
712 
713 PythonDictionary::PythonDictionary(PyInitialValue value) : PythonObject() {
714   if (value == PyInitialValue::Empty)
715     Reset(PyRefType::Owned, PyDict_New());
716 }
717 
718 PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj)
719     : PythonObject() {
720   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
721 }
722 
723 PythonDictionary::PythonDictionary(const PythonDictionary &object)
724     : PythonObject(object) {}
725 
726 PythonDictionary::~PythonDictionary() {}
727 
728 bool PythonDictionary::Check(PyObject *py_obj) {
729   if (!py_obj)
730     return false;
731 
732   return PyDict_Check(py_obj);
733 }
734 
735 void PythonDictionary::Reset(PyRefType type, PyObject *py_obj) {
736   // Grab the desired reference type so that if we end up rejecting `py_obj` it
737   // still gets decremented if necessary.
738   PythonObject result(type, py_obj);
739 
740   if (!PythonDictionary::Check(py_obj)) {
741     PythonObject::Reset();
742     return;
743   }
744 
745   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
746   // overflow since it calls back into the virtual implementation.
747   PythonObject::Reset(PyRefType::Borrowed, result.get());
748 }
749 
750 uint32_t PythonDictionary::GetSize() const {
751   if (IsValid())
752     return PyDict_Size(m_py_obj);
753   return 0;
754 }
755 
756 PythonList PythonDictionary::GetKeys() const {
757   if (IsValid())
758     return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj));
759   return PythonList(PyInitialValue::Invalid);
760 }
761 
762 PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const {
763   if (IsAllocated() && key.IsValid())
764     return PythonObject(PyRefType::Borrowed,
765                         PyDict_GetItem(m_py_obj, key.get()));
766   return PythonObject();
767 }
768 
769 void PythonDictionary::SetItemForKey(const PythonObject &key,
770                                      const PythonObject &value) {
771   if (IsAllocated() && key.IsValid() && value.IsValid())
772     PyDict_SetItem(m_py_obj, key.get(), value.get());
773 }
774 
775 StructuredData::DictionarySP
776 PythonDictionary::CreateStructuredDictionary() const {
777   StructuredData::DictionarySP result(new StructuredData::Dictionary);
778   PythonList keys(GetKeys());
779   uint32_t num_keys = keys.GetSize();
780   for (uint32_t i = 0; i < num_keys; ++i) {
781     PythonObject key = keys.GetItemAtIndex(i);
782     PythonObject value = GetItemForKey(key);
783     StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
784     result->AddItem(key.Str().GetString(), structured_value);
785   }
786   return result;
787 }
788 
789 PythonModule::PythonModule() : PythonObject() {}
790 
791 PythonModule::PythonModule(PyRefType type, PyObject *py_obj) {
792   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a module
793 }
794 
795 PythonModule::PythonModule(const PythonModule &dict) : PythonObject(dict) {}
796 
797 PythonModule::~PythonModule() {}
798 
799 PythonModule PythonModule::BuiltinsModule() {
800 #if PY_MAJOR_VERSION >= 3
801   return AddModule("builtins");
802 #else
803   return AddModule("__builtin__");
804 #endif
805 }
806 
807 PythonModule PythonModule::MainModule() { return AddModule("__main__"); }
808 
809 PythonModule PythonModule::AddModule(llvm::StringRef module) {
810   std::string str = module.str();
811   return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
812 }
813 
814 PythonModule PythonModule::ImportModule(llvm::StringRef module) {
815   std::string str = module.str();
816   return PythonModule(PyRefType::Owned, PyImport_ImportModule(str.c_str()));
817 }
818 
819 bool PythonModule::Check(PyObject *py_obj) {
820   if (!py_obj)
821     return false;
822 
823   return PyModule_Check(py_obj);
824 }
825 
826 void PythonModule::Reset(PyRefType type, PyObject *py_obj) {
827   // Grab the desired reference type so that if we end up rejecting `py_obj` it
828   // still gets decremented if necessary.
829   PythonObject result(type, py_obj);
830 
831   if (!PythonModule::Check(py_obj)) {
832     PythonObject::Reset();
833     return;
834   }
835 
836   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
837   // overflow since it calls back into the virtual implementation.
838   PythonObject::Reset(PyRefType::Borrowed, result.get());
839 }
840 
841 PythonDictionary PythonModule::GetDictionary() const {
842   return PythonDictionary(PyRefType::Borrowed, PyModule_GetDict(m_py_obj));
843 }
844 
845 PythonCallable::PythonCallable() : PythonObject() {}
846 
847 PythonCallable::PythonCallable(PyRefType type, PyObject *py_obj) {
848   Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a callable
849 }
850 
851 PythonCallable::PythonCallable(const PythonCallable &callable)
852     : PythonObject(callable) {}
853 
854 PythonCallable::~PythonCallable() {}
855 
856 bool PythonCallable::Check(PyObject *py_obj) {
857   if (!py_obj)
858     return false;
859 
860   return PyCallable_Check(py_obj);
861 }
862 
863 void PythonCallable::Reset(PyRefType type, PyObject *py_obj) {
864   // Grab the desired reference type so that if we end up rejecting `py_obj` it
865   // still gets decremented if necessary.
866   PythonObject result(type, py_obj);
867 
868   if (!PythonCallable::Check(py_obj)) {
869     PythonObject::Reset();
870     return;
871   }
872 
873   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
874   // overflow since it calls back into the virtual implementation.
875   PythonObject::Reset(PyRefType::Borrowed, result.get());
876 }
877 
878 PythonCallable::ArgInfo PythonCallable::GetNumArguments() const {
879   ArgInfo result = {0, false, false, false};
880   if (!IsValid())
881     return result;
882 
883   PyObject *py_func_obj = m_py_obj;
884   if (PyMethod_Check(py_func_obj)) {
885     py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
886     PythonObject im_self = GetAttributeValue("im_self");
887     if (im_self.IsValid() && !im_self.IsNone())
888       result.is_bound_method = true;
889   } else {
890     // see if this is a callable object with an __call__ method
891     if (!PyFunction_Check(py_func_obj)) {
892       PythonObject __call__ = GetAttributeValue("__call__");
893       if (__call__.IsValid()) {
894         auto __callable__ = __call__.AsType<PythonCallable>();
895         if (__callable__.IsValid()) {
896           py_func_obj = PyMethod_GET_FUNCTION(__callable__.get());
897           PythonObject im_self = GetAttributeValue("im_self");
898           if (im_self.IsValid() && !im_self.IsNone())
899             result.is_bound_method = true;
900         }
901       }
902     }
903   }
904 
905   if (!py_func_obj)
906     return result;
907 
908   PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj);
909   if (!code)
910     return result;
911 
912   result.count = code->co_argcount;
913   result.has_varargs = !!(code->co_flags & CO_VARARGS);
914   result.has_kwargs = !!(code->co_flags & CO_VARKEYWORDS);
915   return result;
916 }
917 
918 PythonObject PythonCallable::operator()() {
919   return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr));
920 }
921 
922 PythonObject PythonCallable::
923 operator()(std::initializer_list<PyObject *> args) {
924   PythonTuple arg_tuple(args);
925   return PythonObject(PyRefType::Owned,
926                       PyObject_CallObject(m_py_obj, arg_tuple.get()));
927 }
928 
929 PythonObject PythonCallable::
930 operator()(std::initializer_list<PythonObject> args) {
931   PythonTuple arg_tuple(args);
932   return PythonObject(PyRefType::Owned,
933                       PyObject_CallObject(m_py_obj, arg_tuple.get()));
934 }
935 
936 PythonFile::PythonFile() : PythonObject() {}
937 
938 PythonFile::PythonFile(File &file, const char *mode) { Reset(file, mode); }
939 
940 PythonFile::PythonFile(const char *path, const char *mode) {
941   lldb_private::File file(path, GetOptionsFromMode(mode));
942   Reset(file, mode);
943 }
944 
945 PythonFile::PythonFile(PyRefType type, PyObject *o) { Reset(type, o); }
946 
947 PythonFile::~PythonFile() {}
948 
949 bool PythonFile::Check(PyObject *py_obj) {
950 #if PY_MAJOR_VERSION < 3
951   return PyFile_Check(py_obj);
952 #else
953   // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a
954   // first-class object type anymore.  `PyFile_FromFd` is just a thin wrapper
955   // over `io.open()`, which returns some object derived from `io.IOBase`. As a
956   // result, the only way to detect a file in Python 3 is to check whether it
957   // inherits from `io.IOBase`.  Since it is possible for non-files to also
958   // inherit from `io.IOBase`, we additionally verify that it has the `fileno`
959   // attribute, which should guarantee that it is backed by the file system.
960   PythonObject io_module(PyRefType::Owned, PyImport_ImportModule("io"));
961   PythonDictionary io_dict(PyRefType::Borrowed,
962                            PyModule_GetDict(io_module.get()));
963   PythonObject io_base_class = io_dict.GetItemForKey(PythonString("IOBase"));
964 
965   PythonObject object_type(PyRefType::Owned, PyObject_Type(py_obj));
966 
967   if (1 != PyObject_IsSubclass(object_type.get(), io_base_class.get()))
968     return false;
969   if (!object_type.HasAttribute("fileno"))
970     return false;
971 
972   return true;
973 #endif
974 }
975 
976 void PythonFile::Reset(PyRefType type, PyObject *py_obj) {
977   // Grab the desired reference type so that if we end up rejecting `py_obj` it
978   // still gets decremented if necessary.
979   PythonObject result(type, py_obj);
980 
981   if (!PythonFile::Check(py_obj)) {
982     PythonObject::Reset();
983     return;
984   }
985 
986   // Calling PythonObject::Reset(const PythonObject&) will lead to stack
987   // overflow since it calls back into the virtual implementation.
988   PythonObject::Reset(PyRefType::Borrowed, result.get());
989 }
990 
991 void PythonFile::Reset(File &file, const char *mode) {
992   if (!file.IsValid()) {
993     Reset();
994     return;
995   }
996 
997   char *cmode = const_cast<char *>(mode);
998 #if PY_MAJOR_VERSION >= 3
999   Reset(PyRefType::Owned, PyFile_FromFd(file.GetDescriptor(), nullptr, cmode,
1000                                         -1, nullptr, "ignore", nullptr, 0));
1001 #else
1002   // Read through the Python source, doesn't seem to modify these strings
1003   Reset(PyRefType::Owned,
1004         PyFile_FromFile(file.GetStream(), const_cast<char *>(""), cmode,
1005                         nullptr));
1006 #endif
1007 }
1008 
1009 uint32_t PythonFile::GetOptionsFromMode(llvm::StringRef mode) {
1010   if (mode.empty())
1011     return 0;
1012 
1013   return llvm::StringSwitch<uint32_t>(mode.str())
1014       .Case("r", File::eOpenOptionRead)
1015       .Case("w", File::eOpenOptionWrite)
1016       .Case("a", File::eOpenOptionWrite | File::eOpenOptionAppend |
1017                      File::eOpenOptionCanCreate)
1018       .Case("r+", File::eOpenOptionRead | File::eOpenOptionWrite)
1019       .Case("w+", File::eOpenOptionRead | File::eOpenOptionWrite |
1020                       File::eOpenOptionCanCreate | File::eOpenOptionTruncate)
1021       .Case("a+", File::eOpenOptionRead | File::eOpenOptionWrite |
1022                       File::eOpenOptionAppend | File::eOpenOptionCanCreate)
1023       .Default(0);
1024 }
1025 
1026 bool PythonFile::GetUnderlyingFile(File &file) const {
1027   if (!IsValid())
1028     return false;
1029 
1030   file.Close();
1031   // We don't own the file descriptor returned by this function, make sure the
1032   // File object knows about that.
1033   file.SetDescriptor(PyObject_AsFileDescriptor(m_py_obj), false);
1034   PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>();
1035   file.SetOptions(PythonFile::GetOptionsFromMode(py_mode.GetString()));
1036   return file.IsValid();
1037 }
1038 
1039 #endif
1040