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