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