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 #ifdef LLDB_DISABLE_PYTHON 11 12 // Python is disabled in this build 13 14 #else 15 16 #include "lldb-python.h" 17 #include "PythonDataObjects.h" 18 #include "ScriptInterpreterPython.h" 19 20 #include "lldb/Core/Stream.h" 21 #include "lldb/Host/File.h" 22 #include "lldb/Interpreter/ScriptInterpreter.h" 23 24 #include <stdio.h> 25 26 using namespace lldb_private; 27 using namespace lldb; 28 29 void 30 StructuredPythonObject::Dump(Stream &s) const 31 { 32 s << "Python Obj: 0x" << GetValue(); 33 } 34 35 //---------------------------------------------------------------------- 36 // PythonObject 37 //---------------------------------------------------------------------- 38 39 void 40 PythonObject::Dump (Stream &strm) const 41 { 42 if (m_py_obj) 43 { 44 FILE *file = ::tmpfile(); 45 if (file) 46 { 47 ::PyObject_Print (m_py_obj, file, 0); 48 const long length = ftell (file); 49 if (length) 50 { 51 ::rewind(file); 52 std::vector<char> file_contents (length,'\0'); 53 const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file); 54 if (length_read > 0) 55 strm.Write (file_contents.data(), length_read); 56 } 57 ::fclose (file); 58 } 59 } 60 else 61 strm.PutCString ("NULL"); 62 } 63 64 PyObjectType 65 PythonObject::GetObjectType() const 66 { 67 if (IsNULLOrNone()) 68 return PyObjectType::None; 69 70 if (PyList_Check(m_py_obj)) 71 return PyObjectType::List; 72 if (PyDict_Check(m_py_obj)) 73 return PyObjectType::Dictionary; 74 if (PyUnicode_Check(m_py_obj)) 75 return PyObjectType::String; 76 if (PyLong_Check(m_py_obj)) 77 return PyObjectType::Integer; 78 #if PY_MAJOR_VERSION < 3 79 // These functions don't exist in Python 3.x. PyString is PyUnicode 80 // and PyInt is PyLong. 81 if (PyString_Check(m_py_obj)) 82 return PyObjectType::String; 83 if (PyInt_Check(m_py_obj)) 84 return PyObjectType::Integer; 85 #endif 86 return PyObjectType::Unknown; 87 } 88 89 PythonString 90 PythonObject::Repr () 91 { 92 if (!m_py_obj) 93 return PythonString (); 94 PyObject *repr = PyObject_Repr(m_py_obj); 95 if (!repr) 96 return PythonString (); 97 return PythonString(repr); 98 } 99 100 PythonString 101 PythonObject::Str () 102 { 103 if (!m_py_obj) 104 return PythonString (); 105 PyObject *str = PyObject_Str(m_py_obj); 106 if (!str) 107 return PythonString (); 108 return PythonString(str); 109 } 110 111 bool 112 PythonObject::IsNULLOrNone () const 113 { 114 return ((m_py_obj == nullptr) || (m_py_obj == Py_None)); 115 } 116 117 StructuredData::ObjectSP 118 PythonObject::CreateStructuredObject() const 119 { 120 switch (GetObjectType()) 121 { 122 case PyObjectType::Dictionary: 123 return PythonDictionary(m_py_obj).CreateStructuredDictionary(); 124 case PyObjectType::Integer: 125 return PythonInteger(m_py_obj).CreateStructuredInteger(); 126 case PyObjectType::List: 127 return PythonList(m_py_obj).CreateStructuredArray(); 128 case PyObjectType::String: 129 return PythonString(m_py_obj).CreateStructuredString(); 130 case PyObjectType::None: 131 return StructuredData::ObjectSP(); 132 default: 133 return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj)); 134 } 135 } 136 137 //---------------------------------------------------------------------- 138 // PythonString 139 //---------------------------------------------------------------------- 140 141 PythonString::PythonString (PyObject *py_obj) : 142 PythonObject() 143 { 144 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string 145 } 146 147 PythonString::PythonString (const PythonObject &object) : 148 PythonObject() 149 { 150 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string 151 } 152 153 PythonString::PythonString(llvm::StringRef string) 154 : PythonObject() 155 { 156 SetString(string); 157 } 158 159 PythonString::PythonString(const char *string) 160 : PythonObject() 161 { 162 SetString(llvm::StringRef(string)); 163 } 164 165 PythonString::PythonString () : 166 PythonObject() 167 { 168 } 169 170 PythonString::~PythonString () 171 { 172 } 173 174 bool 175 PythonString::Check(PyObject *py_obj) 176 { 177 if (!py_obj) 178 return false; 179 #if PY_MAJOR_VERSION >= 3 180 // Python 3 does not have PyString objects, only PyUnicode. 181 return PyUnicode_Check(py_obj); 182 #else 183 return PyUnicode_Check(py_obj) || PyString_Check(py_obj); 184 #endif 185 } 186 187 bool 188 PythonString::Reset(PyObject *py_obj) 189 { 190 if (!PythonString::Check(py_obj)) 191 { 192 PythonObject::Reset(nullptr); 193 return false; 194 } 195 196 // Convert this to a PyBytes object, and only store the PyBytes. Note that in 197 // Python 2.x, PyString and PyUnicode are interchangeable, and PyBytes is an alias 198 // of PyString. So on 2.x, if we get into this branch, we already have a PyBytes. 199 //#if PY_MAJOR_VERSION >= 3 200 if (PyUnicode_Check(py_obj)) 201 { 202 PyObject *unicode = py_obj; 203 py_obj = PyUnicode_AsUTF8String(py_obj); 204 Py_XDECREF(unicode); 205 } 206 //#endif 207 208 assert(PyBytes_Check(py_obj) && "PythonString::Reset received a non-string"); 209 return PythonObject::Reset(py_obj); 210 } 211 212 llvm::StringRef 213 PythonString::GetString() const 214 { 215 if (m_py_obj) 216 { 217 Py_ssize_t size; 218 char *c; 219 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 220 return llvm::StringRef(c, size); 221 } 222 return llvm::StringRef(); 223 } 224 225 size_t 226 PythonString::GetSize() const 227 { 228 if (m_py_obj) 229 return PyBytes_Size(m_py_obj); 230 return 0; 231 } 232 233 void 234 PythonString::SetString (llvm::StringRef string) 235 { 236 #if PY_MAJOR_VERSION >= 3 237 PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size()); 238 PyObject *bytes = PyUnicode_AsUTF8String(unicode); 239 PythonObject::Reset(bytes); 240 Py_XDECREF(unicode); 241 #else 242 PythonObject::Reset(PyString_FromStringAndSize(string.data(), string.size())); 243 #endif 244 } 245 246 StructuredData::StringSP 247 PythonString::CreateStructuredString() const 248 { 249 StructuredData::StringSP result(new StructuredData::String); 250 result->SetValue(GetString()); 251 return result; 252 } 253 254 //---------------------------------------------------------------------- 255 // PythonInteger 256 //---------------------------------------------------------------------- 257 258 PythonInteger::PythonInteger (PyObject *py_obj) : 259 PythonObject() 260 { 261 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type 262 } 263 264 PythonInteger::PythonInteger (const PythonObject &object) : 265 PythonObject() 266 { 267 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a integer type 268 } 269 270 PythonInteger::PythonInteger (int64_t value) : 271 PythonObject() 272 { 273 SetInteger (value); 274 } 275 276 277 PythonInteger::~PythonInteger () 278 { 279 } 280 281 bool 282 PythonInteger::Check(PyObject *py_obj) 283 { 284 if (!py_obj) 285 return false; 286 287 #if PY_MAJOR_VERSION >= 3 288 // Python 3 does not have PyInt_Check. There is only one type of 289 // integral value, long. 290 return PyLong_Check(py_obj); 291 #else 292 return PyLong_Check(py_obj) || PyInt_Check(py_obj); 293 #endif 294 } 295 296 bool 297 PythonInteger::Reset(PyObject *py_obj) 298 { 299 if (!PythonInteger::Check(py_obj)) 300 { 301 PythonObject::Reset(nullptr); 302 return false; 303 } 304 305 #if PY_MAJOR_VERSION < 3 306 // Always store this as a PyLong, which makes interoperability between 307 // Python 2.x and Python 3.x easier. This is only necessary in 2.x, 308 // since 3.x doesn't even have a PyInt. 309 if (PyInt_Check(py_obj)) 310 { 311 PyObject *py_long = PyLong_FromLongLong(PyInt_AsLong(py_obj)); 312 Py_XDECREF(py_obj); 313 py_obj = py_long; 314 } 315 #endif 316 317 assert(PyLong_Check(py_obj) && "Couldn't get a PyLong from this PyObject"); 318 319 return PythonObject::Reset(py_obj); 320 } 321 322 int64_t 323 PythonInteger::GetInteger() const 324 { 325 if (m_py_obj) 326 { 327 assert(PyLong_Check(m_py_obj) && "PythonInteger::GetInteger has a PyObject that isn't a PyLong"); 328 329 return PyLong_AsLongLong(m_py_obj); 330 } 331 return UINT64_MAX; 332 } 333 334 void 335 PythonInteger::SetInteger (int64_t value) 336 { 337 PythonObject::Reset(PyLong_FromLongLong(value)); 338 } 339 340 StructuredData::IntegerSP 341 PythonInteger::CreateStructuredInteger() const 342 { 343 StructuredData::IntegerSP result(new StructuredData::Integer); 344 result->SetValue(GetInteger()); 345 return result; 346 } 347 348 //---------------------------------------------------------------------- 349 // PythonList 350 //---------------------------------------------------------------------- 351 352 PythonList::PythonList() 353 : PythonObject(PyList_New(0)) 354 { 355 } 356 357 PythonList::PythonList (PyObject *py_obj) : 358 PythonObject() 359 { 360 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list 361 } 362 363 364 PythonList::PythonList (const PythonObject &object) : 365 PythonObject() 366 { 367 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a list 368 } 369 370 PythonList::~PythonList () 371 { 372 } 373 374 bool 375 PythonList::Check(PyObject *py_obj) 376 { 377 if (!py_obj) 378 return false; 379 return PyList_Check(py_obj); 380 } 381 382 bool 383 PythonList::Reset(PyObject *py_obj) 384 { 385 if (!PythonList::Check(py_obj)) 386 { 387 PythonObject::Reset(nullptr); 388 return false; 389 } 390 391 return PythonObject::Reset(py_obj); 392 } 393 394 uint32_t 395 PythonList::GetSize() const 396 { 397 if (m_py_obj) 398 return PyList_GET_SIZE(m_py_obj); 399 return 0; 400 } 401 402 PythonObject 403 PythonList::GetItemAtIndex(uint32_t index) const 404 { 405 if (m_py_obj) 406 return PythonObject(PyList_GetItem(m_py_obj, index)); 407 return PythonObject(); 408 } 409 410 void 411 PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object) 412 { 413 if (m_py_obj && object) 414 PyList_SetItem(m_py_obj, index, object.get()); 415 } 416 417 void 418 PythonList::AppendItem (const PythonObject &object) 419 { 420 if (m_py_obj && object) 421 PyList_Append(m_py_obj, object.get()); 422 } 423 424 StructuredData::ArraySP 425 PythonList::CreateStructuredArray() const 426 { 427 StructuredData::ArraySP result(new StructuredData::Array); 428 uint32_t count = GetSize(); 429 for (uint32_t i = 0; i < count; ++i) 430 { 431 PythonObject obj = GetItemAtIndex(i); 432 result->AddItem(obj.CreateStructuredObject()); 433 } 434 return result; 435 } 436 437 //---------------------------------------------------------------------- 438 // PythonDictionary 439 //---------------------------------------------------------------------- 440 441 PythonDictionary::PythonDictionary() 442 : PythonObject(PyDict_New()) 443 { 444 } 445 446 PythonDictionary::PythonDictionary (PyObject *py_obj) : 447 PythonObject(py_obj) 448 { 449 Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary 450 } 451 452 453 PythonDictionary::PythonDictionary (const PythonObject &object) : 454 PythonObject() 455 { 456 Reset(object.get()); // Use "Reset()" to ensure that py_obj is a dictionary 457 } 458 459 PythonDictionary::~PythonDictionary () 460 { 461 } 462 463 bool 464 PythonDictionary::Check(PyObject *py_obj) 465 { 466 if (!py_obj) 467 return false; 468 469 return PyDict_Check(py_obj); 470 } 471 472 bool 473 PythonDictionary::Reset(PyObject *py_obj) 474 { 475 if (!PythonDictionary::Check(py_obj)) 476 { 477 PythonObject::Reset(nullptr); 478 return false; 479 } 480 481 return PythonObject::Reset(py_obj); 482 } 483 484 uint32_t 485 PythonDictionary::GetSize() const 486 { 487 if (m_py_obj) 488 return PyDict_Size(m_py_obj); 489 return 0; 490 } 491 492 PythonObject 493 PythonDictionary::GetItemForKey (const char *key) const 494 { 495 if (key && key[0]) 496 { 497 PythonString python_key(key); 498 return GetItemForKey(python_key); 499 } 500 return PythonObject(); 501 } 502 503 504 PythonObject 505 PythonDictionary::GetItemForKey (const PythonString &key) const 506 { 507 if (m_py_obj && key) 508 return PythonObject(PyDict_GetItem(m_py_obj, key.get())); 509 return PythonObject(); 510 } 511 512 513 const char * 514 PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const 515 { 516 if (m_py_obj && key) 517 { 518 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get()); 519 if (py_obj && PythonString::Check(py_obj)) 520 { 521 PythonString str(py_obj); 522 return str.GetString().data(); 523 } 524 } 525 return fail_value; 526 } 527 528 int64_t 529 PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const 530 { 531 if (m_py_obj && key) 532 { 533 PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get()); 534 if (PythonInteger::Check(py_obj)) 535 { 536 PythonInteger int_obj(py_obj); 537 return int_obj.GetInteger(); 538 } 539 } 540 return fail_value; 541 } 542 543 PythonList 544 PythonDictionary::GetKeys () const 545 { 546 if (m_py_obj) 547 return PythonList(PyDict_Keys(m_py_obj)); 548 return PythonList(); 549 } 550 551 PythonString 552 PythonDictionary::GetKeyAtPosition (uint32_t pos) const 553 { 554 PyObject *key, *value; 555 Py_ssize_t pos_iter = 0; 556 557 if (m_py_obj) 558 { 559 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) 560 { 561 if (pos-- == 0) 562 return PythonString(key); 563 } 564 } 565 return PythonString(); 566 } 567 568 PythonObject 569 PythonDictionary::GetValueAtPosition (uint32_t pos) const 570 { 571 PyObject *key, *value; 572 Py_ssize_t pos_iter = 0; 573 574 if (!m_py_obj) 575 return PythonObject(); 576 577 while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) { 578 if (pos-- == 0) 579 return PythonObject(value); 580 } 581 return PythonObject(); 582 } 583 584 void 585 PythonDictionary::SetItemForKey (const PythonString &key, PyObject *value) 586 { 587 if (m_py_obj && key && value) 588 PyDict_SetItem(m_py_obj, key.get(), value); 589 } 590 591 void 592 PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value) 593 { 594 if (m_py_obj && key && value) 595 PyDict_SetItem(m_py_obj, key.get(), value.get()); 596 } 597 598 StructuredData::DictionarySP 599 PythonDictionary::CreateStructuredDictionary() const 600 { 601 StructuredData::DictionarySP result(new StructuredData::Dictionary); 602 PythonList keys(GetKeys()); 603 uint32_t num_keys = keys.GetSize(); 604 for (uint32_t i = 0; i < num_keys; ++i) 605 { 606 PythonObject key = keys.GetItemAtIndex(i); 607 PythonString key_str = key.Str(); 608 PythonObject value = GetItemForKey(key); 609 StructuredData::ObjectSP structured_value = value.CreateStructuredObject(); 610 result->AddItem(key_str.GetString(), structured_value); 611 } 612 return result; 613 } 614 615 #endif 616