PythonDataObjects.cpp revision 269024
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 87bool 88PythonObject::IsNULLOrNone () const 89{ 90 return ((m_py_obj == nullptr) || (m_py_obj == Py_None)); 91} 92 93//---------------------------------------------------------------------- 94// PythonString 95//---------------------------------------------------------------------- 96 97PythonString::PythonString (PyObject *py_obj) : 98 PythonObject() 99{ 100 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string 101} 102 103PythonString::PythonString (const PythonObject &object) : 104 PythonObject() 105{ 106 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string 107} 108 109PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 110 PythonObject() 111{ 112 if (script_object_sp) 113 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string 114} 115 116PythonString::PythonString (const char* string) : 117 PythonObject(PyString_FromString(string)) 118{ 119} 120 121PythonString::PythonString () : 122 PythonObject() 123{ 124} 125 126PythonString::~PythonString () 127{ 128} 129 130bool 131PythonString::Reset (PyObject *py_obj) 132{ 133 if (py_obj && PyString_Check(py_obj)) 134 return PythonObject::Reset(py_obj); 135 136 PythonObject::Reset(NULL); 137 return py_obj == NULL; 138} 139 140const char* 141PythonString::GetString() const 142{ 143 if (m_py_obj) 144 return PyString_AsString(m_py_obj); 145 return NULL; 146} 147 148size_t 149PythonString::GetSize() const 150{ 151 if (m_py_obj) 152 return PyString_Size(m_py_obj); 153 return 0; 154} 155 156void 157PythonString::SetString (const char* string) 158{ 159 PythonObject::Reset(PyString_FromString(string)); 160} 161 162//---------------------------------------------------------------------- 163// PythonInteger 164//---------------------------------------------------------------------- 165 166PythonInteger::PythonInteger (PyObject *py_obj) : 167 PythonObject() 168{ 169 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type 170} 171 172PythonInteger::PythonInteger (const PythonObject &object) : 173 PythonObject() 174{ 175 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a integer type 176} 177 178PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 179 PythonObject() 180{ 181 if (script_object_sp) 182 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string 183} 184 185PythonInteger::PythonInteger (int64_t value) : 186 PythonObject() 187{ 188 SetInteger (value); 189} 190 191 192PythonInteger::~PythonInteger () 193{ 194} 195 196bool 197PythonInteger::Reset (PyObject *py_obj) 198{ 199 if (py_obj) 200 { 201 if (PyInt_Check (py_obj) || PyLong_Check(py_obj)) 202 return PythonObject::Reset(py_obj); 203 } 204 205 PythonObject::Reset(NULL); 206 return py_obj == NULL; 207} 208 209int64_t 210PythonInteger::GetInteger() 211{ 212 if (m_py_obj) 213 { 214 if (PyInt_Check(m_py_obj)) 215 return PyInt_AsLong(m_py_obj); 216 else if (PyLong_Check(m_py_obj)) 217 return PyLong_AsLongLong(m_py_obj); 218 } 219 return UINT64_MAX; 220} 221 222void 223PythonInteger::SetInteger (int64_t value) 224{ 225 PythonObject::Reset(PyLong_FromLongLong(value)); 226} 227 228//---------------------------------------------------------------------- 229// PythonList 230//---------------------------------------------------------------------- 231 232PythonList::PythonList (bool create_empty) : 233 PythonObject(create_empty ? PyList_New(0) : NULL) 234{ 235} 236 237PythonList::PythonList (uint32_t count) : 238 PythonObject(PyList_New(count)) 239{ 240} 241 242PythonList::PythonList (PyObject *py_obj) : 243 PythonObject() 244{ 245 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list 246} 247 248 249PythonList::PythonList (const PythonObject &object) : 250 PythonObject() 251{ 252 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a list 253} 254 255PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 256 PythonObject() 257{ 258 if (script_object_sp) 259 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list 260} 261 262PythonList::~PythonList () 263{ 264} 265 266bool 267PythonList::Reset (PyObject *py_obj) 268{ 269 if (py_obj && PyList_Check(py_obj)) 270 return PythonObject::Reset(py_obj); 271 272 PythonObject::Reset(NULL); 273 return py_obj == NULL; 274} 275 276uint32_t 277PythonList::GetSize() 278{ 279 if (m_py_obj) 280 return PyList_GET_SIZE(m_py_obj); 281 return 0; 282} 283 284PythonObject 285PythonList::GetItemAtIndex (uint32_t index) 286{ 287 if (m_py_obj) 288 return PythonObject(PyList_GetItem(m_py_obj, index)); 289 return PythonObject(); 290} 291 292void 293PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object) 294{ 295 if (m_py_obj && object) 296 PyList_SetItem(m_py_obj, index, object.get()); 297} 298 299void 300PythonList::AppendItem (const PythonObject &object) 301{ 302 if (m_py_obj && object) 303 PyList_Append(m_py_obj, object.get()); 304} 305 306//---------------------------------------------------------------------- 307// PythonDictionary 308//---------------------------------------------------------------------- 309 310PythonDictionary::PythonDictionary (bool create_empty) : 311PythonObject(create_empty ? PyDict_New() : NULL) 312{ 313} 314 315PythonDictionary::PythonDictionary (PyObject *py_obj) : 316 PythonObject(py_obj) 317{ 318 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary 319} 320 321 322PythonDictionary::PythonDictionary (const PythonObject &object) : 323 PythonObject() 324{ 325 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a dictionary 326} 327 328PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) : 329 PythonObject () 330{ 331 if (script_object_sp) 332 Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary 333} 334 335PythonDictionary::~PythonDictionary () 336{ 337} 338 339bool 340PythonDictionary::Reset (PyObject *py_obj) 341{ 342 if (py_obj && PyDict_Check(py_obj)) 343 return PythonObject::Reset(py_obj); 344 345 PythonObject::Reset(NULL); 346 return py_obj == NULL; 347} 348 349uint32_t 350PythonDictionary::GetSize() 351{ 352 if (m_py_obj) 353 return PyDict_Size(m_py_obj); 354 return 0; 355} 356 357PythonObject 358PythonDictionary::GetItemForKey (const char *key) const 359{ 360 if (key && key[0]) 361 { 362 PythonString python_key(key); 363 return GetItemForKey(python_key); 364 } 365 return PythonObject(); 366} 367 368 369PythonObject 370PythonDictionary::GetItemForKey (const PythonString &key) const 371{ 372 if (m_py_obj && key) 373 return PythonObject(PyDict_GetItem(m_py_obj, key.get())); 374 return PythonObject(); 375} 376 377 378const char * 379PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const 380{ 381 if (m_py_obj && key) 382 { 383 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get()); 384 if (py_obj && PyString_Check(py_obj)) 385 return PyString_AsString(py_obj); 386 } 387 return fail_value; 388} 389 390int64_t 391PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const 392{ 393 if (m_py_obj && key) 394 { 395 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get()); 396 if (py_obj) 397 { 398 if (PyInt_Check(py_obj)) 399 return PyInt_AsLong(py_obj); 400 401 if (PyLong_Check(py_obj)) 402 return PyLong_AsLong(py_obj); 403 } 404 } 405 return fail_value; 406} 407 408PythonList 409PythonDictionary::GetKeys () const 410{ 411 if (m_py_obj) 412 return PythonList(PyDict_Keys(m_py_obj)); 413 return PythonList(true); 414} 415 416PythonString 417PythonDictionary::GetKeyAtPosition (uint32_t pos) const 418{ 419 PyObject *key, *value; 420 Py_ssize_t pos_iter = 0; 421 422 if (m_py_obj) 423 { 424 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) 425 { 426 if (pos-- == 0) 427 return PythonString(key); 428 } 429 } 430 return PythonString(); 431} 432 433PythonObject 434PythonDictionary::GetValueAtPosition (uint32_t pos) const 435{ 436 PyObject *key, *value; 437 Py_ssize_t pos_iter = 0; 438 439 if (!m_py_obj) 440 return PythonObject(); 441 442 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) { 443 if (pos-- == 0) 444 return PythonObject(value); 445 } 446 return PythonObject(); 447} 448 449void 450PythonDictionary::SetItemForKey (const PythonString &key, PyObject *value) 451{ 452 if (m_py_obj && key && value) 453 PyDict_SetItem(m_py_obj, key.get(), value); 454} 455 456void 457PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value) 458{ 459 if (m_py_obj && key && value) 460 PyDict_SetItem(m_py_obj, key.get(), value.get()); 461} 462 463#endif 464