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