xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (revision f8b22f8fea181d2564267282e6c8249fde01bc13)
1 //===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifdef LLDB_DISABLE_PYTHON
11 
12 // Python is disabled in this build
13 
14 #else
15 
16 #include "lldb-python.h"
17 #include "PythonDataObjects.h"
18 #include "ScriptInterpreterPython.h"
19 
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Host/File.h"
22 #include "lldb/Interpreter/ScriptInterpreter.h"
23 
24 #include <stdio.h>
25 
26 using namespace lldb_private;
27 using namespace lldb;
28 
29 void
30 StructuredPythonObject::Dump(Stream &s) const
31 {
32     s << "Python Obj: 0x" << GetValue();
33 }
34 
35 //----------------------------------------------------------------------
36 // PythonObject
37 //----------------------------------------------------------------------
38 
39 void
40 PythonObject::Dump(Stream &strm) const
41 {
42     if (m_py_obj)
43     {
44         FILE *file = ::tmpfile();
45         if (file)
46         {
47             ::PyObject_Print (m_py_obj, file, 0);
48             const long length = ftell (file);
49             if (length)
50             {
51                 ::rewind(file);
52                 std::vector<char> file_contents (length,'\0');
53                 const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
54                 if (length_read > 0)
55                     strm.Write (file_contents.data(), length_read);
56             }
57             ::fclose (file);
58         }
59     }
60     else
61         strm.PutCString ("NULL");
62 }
63 
64 PyObjectType
65 PythonObject::GetObjectType() const
66 {
67     if (!IsAllocated())
68         return PyObjectType::None;
69 
70     if (PyList_Check(m_py_obj))
71         return PyObjectType::List;
72     if (PyDict_Check(m_py_obj))
73         return PyObjectType::Dictionary;
74     if (PyUnicode_Check(m_py_obj))
75         return PyObjectType::String;
76     if (PyLong_Check(m_py_obj))
77         return PyObjectType::Integer;
78 #if PY_MAJOR_VERSION < 3
79     // These functions don't exist in Python 3.x.  PyString is PyUnicode
80     // and PyInt is PyLong.
81     if (PyString_Check(m_py_obj))
82         return PyObjectType::String;
83     if (PyInt_Check(m_py_obj))
84         return PyObjectType::Integer;
85 #endif
86     return PyObjectType::Unknown;
87 }
88 
89 PythonString
90 PythonObject::Repr()
91 {
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
101 PythonObject::Str()
102 {
103     if (!m_py_obj)
104         return PythonString();
105     PyObject *str = PyObject_Str(m_py_obj);
106     if (!str)
107         return PythonString();
108     return PythonString(PyRefType::Owned, str);
109 }
110 
111 bool
112 PythonObject::IsNone() const
113 {
114     return m_py_obj == Py_None;
115 }
116 
117 bool
118 PythonObject::IsValid() const
119 {
120     return m_py_obj != nullptr;
121 }
122 
123 bool
124 PythonObject::IsAllocated() const
125 {
126     return IsValid() && !IsNone();
127 }
128 
129 StructuredData::ObjectSP
130 PythonObject::CreateStructuredObject() const
131 {
132     switch (GetObjectType())
133     {
134         case PyObjectType::Dictionary:
135             return PythonDictionary(PyRefType::Borrowed, m_py_obj).CreateStructuredDictionary();
136         case PyObjectType::Integer:
137             return PythonInteger(PyRefType::Borrowed, m_py_obj).CreateStructuredInteger();
138         case PyObjectType::List:
139             return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
140         case PyObjectType::String:
141             return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
142         case PyObjectType::None:
143             return StructuredData::ObjectSP();
144         default:
145             return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
146     }
147 }
148 
149 //----------------------------------------------------------------------
150 // PythonString
151 //----------------------------------------------------------------------
152 
153 PythonString::PythonString(PyRefType type, PyObject *py_obj)
154     : PythonObject()
155 {
156     Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
157 }
158 
159 PythonString::PythonString(const PythonString &object)
160     : PythonObject(object)
161 {
162 }
163 
164 PythonString::PythonString(llvm::StringRef string)
165     : PythonObject()
166 {
167     SetString(string);
168 }
169 
170 PythonString::PythonString(const char *string)
171     : PythonObject()
172 {
173     SetString(llvm::StringRef(string));
174 }
175 
176 PythonString::PythonString()
177     : PythonObject()
178 {
179 }
180 
181 PythonString::~PythonString ()
182 {
183 }
184 
185 bool
186 PythonString::Check(PyObject *py_obj)
187 {
188     if (!py_obj)
189         return false;
190 #if PY_MAJOR_VERSION >= 3
191     // Python 3 does not have PyString objects, only PyUnicode.
192     return PyUnicode_Check(py_obj);
193 #else
194     return PyUnicode_Check(py_obj) || PyString_Check(py_obj);
195 #endif
196 }
197 
198 void
199 PythonString::Reset(PyRefType type, PyObject *py_obj)
200 {
201     // Grab the desired reference type so that if we end up rejecting
202     // `py_obj` it still gets decremented if necessary.
203     PythonObject result(type, py_obj);
204 
205     if (!PythonString::Check(py_obj))
206     {
207         PythonObject::Reset();
208         return;
209     }
210 
211     // Convert this to a PyBytes object, and only store the PyBytes.  Note that in
212     // Python 2.x, PyString and PyUnicode are interchangeable, and PyBytes is an alias
213     // of PyString.  So on 2.x, if we get into this branch, we already have a PyBytes.
214     if (PyUnicode_Check(py_obj))
215     {
216         // Since we're converting this to a different object, we assume ownership of the
217         // new object regardless of the value of `type`.
218         result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(py_obj));
219     }
220 
221     assert(PyBytes_Check(result.get()) && "PythonString::Reset received a non-string");
222 
223     // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
224     // back into the virtual implementation.
225     PythonObject::Reset(PyRefType::Borrowed, result.get());
226 }
227 
228 llvm::StringRef
229 PythonString::GetString() const
230 {
231     if (IsValid())
232     {
233         Py_ssize_t size;
234         char *c;
235         PyBytes_AsStringAndSize(m_py_obj, &c, &size);
236         return llvm::StringRef(c, size);
237     }
238     return llvm::StringRef();
239 }
240 
241 size_t
242 PythonString::GetSize() const
243 {
244     if (IsValid())
245         return PyBytes_Size(m_py_obj);
246     return 0;
247 }
248 
249 void
250 PythonString::SetString (llvm::StringRef string)
251 {
252 #if PY_MAJOR_VERSION >= 3
253     PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size());
254     PyObject *bytes = PyUnicode_AsUTF8String(unicode);
255     PythonObject::Reset(PyRefType::Owned, bytes);
256     Py_DECREF(unicode);
257 #else
258     PythonObject::Reset(PyRefType::Owned, PyString_FromStringAndSize(string.data(), string.size()));
259 #endif
260 }
261 
262 StructuredData::StringSP
263 PythonString::CreateStructuredString() const
264 {
265     StructuredData::StringSP result(new StructuredData::String);
266     result->SetValue(GetString());
267     return result;
268 }
269 
270 //----------------------------------------------------------------------
271 // PythonInteger
272 //----------------------------------------------------------------------
273 
274 PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj)
275     : PythonObject()
276 {
277     Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type
278 }
279 
280 PythonInteger::PythonInteger(const PythonInteger &object)
281     : PythonObject(object)
282 {
283 }
284 
285 PythonInteger::PythonInteger(int64_t value)
286     : PythonObject()
287 {
288     SetInteger(value);
289 }
290 
291 
292 PythonInteger::~PythonInteger ()
293 {
294 }
295 
296 bool
297 PythonInteger::Check(PyObject *py_obj)
298 {
299     if (!py_obj)
300         return false;
301 
302 #if PY_MAJOR_VERSION >= 3
303     // Python 3 does not have PyInt_Check.  There is only one type of
304     // integral value, long.
305     return PyLong_Check(py_obj);
306 #else
307     return PyLong_Check(py_obj) || PyInt_Check(py_obj);
308 #endif
309 }
310 
311 void
312 PythonInteger::Reset(PyRefType type, PyObject *py_obj)
313 {
314     // Grab the desired reference type so that if we end up rejecting
315     // `py_obj` it still gets decremented if necessary.
316     PythonObject result(type, py_obj);
317 
318     if (!PythonInteger::Check(py_obj))
319     {
320         PythonObject::Reset();
321         return;
322     }
323 
324 #if PY_MAJOR_VERSION < 3
325     // Always store this as a PyLong, which makes interoperability between
326     // Python 2.x and Python 3.x easier.  This is only necessary in 2.x,
327     // since 3.x doesn't even have a PyInt.
328     if (PyInt_Check(py_obj))
329     {
330         // Since we converted the original object to a different type, the new
331         // object is an owned object regardless of the ownership semantics requested
332         // by the user.
333         result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj)));
334     }
335 #endif
336 
337     assert(PyLong_Check(result.get()) && "Couldn't get a PyLong from this PyObject");
338 
339     // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
340     // back into the virtual implementation.
341     PythonObject::Reset(PyRefType::Borrowed, result.get());
342 }
343 
344 int64_t
345 PythonInteger::GetInteger() const
346 {
347     if (m_py_obj)
348     {
349         assert(PyLong_Check(m_py_obj) && "PythonInteger::GetInteger has a PyObject that isn't a PyLong");
350 
351         return PyLong_AsLongLong(m_py_obj);
352     }
353     return UINT64_MAX;
354 }
355 
356 void
357 PythonInteger::SetInteger(int64_t value)
358 {
359     PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value));
360 }
361 
362 StructuredData::IntegerSP
363 PythonInteger::CreateStructuredInteger() const
364 {
365     StructuredData::IntegerSP result(new StructuredData::Integer);
366     result->SetValue(GetInteger());
367     return result;
368 }
369 
370 //----------------------------------------------------------------------
371 // PythonList
372 //----------------------------------------------------------------------
373 
374 PythonList::PythonList(PyInitialValue value)
375     : PythonObject()
376 {
377     if (value == PyInitialValue::Empty)
378         Reset(PyRefType::Owned, PyList_New(0));
379 }
380 
381 PythonList::PythonList(PyRefType type, PyObject *py_obj)
382     : PythonObject()
383 {
384     Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list
385 }
386 
387 PythonList::PythonList(const PythonList &list)
388     : PythonObject(list)
389 {
390 }
391 
392 PythonList::~PythonList ()
393 {
394 }
395 
396 bool
397 PythonList::Check(PyObject *py_obj)
398 {
399     if (!py_obj)
400         return false;
401     return PyList_Check(py_obj);
402 }
403 
404 void
405 PythonList::Reset(PyRefType type, PyObject *py_obj)
406 {
407     // Grab the desired reference type so that if we end up rejecting
408     // `py_obj` it still gets decremented if necessary.
409     PythonObject result(type, py_obj);
410 
411     if (!PythonList::Check(py_obj))
412     {
413         PythonObject::Reset();
414         return;
415     }
416 
417     // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
418     // back into the virtual implementation.
419     PythonObject::Reset(PyRefType::Borrowed, result.get());
420 }
421 
422 uint32_t
423 PythonList::GetSize() const
424 {
425     if (IsValid())
426         return PyList_GET_SIZE(m_py_obj);
427     return 0;
428 }
429 
430 PythonObject
431 PythonList::GetItemAtIndex(uint32_t index) const
432 {
433     if (IsValid())
434         return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
435     return PythonObject();
436 }
437 
438 void
439 PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object)
440 {
441     if (IsAllocated() && object.IsValid())
442     {
443         // PyList_SetItem is documented to "steal" a reference, so we need to
444         // convert it to an owned reference by incrementing it.
445         Py_INCREF(object.get());
446         PyList_SetItem(m_py_obj, index, object.get());
447     }
448 }
449 
450 void
451 PythonList::AppendItem(const PythonObject &object)
452 {
453     if (IsAllocated() && object.IsValid())
454     {
455         // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
456         // here like we do with `PyList_SetItem`.
457         PyList_Append(m_py_obj, object.get());
458     }
459 }
460 
461 StructuredData::ArraySP
462 PythonList::CreateStructuredArray() const
463 {
464     StructuredData::ArraySP result(new StructuredData::Array);
465     uint32_t count = GetSize();
466     for (uint32_t i = 0; i < count; ++i)
467     {
468         PythonObject obj = GetItemAtIndex(i);
469         result->AddItem(obj.CreateStructuredObject());
470     }
471     return result;
472 }
473 
474 //----------------------------------------------------------------------
475 // PythonDictionary
476 //----------------------------------------------------------------------
477 
478 PythonDictionary::PythonDictionary(PyInitialValue value)
479     : PythonObject()
480 {
481     if (value == PyInitialValue::Empty)
482         Reset(PyRefType::Owned, PyDict_New());
483 }
484 
485 PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj)
486     : PythonObject()
487 {
488     Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
489 }
490 
491 PythonDictionary::PythonDictionary(const PythonDictionary &object)
492     : PythonObject(object)
493 {
494 }
495 
496 PythonDictionary::~PythonDictionary ()
497 {
498 }
499 
500 bool
501 PythonDictionary::Check(PyObject *py_obj)
502 {
503     if (!py_obj)
504         return false;
505 
506     return PyDict_Check(py_obj);
507 }
508 
509 void
510 PythonDictionary::Reset(PyRefType type, PyObject *py_obj)
511 {
512     // Grab the desired reference type so that if we end up rejecting
513     // `py_obj` it still gets decremented if necessary.
514     PythonObject result(type, py_obj);
515 
516     if (!PythonDictionary::Check(py_obj))
517     {
518         PythonObject::Reset();
519         return;
520     }
521 
522     // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls
523     // back into the virtual implementation.
524     PythonObject::Reset(PyRefType::Borrowed, result.get());
525 }
526 
527 uint32_t
528 PythonDictionary::GetSize() const
529 {
530     if (IsValid())
531         return PyDict_Size(m_py_obj);
532     return 0;
533 }
534 
535 PythonList
536 PythonDictionary::GetKeys() const
537 {
538     if (IsValid())
539         return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj));
540     return PythonList(PyInitialValue::Invalid);
541 }
542 
543 PythonObject
544 PythonDictionary::GetItemForKey(const PythonObject &key) const
545 {
546     if (IsAllocated() && key.IsValid())
547         return PythonObject(PyRefType::Borrowed, PyDict_GetItem(m_py_obj, key.get()));
548     return PythonObject();
549 }
550 
551 void
552 PythonDictionary::SetItemForKey(const PythonObject &key, const PythonObject &value)
553 {
554     if (IsAllocated() && key.IsValid() && value.IsValid())
555         PyDict_SetItem(m_py_obj, key.get(), value.get());
556 }
557 
558 StructuredData::DictionarySP
559 PythonDictionary::CreateStructuredDictionary() const
560 {
561     StructuredData::DictionarySP result(new StructuredData::Dictionary);
562     PythonList keys(GetKeys());
563     uint32_t num_keys = keys.GetSize();
564     for (uint32_t i = 0; i < num_keys; ++i)
565     {
566         PythonObject key = keys.GetItemAtIndex(i);
567         PythonObject value = GetItemForKey(key);
568         StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
569         result->AddItem(key.Str().GetString(), structured_value);
570     }
571     return result;
572 }
573 
574 #endif
575