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 #if defined (__APPLE__)
19 #include <Python/Python.h>
26 #include "lldb/Core/Stream.h"
27 #include "lldb/Host/File.h"
28 #include "lldb/Interpreter/PythonDataObjects.h"
29 #include "lldb/Interpreter/ScriptInterpreter.h"
31 using namespace lldb_private;
34 //----------------------------------------------------------------------
36 //----------------------------------------------------------------------
37 PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
41 Reset ((PyObject *)script_object_sp->GetObject());
45 PythonObject::Dump (Stream &strm) const
49 FILE *file = ::tmpfile();
52 ::PyObject_Print (m_py_obj, file, 0);
53 const long length = ftell (file);
57 std::vector<char> file_contents (length,'\0');
58 const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
60 strm.Write (file_contents.data(), length_read);
66 strm.PutCString ("NULL");
73 return PythonString ();
74 PyObject *repr = PyObject_Repr(m_py_obj);
76 return PythonString ();
77 return PythonString(repr);
84 return PythonString ();
85 PyObject *str = PyObject_Str(m_py_obj);
87 return PythonString ();
88 return PythonString(str);
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
95 PythonString::PythonString (PyObject *py_obj) :
98 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string
101 PythonString::PythonString (const PythonObject &object) :
104 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a string
107 PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
110 if (script_object_sp)
111 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
114 PythonString::PythonString (const char* string) :
115 PythonObject(PyString_FromString(string))
119 PythonString::PythonString () :
124 PythonString::~PythonString ()
129 PythonString::Reset (PyObject *py_obj)
131 if (py_obj && PyString_Check(py_obj))
132 return PythonObject::Reset(py_obj);
134 PythonObject::Reset(NULL);
135 return py_obj == NULL;
139 PythonString::GetString() const
142 return PyString_AsString(m_py_obj);
147 PythonString::GetSize() const
150 return PyString_Size(m_py_obj);
155 PythonString::SetString (const char* string)
157 PythonObject::Reset(PyString_FromString(string));
160 //----------------------------------------------------------------------
162 //----------------------------------------------------------------------
164 PythonInteger::PythonInteger (PyObject *py_obj) :
167 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type
170 PythonInteger::PythonInteger (const PythonObject &object) :
173 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a integer type
176 PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
179 if (script_object_sp)
180 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
183 PythonInteger::PythonInteger (int64_t value) :
190 PythonInteger::~PythonInteger ()
195 PythonInteger::Reset (PyObject *py_obj)
199 if (PyInt_Check (py_obj) || PyLong_Check(py_obj))
200 return PythonObject::Reset(py_obj);
203 PythonObject::Reset(NULL);
204 return py_obj == NULL;
208 PythonInteger::GetInteger()
212 if (PyInt_Check(m_py_obj))
213 return PyInt_AsLong(m_py_obj);
214 else if (PyLong_Check(m_py_obj))
215 return PyLong_AsLongLong(m_py_obj);
221 PythonInteger::SetInteger (int64_t value)
223 PythonObject::Reset(PyLong_FromLongLong(value));
226 //----------------------------------------------------------------------
228 //----------------------------------------------------------------------
230 PythonList::PythonList () :
231 PythonObject(PyList_New(0))
235 PythonList::PythonList (uint32_t count) :
236 PythonObject(PyList_New(count))
240 PythonList::PythonList (PyObject *py_obj) :
243 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list
247 PythonList::PythonList (const PythonObject &object) :
250 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a list
253 PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
256 if (script_object_sp)
257 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list
260 PythonList::~PythonList ()
265 PythonList::Reset (PyObject *py_obj)
267 if (py_obj && PyList_Check(py_obj))
268 return PythonObject::Reset(py_obj);
270 PythonObject::Reset(NULL);
271 return py_obj == NULL;
275 PythonList::GetSize()
278 return PyList_GET_SIZE(m_py_obj);
283 PythonList::GetItemAtIndex (uint32_t index)
286 return PythonObject(PyList_GetItem(m_py_obj, index));
291 PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
293 if (m_py_obj && object)
294 PyList_SetItem(m_py_obj, index, object.GetPythonObject());
298 PythonList::AppendItem (const PythonObject &object)
300 if (m_py_obj && object)
301 PyList_Append(m_py_obj, object.GetPythonObject());
304 //----------------------------------------------------------------------
306 //----------------------------------------------------------------------
308 PythonDictionary::PythonDictionary () :
309 PythonObject(PyDict_New())
313 PythonDictionary::PythonDictionary (PyObject *py_obj) :
316 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
320 PythonDictionary::PythonDictionary (const PythonObject &object) :
323 Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a dictionary
326 PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
329 if (script_object_sp)
330 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary
333 PythonDictionary::~PythonDictionary ()
338 PythonDictionary::Reset (PyObject *py_obj)
340 if (py_obj && PyDict_Check(py_obj))
341 return PythonObject::Reset(py_obj);
343 PythonObject::Reset(NULL);
344 return py_obj == NULL;
348 PythonDictionary::GetSize()
351 return PyDict_Size(m_py_obj);
356 PythonDictionary::GetItemForKey (const char *key) const
360 PythonString python_key(key);
361 return GetItemForKey(python_key);
368 PythonDictionary::GetItemForKey (const PythonString &key) const
371 return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
372 return PythonObject();
377 PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
381 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
382 if (py_obj && PyString_Check(py_obj))
383 return PyString_AsString(py_obj);
389 PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
393 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
396 if (PyInt_Check(py_obj))
397 return PyInt_AsLong(py_obj);
399 if (PyLong_Check(py_obj))
400 return PyLong_AsLong(py_obj);
407 PythonDictionary::GetKeys () const
410 return PythonList(PyDict_Keys(m_py_obj));
415 PythonDictionary::GetKeyAtPosition (uint32_t pos) const
417 PyObject *key, *value;
418 Py_ssize_t pos_iter = 0;
422 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
425 return PythonString(key);
428 return PythonString();
432 PythonDictionary::GetValueAtPosition (uint32_t pos) const
434 PyObject *key, *value;
435 Py_ssize_t pos_iter = 0;
440 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
442 return PythonObject(value);
444 return PythonObject();
448 PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
450 if (m_py_obj && key && value)
451 PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());