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);
87 //----------------------------------------------------------------------
89 //----------------------------------------------------------------------
91 PythonString::PythonString (PyObject *py_obj) :
94 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string
97 PythonString::PythonString (const PythonObject &object) :
100 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a string
103 PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
106 if (script_object_sp)
107 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
110 PythonString::PythonString (const char* string) :
111 PythonObject(PyString_FromString(string))
115 PythonString::PythonString () :
120 PythonString::~PythonString ()
125 PythonString::Reset (PyObject *py_obj)
127 if (py_obj && PyString_Check(py_obj))
128 return PythonObject::Reset(py_obj);
130 PythonObject::Reset(NULL);
131 return py_obj == NULL;
135 PythonString::GetString() const
138 return PyString_AsString(m_py_obj);
143 PythonString::GetSize() const
146 return PyString_Size(m_py_obj);
151 PythonString::SetString (const char* string)
153 PythonObject::Reset(PyString_FromString(string));
156 //----------------------------------------------------------------------
158 //----------------------------------------------------------------------
160 PythonInteger::PythonInteger (PyObject *py_obj) :
163 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type
166 PythonInteger::PythonInteger (const PythonObject &object) :
169 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a integer type
172 PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
175 if (script_object_sp)
176 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
179 PythonInteger::PythonInteger (int64_t value) :
186 PythonInteger::~PythonInteger ()
191 PythonInteger::Reset (PyObject *py_obj)
195 if (PyInt_Check (py_obj) || PyLong_Check(py_obj))
196 return PythonObject::Reset(py_obj);
199 PythonObject::Reset(NULL);
200 return py_obj == NULL;
204 PythonInteger::GetInteger()
208 if (PyInt_Check(m_py_obj))
209 return PyInt_AsLong(m_py_obj);
210 else if (PyLong_Check(m_py_obj))
211 return PyLong_AsLongLong(m_py_obj);
217 PythonInteger::SetInteger (int64_t value)
219 PythonObject::Reset(PyLong_FromLongLong(value));
222 //----------------------------------------------------------------------
224 //----------------------------------------------------------------------
226 PythonList::PythonList () :
227 PythonObject(PyList_New(0))
231 PythonList::PythonList (uint32_t count) :
232 PythonObject(PyList_New(count))
236 PythonList::PythonList (PyObject *py_obj) :
239 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list
243 PythonList::PythonList (const PythonObject &object) :
246 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a list
249 PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
252 if (script_object_sp)
253 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list
256 PythonList::~PythonList ()
261 PythonList::Reset (PyObject *py_obj)
263 if (py_obj && PyList_Check(py_obj))
264 return PythonObject::Reset(py_obj);
266 PythonObject::Reset(NULL);
267 return py_obj == NULL;
271 PythonList::GetSize()
274 return PyList_GET_SIZE(m_py_obj);
279 PythonList::GetItemAtIndex (uint32_t index)
282 return PythonObject(PyList_GetItem(m_py_obj, index));
287 PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
289 if (m_py_obj && object)
290 PyList_SetItem(m_py_obj, index, object.GetPythonObject());
294 PythonList::AppendItem (const PythonObject &object)
296 if (m_py_obj && object)
297 PyList_Append(m_py_obj, object.GetPythonObject());
300 //----------------------------------------------------------------------
302 //----------------------------------------------------------------------
304 PythonDictionary::PythonDictionary () :
305 PythonObject(PyDict_New())
309 PythonDictionary::PythonDictionary (PyObject *py_obj) :
312 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
316 PythonDictionary::PythonDictionary (const PythonObject &object) :
319 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a dictionary
322 PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
325 if (script_object_sp)
326 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary
329 PythonDictionary::~PythonDictionary ()
334 PythonDictionary::Reset (PyObject *py_obj)
336 if (py_obj && PyDict_Check(py_obj))
337 return PythonObject::Reset(py_obj);
339 PythonObject::Reset(NULL);
340 return py_obj == NULL;
344 PythonDictionary::GetSize()
347 return PyDict_Size(m_py_obj);
352 PythonDictionary::GetItemForKey (const char *key) const
356 PythonString python_key(key);
357 return GetItemForKey(python_key);
364 PythonDictionary::GetItemForKey (const PythonString &key) const
367 return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
368 return PythonObject();
373 PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
377 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
378 if (py_obj && PyString_Check(py_obj))
379 return PyString_AsString(py_obj);
385 PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
389 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
392 if (PyInt_Check(py_obj))
393 return PyInt_AsLong(py_obj);
395 if (PyLong_Check(py_obj))
396 return PyLong_AsLong(py_obj);
403 PythonDictionary::GetKeys () const
406 return PythonList(PyDict_Keys(m_py_obj));
411 PythonDictionary::GetKeyAtPosition (uint32_t pos) const
413 PyObject *key, *value;
414 Py_ssize_t pos_iter = 0;
418 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
421 return PythonString(key);
424 return PythonString();
428 PythonDictionary::GetValueAtPosition (uint32_t pos) const
430 PyObject *key, *value;
431 Py_ssize_t pos_iter = 0;
436 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
438 return PythonObject(value);
440 return PythonObject();
444 PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
446 if (m_py_obj && key && value)
447 PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());