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