1 //===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // In order to guarantee correct working with Python, Python.h *MUST* be
11 // the *FIRST* header file included here.
12 #ifdef LLDB_DISABLE_PYTHON
14 // Python is disabled in this build
18 #include "lldb/lldb-python.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Host/File.h"
24 #include "lldb/Interpreter/PythonDataObjects.h"
25 #include "lldb/Interpreter/ScriptInterpreter.h"
27 using namespace lldb_private;
30 //----------------------------------------------------------------------
32 //----------------------------------------------------------------------
33 PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
37 Reset ((PyObject *)script_object_sp->GetObject());
41 PythonObject::Dump (Stream &strm) const
45 FILE *file = ::tmpfile();
48 ::PyObject_Print (m_py_obj, file, 0);
49 const long length = ftell (file);
53 std::vector<char> file_contents (length,'\0');
54 const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
56 strm.Write (file_contents.data(), length_read);
62 strm.PutCString ("NULL");
69 return PythonString ();
70 PyObject *repr = PyObject_Repr(m_py_obj);
72 return PythonString ();
73 return PythonString(repr);
80 return PythonString ();
81 PyObject *str = PyObject_Str(m_py_obj);
83 return PythonString ();
84 return PythonString(str);
88 PythonObject::IsNULLOrNone () const
90 return ((m_py_obj == nullptr) || (m_py_obj == Py_None));
93 //----------------------------------------------------------------------
95 //----------------------------------------------------------------------
97 PythonString::PythonString (PyObject *py_obj) :
100 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string
103 PythonString::PythonString (const PythonObject &object) :
106 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string
109 PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
112 if (script_object_sp)
113 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
116 PythonString::PythonString (const char* string) :
117 PythonObject(PyString_FromString(string))
121 PythonString::PythonString () :
126 PythonString::~PythonString ()
131 PythonString::Reset (PyObject *py_obj)
133 if (py_obj && PyString_Check(py_obj))
134 return PythonObject::Reset(py_obj);
136 PythonObject::Reset(NULL);
137 return py_obj == NULL;
141 PythonString::GetString() const
144 return PyString_AsString(m_py_obj);
149 PythonString::GetSize() const
152 return PyString_Size(m_py_obj);
157 PythonString::SetString (const char* string)
159 PythonObject::Reset(PyString_FromString(string));
162 //----------------------------------------------------------------------
164 //----------------------------------------------------------------------
166 PythonInteger::PythonInteger (PyObject *py_obj) :
169 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type
172 PythonInteger::PythonInteger (const PythonObject &object) :
175 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a integer type
178 PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
181 if (script_object_sp)
182 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
185 PythonInteger::PythonInteger (int64_t value) :
192 PythonInteger::~PythonInteger ()
197 PythonInteger::Reset (PyObject *py_obj)
201 if (PyInt_Check (py_obj) || PyLong_Check(py_obj))
202 return PythonObject::Reset(py_obj);
205 PythonObject::Reset(NULL);
206 return py_obj == NULL;
210 PythonInteger::GetInteger()
214 if (PyInt_Check(m_py_obj))
215 return PyInt_AsLong(m_py_obj);
216 else if (PyLong_Check(m_py_obj))
217 return PyLong_AsLongLong(m_py_obj);
223 PythonInteger::SetInteger (int64_t value)
225 PythonObject::Reset(PyLong_FromLongLong(value));
228 //----------------------------------------------------------------------
230 //----------------------------------------------------------------------
232 PythonList::PythonList (bool create_empty) :
233 PythonObject(create_empty ? PyList_New(0) : NULL)
237 PythonList::PythonList (uint32_t count) :
238 PythonObject(PyList_New(count))
242 PythonList::PythonList (PyObject *py_obj) :
245 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list
249 PythonList::PythonList (const PythonObject &object) :
252 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a list
255 PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
258 if (script_object_sp)
259 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list
262 PythonList::~PythonList ()
267 PythonList::Reset (PyObject *py_obj)
269 if (py_obj && PyList_Check(py_obj))
270 return PythonObject::Reset(py_obj);
272 PythonObject::Reset(NULL);
273 return py_obj == NULL;
277 PythonList::GetSize()
280 return PyList_GET_SIZE(m_py_obj);
285 PythonList::GetItemAtIndex (uint32_t index)
288 return PythonObject(PyList_GetItem(m_py_obj, index));
289 return PythonObject();
293 PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
295 if (m_py_obj && object)
296 PyList_SetItem(m_py_obj, index, object.get());
300 PythonList::AppendItem (const PythonObject &object)
302 if (m_py_obj && object)
303 PyList_Append(m_py_obj, object.get());
306 //----------------------------------------------------------------------
308 //----------------------------------------------------------------------
310 PythonDictionary::PythonDictionary (bool create_empty) :
311 PythonObject(create_empty ? PyDict_New() : NULL)
315 PythonDictionary::PythonDictionary (PyObject *py_obj) :
318 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
322 PythonDictionary::PythonDictionary (const PythonObject &object) :
325 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a dictionary
328 PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
331 if (script_object_sp)
332 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary
335 PythonDictionary::~PythonDictionary ()
340 PythonDictionary::Reset (PyObject *py_obj)
342 if (py_obj && PyDict_Check(py_obj))
343 return PythonObject::Reset(py_obj);
345 PythonObject::Reset(NULL);
346 return py_obj == NULL;
350 PythonDictionary::GetSize()
353 return PyDict_Size(m_py_obj);
358 PythonDictionary::GetItemForKey (const char *key) const
362 PythonString python_key(key);
363 return GetItemForKey(python_key);
365 return PythonObject();
370 PythonDictionary::GetItemForKey (const PythonString &key) const
373 return PythonObject(PyDict_GetItem(m_py_obj, key.get()));
374 return PythonObject();
379 PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
383 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get());
384 if (py_obj && PyString_Check(py_obj))
385 return PyString_AsString(py_obj);
391 PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
395 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get());
398 if (PyInt_Check(py_obj))
399 return PyInt_AsLong(py_obj);
401 if (PyLong_Check(py_obj))
402 return PyLong_AsLong(py_obj);
409 PythonDictionary::GetKeys () const
412 return PythonList(PyDict_Keys(m_py_obj));
413 return PythonList(true);
417 PythonDictionary::GetKeyAtPosition (uint32_t pos) const
419 PyObject *key, *value;
420 Py_ssize_t pos_iter = 0;
424 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
427 return PythonString(key);
430 return PythonString();
434 PythonDictionary::GetValueAtPosition (uint32_t pos) const
436 PyObject *key, *value;
437 Py_ssize_t pos_iter = 0;
440 return PythonObject();
442 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
444 return PythonObject(value);
446 return PythonObject();
450 PythonDictionary::SetItemForKey (const PythonString &key, PyObject *value)
452 if (m_py_obj && key && value)
453 PyDict_SetItem(m_py_obj, key.get(), value);
457 PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
459 if (m_py_obj && key && value)
460 PyDict_SetItem(m_py_obj, key.get(), value.get());