1254721Semaste//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste// In order to guarantee correct working with Python, Python.h *MUST* be 11254721Semaste// the *FIRST* header file included here. 12254721Semaste#ifdef LLDB_DISABLE_PYTHON 13254721Semaste 14254721Semaste// Python is disabled in this build 15254721Semaste 16254721Semaste#else 17254721Semaste 18254721Semaste#if defined (__APPLE__) 19254721Semaste#include <Python/Python.h> 20254721Semaste#else 21254721Semaste#include <Python.h> 22254721Semaste#endif 23254721Semaste 24254721Semaste#include <stdio.h> 25254721Semaste 26254721Semaste#include "lldb/Core/Stream.h" 27254721Semaste#include "lldb/Host/File.h" 28254721Semaste#include "lldb/Interpreter/PythonDataObjects.h" 29254721Semaste#include "lldb/Interpreter/ScriptInterpreter.h" 30254721Semaste 31254721Semasteusing namespace lldb_private; 32254721Semasteusing namespace lldb; 33254721Semaste 34254721Semaste//---------------------------------------------------------------------- 35254721Semaste// PythonObject 36254721Semaste//---------------------------------------------------------------------- 37254721SemastePythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 38254721Semaste m_py_obj (NULL) 39254721Semaste{ 40254721Semaste if (script_object_sp) 41254721Semaste Reset ((PyObject *)script_object_sp->GetObject()); 42254721Semaste} 43254721Semaste 44254721Semastevoid 45254721SemastePythonObject::Dump (Stream &strm) const 46254721Semaste{ 47254721Semaste if (m_py_obj) 48254721Semaste { 49254721Semaste FILE *file = ::tmpfile(); 50254721Semaste if (file) 51254721Semaste { 52254721Semaste ::PyObject_Print (m_py_obj, file, 0); 53254721Semaste const long length = ftell (file); 54254721Semaste if (length) 55254721Semaste { 56254721Semaste ::rewind(file); 57254721Semaste std::vector<char> file_contents (length,'\0'); 58254721Semaste const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file); 59254721Semaste if (length_read > 0) 60254721Semaste strm.Write (file_contents.data(), length_read); 61254721Semaste } 62254721Semaste ::fclose (file); 63254721Semaste } 64254721Semaste } 65254721Semaste else 66254721Semaste strm.PutCString ("NULL"); 67254721Semaste} 68254721Semaste 69254721SemastePythonString 70254721SemastePythonObject::Repr () 71254721Semaste{ 72254721Semaste if (!m_py_obj) 73254721Semaste return PythonString (); 74254721Semaste PyObject *repr = PyObject_Repr(m_py_obj); 75254721Semaste if (!repr) 76254721Semaste return PythonString (); 77254721Semaste return PythonString(repr); 78254721Semaste} 79254721Semaste 80254721SemastePythonString 81254721SemastePythonObject::Str () 82254721Semaste{ 83254721Semaste if (!m_py_obj) 84254721Semaste return PythonString (); 85254721Semaste PyObject *str = PyObject_Str(m_py_obj); 86254721Semaste if (!str) 87254721Semaste return PythonString (); 88254721Semaste return PythonString(str); 89254721Semaste} 90254721Semaste 91254721Semaste//---------------------------------------------------------------------- 92254721Semaste// PythonString 93254721Semaste//---------------------------------------------------------------------- 94254721Semaste 95254721SemastePythonString::PythonString (PyObject *py_obj) : 96254721Semaste PythonObject(py_obj) 97254721Semaste{ 98254721Semaste} 99254721Semaste 100254721SemastePythonString::PythonString (const PythonObject &object) : 101254721Semaste PythonObject(object.GetPythonObject()) 102254721Semaste{ 103254721Semaste} 104254721Semaste 105254721SemastePythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 106254721Semaste PythonObject (script_object_sp) 107254721Semaste{ 108254721Semaste} 109254721Semaste 110254721SemastePythonString::PythonString (const char* string) : 111254721Semaste PythonObject(PyString_FromString(string)) 112254721Semaste{ 113254721Semaste} 114254721Semaste 115254721SemastePythonString::PythonString () : 116254721Semaste PythonObject() 117254721Semaste{ 118254721Semaste} 119254721Semaste 120254721SemastePythonString::~PythonString () 121254721Semaste{ 122254721Semaste} 123254721Semaste 124254721Semastebool 125254721SemastePythonString::Reset (PyObject *py_obj) 126254721Semaste{ 127254721Semaste if (py_obj && PyString_Check(py_obj)) 128254721Semaste return PythonObject::Reset(py_obj); 129254721Semaste 130254721Semaste PythonObject::Reset(NULL); 131254721Semaste return py_obj == NULL; 132254721Semaste} 133254721Semaste 134254721Semasteconst char* 135254721SemastePythonString::GetString() const 136254721Semaste{ 137254721Semaste if (m_py_obj) 138254721Semaste return PyString_AsString(m_py_obj); 139254721Semaste return NULL; 140254721Semaste} 141254721Semaste 142254721Semastesize_t 143254721SemastePythonString::GetSize() const 144254721Semaste{ 145254721Semaste if (m_py_obj) 146254721Semaste return PyString_Size(m_py_obj); 147254721Semaste return 0; 148254721Semaste} 149254721Semaste 150254721Semastevoid 151254721SemastePythonString::SetString (const char* string) 152254721Semaste{ 153254721Semaste PythonObject::Reset(PyString_FromString(string)); 154254721Semaste} 155254721Semaste 156254721Semaste//---------------------------------------------------------------------- 157254721Semaste// PythonInteger 158254721Semaste//---------------------------------------------------------------------- 159254721Semaste 160254721SemastePythonInteger::PythonInteger (PyObject *py_obj) : 161254721Semaste PythonObject(py_obj) 162254721Semaste{ 163254721Semaste} 164254721Semaste 165254721SemastePythonInteger::PythonInteger (const PythonObject &object) : 166254721Semaste PythonObject(object.GetPythonObject()) 167254721Semaste{ 168254721Semaste} 169254721Semaste 170254721SemastePythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 171254721Semaste PythonObject (script_object_sp) 172254721Semaste{ 173254721Semaste} 174254721Semaste 175254721SemastePythonInteger::PythonInteger (int64_t value) : 176254721Semaste PythonObject(PyInt_FromLong(value)) 177254721Semaste{ 178254721Semaste} 179254721Semaste 180254721Semaste 181254721SemastePythonInteger::~PythonInteger () 182254721Semaste{ 183254721Semaste} 184254721Semaste 185254721Semastebool 186254721SemastePythonInteger::Reset (PyObject *py_obj) 187254721Semaste{ 188254721Semaste if (py_obj && PyInt_Check(py_obj)) 189254721Semaste return PythonObject::Reset(py_obj); 190254721Semaste 191254721Semaste PythonObject::Reset(NULL); 192254721Semaste return py_obj == NULL; 193254721Semaste} 194254721Semaste 195254721Semasteint64_t 196254721SemastePythonInteger::GetInteger() 197254721Semaste{ 198254721Semaste if (m_py_obj) 199254721Semaste return PyInt_AsLong(m_py_obj); 200254721Semaste else 201254721Semaste return UINT64_MAX; 202254721Semaste} 203254721Semaste 204254721Semastevoid 205254721SemastePythonInteger::SetInteger (int64_t value) 206254721Semaste{ 207254721Semaste PythonObject::Reset(PyInt_FromLong(value)); 208254721Semaste} 209254721Semaste 210254721Semaste//---------------------------------------------------------------------- 211254721Semaste// PythonList 212254721Semaste//---------------------------------------------------------------------- 213254721Semaste 214254721SemastePythonList::PythonList () : 215254721Semaste PythonObject(PyList_New(0)) 216254721Semaste{ 217254721Semaste} 218254721Semaste 219254721SemastePythonList::PythonList (uint32_t count) : 220254721Semaste PythonObject(PyList_New(count)) 221254721Semaste{ 222254721Semaste} 223254721Semaste 224254721SemastePythonList::PythonList (PyObject *py_obj) : 225254721Semaste PythonObject(py_obj) 226254721Semaste{ 227254721Semaste} 228254721Semaste 229254721Semaste 230254721SemastePythonList::PythonList (const PythonObject &object) : 231254721Semaste PythonObject(object.GetPythonObject()) 232254721Semaste{ 233254721Semaste} 234254721Semaste 235254721SemastePythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 236254721Semaste PythonObject (script_object_sp) 237254721Semaste{ 238254721Semaste} 239254721Semaste 240254721SemastePythonList::~PythonList () 241254721Semaste{ 242254721Semaste} 243254721Semaste 244254721Semastebool 245254721SemastePythonList::Reset (PyObject *py_obj) 246254721Semaste{ 247254721Semaste if (py_obj && PyList_Check(py_obj)) 248254721Semaste return PythonObject::Reset(py_obj); 249254721Semaste 250254721Semaste PythonObject::Reset(NULL); 251254721Semaste return py_obj == NULL; 252254721Semaste} 253254721Semaste 254254721Semasteuint32_t 255254721SemastePythonList::GetSize() 256254721Semaste{ 257254721Semaste if (m_py_obj) 258254721Semaste return PyList_GET_SIZE(m_py_obj); 259254721Semaste return 0; 260254721Semaste} 261254721Semaste 262254721SemastePythonObject 263254721SemastePythonList::GetItemAtIndex (uint32_t index) 264254721Semaste{ 265254721Semaste if (m_py_obj) 266254721Semaste return PythonObject(PyList_GetItem(m_py_obj, index)); 267254721Semaste return NULL; 268254721Semaste} 269254721Semaste 270254721Semastevoid 271254721SemastePythonList::SetItemAtIndex (uint32_t index, const PythonObject & object) 272254721Semaste{ 273254721Semaste if (m_py_obj && object) 274254721Semaste PyList_SetItem(m_py_obj, index, object.GetPythonObject()); 275254721Semaste} 276254721Semaste 277254721Semastevoid 278254721SemastePythonList::AppendItem (const PythonObject &object) 279254721Semaste{ 280254721Semaste if (m_py_obj && object) 281254721Semaste PyList_Append(m_py_obj, object.GetPythonObject()); 282254721Semaste} 283254721Semaste 284254721Semaste//---------------------------------------------------------------------- 285254721Semaste// PythonDictionary 286254721Semaste//---------------------------------------------------------------------- 287254721Semaste 288254721SemastePythonDictionary::PythonDictionary () : 289254721Semaste PythonObject(PyDict_New()) 290254721Semaste{ 291254721Semaste} 292254721Semaste 293254721SemastePythonDictionary::PythonDictionary (PyObject *py_obj) : 294254721Semaste PythonObject(py_obj) 295254721Semaste{ 296254721Semaste} 297254721Semaste 298254721Semaste 299254721SemastePythonDictionary::PythonDictionary (const PythonObject &object) : 300254721Semaste PythonObject(object.GetPythonObject()) 301254721Semaste{ 302254721Semaste} 303254721Semaste 304254721SemastePythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 305254721Semaste PythonObject (script_object_sp) 306254721Semaste{ 307254721Semaste} 308254721Semaste 309254721SemastePythonDictionary::~PythonDictionary () 310254721Semaste{ 311254721Semaste} 312254721Semaste 313254721Semastebool 314254721SemastePythonDictionary::Reset (PyObject *py_obj) 315254721Semaste{ 316254721Semaste if (py_obj && PyDict_Check(py_obj)) 317254721Semaste return PythonObject::Reset(py_obj); 318254721Semaste 319254721Semaste PythonObject::Reset(NULL); 320254721Semaste return py_obj == NULL; 321254721Semaste} 322254721Semaste 323254721Semasteuint32_t 324254721SemastePythonDictionary::GetSize() 325254721Semaste{ 326254721Semaste if (m_py_obj) 327254721Semaste return PyDict_Size(m_py_obj); 328254721Semaste return 0; 329254721Semaste} 330254721Semaste 331254721SemastePythonObject 332254721SemastePythonDictionary::GetItemForKey (const char *key) const 333254721Semaste{ 334254721Semaste if (key && key[0]) 335254721Semaste { 336254721Semaste PythonString python_key(key); 337254721Semaste return GetItemForKey(python_key); 338254721Semaste } 339254721Semaste return NULL; 340254721Semaste} 341254721Semaste 342254721Semaste 343254721SemastePythonObject 344254721SemastePythonDictionary::GetItemForKey (const PythonString &key) const 345254721Semaste{ 346254721Semaste if (m_py_obj && key) 347254721Semaste return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject())); 348254721Semaste return PythonObject(); 349254721Semaste} 350254721Semaste 351254721Semaste 352254721Semasteconst char * 353254721SemastePythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const 354254721Semaste{ 355254721Semaste if (m_py_obj && key) 356254721Semaste { 357254721Semaste PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject()); 358254721Semaste if (py_obj && PyString_Check(py_obj)) 359254721Semaste return PyString_AsString(py_obj); 360254721Semaste } 361254721Semaste return fail_value; 362254721Semaste} 363254721Semaste 364254721Semasteint64_t 365254721SemastePythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const 366254721Semaste{ 367254721Semaste if (m_py_obj && key) 368254721Semaste { 369254721Semaste PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject()); 370254721Semaste if (py_obj) 371254721Semaste { 372254721Semaste if (PyInt_Check(py_obj)) 373254721Semaste return PyInt_AsLong(py_obj); 374254721Semaste 375254721Semaste if (PyLong_Check(py_obj)) 376254721Semaste return PyLong_AsLong(py_obj); 377254721Semaste } 378254721Semaste } 379254721Semaste return fail_value; 380254721Semaste} 381254721Semaste 382254721SemastePythonList 383254721SemastePythonDictionary::GetKeys () const 384254721Semaste{ 385254721Semaste if (m_py_obj) 386254721Semaste return PythonList(PyDict_Keys(m_py_obj)); 387254721Semaste return PythonList(); 388254721Semaste} 389254721Semaste 390254721SemastePythonString 391254721SemastePythonDictionary::GetKeyAtPosition (uint32_t pos) const 392254721Semaste{ 393254721Semaste PyObject *key, *value; 394254721Semaste Py_ssize_t pos_iter = 0; 395254721Semaste 396254721Semaste if (m_py_obj) 397254721Semaste { 398254721Semaste while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) 399254721Semaste { 400254721Semaste if (pos-- == 0) 401254721Semaste return PythonString(key); 402254721Semaste } 403254721Semaste } 404254721Semaste return PythonString(); 405254721Semaste} 406254721Semaste 407254721SemastePythonObject 408254721SemastePythonDictionary::GetValueAtPosition (uint32_t pos) const 409254721Semaste{ 410254721Semaste PyObject *key, *value; 411254721Semaste Py_ssize_t pos_iter = 0; 412254721Semaste 413254721Semaste if (!m_py_obj) 414254721Semaste return NULL; 415254721Semaste 416254721Semaste while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) { 417254721Semaste if (pos-- == 0) 418254721Semaste return PythonObject(value); 419254721Semaste } 420254721Semaste return PythonObject(); 421254721Semaste} 422254721Semaste 423254721Semastevoid 424254721SemastePythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value) 425254721Semaste{ 426254721Semaste if (m_py_obj && key && value) 427254721Semaste PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject()); 428254721Semaste} 429254721Semaste 430254721Semaste#endif 431