PythonDataObjects.cpp revision e15e58facd4814a2be1cc1aa385e9f9125b92993
1//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 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 13 14// Python is disabled in this build 15 16#else 17 18#if defined (__APPLE__) 19#include <Python/Python.h> 20#else 21#include <Python.h> 22#endif 23#include "lldb/Core/Stream.h" 24#include "lldb/Host/File.h" 25#include "lldb/Interpreter/PythonDataObjects.h" 26#include "lldb/Interpreter/ScriptInterpreter.h" 27 28using namespace lldb_private; 29using namespace lldb; 30 31//---------------------------------------------------------------------- 32// PythonObject 33//---------------------------------------------------------------------- 34PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 35 m_py_obj (NULL) 36{ 37 if (script_object_sp) 38 Reset ((PyObject *)script_object_sp->GetObject()); 39} 40 41void 42PythonObject::Dump (Stream &strm) const 43{ 44 if (m_py_obj) 45 { 46 FILE *file = tmpfile(); 47 if (file) 48 { 49 PyObject_Print (m_py_obj, file, 0); 50 const long length = ftell (file); 51 if (length) 52 { 53 rewind(file); 54 std::vector<char> file_contents (length,'\0'); 55 const size_t length_read = fread(file_contents.data(), 1, file_contents.size(), file); 56 if (length_read > 0) 57 strm.Write(file_contents.data(), length_read); 58 } 59 } 60 } 61 else 62 strm.PutCString ("NULL"); 63} 64 65//---------------------------------------------------------------------- 66// PythonString 67//---------------------------------------------------------------------- 68 69PythonString::PythonString (PyObject *py_obj) : 70 PythonObject(py_obj) 71{ 72} 73 74PythonString::PythonString (const PythonObject &object) : 75 PythonObject(object.GetPythonObject()) 76{ 77} 78 79PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 80 PythonObject (script_object_sp) 81{ 82} 83 84PythonString::PythonString (const char* string) : 85 PythonObject(PyString_FromString(string)) 86{ 87} 88 89PythonString::PythonString () : 90 PythonObject() 91{ 92} 93 94PythonString::~PythonString () 95{ 96} 97 98bool 99PythonString::Reset (PyObject *py_obj) 100{ 101 if (py_obj && PyString_Check(py_obj)) 102 return PythonObject::Reset(py_obj); 103 104 PythonObject::Reset(NULL); 105 return py_obj == NULL; 106} 107 108const char* 109PythonString::GetString() const 110{ 111 if (m_py_obj) 112 return PyString_AsString(m_py_obj); 113 return NULL; 114} 115 116size_t 117PythonString::GetSize() const 118{ 119 if (m_py_obj) 120 return PyString_Size(m_py_obj); 121 return 0; 122} 123 124void 125PythonString::SetString (const char* string) 126{ 127 PythonObject::Reset(PyString_FromString(string)); 128} 129 130//---------------------------------------------------------------------- 131// PythonInteger 132//---------------------------------------------------------------------- 133 134PythonInteger::PythonInteger (PyObject *py_obj) : 135 PythonObject(py_obj) 136{ 137} 138 139PythonInteger::PythonInteger (const PythonObject &object) : 140 PythonObject(object.GetPythonObject()) 141{ 142} 143 144PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 145 PythonObject (script_object_sp) 146{ 147} 148 149PythonInteger::PythonInteger (int64_t value) : 150 PythonObject(PyInt_FromLong(value)) 151{ 152} 153 154 155PythonInteger::~PythonInteger () 156{ 157} 158 159bool 160PythonInteger::Reset (PyObject *py_obj) 161{ 162 if (py_obj && PyInt_Check(py_obj)) 163 return PythonObject::Reset(py_obj); 164 165 PythonObject::Reset(NULL); 166 return py_obj == NULL; 167} 168 169int64_t 170PythonInteger::GetInteger() 171{ 172 if (m_py_obj) 173 return PyInt_AsLong(m_py_obj); 174 else 175 return UINT64_MAX; 176} 177 178void 179PythonInteger::SetInteger (int64_t value) 180{ 181 PythonObject::Reset(PyInt_FromLong(value)); 182} 183 184//---------------------------------------------------------------------- 185// PythonList 186//---------------------------------------------------------------------- 187 188PythonList::PythonList () : 189 PythonObject(PyList_New(0)) 190{ 191} 192 193PythonList::PythonList (uint32_t count) : 194 PythonObject(PyList_New(count)) 195{ 196} 197 198PythonList::PythonList (PyObject *py_obj) : 199 PythonObject(py_obj) 200{ 201} 202 203 204PythonList::PythonList (const PythonObject &object) : 205 PythonObject(object.GetPythonObject()) 206{ 207} 208 209PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 210 PythonObject (script_object_sp) 211{ 212} 213 214PythonList::~PythonList () 215{ 216} 217 218bool 219PythonList::Reset (PyObject *py_obj) 220{ 221 if (py_obj && PyList_Check(py_obj)) 222 return PythonObject::Reset(py_obj); 223 224 PythonObject::Reset(NULL); 225 return py_obj == NULL; 226} 227 228uint32_t 229PythonList::GetSize() 230{ 231 if (m_py_obj) 232 return PyList_GET_SIZE(m_py_obj); 233 return 0; 234} 235 236PythonObject 237PythonList::GetItemAtIndex (uint32_t index) 238{ 239 if (m_py_obj) 240 return PythonObject(PyList_GetItem(m_py_obj, index)); 241 return NULL; 242} 243 244void 245PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object) 246{ 247 if (m_py_obj && object) 248 PyList_SetItem(m_py_obj, index, object.GetPythonObject()); 249} 250 251void 252PythonList::AppendItem (const PythonObject &object) 253{ 254 if (m_py_obj && object) 255 PyList_Append(m_py_obj, object.GetPythonObject()); 256} 257 258//---------------------------------------------------------------------- 259// PythonDictionary 260//---------------------------------------------------------------------- 261 262PythonDictionary::PythonDictionary () : 263 PythonObject(PyDict_New()) 264{ 265} 266 267PythonDictionary::PythonDictionary (PyObject *py_obj) : 268 PythonObject(py_obj) 269{ 270} 271 272 273PythonDictionary::PythonDictionary (const PythonObject &object) : 274 PythonObject(object.GetPythonObject()) 275{ 276} 277 278PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 279 PythonObject (script_object_sp) 280{ 281} 282 283PythonDictionary::~PythonDictionary () 284{ 285} 286 287bool 288PythonDictionary::Reset (PyObject *py_obj) 289{ 290 if (py_obj && PyDict_Check(py_obj)) 291 return PythonObject::Reset(py_obj); 292 293 PythonObject::Reset(NULL); 294 return py_obj == NULL; 295} 296 297uint32_t 298PythonDictionary::GetSize() 299{ 300 if (m_py_obj) 301 return PyDict_Size(m_py_obj); 302 return 0; 303} 304 305PythonObject 306PythonDictionary::GetItemForKey (const char *key) const 307{ 308 if (key && key[0]) 309 { 310 PythonString python_key(key); 311 return GetItemForKey(python_key); 312 } 313 return NULL; 314} 315 316 317PythonObject 318PythonDictionary::GetItemForKey (const PythonString &key) const 319{ 320 if (m_py_obj && key) 321 return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject())); 322 return PythonObject(); 323} 324 325 326const char * 327PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const 328{ 329 if (m_py_obj && key) 330 { 331 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject()); 332 if (py_obj && PyString_Check(py_obj)) 333 return PyString_AsString(py_obj); 334 } 335 return fail_value; 336} 337 338int64_t 339PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const 340{ 341 if (m_py_obj && key) 342 { 343 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject()); 344 if (py_obj) 345 { 346 if (PyInt_Check(py_obj)) 347 return PyInt_AsLong(py_obj); 348 349 if (PyLong_Check(py_obj)) 350 return PyLong_AsLong(py_obj); 351 } 352 } 353 return fail_value; 354} 355 356PythonList 357PythonDictionary::GetKeys () const 358{ 359 if (m_py_obj) 360 return PythonList(PyDict_Keys(m_py_obj)); 361 return PythonList(); 362} 363 364PythonString 365PythonDictionary::GetKeyAtPosition (uint32_t pos) const 366{ 367 PyObject *key, *value; 368 Py_ssize_t pos_iter = 0; 369 370 if (m_py_obj) 371 { 372 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) 373 { 374 if (pos-- == 0) 375 return PythonString(key); 376 } 377 } 378 return PythonString(); 379} 380 381PythonObject 382PythonDictionary::GetValueAtPosition (uint32_t pos) const 383{ 384 PyObject *key, *value; 385 Py_ssize_t pos_iter = 0; 386 387 if (!m_py_obj) 388 return NULL; 389 390 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) { 391 if (pos-- == 0) 392 return PythonObject(value); 393 } 394 return PythonObject(); 395} 396 397void 398PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value) 399{ 400 if (m_py_obj && key && value) 401 PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject()); 402} 403 404#endif 405