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/Log.h" 22 #include "lldb/Utility/Stream.h" 23 24 #include "llvm/ADT/StringSwitch.h" 25 #include "llvm/Support/ConvertUTF.h" 26 #include "llvm/Support/Errno.h" 27 28 #include <stdio.h> 29 30 using namespace lldb_private; 31 using namespace lldb; 32 using namespace lldb_private::python; 33 using llvm::Error; 34 using llvm::Expected; 35 36 template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) { 37 if (!obj) 38 return obj.takeError(); 39 return obj.get().IsTrue(); 40 } 41 42 template <> 43 Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) { 44 if (!obj) 45 return obj.takeError(); 46 return obj.get().AsLongLong(); 47 } 48 49 void StructuredPythonObject::Serialize(llvm::json::OStream &s) const { 50 s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str()); 51 } 52 53 // PythonObject 54 55 void PythonObject::Dump(Stream &strm) const { 56 if (m_py_obj) { 57 FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile); 58 if (file) { 59 ::PyObject_Print(m_py_obj, file, 0); 60 const long length = ftell(file); 61 if (length) { 62 ::rewind(file); 63 std::vector<char> file_contents(length, '\0'); 64 const size_t length_read = 65 ::fread(file_contents.data(), 1, file_contents.size(), file); 66 if (length_read > 0) 67 strm.Write(file_contents.data(), length_read); 68 } 69 ::fclose(file); 70 } 71 } else 72 strm.PutCString("NULL"); 73 } 74 75 PyObjectType PythonObject::GetObjectType() const { 76 if (!IsAllocated()) 77 return PyObjectType::None; 78 79 if (PythonModule::Check(m_py_obj)) 80 return PyObjectType::Module; 81 if (PythonList::Check(m_py_obj)) 82 return PyObjectType::List; 83 if (PythonTuple::Check(m_py_obj)) 84 return PyObjectType::Tuple; 85 if (PythonDictionary::Check(m_py_obj)) 86 return PyObjectType::Dictionary; 87 if (PythonString::Check(m_py_obj)) 88 return PyObjectType::String; 89 #if PY_MAJOR_VERSION >= 3 90 if (PythonBytes::Check(m_py_obj)) 91 return PyObjectType::Bytes; 92 #endif 93 if (PythonByteArray::Check(m_py_obj)) 94 return PyObjectType::ByteArray; 95 if (PythonBoolean::Check(m_py_obj)) 96 return PyObjectType::Boolean; 97 if (PythonInteger::Check(m_py_obj)) 98 return PyObjectType::Integer; 99 if (PythonFile::Check(m_py_obj)) 100 return PyObjectType::File; 101 if (PythonCallable::Check(m_py_obj)) 102 return PyObjectType::Callable; 103 return PyObjectType::Unknown; 104 } 105 106 PythonString PythonObject::Repr() const { 107 if (!m_py_obj) 108 return PythonString(); 109 PyObject *repr = PyObject_Repr(m_py_obj); 110 if (!repr) 111 return PythonString(); 112 return PythonString(PyRefType::Owned, repr); 113 } 114 115 PythonString PythonObject::Str() const { 116 if (!m_py_obj) 117 return PythonString(); 118 PyObject *str = PyObject_Str(m_py_obj); 119 if (!str) 120 return PythonString(); 121 return PythonString(PyRefType::Owned, str); 122 } 123 124 PythonObject 125 PythonObject::ResolveNameWithDictionary(llvm::StringRef name, 126 const PythonDictionary &dict) { 127 size_t dot_pos = name.find('.'); 128 llvm::StringRef piece = name.substr(0, dot_pos); 129 PythonObject result = dict.GetItemForKey(PythonString(piece)); 130 if (dot_pos == llvm::StringRef::npos) { 131 // There was no dot, we're done. 132 return result; 133 } 134 135 // There was a dot. The remaining portion of the name should be looked up in 136 // the context of the object that was found in the dictionary. 137 return result.ResolveName(name.substr(dot_pos + 1)); 138 } 139 140 PythonObject PythonObject::ResolveName(llvm::StringRef name) const { 141 // Resolve the name in the context of the specified object. If, for example, 142 // `this` refers to a PyModule, then this will look for `name` in this 143 // module. If `this` refers to a PyType, then it will resolve `name` as an 144 // attribute of that type. If `this` refers to an instance of an object, 145 // then it will resolve `name` as the value of the specified field. 146 // 147 // This function handles dotted names so that, for example, if `m_py_obj` 148 // refers to the `sys` module, and `name` == "path.append", then it will find 149 // the function `sys.path.append`. 150 151 size_t dot_pos = name.find('.'); 152 if (dot_pos == llvm::StringRef::npos) { 153 // No dots in the name, we should be able to find the value immediately as 154 // an attribute of `m_py_obj`. 155 return GetAttributeValue(name); 156 } 157 158 // Look up the first piece of the name, and resolve the rest as a child of 159 // that. 160 PythonObject parent = ResolveName(name.substr(0, dot_pos)); 161 if (!parent.IsAllocated()) 162 return PythonObject(); 163 164 // Tail recursion.. should be optimized by the compiler 165 return parent.ResolveName(name.substr(dot_pos + 1)); 166 } 167 168 bool PythonObject::HasAttribute(llvm::StringRef attr) const { 169 if (!IsValid()) 170 return false; 171 PythonString py_attr(attr); 172 return !!PyObject_HasAttr(m_py_obj, py_attr.get()); 173 } 174 175 PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const { 176 if (!IsValid()) 177 return PythonObject(); 178 179 PythonString py_attr(attr); 180 if (!PyObject_HasAttr(m_py_obj, py_attr.get())) 181 return PythonObject(); 182 183 return PythonObject(PyRefType::Owned, 184 PyObject_GetAttr(m_py_obj, py_attr.get())); 185 } 186 187 StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { 188 switch (GetObjectType()) { 189 case PyObjectType::Dictionary: 190 return PythonDictionary(PyRefType::Borrowed, m_py_obj) 191 .CreateStructuredDictionary(); 192 case PyObjectType::Boolean: 193 return PythonBoolean(PyRefType::Borrowed, m_py_obj) 194 .CreateStructuredBoolean(); 195 case PyObjectType::Integer: 196 return PythonInteger(PyRefType::Borrowed, m_py_obj) 197 .CreateStructuredInteger(); 198 case PyObjectType::List: 199 return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray(); 200 case PyObjectType::String: 201 return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 202 case PyObjectType::Bytes: 203 return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 204 case PyObjectType::ByteArray: 205 return PythonByteArray(PyRefType::Borrowed, m_py_obj) 206 .CreateStructuredString(); 207 case PyObjectType::None: 208 return StructuredData::ObjectSP(); 209 default: 210 return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj)); 211 } 212 } 213 214 // PythonString 215 PythonBytes::PythonBytes() : PythonObject() {} 216 217 PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) : PythonObject() { 218 SetBytes(bytes); 219 } 220 221 PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) : PythonObject() { 222 SetBytes(llvm::ArrayRef<uint8_t>(bytes, length)); 223 } 224 225 PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() { 226 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string 227 } 228 229 PythonBytes::~PythonBytes() {} 230 231 bool PythonBytes::Check(PyObject *py_obj) { 232 if (!py_obj) 233 return false; 234 return PyBytes_Check(py_obj); 235 } 236 237 void PythonBytes::Reset(PyRefType type, PyObject *py_obj) { 238 // Grab the desired reference type so that if we end up rejecting `py_obj` it 239 // still gets decremented if necessary. 240 PythonObject result(type, py_obj); 241 242 if (!PythonBytes::Check(py_obj)) { 243 PythonObject::Reset(); 244 return; 245 } 246 247 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 248 // overflow since it calls back into the virtual implementation. 249 PythonObject::Reset(PyRefType::Borrowed, result.get()); 250 } 251 252 llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const { 253 if (!IsValid()) 254 return llvm::ArrayRef<uint8_t>(); 255 256 Py_ssize_t size; 257 char *c; 258 259 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 260 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 261 } 262 263 size_t PythonBytes::GetSize() const { 264 if (!IsValid()) 265 return 0; 266 return PyBytes_Size(m_py_obj); 267 } 268 269 void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) { 270 const char *data = reinterpret_cast<const char *>(bytes.data()); 271 PyObject *py_bytes = PyBytes_FromStringAndSize(data, bytes.size()); 272 PythonObject::Reset(PyRefType::Owned, py_bytes); 273 } 274 275 StructuredData::StringSP PythonBytes::CreateStructuredString() const { 276 StructuredData::StringSP result(new StructuredData::String); 277 Py_ssize_t size; 278 char *c; 279 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 280 result->SetValue(std::string(c, size)); 281 return result; 282 } 283 284 PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes) 285 : PythonByteArray(bytes.data(), bytes.size()) {} 286 287 PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) { 288 const char *str = reinterpret_cast<const char *>(bytes); 289 Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length)); 290 } 291 292 PythonByteArray::PythonByteArray(PyRefType type, PyObject *o) { 293 Reset(type, o); 294 } 295 296 PythonByteArray::PythonByteArray(const PythonBytes &object) 297 : PythonObject(object) {} 298 299 PythonByteArray::~PythonByteArray() {} 300 301 bool PythonByteArray::Check(PyObject *py_obj) { 302 if (!py_obj) 303 return false; 304 return PyByteArray_Check(py_obj); 305 } 306 307 void PythonByteArray::Reset(PyRefType type, PyObject *py_obj) { 308 // Grab the desired reference type so that if we end up rejecting `py_obj` it 309 // still gets decremented if necessary. 310 PythonObject result(type, py_obj); 311 312 if (!PythonByteArray::Check(py_obj)) { 313 PythonObject::Reset(); 314 return; 315 } 316 317 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 318 // overflow since it calls 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 // PythonString 347 348 Expected<PythonString> PythonString::FromUTF8(llvm::StringRef string) { 349 #if PY_MAJOR_VERSION >= 3 350 PyObject *str = PyUnicode_FromStringAndSize(string.data(), string.size()); 351 #else 352 PyObject *str = PyString_FromStringAndSize(string.data(), string.size()); 353 #endif 354 if (!str) 355 return llvm::make_error<PythonException>(); 356 return Take<PythonString>(str); 357 } 358 359 PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() { 360 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string 361 } 362 363 PythonString::PythonString(llvm::StringRef string) : PythonObject() { 364 SetString(string); 365 } 366 367 PythonString::PythonString() : PythonObject() {} 368 369 PythonString::~PythonString() {} 370 371 bool PythonString::Check(PyObject *py_obj) { 372 if (!py_obj) 373 return false; 374 375 if (PyUnicode_Check(py_obj)) 376 return true; 377 #if PY_MAJOR_VERSION < 3 378 if (PyString_Check(py_obj)) 379 return true; 380 #endif 381 return false; 382 } 383 384 void PythonString::Reset(PyRefType type, PyObject *py_obj) { 385 // Grab the desired reference type so that if we end up rejecting `py_obj` it 386 // still gets decremented if necessary. 387 PythonObject result(type, py_obj); 388 389 if (!PythonString::Check(py_obj)) { 390 PythonObject::Reset(); 391 return; 392 } 393 #if PY_MAJOR_VERSION < 3 394 // In Python 2, Don't store PyUnicode objects directly, because we need 395 // access to their underlying character buffers which Python 2 doesn't 396 // provide. 397 if (PyUnicode_Check(py_obj)) { 398 PyObject *s = PyUnicode_AsUTF8String(result.get()); 399 if (s == NULL) 400 PyErr_Clear(); 401 result.Reset(PyRefType::Owned, s); 402 } 403 #endif 404 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 405 // overflow since it calls back into the virtual implementation. 406 PythonObject::Reset(PyRefType::Borrowed, result.get()); 407 } 408 409 llvm::StringRef PythonString::GetString() const { 410 auto s = AsUTF8(); 411 if (!s) { 412 llvm::consumeError(s.takeError()); 413 return llvm::StringRef(""); 414 } 415 return s.get(); 416 } 417 418 Expected<llvm::StringRef> PythonString::AsUTF8() const { 419 if (!IsValid()) 420 return nullDeref(); 421 422 Py_ssize_t size; 423 const char *data; 424 425 #if PY_MAJOR_VERSION >= 3 426 data = PyUnicode_AsUTF8AndSize(m_py_obj, &size); 427 #else 428 char *c = NULL; 429 int r = PyString_AsStringAndSize(m_py_obj, &c, &size); 430 if (r < 0) 431 c = NULL; 432 data = c; 433 #endif 434 435 if (!data) 436 return exception(); 437 438 return llvm::StringRef(data, size); 439 } 440 441 size_t PythonString::GetSize() const { 442 if (IsValid()) { 443 #if PY_MAJOR_VERSION >= 3 444 return PyUnicode_GetSize(m_py_obj); 445 #else 446 return PyString_Size(m_py_obj); 447 #endif 448 } 449 return 0; 450 } 451 452 void PythonString::SetString(llvm::StringRef string) { 453 auto s = FromUTF8(string); 454 if (!s) { 455 llvm::consumeError(s.takeError()); 456 Reset(); 457 } else { 458 PythonObject::Reset(std::move(s.get())); 459 } 460 } 461 462 StructuredData::StringSP PythonString::CreateStructuredString() const { 463 StructuredData::StringSP result(new StructuredData::String); 464 result->SetValue(GetString()); 465 return result; 466 } 467 468 // PythonInteger 469 470 PythonInteger::PythonInteger() : PythonObject() {} 471 472 PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj) 473 : PythonObject() { 474 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type 475 } 476 477 PythonInteger::PythonInteger(int64_t value) : PythonObject() { 478 SetInteger(value); 479 } 480 481 PythonInteger::~PythonInteger() {} 482 483 bool PythonInteger::Check(PyObject *py_obj) { 484 if (!py_obj) 485 return false; 486 487 #if PY_MAJOR_VERSION >= 3 488 // Python 3 does not have PyInt_Check. There is only one type of integral 489 // value, long. 490 return PyLong_Check(py_obj); 491 #else 492 return PyLong_Check(py_obj) || PyInt_Check(py_obj); 493 #endif 494 } 495 496 void PythonInteger::Reset(PyRefType type, PyObject *py_obj) { 497 // Grab the desired reference type so that if we end up rejecting `py_obj` it 498 // still gets decremented if necessary. 499 PythonObject result(type, py_obj); 500 501 if (!PythonInteger::Check(py_obj)) { 502 PythonObject::Reset(); 503 return; 504 } 505 506 #if PY_MAJOR_VERSION < 3 507 // Always store this as a PyLong, which makes interoperability between Python 508 // 2.x and Python 3.x easier. This is only necessary in 2.x, since 3.x 509 // doesn't even have a PyInt. 510 if (PyInt_Check(py_obj)) { 511 // Since we converted the original object to a different type, the new 512 // object is an owned object regardless of the ownership semantics 513 // requested by the user. 514 result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj))); 515 } 516 #endif 517 518 assert(PyLong_Check(result.get()) && 519 "Couldn't get a PyLong from this PyObject"); 520 521 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 522 // overflow since it calls back into the virtual implementation. 523 PythonObject::Reset(PyRefType::Borrowed, result.get()); 524 } 525 526 int64_t PythonInteger::GetInteger() const { 527 if (m_py_obj) { 528 assert(PyLong_Check(m_py_obj) && 529 "PythonInteger::GetInteger has a PyObject that isn't a PyLong"); 530 531 int overflow = 0; 532 int64_t result = PyLong_AsLongLongAndOverflow(m_py_obj, &overflow); 533 if (overflow != 0) { 534 // We got an integer that overflows, like 18446744072853913392L we can't 535 // use PyLong_AsLongLong() as it will return 0xffffffffffffffff. If we 536 // use the unsigned long long it will work as expected. 537 const uint64_t uval = PyLong_AsUnsignedLongLong(m_py_obj); 538 result = static_cast<int64_t>(uval); 539 } 540 return result; 541 } 542 return UINT64_MAX; 543 } 544 545 void PythonInteger::SetInteger(int64_t value) { 546 PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value)); 547 } 548 549 StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const { 550 StructuredData::IntegerSP result(new StructuredData::Integer); 551 result->SetValue(GetInteger()); 552 return result; 553 } 554 555 // PythonBoolean 556 557 PythonBoolean::PythonBoolean(PyRefType type, PyObject *py_obj) 558 : PythonObject() { 559 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a boolean type 560 } 561 562 PythonBoolean::PythonBoolean(bool value) { 563 SetValue(value); 564 } 565 566 bool PythonBoolean::Check(PyObject *py_obj) { 567 return py_obj ? PyBool_Check(py_obj) : false; 568 } 569 570 void PythonBoolean::Reset(PyRefType type, PyObject *py_obj) { 571 // Grab the desired reference type so that if we end up rejecting `py_obj` it 572 // still gets decremented if necessary. 573 PythonObject result(type, py_obj); 574 575 if (!PythonBoolean::Check(py_obj)) { 576 PythonObject::Reset(); 577 return; 578 } 579 580 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 581 // overflow since it calls back into the virtual implementation. 582 PythonObject::Reset(PyRefType::Borrowed, result.get()); 583 } 584 585 bool PythonBoolean::GetValue() const { 586 return m_py_obj ? PyObject_IsTrue(m_py_obj) : false; 587 } 588 589 void PythonBoolean::SetValue(bool value) { 590 PythonObject::Reset(PyRefType::Owned, PyBool_FromLong(value)); 591 } 592 593 StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const { 594 StructuredData::BooleanSP result(new StructuredData::Boolean); 595 result->SetValue(GetValue()); 596 return result; 597 } 598 599 // PythonList 600 601 PythonList::PythonList(PyInitialValue value) : PythonObject() { 602 if (value == PyInitialValue::Empty) 603 Reset(PyRefType::Owned, PyList_New(0)); 604 } 605 606 PythonList::PythonList(int list_size) : PythonObject() { 607 Reset(PyRefType::Owned, PyList_New(list_size)); 608 } 609 610 PythonList::PythonList(PyRefType type, PyObject *py_obj) : PythonObject() { 611 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list 612 } 613 614 PythonList::~PythonList() {} 615 616 bool PythonList::Check(PyObject *py_obj) { 617 if (!py_obj) 618 return false; 619 return PyList_Check(py_obj); 620 } 621 622 void PythonList::Reset(PyRefType type, PyObject *py_obj) { 623 // Grab the desired reference type so that if we end up rejecting `py_obj` it 624 // still gets decremented if necessary. 625 PythonObject result(type, py_obj); 626 627 if (!PythonList::Check(py_obj)) { 628 PythonObject::Reset(); 629 return; 630 } 631 632 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 633 // overflow since it calls back into the virtual implementation. 634 PythonObject::Reset(PyRefType::Borrowed, result.get()); 635 } 636 637 uint32_t PythonList::GetSize() const { 638 if (IsValid()) 639 return PyList_GET_SIZE(m_py_obj); 640 return 0; 641 } 642 643 PythonObject PythonList::GetItemAtIndex(uint32_t index) const { 644 if (IsValid()) 645 return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index)); 646 return PythonObject(); 647 } 648 649 void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) { 650 if (IsAllocated() && object.IsValid()) { 651 // PyList_SetItem is documented to "steal" a reference, so we need to 652 // convert it to an owned reference by incrementing it. 653 Py_INCREF(object.get()); 654 PyList_SetItem(m_py_obj, index, object.get()); 655 } 656 } 657 658 void PythonList::AppendItem(const PythonObject &object) { 659 if (IsAllocated() && object.IsValid()) { 660 // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF` 661 // here like we do with `PyList_SetItem`. 662 PyList_Append(m_py_obj, object.get()); 663 } 664 } 665 666 StructuredData::ArraySP PythonList::CreateStructuredArray() const { 667 StructuredData::ArraySP result(new StructuredData::Array); 668 uint32_t count = GetSize(); 669 for (uint32_t i = 0; i < count; ++i) { 670 PythonObject obj = GetItemAtIndex(i); 671 result->AddItem(obj.CreateStructuredObject()); 672 } 673 return result; 674 } 675 676 // PythonTuple 677 678 PythonTuple::PythonTuple(PyInitialValue value) : PythonObject() { 679 if (value == PyInitialValue::Empty) 680 Reset(PyRefType::Owned, PyTuple_New(0)); 681 } 682 683 PythonTuple::PythonTuple(int tuple_size) : PythonObject() { 684 Reset(PyRefType::Owned, PyTuple_New(tuple_size)); 685 } 686 687 PythonTuple::PythonTuple(PyRefType type, PyObject *py_obj) : PythonObject() { 688 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a tuple 689 } 690 691 PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) { 692 m_py_obj = PyTuple_New(objects.size()); 693 694 uint32_t idx = 0; 695 for (auto object : objects) { 696 if (object.IsValid()) 697 SetItemAtIndex(idx, object); 698 idx++; 699 } 700 } 701 702 PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) { 703 m_py_obj = PyTuple_New(objects.size()); 704 705 uint32_t idx = 0; 706 for (auto py_object : objects) { 707 PythonObject object(PyRefType::Borrowed, py_object); 708 if (object.IsValid()) 709 SetItemAtIndex(idx, object); 710 idx++; 711 } 712 } 713 714 PythonTuple::~PythonTuple() {} 715 716 bool PythonTuple::Check(PyObject *py_obj) { 717 if (!py_obj) 718 return false; 719 return PyTuple_Check(py_obj); 720 } 721 722 void PythonTuple::Reset(PyRefType type, PyObject *py_obj) { 723 // Grab the desired reference type so that if we end up rejecting `py_obj` it 724 // still gets decremented if necessary. 725 PythonObject result(type, py_obj); 726 727 if (!PythonTuple::Check(py_obj)) { 728 PythonObject::Reset(); 729 return; 730 } 731 732 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 733 // overflow since it calls back into the virtual implementation. 734 PythonObject::Reset(PyRefType::Borrowed, result.get()); 735 } 736 737 uint32_t PythonTuple::GetSize() const { 738 if (IsValid()) 739 return PyTuple_GET_SIZE(m_py_obj); 740 return 0; 741 } 742 743 PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const { 744 if (IsValid()) 745 return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index)); 746 return PythonObject(); 747 } 748 749 void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) { 750 if (IsAllocated() && object.IsValid()) { 751 // PyTuple_SetItem is documented to "steal" a reference, so we need to 752 // convert it to an owned reference by incrementing it. 753 Py_INCREF(object.get()); 754 PyTuple_SetItem(m_py_obj, index, object.get()); 755 } 756 } 757 758 StructuredData::ArraySP PythonTuple::CreateStructuredArray() const { 759 StructuredData::ArraySP result(new StructuredData::Array); 760 uint32_t count = GetSize(); 761 for (uint32_t i = 0; i < count; ++i) { 762 PythonObject obj = GetItemAtIndex(i); 763 result->AddItem(obj.CreateStructuredObject()); 764 } 765 return result; 766 } 767 768 // PythonDictionary 769 770 PythonDictionary::PythonDictionary(PyInitialValue value) : PythonObject() { 771 if (value == PyInitialValue::Empty) 772 Reset(PyRefType::Owned, PyDict_New()); 773 } 774 775 PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj) 776 : PythonObject() { 777 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary 778 } 779 780 PythonDictionary::~PythonDictionary() {} 781 782 bool PythonDictionary::Check(PyObject *py_obj) { 783 if (!py_obj) 784 return false; 785 786 return PyDict_Check(py_obj); 787 } 788 789 void PythonDictionary::Reset(PyRefType type, PyObject *py_obj) { 790 // Grab the desired reference type so that if we end up rejecting `py_obj` it 791 // still gets decremented if necessary. 792 PythonObject result(type, py_obj); 793 794 if (!PythonDictionary::Check(py_obj)) { 795 PythonObject::Reset(); 796 return; 797 } 798 799 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 800 // overflow since it calls back into the virtual implementation. 801 PythonObject::Reset(PyRefType::Borrowed, result.get()); 802 } 803 804 uint32_t PythonDictionary::GetSize() const { 805 if (IsValid()) 806 return PyDict_Size(m_py_obj); 807 return 0; 808 } 809 810 PythonList PythonDictionary::GetKeys() const { 811 if (IsValid()) 812 return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj)); 813 return PythonList(PyInitialValue::Invalid); 814 } 815 816 PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const { 817 if (IsAllocated() && key.IsValid()) 818 return PythonObject(PyRefType::Borrowed, 819 PyDict_GetItem(m_py_obj, key.get())); 820 return PythonObject(); 821 } 822 823 void PythonDictionary::SetItemForKey(const PythonObject &key, 824 const PythonObject &value) { 825 if (IsAllocated() && key.IsValid() && value.IsValid()) 826 PyDict_SetItem(m_py_obj, key.get(), value.get()); 827 } 828 829 StructuredData::DictionarySP 830 PythonDictionary::CreateStructuredDictionary() const { 831 StructuredData::DictionarySP result(new StructuredData::Dictionary); 832 PythonList keys(GetKeys()); 833 uint32_t num_keys = keys.GetSize(); 834 for (uint32_t i = 0; i < num_keys; ++i) { 835 PythonObject key = keys.GetItemAtIndex(i); 836 PythonObject value = GetItemForKey(key); 837 StructuredData::ObjectSP structured_value = value.CreateStructuredObject(); 838 result->AddItem(key.Str().GetString(), structured_value); 839 } 840 return result; 841 } 842 843 PythonModule::PythonModule() : PythonObject() {} 844 845 PythonModule::PythonModule(PyRefType type, PyObject *py_obj) { 846 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a module 847 } 848 849 PythonModule::~PythonModule() {} 850 851 PythonModule PythonModule::BuiltinsModule() { 852 #if PY_MAJOR_VERSION >= 3 853 return AddModule("builtins"); 854 #else 855 return AddModule("__builtin__"); 856 #endif 857 } 858 859 PythonModule PythonModule::MainModule() { return AddModule("__main__"); } 860 861 PythonModule PythonModule::AddModule(llvm::StringRef module) { 862 std::string str = module.str(); 863 return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str())); 864 } 865 866 Expected<PythonModule> PythonModule::Import(const char *name) { 867 PyObject *mod = PyImport_ImportModule(name); 868 if (!mod) 869 return exception(); 870 return Take<PythonModule>(mod); 871 } 872 873 Expected<PythonObject> PythonModule::Get(const char *name) { 874 if (!IsValid()) 875 return nullDeref(); 876 PyObject *dict = PyModule_GetDict(m_py_obj); 877 if (!dict) 878 return exception(); 879 PyObject *item = PyDict_GetItemString(dict, name); 880 if (!item) 881 return exception(); 882 return Retain<PythonObject>(item); 883 } 884 885 bool PythonModule::Check(PyObject *py_obj) { 886 if (!py_obj) 887 return false; 888 889 return PyModule_Check(py_obj); 890 } 891 892 void PythonModule::Reset(PyRefType type, PyObject *py_obj) { 893 // Grab the desired reference type so that if we end up rejecting `py_obj` it 894 // still gets decremented if necessary. 895 PythonObject result(type, py_obj); 896 897 if (!PythonModule::Check(py_obj)) { 898 PythonObject::Reset(); 899 return; 900 } 901 902 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 903 // overflow since it calls back into the virtual implementation. 904 PythonObject::Reset(PyRefType::Borrowed, result.get()); 905 } 906 907 PythonDictionary PythonModule::GetDictionary() const { 908 return PythonDictionary(PyRefType::Borrowed, PyModule_GetDict(m_py_obj)); 909 } 910 911 PythonCallable::PythonCallable() : PythonObject() {} 912 913 PythonCallable::PythonCallable(PyRefType type, PyObject *py_obj) { 914 Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a callable 915 } 916 917 PythonCallable::~PythonCallable() {} 918 919 bool PythonCallable::Check(PyObject *py_obj) { 920 if (!py_obj) 921 return false; 922 923 return PyCallable_Check(py_obj); 924 } 925 926 void PythonCallable::Reset(PyRefType type, PyObject *py_obj) { 927 // Grab the desired reference type so that if we end up rejecting `py_obj` it 928 // still gets decremented if necessary. 929 PythonObject result(type, py_obj); 930 931 if (!PythonCallable::Check(py_obj)) { 932 PythonObject::Reset(); 933 return; 934 } 935 936 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 937 // overflow since it calls back into the virtual implementation. 938 PythonObject::Reset(PyRefType::Borrowed, result.get()); 939 } 940 941 PythonCallable::ArgInfo PythonCallable::GetNumInitArguments() const { 942 ArgInfo result = {0, false, false, false}; 943 if (!IsValid()) 944 return result; 945 946 PythonObject __init__ = GetAttributeValue("__init__"); 947 if (__init__.IsValid() ) { 948 auto __init_callable__ = __init__.AsType<PythonCallable>(); 949 if (__init_callable__.IsValid()) 950 return __init_callable__.GetNumArguments(); 951 } 952 return result; 953 } 954 955 PythonCallable::ArgInfo PythonCallable::GetNumArguments() const { 956 ArgInfo result = {0, false, false, false}; 957 if (!IsValid()) 958 return result; 959 960 PyObject *py_func_obj = m_py_obj; 961 if (PyMethod_Check(py_func_obj)) { 962 py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); 963 PythonObject im_self = GetAttributeValue("im_self"); 964 if (im_self.IsValid() && !im_self.IsNone()) 965 result.is_bound_method = true; 966 } else { 967 // see if this is a callable object with an __call__ method 968 if (!PyFunction_Check(py_func_obj)) { 969 PythonObject __call__ = GetAttributeValue("__call__"); 970 if (__call__.IsValid()) { 971 auto __callable__ = __call__.AsType<PythonCallable>(); 972 if (__callable__.IsValid()) { 973 py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); 974 PythonObject im_self = GetAttributeValue("im_self"); 975 if (im_self.IsValid() && !im_self.IsNone()) 976 result.is_bound_method = true; 977 } 978 } 979 } 980 } 981 982 if (!py_func_obj) 983 return result; 984 985 PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj); 986 if (!code) 987 return result; 988 989 result.count = code->co_argcount; 990 result.has_varargs = !!(code->co_flags & CO_VARARGS); 991 result.has_kwargs = !!(code->co_flags & CO_VARKEYWORDS); 992 return result; 993 } 994 995 PythonObject PythonCallable::operator()() { 996 return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr)); 997 } 998 999 PythonObject PythonCallable:: 1000 operator()(std::initializer_list<PyObject *> args) { 1001 PythonTuple arg_tuple(args); 1002 return PythonObject(PyRefType::Owned, 1003 PyObject_CallObject(m_py_obj, arg_tuple.get())); 1004 } 1005 1006 PythonObject PythonCallable:: 1007 operator()(std::initializer_list<PythonObject> args) { 1008 PythonTuple arg_tuple(args); 1009 return PythonObject(PyRefType::Owned, 1010 PyObject_CallObject(m_py_obj, arg_tuple.get())); 1011 } 1012 1013 PythonFile::PythonFile() : PythonObject() {} 1014 1015 PythonFile::PythonFile(File &file, const char *mode) { Reset(file, mode); } 1016 1017 PythonFile::PythonFile(PyRefType type, PyObject *o) { Reset(type, o); } 1018 1019 PythonFile::~PythonFile() {} 1020 1021 bool PythonFile::Check(PyObject *py_obj) { 1022 if (!py_obj) 1023 return false; 1024 #if PY_MAJOR_VERSION < 3 1025 return PyFile_Check(py_obj); 1026 #else 1027 // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a 1028 // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper 1029 // over `io.open()`, which returns some object derived from `io.IOBase`. As a 1030 // result, the only way to detect a file in Python 3 is to check whether it 1031 // inherits from `io.IOBase`. 1032 auto io_module = PythonModule::Import("io"); 1033 if (!io_module) { 1034 llvm::consumeError(io_module.takeError()); 1035 return false; 1036 } 1037 auto iobase = io_module.get().Get("IOBase"); 1038 if (!iobase) { 1039 llvm::consumeError(iobase.takeError()); 1040 return false; 1041 } 1042 int r = PyObject_IsInstance(py_obj, iobase.get().get()); 1043 if (r < 0) { 1044 llvm::consumeError(exception()); // clear the exception and log it. 1045 return false; 1046 } 1047 return !!r; 1048 #endif 1049 } 1050 1051 void PythonFile::Reset(PyRefType type, PyObject *py_obj) { 1052 // Grab the desired reference type so that if we end up rejecting `py_obj` it 1053 // still gets decremented if necessary. 1054 PythonObject result(type, py_obj); 1055 1056 if (!PythonFile::Check(py_obj)) { 1057 PythonObject::Reset(); 1058 return; 1059 } 1060 1061 // Calling PythonObject::Reset(const PythonObject&) will lead to stack 1062 // overflow since it calls back into the virtual implementation. 1063 PythonObject::Reset(PyRefType::Borrowed, result.get()); 1064 } 1065 1066 void PythonFile::Reset(File &file, const char *mode) { 1067 if (!file.IsValid()) { 1068 Reset(); 1069 return; 1070 } 1071 1072 char *cmode = const_cast<char *>(mode); 1073 #if PY_MAJOR_VERSION >= 3 1074 Reset(PyRefType::Owned, PyFile_FromFd(file.GetDescriptor(), nullptr, cmode, 1075 -1, nullptr, "ignore", nullptr, 0)); 1076 #else 1077 // Read through the Python source, doesn't seem to modify these strings 1078 Reset(PyRefType::Owned, 1079 PyFile_FromFile(file.GetStream(), const_cast<char *>(""), cmode, 1080 nullptr)); 1081 #endif 1082 } 1083 1084 1085 FileUP PythonFile::GetUnderlyingFile() const { 1086 if (!IsValid()) 1087 return nullptr; 1088 1089 // We don't own the file descriptor returned by this function, make sure the 1090 // File object knows about that. 1091 PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>(); 1092 auto options = File::GetOptionsFromMode(py_mode.GetString()); 1093 auto file = std::unique_ptr<File>( 1094 new NativeFile(PyObject_AsFileDescriptor(m_py_obj), options, false)); 1095 if (!file->IsValid()) 1096 return nullptr; 1097 return file; 1098 } 1099 1100 namespace { 1101 class GIL { 1102 public: 1103 GIL() { 1104 m_state = PyGILState_Ensure(); 1105 assert(!PyErr_Occurred()); 1106 } 1107 ~GIL() { PyGILState_Release(m_state); } 1108 1109 protected: 1110 PyGILState_STATE m_state; 1111 }; 1112 } // namespace 1113 1114 const char *PythonException::toCString() const { 1115 if (!m_repr_bytes) 1116 return "unknown exception"; 1117 return PyBytes_AS_STRING(m_repr_bytes); 1118 } 1119 1120 PythonException::PythonException(const char *caller) { 1121 assert(PyErr_Occurred()); 1122 m_exception_type = m_exception = m_traceback = m_repr_bytes = NULL; 1123 PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback); 1124 PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback); 1125 PyErr_Clear(); 1126 if (m_exception) { 1127 PyObject *repr = PyObject_Repr(m_exception); 1128 if (repr) { 1129 m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr); 1130 if (!m_repr_bytes) { 1131 PyErr_Clear(); 1132 } 1133 Py_XDECREF(repr); 1134 } else { 1135 PyErr_Clear(); 1136 } 1137 } 1138 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT); 1139 if (caller) 1140 LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString()); 1141 else 1142 LLDB_LOGF(log, "python exception: %s", toCString()); 1143 } 1144 void PythonException::Restore() { 1145 if (m_exception_type && m_exception) { 1146 PyErr_Restore(m_exception_type, m_exception, m_traceback); 1147 } else { 1148 PyErr_SetString(PyExc_Exception, toCString()); 1149 } 1150 m_exception_type = m_exception = m_traceback = NULL; 1151 } 1152 1153 PythonException::~PythonException() { 1154 Py_XDECREF(m_exception_type); 1155 Py_XDECREF(m_exception); 1156 Py_XDECREF(m_traceback); 1157 Py_XDECREF(m_repr_bytes); 1158 } 1159 1160 void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); } 1161 1162 std::error_code PythonException::convertToErrorCode() const { 1163 return llvm::inconvertibleErrorCode(); 1164 } 1165 1166 char PythonException::ID = 0; 1167 1168 llvm::Expected<uint32_t> GetOptionsForPyObject(const PythonObject &obj) { 1169 uint32_t options = 0; 1170 #if PY_MAJOR_VERSION >= 3 1171 auto readable = As<bool>(obj.CallMethod("readable")); 1172 if (!readable) 1173 return readable.takeError(); 1174 auto writable = As<bool>(obj.CallMethod("writable")); 1175 if (!writable) 1176 return writable.takeError(); 1177 if (readable.get()) 1178 options |= File::eOpenOptionRead; 1179 if (writable.get()) 1180 options |= File::eOpenOptionWrite; 1181 #else 1182 PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>(); 1183 options = File::GetOptionsFromMode(py_mode.GetString()); 1184 #endif 1185 return options; 1186 } 1187 1188 // Base class template for python files. All it knows how to do 1189 // is hold a reference to the python object and close or flush it 1190 // when the File is closed. 1191 namespace { 1192 template <typename Base> class OwnedPythonFile : public Base { 1193 public: 1194 template <typename... Args> 1195 OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args) 1196 : Base(args...), m_py_obj(file), m_borrowed(borrowed) { 1197 assert(m_py_obj); 1198 } 1199 1200 ~OwnedPythonFile() override { 1201 assert(m_py_obj); 1202 GIL takeGIL; 1203 Close(); 1204 m_py_obj.Reset(); 1205 } 1206 1207 bool IsPythonSideValid() const { 1208 GIL takeGIL; 1209 auto closed = As<bool>(m_py_obj.GetAttribute("closed")); 1210 if (!closed) { 1211 llvm::consumeError(closed.takeError()); 1212 return false; 1213 } 1214 return !closed.get(); 1215 } 1216 1217 bool IsValid() const override { 1218 return IsPythonSideValid() && Base::IsValid(); 1219 } 1220 1221 Status Close() override { 1222 assert(m_py_obj); 1223 Status py_error, base_error; 1224 GIL takeGIL; 1225 if (!m_borrowed) { 1226 auto r = m_py_obj.CallMethod("close"); 1227 if (!r) 1228 py_error = Status(r.takeError()); 1229 } 1230 base_error = Base::Close(); 1231 if (py_error.Fail()) 1232 return py_error; 1233 return base_error; 1234 }; 1235 1236 protected: 1237 PythonFile m_py_obj; 1238 bool m_borrowed; 1239 }; 1240 } // namespace 1241 1242 // A SimplePythonFile is a OwnedPythonFile that just does all I/O as 1243 // a NativeFile 1244 namespace { 1245 class SimplePythonFile : public OwnedPythonFile<NativeFile> { 1246 public: 1247 SimplePythonFile(const PythonFile &file, bool borrowed, int fd, 1248 uint32_t options) 1249 : OwnedPythonFile(file, borrowed, fd, options, false) {} 1250 }; 1251 } // namespace 1252 1253 #if PY_MAJOR_VERSION >= 3 1254 1255 namespace { 1256 class PythonBuffer { 1257 public: 1258 PythonBuffer &operator=(const PythonBuffer &) = delete; 1259 PythonBuffer(const PythonBuffer &) = delete; 1260 1261 static Expected<PythonBuffer> Create(PythonObject &obj, 1262 int flags = PyBUF_SIMPLE) { 1263 Py_buffer py_buffer = {}; 1264 PyObject_GetBuffer(obj.get(), &py_buffer, flags); 1265 if (!py_buffer.obj) 1266 return llvm::make_error<PythonException>(); 1267 return PythonBuffer(py_buffer); 1268 } 1269 1270 PythonBuffer(PythonBuffer &&other) { 1271 m_buffer = other.m_buffer; 1272 other.m_buffer.obj = nullptr; 1273 } 1274 1275 ~PythonBuffer() { 1276 if (m_buffer.obj) 1277 PyBuffer_Release(&m_buffer); 1278 } 1279 1280 Py_buffer &get() { return m_buffer; } 1281 1282 private: 1283 // takes ownership of the buffer. 1284 PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {} 1285 Py_buffer m_buffer; 1286 }; 1287 } // namespace 1288 1289 // Shared methods between TextPythonFile and BinaryPythonFile 1290 namespace { 1291 class PythonIOFile : public OwnedPythonFile<File> { 1292 public: 1293 PythonIOFile(const PythonFile &file, bool borrowed) 1294 : OwnedPythonFile(file, borrowed) {} 1295 1296 ~PythonIOFile() override { Close(); } 1297 1298 bool IsValid() const override { return IsPythonSideValid(); } 1299 1300 Status Close() override { 1301 assert(m_py_obj); 1302 GIL takeGIL; 1303 if (m_borrowed) 1304 return Flush(); 1305 auto r = m_py_obj.CallMethod("close"); 1306 if (!r) 1307 return Status(r.takeError()); 1308 return Status(); 1309 } 1310 1311 Status Flush() override { 1312 GIL takeGIL; 1313 auto r = m_py_obj.CallMethod("flush"); 1314 if (!r) 1315 return Status(r.takeError()); 1316 return Status(); 1317 } 1318 1319 }; 1320 } // namespace 1321 1322 namespace { 1323 class BinaryPythonFile : public PythonIOFile { 1324 protected: 1325 int m_descriptor; 1326 1327 public: 1328 BinaryPythonFile(int fd, const PythonFile &file, bool borrowed) 1329 : PythonIOFile(file, borrowed), 1330 m_descriptor(File::DescriptorIsValid(fd) ? fd 1331 : File::kInvalidDescriptor) {} 1332 1333 int GetDescriptor() const override { return m_descriptor; } 1334 1335 Status Write(const void *buf, size_t &num_bytes) override { 1336 GIL takeGIL; 1337 PyObject *pybuffer_p = PyMemoryView_FromMemory( 1338 const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ); 1339 if (!pybuffer_p) 1340 return Status(llvm::make_error<PythonException>()); 1341 auto pybuffer = Take<PythonObject>(pybuffer_p); 1342 num_bytes = 0; 1343 auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer)); 1344 if (!bytes_written) 1345 return Status(bytes_written.takeError()); 1346 if (bytes_written.get() < 0) 1347 return Status(".write() method returned a negative number!"); 1348 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1349 num_bytes = bytes_written.get(); 1350 return Status(); 1351 } 1352 1353 Status Read(void *buf, size_t &num_bytes) override { 1354 GIL takeGIL; 1355 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1356 auto pybuffer_obj = 1357 m_py_obj.CallMethod("read", (unsigned long long)num_bytes); 1358 if (!pybuffer_obj) 1359 return Status(pybuffer_obj.takeError()); 1360 num_bytes = 0; 1361 if (pybuffer_obj.get().IsNone()) { 1362 // EOF 1363 num_bytes = 0; 1364 return Status(); 1365 } 1366 auto pybuffer = PythonBuffer::Create(pybuffer_obj.get()); 1367 if (!pybuffer) 1368 return Status(pybuffer.takeError()); 1369 memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len); 1370 num_bytes = pybuffer.get().get().len; 1371 return Status(); 1372 } 1373 }; 1374 } // namespace 1375 1376 namespace { 1377 class TextPythonFile : public PythonIOFile { 1378 protected: 1379 int m_descriptor; 1380 1381 public: 1382 TextPythonFile(int fd, const PythonFile &file, bool borrowed) 1383 : PythonIOFile(file, borrowed), 1384 m_descriptor(File::DescriptorIsValid(fd) ? fd 1385 : File::kInvalidDescriptor) {} 1386 1387 int GetDescriptor() const override { return m_descriptor; } 1388 1389 Status Write(const void *buf, size_t &num_bytes) override { 1390 GIL takeGIL; 1391 auto pystring = 1392 PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes)); 1393 if (!pystring) 1394 return Status(pystring.takeError()); 1395 num_bytes = 0; 1396 auto bytes_written = 1397 As<long long>(m_py_obj.CallMethod("write", pystring.get())); 1398 if (!bytes_written) 1399 return Status(bytes_written.takeError()); 1400 if (bytes_written.get() < 0) 1401 return Status(".write() method returned a negative number!"); 1402 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1403 num_bytes = bytes_written.get(); 1404 return Status(); 1405 } 1406 1407 Status Read(void *buf, size_t &num_bytes) override { 1408 GIL takeGIL; 1409 size_t num_chars = num_bytes / 6; 1410 size_t orig_num_bytes = num_bytes; 1411 num_bytes = 0; 1412 if (orig_num_bytes < 6) { 1413 return Status("can't read less than 6 bytes from a utf8 text stream"); 1414 } 1415 auto pystring = As<PythonString>( 1416 m_py_obj.CallMethod("read", (unsigned long long)num_chars)); 1417 if (!pystring) 1418 return Status(pystring.takeError()); 1419 if (pystring.get().IsNone()) { 1420 // EOF 1421 return Status(); 1422 } 1423 auto stringref = pystring.get().AsUTF8(); 1424 if (!stringref) 1425 return Status(stringref.takeError()); 1426 num_bytes = stringref.get().size(); 1427 memcpy(buf, stringref.get().begin(), num_bytes); 1428 return Status(); 1429 } 1430 }; 1431 } // namespace 1432 1433 #endif 1434 1435 llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) { 1436 if (!IsValid()) 1437 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1438 "invalid PythonFile"); 1439 1440 int fd = PyObject_AsFileDescriptor(m_py_obj); 1441 if (fd < 0) { 1442 PyErr_Clear(); 1443 return ConvertToFileForcingUseOfScriptingIOMethods(borrowed); 1444 } 1445 auto options = GetOptionsForPyObject(*this); 1446 if (!options) 1447 return options.takeError(); 1448 1449 // LLDB and python will not share I/O buffers. We should probably 1450 // flush the python buffers now. 1451 auto r = CallMethod("flush"); 1452 if (!r) 1453 return r.takeError(); 1454 1455 FileSP file_sp; 1456 if (borrowed) { 1457 // In this case we we don't need to retain the python 1458 // object at all. 1459 file_sp = std::make_shared<NativeFile>(fd, options.get(), false); 1460 } else { 1461 file_sp = std::static_pointer_cast<File>( 1462 std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get())); 1463 } 1464 if (!file_sp->IsValid()) 1465 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1466 "invalid File"); 1467 1468 return file_sp; 1469 } 1470 1471 llvm::Expected<FileSP> 1472 PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) { 1473 1474 assert(!PyErr_Occurred()); 1475 1476 if (!IsValid()) 1477 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1478 "invalid PythonFile"); 1479 1480 #if PY_MAJOR_VERSION < 3 1481 1482 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1483 "not supported on python 2"); 1484 1485 #else 1486 1487 int fd = PyObject_AsFileDescriptor(m_py_obj); 1488 if (fd < 0) { 1489 PyErr_Clear(); 1490 fd = File::kInvalidDescriptor; 1491 } 1492 1493 auto io_module = PythonModule::Import("io"); 1494 if (!io_module) 1495 return io_module.takeError(); 1496 auto textIOBase = io_module.get().Get("TextIOBase"); 1497 if (!textIOBase) 1498 return textIOBase.takeError(); 1499 auto rawIOBase = io_module.get().Get("RawIOBase"); 1500 if (!rawIOBase) 1501 return rawIOBase.takeError(); 1502 auto bufferedIOBase = io_module.get().Get("BufferedIOBase"); 1503 if (!bufferedIOBase) 1504 return bufferedIOBase.takeError(); 1505 1506 FileSP file_sp; 1507 1508 auto isTextIO = IsInstance(textIOBase.get()); 1509 if (!isTextIO) 1510 return isTextIO.takeError(); 1511 if (isTextIO.get()) 1512 file_sp = std::static_pointer_cast<File>( 1513 std::make_shared<TextPythonFile>(fd, *this, borrowed)); 1514 1515 auto isRawIO = IsInstance(rawIOBase.get()); 1516 if (!isRawIO) 1517 return isRawIO.takeError(); 1518 auto isBufferedIO = IsInstance(bufferedIOBase.get()); 1519 if (!isBufferedIO) 1520 return isBufferedIO.takeError(); 1521 1522 if (isRawIO.get() || isBufferedIO.get()) { 1523 file_sp = std::static_pointer_cast<File>( 1524 std::make_shared<BinaryPythonFile>(fd, *this, borrowed)); 1525 } 1526 1527 if (!file_sp) 1528 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1529 "python file is neither text nor binary"); 1530 1531 if (!file_sp->IsValid()) 1532 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1533 "invalid File"); 1534 1535 return file_sp; 1536 1537 #endif 1538 } 1539 1540 #endif 1541