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) :
100 PythonString::PythonString (const PythonObject &object) :
101 PythonObject(object.GetPythonObject())
105 PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
106 PythonObject (script_object_sp)
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) :
165 PythonInteger::PythonInteger (const PythonObject &object) :
166 PythonObject(object.GetPythonObject())
170 PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
171 PythonObject (script_object_sp)
175 PythonInteger::PythonInteger (int64_t value) :
176 PythonObject(PyInt_FromLong(value))
181 PythonInteger::~PythonInteger ()
186 PythonInteger::Reset (PyObject *py_obj)
188 if (py_obj && PyInt_Check(py_obj))
189 return PythonObject::Reset(py_obj);
191 PythonObject::Reset(NULL);
192 return py_obj == NULL;
196 PythonInteger::GetInteger()
199 return PyInt_AsLong(m_py_obj);
205 PythonInteger::SetInteger (int64_t value)
207 PythonObject::Reset(PyInt_FromLong(value));
210 //----------------------------------------------------------------------
212 //----------------------------------------------------------------------
214 PythonList::PythonList () :
215 PythonObject(PyList_New(0))
219 PythonList::PythonList (uint32_t count) :
220 PythonObject(PyList_New(count))
224 PythonList::PythonList (PyObject *py_obj) :
230 PythonList::PythonList (const PythonObject &object) :
231 PythonObject(object.GetPythonObject())
235 PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
236 PythonObject (script_object_sp)
240 PythonList::~PythonList ()
245 PythonList::Reset (PyObject *py_obj)
247 if (py_obj && PyList_Check(py_obj))
248 return PythonObject::Reset(py_obj);
250 PythonObject::Reset(NULL);
251 return py_obj == NULL;
255 PythonList::GetSize()
258 return PyList_GET_SIZE(m_py_obj);
263 PythonList::GetItemAtIndex (uint32_t index)
266 return PythonObject(PyList_GetItem(m_py_obj, index));
271 PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
273 if (m_py_obj && object)
274 PyList_SetItem(m_py_obj, index, object.GetPythonObject());
278 PythonList::AppendItem (const PythonObject &object)
280 if (m_py_obj && object)
281 PyList_Append(m_py_obj, object.GetPythonObject());
284 //----------------------------------------------------------------------
286 //----------------------------------------------------------------------
288 PythonDictionary::PythonDictionary () :
289 PythonObject(PyDict_New())
293 PythonDictionary::PythonDictionary (PyObject *py_obj) :
299 PythonDictionary::PythonDictionary (const PythonObject &object) :
300 PythonObject(object.GetPythonObject())
304 PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
305 PythonObject (script_object_sp)
309 PythonDictionary::~PythonDictionary ()
314 PythonDictionary::Reset (PyObject *py_obj)
316 if (py_obj && PyDict_Check(py_obj))
317 return PythonObject::Reset(py_obj);
319 PythonObject::Reset(NULL);
320 return py_obj == NULL;
324 PythonDictionary::GetSize()
327 return PyDict_Size(m_py_obj);
332 PythonDictionary::GetItemForKey (const char *key) const
336 PythonString python_key(key);
337 return GetItemForKey(python_key);
344 PythonDictionary::GetItemForKey (const PythonString &key) const
347 return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
348 return PythonObject();
353 PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
357 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
358 if (py_obj && PyString_Check(py_obj))
359 return PyString_AsString(py_obj);
365 PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
369 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
372 if (PyInt_Check(py_obj))
373 return PyInt_AsLong(py_obj);
375 if (PyLong_Check(py_obj))
376 return PyLong_AsLong(py_obj);
383 PythonDictionary::GetKeys () const
386 return PythonList(PyDict_Keys(m_py_obj));
391 PythonDictionary::GetKeyAtPosition (uint32_t pos) const
393 PyObject *key, *value;
394 Py_ssize_t pos_iter = 0;
398 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
401 return PythonString(key);
404 return PythonString();
408 PythonDictionary::GetValueAtPosition (uint32_t pos) const
410 PyObject *key, *value;
411 Py_ssize_t pos_iter = 0;
416 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
418 return PythonObject(value);
420 return PythonObject();
424 PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
426 if (m_py_obj && key && value)
427 PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());