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