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