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