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