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/Host/FileSystem.h" 23 #include "lldb/Interpreter/ScriptInterpreter.h" 24 25 #include "llvm/Support/ConvertUTF.h" 26 27 #include <stdio.h> 28 29 #include "llvm/ADT/StringSwitch.h" 30 31 using namespace lldb_private; 32 using namespace lldb; 33 34 void 35 StructuredPythonObject::Dump(Stream &s, bool pretty_print) const 36 { 37 s << "Python Obj: 0x" << GetValue(); 38 } 39 40 //---------------------------------------------------------------------- 41 // PythonObject 42 //---------------------------------------------------------------------- 43 44 void 45 PythonObject::Dump(Stream &strm) const 46 { 47 if (m_py_obj) 48 { 49 FILE *file = ::tmpfile(); 50 if (file) 51 { 52 ::PyObject_Print (m_py_obj, file, 0); 53 const long length = ftell (file); 54 if (length) 55 { 56 ::rewind(file); 57 std::vector<char> file_contents (length,'\0'); 58 const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file); 59 if (length_read > 0) 60 strm.Write (file_contents.data(), length_read); 61 } 62 ::fclose (file); 63 } 64 } 65 else 66 strm.PutCString ("NULL"); 67 } 68 69 PyObjectType 70 PythonObject::GetObjectType() const 71 { 72 if (!IsAllocated()) 73 return PyObjectType::None; 74 75 if (PythonModule::Check(m_py_obj)) 76 return PyObjectType::Module; 77 if (PythonList::Check(m_py_obj)) 78 return PyObjectType::List; 79 if (PythonTuple::Check(m_py_obj)) 80 return PyObjectType::Tuple; 81 if (PythonDictionary::Check(m_py_obj)) 82 return PyObjectType::Dictionary; 83 if (PythonString::Check(m_py_obj)) 84 return PyObjectType::String; 85 #if PY_MAJOR_VERSION >= 3 86 if (PythonBytes::Check(m_py_obj)) 87 return PyObjectType::Bytes; 88 #endif 89 if (PythonByteArray::Check(m_py_obj)) 90 return PyObjectType::ByteArray; 91 if (PythonInteger::Check(m_py_obj)) 92 return PyObjectType::Integer; 93 if (PythonFile::Check(m_py_obj)) 94 return PyObjectType::File; 95 if (PythonCallable::Check(m_py_obj)) 96 return PyObjectType::Callable; 97 return PyObjectType::Unknown; 98 } 99 100 PythonString 101 PythonObject::Repr() const 102 { 103 if (!m_py_obj) 104 return PythonString(); 105 PyObject *repr = PyObject_Repr(m_py_obj); 106 if (!repr) 107 return PythonString(); 108 return PythonString(PyRefType::Owned, repr); 109 } 110 111 PythonString 112 PythonObject::Str() const 113 { 114 if (!m_py_obj) 115 return PythonString(); 116 PyObject *str = PyObject_Str(m_py_obj); 117 if (!str) 118 return PythonString(); 119 return PythonString(PyRefType::Owned, str); 120 } 121 122 PythonObject 123 PythonObject::ResolveNameWithDictionary(llvm::StringRef name, const PythonDictionary &dict) 124 { 125 size_t dot_pos = name.find_first_of('.'); 126 llvm::StringRef piece = name.substr(0, dot_pos); 127 PythonObject result = dict.GetItemForKey(PythonString(piece)); 128 if (dot_pos == llvm::StringRef::npos) 129 { 130 // There was no dot, we're done. 131 return result; 132 } 133 134 // There was a dot. The remaining portion of the name should be looked up in 135 // the context of the object that was found in the dictionary. 136 return result.ResolveName(name.substr(dot_pos + 1)); 137 } 138 139 PythonObject 140 PythonObject::ResolveName(llvm::StringRef name) const 141 { 142 // Resolve the name in the context of the specified object. If, 143 // for example, `this` refers to a PyModule, then this will look for 144 // `name` in this module. If `this` refers to a PyType, then it will 145 // resolve `name` as an attribute of that type. If `this` refers to 146 // an instance of an object, then it will resolve `name` as the value 147 // of the specified field. 148 // 149 // This function handles dotted names so that, for example, if `m_py_obj` 150 // refers to the `sys` module, and `name` == "path.append", then it 151 // will find the function `sys.path.append`. 152 153 size_t dot_pos = name.find_first_of('.'); 154 if (dot_pos == llvm::StringRef::npos) 155 { 156 // No dots in the name, we should be able to find the value immediately 157 // as an attribute of `m_py_obj`. 158 return GetAttributeValue(name); 159 } 160 161 // Look up the first piece of the name, and resolve the rest as a child of that. 162 PythonObject parent = ResolveName(name.substr(0, dot_pos)); 163 if (!parent.IsAllocated()) 164 return PythonObject(); 165 166 // Tail recursion.. should be optimized by the compiler 167 return parent.ResolveName(name.substr(dot_pos + 1)); 168 } 169 170 bool 171 PythonObject::HasAttribute(llvm::StringRef attr) const 172 { 173 if (!IsValid()) 174 return false; 175 PythonString py_attr(attr); 176 return !!PyObject_HasAttr(m_py_obj, py_attr.get()); 177 } 178 179 PythonObject 180 PythonObject::GetAttributeValue(llvm::StringRef attr) const 181 { 182 if (!IsValid()) 183 return PythonObject(); 184 185 PythonString py_attr(attr); 186 if (!PyObject_HasAttr(m_py_obj, py_attr.get())) 187 return PythonObject(); 188 189 return PythonObject(PyRefType::Owned, 190 PyObject_GetAttr(m_py_obj, py_attr.get())); 191 } 192 193 bool 194 PythonObject::IsNone() const 195 { 196 return m_py_obj == Py_None; 197 } 198 199 bool 200 PythonObject::IsValid() const 201 { 202 return m_py_obj != nullptr; 203 } 204 205 bool 206 PythonObject::IsAllocated() const 207 { 208 return IsValid() && !IsNone(); 209 } 210 211 StructuredData::ObjectSP 212 PythonObject::CreateStructuredObject() const 213 { 214 switch (GetObjectType()) 215 { 216 case PyObjectType::Dictionary: 217 return PythonDictionary(PyRefType::Borrowed, m_py_obj).CreateStructuredDictionary(); 218 case PyObjectType::Integer: 219 return PythonInteger(PyRefType::Borrowed, m_py_obj).CreateStructuredInteger(); 220 case PyObjectType::List: 221 return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray(); 222 case PyObjectType::String: 223 return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 224 case PyObjectType::Bytes: 225 return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 226 case PyObjectType::ByteArray: 227 return PythonByteArray(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 228 case PyObjectType::None: 229 return StructuredData::ObjectSP(); 230 default: 231 return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj)); 232 } 233 } 234 235 //---------------------------------------------------------------------- 236 // PythonString 237 //---------------------------------------------------------------------- 238 PythonBytes::PythonBytes() : PythonObject() 239 { 240 } 241 242 PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) : PythonObject() 243 { 244 SetBytes(bytes); 245 } 246 247 PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) : PythonObject() 248 { 249 SetBytes(llvm::ArrayRef<uint8_t>(bytes, length)); 250 } 251 252 PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() 253 { 254 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string 255 } 256 257 PythonBytes::PythonBytes(const PythonBytes &object) : PythonObject(object) 258 { 259 } 260 261 PythonBytes::~PythonBytes() 262 { 263 } 264 265 bool 266 PythonBytes::Check(PyObject *py_obj) 267 { 268 if (!py_obj) 269 return false; 270 if (PyBytes_Check(py_obj)) 271 return true; 272 return false; 273 } 274 275 void 276 PythonBytes::Reset(PyRefType type, PyObject *py_obj) 277 { 278 // Grab the desired reference type so that if we end up rejecting 279 // `py_obj` it still gets decremented if necessary. 280 PythonObject result(type, py_obj); 281 282 if (!PythonBytes::Check(py_obj)) 283 { 284 PythonObject::Reset(); 285 return; 286 } 287 288 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 289 // back into the virtual implementation. 290 PythonObject::Reset(PyRefType::Borrowed, result.get()); 291 } 292 293 llvm::ArrayRef<uint8_t> 294 PythonBytes::GetBytes() const 295 { 296 if (!IsValid()) 297 return llvm::ArrayRef<uint8_t>(); 298 299 Py_ssize_t size; 300 char *c; 301 302 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 303 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 304 } 305 306 size_t 307 PythonBytes::GetSize() const 308 { 309 if (!IsValid()) 310 return 0; 311 return PyBytes_Size(m_py_obj); 312 } 313 314 void 315 PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) 316 { 317 const char *data = reinterpret_cast<const char *>(bytes.data()); 318 PyObject *py_bytes = PyBytes_FromStringAndSize(data, bytes.size()); 319 PythonObject::Reset(PyRefType::Owned, py_bytes); 320 } 321 322 StructuredData::StringSP 323 PythonBytes::CreateStructuredString() const 324 { 325 StructuredData::StringSP result(new StructuredData::String); 326 Py_ssize_t size; 327 char *c; 328 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 329 result->SetValue(std::string(c, size)); 330 return result; 331 } 332 333 PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes) : PythonByteArray(bytes.data(), bytes.size()) 334 { 335 } 336 337 PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) 338 { 339 const char *str = reinterpret_cast<const char *>(bytes); 340 Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length)); 341 } 342 343 PythonByteArray::PythonByteArray(PyRefType type, PyObject *o) 344 { 345 Reset(type, o); 346 } 347 348 PythonByteArray::PythonByteArray(const PythonBytes &object) : PythonObject(object) 349 { 350 } 351 352 PythonByteArray::~PythonByteArray() 353 { 354 } 355 356 bool 357 PythonByteArray::Check(PyObject *py_obj) 358 { 359 if (!py_obj) 360 return false; 361 if (PyByteArray_Check(py_obj)) 362 return true; 363 return false; 364 } 365 366 void 367 PythonByteArray::Reset(PyRefType type, PyObject *py_obj) 368 { 369 // Grab the desired reference type so that if we end up rejecting 370 // `py_obj` it still gets decremented if necessary. 371 PythonObject result(type, py_obj); 372 373 if (!PythonByteArray::Check(py_obj)) 374 { 375 PythonObject::Reset(); 376 return; 377 } 378 379 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 380 // back into the virtual implementation. 381 PythonObject::Reset(PyRefType::Borrowed, result.get()); 382 } 383 384 llvm::ArrayRef<uint8_t> 385 PythonByteArray::GetBytes() const 386 { 387 if (!IsValid()) 388 return llvm::ArrayRef<uint8_t>(); 389 390 char *c = PyByteArray_AsString(m_py_obj); 391 size_t size = GetSize(); 392 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 393 } 394 395 size_t 396 PythonByteArray::GetSize() const 397 { 398 if (!IsValid()) 399 return 0; 400 401 return PyByteArray_Size(m_py_obj); 402 } 403 404 StructuredData::StringSP 405 PythonByteArray::CreateStructuredString() const 406 { 407 StructuredData::StringSP result(new StructuredData::String); 408 llvm::ArrayRef<uint8_t> bytes = GetBytes(); 409 const char *str = reinterpret_cast<const char *>(bytes.data()); 410 result->SetValue(std::string(str, bytes.size())); 411 return result; 412 } 413 414 //---------------------------------------------------------------------- 415 // PythonString 416 //---------------------------------------------------------------------- 417 418 PythonString::PythonString(PyRefType type, PyObject *py_obj) 419 : PythonObject() 420 { 421 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string 422 } 423 424 PythonString::PythonString(const PythonString &object) 425 : PythonObject(object) 426 { 427 } 428 429 PythonString::PythonString(llvm::StringRef string) 430 : PythonObject() 431 { 432 SetString(string); 433 } 434 435 PythonString::PythonString(const char *string) 436 : PythonObject() 437 { 438 SetString(llvm::StringRef(string)); 439 } 440 441 PythonString::PythonString() 442 : PythonObject() 443 { 444 } 445 446 PythonString::~PythonString () 447 { 448 } 449 450 bool 451 PythonString::Check(PyObject *py_obj) 452 { 453 if (!py_obj) 454 return false; 455 456 if (PyUnicode_Check(py_obj)) 457 return true; 458 #if PY_MAJOR_VERSION < 3 459 if (PyString_Check(py_obj)) 460 return true; 461 #endif 462 return false; 463 } 464 465 void 466 PythonString::Reset(PyRefType type, PyObject *py_obj) 467 { 468 // Grab the desired reference type so that if we end up rejecting 469 // `py_obj` it still gets decremented if necessary. 470 PythonObject result(type, py_obj); 471 472 if (!PythonString::Check(py_obj)) 473 { 474 PythonObject::Reset(); 475 return; 476 } 477 #if PY_MAJOR_VERSION < 3 478 // In Python 2, Don't store PyUnicode objects directly, because we need 479 // access to their underlying character buffers which Python 2 doesn't 480 // provide. 481 if (PyUnicode_Check(py_obj)) 482 result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(result.get())); 483 #endif 484 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 485 // back into the virtual implementation. 486 PythonObject::Reset(PyRefType::Borrowed, result.get()); 487 } 488 489 llvm::StringRef 490 PythonString::GetString() const 491 { 492 if (!IsValid()) 493 return llvm::StringRef(); 494 495 Py_ssize_t size; 496 char *c; 497 498 #if PY_MAJOR_VERSION >= 3 499 c = PyUnicode_AsUTF8AndSize(m_py_obj, &size); 500 #else 501 PyString_AsStringAndSize(m_py_obj, &c, &size); 502 #endif 503 return llvm::StringRef(c, size); 504 } 505 506 size_t 507 PythonString::GetSize() const 508 { 509 if (IsValid()) 510 { 511 #if PY_MAJOR_VERSION >= 3 512 return PyUnicode_GetSize(m_py_obj); 513 #else 514 return PyString_Size(m_py_obj); 515 #endif 516 } 517 return 0; 518 } 519 520 void 521 PythonString::SetString (llvm::StringRef string) 522 { 523 #if PY_MAJOR_VERSION >= 3 524 PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size()); 525 PythonObject::Reset(PyRefType::Owned, unicode); 526 #else 527 PyObject *str = PyString_FromStringAndSize(string.data(), string.size()); 528 PythonObject::Reset(PyRefType::Owned, str); 529 #endif 530 } 531 532 StructuredData::StringSP 533 PythonString::CreateStructuredString() const 534 { 535 StructuredData::StringSP result(new StructuredData::String); 536 result->SetValue(GetString()); 537 return result; 538 } 539 540 //---------------------------------------------------------------------- 541 // PythonInteger 542 //---------------------------------------------------------------------- 543 544 PythonInteger::PythonInteger() 545 : PythonObject() 546 { 547 548 } 549 550 PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj) 551 : PythonObject() 552 { 553 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type 554 } 555 556 PythonInteger::PythonInteger(const PythonInteger &object) 557 : PythonObject(object) 558 { 559 } 560 561 PythonInteger::PythonInteger(int64_t value) 562 : PythonObject() 563 { 564 SetInteger(value); 565 } 566 567 568 PythonInteger::~PythonInteger () 569 { 570 } 571 572 bool 573 PythonInteger::Check(PyObject *py_obj) 574 { 575 if (!py_obj) 576 return false; 577 578 #if PY_MAJOR_VERSION >= 3 579 // Python 3 does not have PyInt_Check. There is only one type of 580 // integral value, long. 581 return PyLong_Check(py_obj); 582 #else 583 return PyLong_Check(py_obj) || PyInt_Check(py_obj); 584 #endif 585 } 586 587 void 588 PythonInteger::Reset(PyRefType type, PyObject *py_obj) 589 { 590 // Grab the desired reference type so that if we end up rejecting 591 // `py_obj` it still gets decremented if necessary. 592 PythonObject result(type, py_obj); 593 594 if (!PythonInteger::Check(py_obj)) 595 { 596 PythonObject::Reset(); 597 return; 598 } 599 600 #if PY_MAJOR_VERSION < 3 601 // Always store this as a PyLong, which makes interoperability between 602 // Python 2.x and Python 3.x easier. This is only necessary in 2.x, 603 // since 3.x doesn't even have a PyInt. 604 if (PyInt_Check(py_obj)) 605 { 606 // Since we converted the original object to a different type, the new 607 // object is an owned object regardless of the ownership semantics requested 608 // by the user. 609 result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj))); 610 } 611 #endif 612 613 assert(PyLong_Check(result.get()) && "Couldn't get a PyLong from this PyObject"); 614 615 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 616 // back into the virtual implementation. 617 PythonObject::Reset(PyRefType::Borrowed, result.get()); 618 } 619 620 int64_t 621 PythonInteger::GetInteger() const 622 { 623 if (m_py_obj) 624 { 625 assert(PyLong_Check(m_py_obj) && "PythonInteger::GetInteger has a PyObject that isn't a PyLong"); 626 627 int overflow = 0; 628 int64_t result = PyLong_AsLongLongAndOverflow(m_py_obj, &overflow); 629 if (overflow != 0) 630 { 631 // We got an integer that overflows, like 18446744072853913392L 632 // we can't use PyLong_AsLongLong() as it will return 633 // 0xffffffffffffffff. If we use the unsigned long long 634 // it will work as expected. 635 const uint64_t uval = PyLong_AsUnsignedLongLong(m_py_obj); 636 result = *((int64_t *)&uval); 637 } 638 return result; 639 } 640 return UINT64_MAX; 641 } 642 643 void 644 PythonInteger::SetInteger(int64_t value) 645 { 646 PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value)); 647 } 648 649 StructuredData::IntegerSP 650 PythonInteger::CreateStructuredInteger() const 651 { 652 StructuredData::IntegerSP result(new StructuredData::Integer); 653 result->SetValue(GetInteger()); 654 return result; 655 } 656 657 //---------------------------------------------------------------------- 658 // PythonList 659 //---------------------------------------------------------------------- 660 661 PythonList::PythonList(PyInitialValue value) 662 : PythonObject() 663 { 664 if (value == PyInitialValue::Empty) 665 Reset(PyRefType::Owned, PyList_New(0)); 666 } 667 668 PythonList::PythonList(int list_size) 669 : PythonObject() 670 { 671 Reset(PyRefType::Owned, PyList_New(list_size)); 672 } 673 674 PythonList::PythonList(PyRefType type, PyObject *py_obj) 675 : PythonObject() 676 { 677 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list 678 } 679 680 PythonList::PythonList(const PythonList &list) 681 : PythonObject(list) 682 { 683 } 684 685 PythonList::~PythonList () 686 { 687 } 688 689 bool 690 PythonList::Check(PyObject *py_obj) 691 { 692 if (!py_obj) 693 return false; 694 return PyList_Check(py_obj); 695 } 696 697 void 698 PythonList::Reset(PyRefType type, PyObject *py_obj) 699 { 700 // Grab the desired reference type so that if we end up rejecting 701 // `py_obj` it still gets decremented if necessary. 702 PythonObject result(type, py_obj); 703 704 if (!PythonList::Check(py_obj)) 705 { 706 PythonObject::Reset(); 707 return; 708 } 709 710 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 711 // back into the virtual implementation. 712 PythonObject::Reset(PyRefType::Borrowed, result.get()); 713 } 714 715 uint32_t 716 PythonList::GetSize() const 717 { 718 if (IsValid()) 719 return PyList_GET_SIZE(m_py_obj); 720 return 0; 721 } 722 723 PythonObject 724 PythonList::GetItemAtIndex(uint32_t index) const 725 { 726 if (IsValid()) 727 return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index)); 728 return PythonObject(); 729 } 730 731 void 732 PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) 733 { 734 if (IsAllocated() && object.IsValid()) 735 { 736 // PyList_SetItem is documented to "steal" a reference, so we need to 737 // convert it to an owned reference by incrementing it. 738 Py_INCREF(object.get()); 739 PyList_SetItem(m_py_obj, index, object.get()); 740 } 741 } 742 743 void 744 PythonList::AppendItem(const PythonObject &object) 745 { 746 if (IsAllocated() && object.IsValid()) 747 { 748 // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF` 749 // here like we do with `PyList_SetItem`. 750 PyList_Append(m_py_obj, object.get()); 751 } 752 } 753 754 StructuredData::ArraySP 755 PythonList::CreateStructuredArray() const 756 { 757 StructuredData::ArraySP result(new StructuredData::Array); 758 uint32_t count = GetSize(); 759 for (uint32_t i = 0; i < count; ++i) 760 { 761 PythonObject obj = GetItemAtIndex(i); 762 result->AddItem(obj.CreateStructuredObject()); 763 } 764 return result; 765 } 766 767 //---------------------------------------------------------------------- 768 // PythonTuple 769 //---------------------------------------------------------------------- 770 771 PythonTuple::PythonTuple(PyInitialValue value) 772 : PythonObject() 773 { 774 if (value == PyInitialValue::Empty) 775 Reset(PyRefType::Owned, PyTuple_New(0)); 776 } 777 778 PythonTuple::PythonTuple(int tuple_size) 779 : PythonObject() 780 { 781 Reset(PyRefType::Owned, PyTuple_New(tuple_size)); 782 } 783 784 PythonTuple::PythonTuple(PyRefType type, PyObject *py_obj) 785 : PythonObject() 786 { 787 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a tuple 788 } 789 790 PythonTuple::PythonTuple(const PythonTuple &tuple) 791 : PythonObject(tuple) 792 { 793 } 794 795 PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) 796 { 797 m_py_obj = PyTuple_New(objects.size()); 798 799 uint32_t idx = 0; 800 for (auto object : objects) 801 { 802 if (object.IsValid()) 803 SetItemAtIndex(idx, object); 804 idx++; 805 } 806 } 807 808 PythonTuple::PythonTuple(std::initializer_list<PyObject*> objects) 809 { 810 m_py_obj = PyTuple_New(objects.size()); 811 812 uint32_t idx = 0; 813 for (auto py_object : objects) 814 { 815 PythonObject object(PyRefType::Borrowed, py_object); 816 if (object.IsValid()) 817 SetItemAtIndex(idx, object); 818 idx++; 819 } 820 } 821 822 PythonTuple::~PythonTuple() 823 { 824 } 825 826 bool 827 PythonTuple::Check(PyObject *py_obj) 828 { 829 if (!py_obj) 830 return false; 831 return PyTuple_Check(py_obj); 832 } 833 834 void 835 PythonTuple::Reset(PyRefType type, PyObject *py_obj) 836 { 837 // Grab the desired reference type so that if we end up rejecting 838 // `py_obj` it still gets decremented if necessary. 839 PythonObject result(type, py_obj); 840 841 if (!PythonTuple::Check(py_obj)) 842 { 843 PythonObject::Reset(); 844 return; 845 } 846 847 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 848 // back into the virtual implementation. 849 PythonObject::Reset(PyRefType::Borrowed, result.get()); 850 } 851 852 uint32_t 853 PythonTuple::GetSize() const 854 { 855 if (IsValid()) 856 return PyTuple_GET_SIZE(m_py_obj); 857 return 0; 858 } 859 860 PythonObject 861 PythonTuple::GetItemAtIndex(uint32_t index) const 862 { 863 if (IsValid()) 864 return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index)); 865 return PythonObject(); 866 } 867 868 void 869 PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) 870 { 871 if (IsAllocated() && object.IsValid()) 872 { 873 // PyTuple_SetItem is documented to "steal" a reference, so we need to 874 // convert it to an owned reference by incrementing it. 875 Py_INCREF(object.get()); 876 PyTuple_SetItem(m_py_obj, index, object.get()); 877 } 878 } 879 880 StructuredData::ArraySP 881 PythonTuple::CreateStructuredArray() const 882 { 883 StructuredData::ArraySP result(new StructuredData::Array); 884 uint32_t count = GetSize(); 885 for (uint32_t i = 0; i < count; ++i) 886 { 887 PythonObject obj = GetItemAtIndex(i); 888 result->AddItem(obj.CreateStructuredObject()); 889 } 890 return result; 891 } 892 893 //---------------------------------------------------------------------- 894 // PythonDictionary 895 //---------------------------------------------------------------------- 896 897 PythonDictionary::PythonDictionary(PyInitialValue value) 898 : PythonObject() 899 { 900 if (value == PyInitialValue::Empty) 901 Reset(PyRefType::Owned, PyDict_New()); 902 } 903 904 PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj) 905 : PythonObject() 906 { 907 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary 908 } 909 910 PythonDictionary::PythonDictionary(const PythonDictionary &object) 911 : PythonObject(object) 912 { 913 } 914 915 PythonDictionary::~PythonDictionary () 916 { 917 } 918 919 bool 920 PythonDictionary::Check(PyObject *py_obj) 921 { 922 if (!py_obj) 923 return false; 924 925 return PyDict_Check(py_obj); 926 } 927 928 void 929 PythonDictionary::Reset(PyRefType type, PyObject *py_obj) 930 { 931 // Grab the desired reference type so that if we end up rejecting 932 // `py_obj` it still gets decremented if necessary. 933 PythonObject result(type, py_obj); 934 935 if (!PythonDictionary::Check(py_obj)) 936 { 937 PythonObject::Reset(); 938 return; 939 } 940 941 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 942 // back into the virtual implementation. 943 PythonObject::Reset(PyRefType::Borrowed, result.get()); 944 } 945 946 uint32_t 947 PythonDictionary::GetSize() const 948 { 949 if (IsValid()) 950 return PyDict_Size(m_py_obj); 951 return 0; 952 } 953 954 PythonList 955 PythonDictionary::GetKeys() const 956 { 957 if (IsValid()) 958 return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj)); 959 return PythonList(PyInitialValue::Invalid); 960 } 961 962 PythonObject 963 PythonDictionary::GetItemForKey(const PythonObject &key) const 964 { 965 if (IsAllocated() && key.IsValid()) 966 return PythonObject(PyRefType::Borrowed, PyDict_GetItem(m_py_obj, key.get())); 967 return PythonObject(); 968 } 969 970 void 971 PythonDictionary::SetItemForKey(const PythonObject &key, const PythonObject &value) 972 { 973 if (IsAllocated() && key.IsValid() && value.IsValid()) 974 PyDict_SetItem(m_py_obj, key.get(), value.get()); 975 } 976 977 StructuredData::DictionarySP 978 PythonDictionary::CreateStructuredDictionary() const 979 { 980 StructuredData::DictionarySP result(new StructuredData::Dictionary); 981 PythonList keys(GetKeys()); 982 uint32_t num_keys = keys.GetSize(); 983 for (uint32_t i = 0; i < num_keys; ++i) 984 { 985 PythonObject key = keys.GetItemAtIndex(i); 986 PythonObject value = GetItemForKey(key); 987 StructuredData::ObjectSP structured_value = value.CreateStructuredObject(); 988 result->AddItem(key.Str().GetString(), structured_value); 989 } 990 return result; 991 } 992 993 PythonModule::PythonModule() : PythonObject() 994 { 995 } 996 997 PythonModule::PythonModule(PyRefType type, PyObject *py_obj) 998 { 999 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a module 1000 } 1001 1002 PythonModule::PythonModule(const PythonModule &dict) : PythonObject(dict) 1003 { 1004 } 1005 1006 PythonModule::~PythonModule() 1007 { 1008 } 1009 1010 PythonModule 1011 PythonModule::BuiltinsModule() 1012 { 1013 #if PY_MAJOR_VERSION >= 3 1014 return AddModule("builtins"); 1015 #else 1016 return AddModule("__builtin__"); 1017 #endif 1018 } 1019 1020 PythonModule 1021 PythonModule::MainModule() 1022 { 1023 return AddModule("__main__"); 1024 } 1025 1026 PythonModule 1027 PythonModule::AddModule(llvm::StringRef module) 1028 { 1029 std::string str = module.str(); 1030 return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str())); 1031 } 1032 1033 1034 PythonModule 1035 PythonModule::ImportModule(llvm::StringRef module) 1036 { 1037 std::string str = module.str(); 1038 return PythonModule(PyRefType::Owned, PyImport_ImportModule(str.c_str())); 1039 } 1040 1041 bool 1042 PythonModule::Check(PyObject *py_obj) 1043 { 1044 if (!py_obj) 1045 return false; 1046 1047 return PyModule_Check(py_obj); 1048 } 1049 1050 void 1051 PythonModule::Reset(PyRefType type, PyObject *py_obj) 1052 { 1053 // Grab the desired reference type so that if we end up rejecting 1054 // `py_obj` it still gets decremented if necessary. 1055 PythonObject result(type, py_obj); 1056 1057 if (!PythonModule::Check(py_obj)) 1058 { 1059 PythonObject::Reset(); 1060 return; 1061 } 1062 1063 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 1064 // back into the virtual implementation. 1065 PythonObject::Reset(PyRefType::Borrowed, result.get()); 1066 } 1067 1068 PythonDictionary 1069 PythonModule::GetDictionary() const 1070 { 1071 return PythonDictionary(PyRefType::Borrowed, PyModule_GetDict(m_py_obj)); 1072 } 1073 1074 PythonCallable::PythonCallable() : PythonObject() 1075 { 1076 } 1077 1078 PythonCallable::PythonCallable(PyRefType type, PyObject *py_obj) 1079 { 1080 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a callable 1081 } 1082 1083 PythonCallable::PythonCallable(const PythonCallable &callable) 1084 : PythonObject(callable) 1085 { 1086 } 1087 1088 PythonCallable::~PythonCallable() 1089 { 1090 } 1091 1092 bool 1093 PythonCallable::Check(PyObject *py_obj) 1094 { 1095 if (!py_obj) 1096 return false; 1097 1098 return PyCallable_Check(py_obj); 1099 } 1100 1101 void 1102 PythonCallable::Reset(PyRefType type, PyObject *py_obj) 1103 { 1104 // Grab the desired reference type so that if we end up rejecting 1105 // `py_obj` it still gets decremented if necessary. 1106 PythonObject result(type, py_obj); 1107 1108 if (!PythonCallable::Check(py_obj)) 1109 { 1110 PythonObject::Reset(); 1111 return; 1112 } 1113 1114 // Calling PythonObject::Reset(const PythonObject&) will lead to stack overflow since it calls 1115 // back into the virtual implementation. 1116 PythonObject::Reset(PyRefType::Borrowed, result.get()); 1117 } 1118 1119 1120 PythonCallable::ArgInfo 1121 PythonCallable::GetNumArguments() const 1122 { 1123 ArgInfo result = { 0, false, false, false }; 1124 if (!IsValid()) 1125 return result; 1126 1127 PyObject *py_func_obj = m_py_obj; 1128 if (PyMethod_Check(py_func_obj)) 1129 { 1130 py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); 1131 PythonObject im_self = GetAttributeValue("im_self"); 1132 if (im_self.IsValid() && !im_self.IsNone()) 1133 result.is_bound_method = true; 1134 } 1135 else 1136 { 1137 // see if this is a callable object with an __call__ method 1138 if (!PyFunction_Check(py_func_obj)) 1139 { 1140 PythonObject __call__ = GetAttributeValue("__call__"); 1141 if (__call__.IsValid()) 1142 { 1143 auto __callable__ = __call__.AsType<PythonCallable>(); 1144 if (__callable__.IsValid()) 1145 { 1146 py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); 1147 PythonObject im_self = GetAttributeValue("im_self"); 1148 if (im_self.IsValid() && !im_self.IsNone()) 1149 result.is_bound_method = true; 1150 } 1151 } 1152 } 1153 } 1154 1155 if (!py_func_obj) 1156 return result; 1157 1158 PyCodeObject* code = (PyCodeObject*)PyFunction_GET_CODE(py_func_obj); 1159 if (!code) 1160 return result; 1161 1162 result.count = code->co_argcount; 1163 result.has_varargs = !!(code->co_flags & CO_VARARGS); 1164 result.has_kwargs = !!(code->co_flags & CO_VARKEYWORDS); 1165 return result; 1166 } 1167 1168 PythonObject 1169 PythonCallable::operator ()() 1170 { 1171 return PythonObject(PyRefType::Owned, 1172 PyObject_CallObject(m_py_obj, nullptr)); 1173 } 1174 1175 PythonObject 1176 PythonCallable::operator ()(std::initializer_list<PyObject*> args) 1177 { 1178 PythonTuple arg_tuple(args); 1179 return PythonObject(PyRefType::Owned, 1180 PyObject_CallObject(m_py_obj, arg_tuple.get())); 1181 } 1182 1183 PythonObject 1184 PythonCallable::operator ()(std::initializer_list<PythonObject> args) 1185 { 1186 PythonTuple arg_tuple(args); 1187 return PythonObject(PyRefType::Owned, 1188 PyObject_CallObject(m_py_obj, arg_tuple.get())); 1189 } 1190 1191 PythonFile::PythonFile() 1192 : PythonObject() 1193 { 1194 } 1195 1196 PythonFile::PythonFile(File &file, const char *mode) 1197 { 1198 Reset(file, mode); 1199 } 1200 1201 PythonFile::PythonFile(const char *path, const char *mode) 1202 { 1203 lldb_private::File file(path, GetOptionsFromMode(mode)); 1204 Reset(file, mode); 1205 } 1206 1207 PythonFile::PythonFile(PyRefType type, PyObject *o) 1208 { 1209 Reset(type, o); 1210 } 1211 1212 PythonFile::~PythonFile() 1213 { 1214 } 1215 1216 bool 1217 PythonFile::Check(PyObject *py_obj) 1218 { 1219 #if PY_MAJOR_VERSION < 3 1220 return PyFile_Check(py_obj); 1221 #else 1222 // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a 1223 // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper 1224 // over `io.open()`, which returns some object derived from `io.IOBase`. 1225 // As a result, the only way to detect a file in Python 3 is to check whether 1226 // it inherits from `io.IOBase`. Since it is possible for non-files to also 1227 // inherit from `io.IOBase`, we additionally verify that it has the `fileno` 1228 // attribute, which should guarantee that it is backed by the file system. 1229 PythonObject io_module(PyRefType::Owned, PyImport_ImportModule("io")); 1230 PythonDictionary io_dict(PyRefType::Borrowed, PyModule_GetDict(io_module.get())); 1231 PythonObject io_base_class = io_dict.GetItemForKey(PythonString("IOBase")); 1232 1233 PythonObject object_type(PyRefType::Owned, PyObject_Type(py_obj)); 1234 1235 if (1 != PyObject_IsSubclass(object_type.get(), io_base_class.get())) 1236 return false; 1237 if (!object_type.HasAttribute("fileno")) 1238 return false; 1239 1240 return true; 1241 #endif 1242 } 1243 1244 void 1245 PythonFile::Reset(PyRefType type, PyObject *py_obj) 1246 { 1247 // Grab the desired reference type so that if we end up rejecting 1248 // `py_obj` it still gets decremented if necessary. 1249 PythonObject result(type, py_obj); 1250 1251 if (!PythonFile::Check(py_obj)) 1252 { 1253 PythonObject::Reset(); 1254 return; 1255 } 1256 1257 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 1258 // overflow since it calls back into the virtual implementation. 1259 PythonObject::Reset(PyRefType::Borrowed, result.get()); 1260 } 1261 1262 void 1263 PythonFile::Reset(File &file, const char *mode) 1264 { 1265 if (!file.IsValid()) 1266 { 1267 Reset(); 1268 return; 1269 } 1270 1271 char *cmode = const_cast<char *>(mode); 1272 #if PY_MAJOR_VERSION >= 3 1273 Reset(PyRefType::Owned, 1274 PyFile_FromFd(file.GetDescriptor(), nullptr, cmode, -1, nullptr, "ignore", nullptr, 0)); 1275 #else 1276 // Read through the Python source, doesn't seem to modify these strings 1277 Reset(PyRefType::Owned, 1278 PyFile_FromFile(file.GetStream(), const_cast<char *>(""), cmode, nullptr)); 1279 #endif 1280 } 1281 1282 uint32_t 1283 PythonFile::GetOptionsFromMode(llvm::StringRef mode) 1284 { 1285 if (mode.empty()) 1286 return 0; 1287 1288 return llvm::StringSwitch<uint32_t>(mode.str().c_str()) 1289 .Case("r", File::eOpenOptionRead) 1290 .Case("w", File::eOpenOptionWrite) 1291 .Case("a", File::eOpenOptionWrite|File::eOpenOptionAppend|File::eOpenOptionCanCreate) 1292 .Case("r+", File::eOpenOptionRead|File::eOpenOptionWrite) 1293 .Case("w+", File::eOpenOptionRead|File::eOpenOptionWrite|File::eOpenOptionCanCreate|File::eOpenOptionTruncate) 1294 .Case("a+", File::eOpenOptionRead|File::eOpenOptionWrite|File::eOpenOptionAppend|File::eOpenOptionCanCreate) 1295 .Default(0); 1296 } 1297 1298 bool 1299 PythonFile::GetUnderlyingFile(File &file) const 1300 { 1301 if (!IsValid()) 1302 return false; 1303 1304 file.Close(); 1305 // We don't own the file descriptor returned by this function, make sure the 1306 // File object knows about that. 1307 file.SetDescriptor(PyObject_AsFileDescriptor(m_py_obj), false); 1308 PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>(); 1309 file.SetOptions(PythonFile::GetOptionsFromMode(py_mode.GetString())); 1310 return file.IsValid(); 1311 } 1312 1313 1314 #endif 1315