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 result.is_bound_method = 880 cantFail(As<bool>(pyarginfo.get().GetAttribute("is_bound_method"))); 881 882 // FIXME emulate old broken behavior 883 if (result.is_bound_method) 884 result.count++; 885 886 #else 887 888 PyObject *py_func_obj = m_py_obj; 889 if (PyMethod_Check(py_func_obj)) { 890 py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); 891 PythonObject im_self = GetAttributeValue("im_self"); 892 if (im_self.IsValid() && !im_self.IsNone()) 893 result.is_bound_method = true; 894 } else { 895 // see if this is a callable object with an __call__ method 896 if (!PyFunction_Check(py_func_obj)) { 897 PythonObject __call__ = GetAttributeValue("__call__"); 898 if (__call__.IsValid()) { 899 auto __callable__ = __call__.AsType<PythonCallable>(); 900 if (__callable__.IsValid()) { 901 py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); 902 PythonObject im_self = GetAttributeValue("im_self"); 903 if (im_self.IsValid() && !im_self.IsNone()) 904 result.is_bound_method = true; 905 } 906 } 907 } 908 } 909 910 if (!py_func_obj) 911 return result; 912 913 PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj); 914 if (!code) 915 return result; 916 917 result.count = code->co_argcount; 918 result.has_varargs = !!(code->co_flags & CO_VARARGS); 919 920 #endif 921 922 return result; 923 } 924 925 PythonCallable::ArgInfo PythonCallable::GetNumArguments() const { 926 auto arginfo = GetArgInfo(); 927 if (!arginfo) { 928 llvm::consumeError(arginfo.takeError()); 929 return ArgInfo{}; 930 } 931 return arginfo.get(); 932 } 933 934 PythonObject PythonCallable::operator()() { 935 return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr)); 936 } 937 938 PythonObject PythonCallable:: 939 operator()(std::initializer_list<PyObject *> args) { 940 PythonTuple arg_tuple(args); 941 return PythonObject(PyRefType::Owned, 942 PyObject_CallObject(m_py_obj, arg_tuple.get())); 943 } 944 945 PythonObject PythonCallable:: 946 operator()(std::initializer_list<PythonObject> args) { 947 PythonTuple arg_tuple(args); 948 return PythonObject(PyRefType::Owned, 949 PyObject_CallObject(m_py_obj, arg_tuple.get())); 950 } 951 952 bool PythonFile::Check(PyObject *py_obj) { 953 if (!py_obj) 954 return false; 955 #if PY_MAJOR_VERSION < 3 956 return PyFile_Check(py_obj); 957 #else 958 // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a 959 // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper 960 // over `io.open()`, which returns some object derived from `io.IOBase`. As a 961 // result, the only way to detect a file in Python 3 is to check whether it 962 // inherits from `io.IOBase`. 963 auto io_module = PythonModule::Import("io"); 964 if (!io_module) { 965 llvm::consumeError(io_module.takeError()); 966 return false; 967 } 968 auto iobase = io_module.get().Get("IOBase"); 969 if (!iobase) { 970 llvm::consumeError(iobase.takeError()); 971 return false; 972 } 973 int r = PyObject_IsInstance(py_obj, iobase.get().get()); 974 if (r < 0) { 975 llvm::consumeError(exception()); // clear the exception and log it. 976 return false; 977 } 978 return !!r; 979 #endif 980 } 981 982 namespace { 983 class GIL { 984 public: 985 GIL() { 986 m_state = PyGILState_Ensure(); 987 assert(!PyErr_Occurred()); 988 } 989 ~GIL() { PyGILState_Release(m_state); } 990 991 protected: 992 PyGILState_STATE m_state; 993 }; 994 } // namespace 995 996 const char *PythonException::toCString() const { 997 if (!m_repr_bytes) 998 return "unknown exception"; 999 return PyBytes_AS_STRING(m_repr_bytes); 1000 } 1001 1002 PythonException::PythonException(const char *caller) { 1003 assert(PyErr_Occurred()); 1004 m_exception_type = m_exception = m_traceback = m_repr_bytes = NULL; 1005 PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback); 1006 PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback); 1007 PyErr_Clear(); 1008 if (m_exception) { 1009 PyObject *repr = PyObject_Repr(m_exception); 1010 if (repr) { 1011 m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr); 1012 if (!m_repr_bytes) { 1013 PyErr_Clear(); 1014 } 1015 Py_XDECREF(repr); 1016 } else { 1017 PyErr_Clear(); 1018 } 1019 } 1020 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT); 1021 if (caller) 1022 LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString()); 1023 else 1024 LLDB_LOGF(log, "python exception: %s", toCString()); 1025 } 1026 void PythonException::Restore() { 1027 if (m_exception_type && m_exception) { 1028 PyErr_Restore(m_exception_type, m_exception, m_traceback); 1029 } else { 1030 PyErr_SetString(PyExc_Exception, toCString()); 1031 } 1032 m_exception_type = m_exception = m_traceback = NULL; 1033 } 1034 1035 PythonException::~PythonException() { 1036 Py_XDECREF(m_exception_type); 1037 Py_XDECREF(m_exception); 1038 Py_XDECREF(m_traceback); 1039 Py_XDECREF(m_repr_bytes); 1040 } 1041 1042 void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); } 1043 1044 std::error_code PythonException::convertToErrorCode() const { 1045 return llvm::inconvertibleErrorCode(); 1046 } 1047 1048 char PythonException::ID = 0; 1049 1050 llvm::Expected<File::OpenOptions> 1051 GetOptionsForPyObject(const PythonObject &obj) { 1052 #if PY_MAJOR_VERSION >= 3 1053 auto options = File::OpenOptions(0); 1054 auto readable = As<bool>(obj.CallMethod("readable")); 1055 if (!readable) 1056 return readable.takeError(); 1057 auto writable = As<bool>(obj.CallMethod("writable")); 1058 if (!writable) 1059 return writable.takeError(); 1060 if (readable.get()) 1061 options |= File::eOpenOptionRead; 1062 if (writable.get()) 1063 options |= File::eOpenOptionWrite; 1064 return options; 1065 #else 1066 PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>(); 1067 return File::GetOptionsFromMode(py_mode.GetString()); 1068 #endif 1069 } 1070 1071 // Base class template for python files. All it knows how to do 1072 // is hold a reference to the python object and close or flush it 1073 // when the File is closed. 1074 namespace { 1075 template <typename Base> class OwnedPythonFile : public Base { 1076 public: 1077 template <typename... Args> 1078 OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args) 1079 : Base(args...), m_py_obj(file), m_borrowed(borrowed) { 1080 assert(m_py_obj); 1081 } 1082 1083 ~OwnedPythonFile() override { 1084 assert(m_py_obj); 1085 GIL takeGIL; 1086 Close(); 1087 m_py_obj.Reset(); 1088 } 1089 1090 bool IsPythonSideValid() const { 1091 GIL takeGIL; 1092 auto closed = As<bool>(m_py_obj.GetAttribute("closed")); 1093 if (!closed) { 1094 llvm::consumeError(closed.takeError()); 1095 return false; 1096 } 1097 return !closed.get(); 1098 } 1099 1100 bool IsValid() const override { 1101 return IsPythonSideValid() && Base::IsValid(); 1102 } 1103 1104 Status Close() override { 1105 assert(m_py_obj); 1106 Status py_error, base_error; 1107 GIL takeGIL; 1108 if (!m_borrowed) { 1109 auto r = m_py_obj.CallMethod("close"); 1110 if (!r) 1111 py_error = Status(r.takeError()); 1112 } 1113 base_error = Base::Close(); 1114 if (py_error.Fail()) 1115 return py_error; 1116 return base_error; 1117 }; 1118 1119 PyObject *GetPythonObject() const { 1120 assert(m_py_obj.IsValid()); 1121 return m_py_obj.get(); 1122 } 1123 1124 static bool classof(const File *file) = delete; 1125 1126 protected: 1127 PythonFile m_py_obj; 1128 bool m_borrowed; 1129 }; 1130 } // namespace 1131 1132 // A SimplePythonFile is a OwnedPythonFile that just does all I/O as 1133 // a NativeFile 1134 namespace { 1135 class SimplePythonFile : public OwnedPythonFile<NativeFile> { 1136 public: 1137 SimplePythonFile(const PythonFile &file, bool borrowed, int fd, 1138 File::OpenOptions options) 1139 : OwnedPythonFile(file, borrowed, fd, options, false) {} 1140 1141 static char ID; 1142 bool isA(const void *classID) const override { 1143 return classID == &ID || NativeFile::isA(classID); 1144 } 1145 static bool classof(const File *file) { return file->isA(&ID); } 1146 }; 1147 char SimplePythonFile::ID = 0; 1148 } // namespace 1149 1150 #if PY_MAJOR_VERSION >= 3 1151 1152 namespace { 1153 class PythonBuffer { 1154 public: 1155 PythonBuffer &operator=(const PythonBuffer &) = delete; 1156 PythonBuffer(const PythonBuffer &) = delete; 1157 1158 static Expected<PythonBuffer> Create(PythonObject &obj, 1159 int flags = PyBUF_SIMPLE) { 1160 Py_buffer py_buffer = {}; 1161 PyObject_GetBuffer(obj.get(), &py_buffer, flags); 1162 if (!py_buffer.obj) 1163 return llvm::make_error<PythonException>(); 1164 return PythonBuffer(py_buffer); 1165 } 1166 1167 PythonBuffer(PythonBuffer &&other) { 1168 m_buffer = other.m_buffer; 1169 other.m_buffer.obj = nullptr; 1170 } 1171 1172 ~PythonBuffer() { 1173 if (m_buffer.obj) 1174 PyBuffer_Release(&m_buffer); 1175 } 1176 1177 Py_buffer &get() { return m_buffer; } 1178 1179 private: 1180 // takes ownership of the buffer. 1181 PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {} 1182 Py_buffer m_buffer; 1183 }; 1184 } // namespace 1185 1186 // Shared methods between TextPythonFile and BinaryPythonFile 1187 namespace { 1188 class PythonIOFile : public OwnedPythonFile<File> { 1189 public: 1190 PythonIOFile(const PythonFile &file, bool borrowed) 1191 : OwnedPythonFile(file, borrowed) {} 1192 1193 ~PythonIOFile() override { Close(); } 1194 1195 bool IsValid() const override { return IsPythonSideValid(); } 1196 1197 Status Close() override { 1198 assert(m_py_obj); 1199 GIL takeGIL; 1200 if (m_borrowed) 1201 return Flush(); 1202 auto r = m_py_obj.CallMethod("close"); 1203 if (!r) 1204 return Status(r.takeError()); 1205 return Status(); 1206 } 1207 1208 Status Flush() override { 1209 GIL takeGIL; 1210 auto r = m_py_obj.CallMethod("flush"); 1211 if (!r) 1212 return Status(r.takeError()); 1213 return Status(); 1214 } 1215 1216 Expected<File::OpenOptions> GetOptions() const override { 1217 GIL takeGIL; 1218 return GetOptionsForPyObject(m_py_obj); 1219 } 1220 1221 static char ID; 1222 bool isA(const void *classID) const override { 1223 return classID == &ID || File::isA(classID); 1224 } 1225 static bool classof(const File *file) { return file->isA(&ID); } 1226 }; 1227 char PythonIOFile::ID = 0; 1228 } // namespace 1229 1230 namespace { 1231 class BinaryPythonFile : public PythonIOFile { 1232 protected: 1233 int m_descriptor; 1234 1235 public: 1236 BinaryPythonFile(int fd, const PythonFile &file, bool borrowed) 1237 : PythonIOFile(file, borrowed), 1238 m_descriptor(File::DescriptorIsValid(fd) ? fd 1239 : File::kInvalidDescriptor) {} 1240 1241 int GetDescriptor() const override { return m_descriptor; } 1242 1243 Status Write(const void *buf, size_t &num_bytes) override { 1244 GIL takeGIL; 1245 PyObject *pybuffer_p = PyMemoryView_FromMemory( 1246 const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ); 1247 if (!pybuffer_p) 1248 return Status(llvm::make_error<PythonException>()); 1249 auto pybuffer = Take<PythonObject>(pybuffer_p); 1250 num_bytes = 0; 1251 auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer)); 1252 if (!bytes_written) 1253 return Status(bytes_written.takeError()); 1254 if (bytes_written.get() < 0) 1255 return Status(".write() method returned a negative number!"); 1256 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1257 num_bytes = bytes_written.get(); 1258 return Status(); 1259 } 1260 1261 Status Read(void *buf, size_t &num_bytes) override { 1262 GIL takeGIL; 1263 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1264 auto pybuffer_obj = 1265 m_py_obj.CallMethod("read", (unsigned long long)num_bytes); 1266 if (!pybuffer_obj) 1267 return Status(pybuffer_obj.takeError()); 1268 num_bytes = 0; 1269 if (pybuffer_obj.get().IsNone()) { 1270 // EOF 1271 num_bytes = 0; 1272 return Status(); 1273 } 1274 auto pybuffer = PythonBuffer::Create(pybuffer_obj.get()); 1275 if (!pybuffer) 1276 return Status(pybuffer.takeError()); 1277 memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len); 1278 num_bytes = pybuffer.get().get().len; 1279 return Status(); 1280 } 1281 }; 1282 } // namespace 1283 1284 namespace { 1285 class TextPythonFile : public PythonIOFile { 1286 protected: 1287 int m_descriptor; 1288 1289 public: 1290 TextPythonFile(int fd, const PythonFile &file, bool borrowed) 1291 : PythonIOFile(file, borrowed), 1292 m_descriptor(File::DescriptorIsValid(fd) ? fd 1293 : File::kInvalidDescriptor) {} 1294 1295 int GetDescriptor() const override { return m_descriptor; } 1296 1297 Status Write(const void *buf, size_t &num_bytes) override { 1298 GIL takeGIL; 1299 auto pystring = 1300 PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes)); 1301 if (!pystring) 1302 return Status(pystring.takeError()); 1303 num_bytes = 0; 1304 auto bytes_written = 1305 As<long long>(m_py_obj.CallMethod("write", pystring.get())); 1306 if (!bytes_written) 1307 return Status(bytes_written.takeError()); 1308 if (bytes_written.get() < 0) 1309 return Status(".write() method returned a negative number!"); 1310 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1311 num_bytes = bytes_written.get(); 1312 return Status(); 1313 } 1314 1315 Status Read(void *buf, size_t &num_bytes) override { 1316 GIL takeGIL; 1317 size_t num_chars = num_bytes / 6; 1318 size_t orig_num_bytes = num_bytes; 1319 num_bytes = 0; 1320 if (orig_num_bytes < 6) { 1321 return Status("can't read less than 6 bytes from a utf8 text stream"); 1322 } 1323 auto pystring = As<PythonString>( 1324 m_py_obj.CallMethod("read", (unsigned long long)num_chars)); 1325 if (!pystring) 1326 return Status(pystring.takeError()); 1327 if (pystring.get().IsNone()) { 1328 // EOF 1329 return Status(); 1330 } 1331 auto stringref = pystring.get().AsUTF8(); 1332 if (!stringref) 1333 return Status(stringref.takeError()); 1334 num_bytes = stringref.get().size(); 1335 memcpy(buf, stringref.get().begin(), num_bytes); 1336 return Status(); 1337 } 1338 }; 1339 } // namespace 1340 1341 #endif 1342 1343 llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) { 1344 if (!IsValid()) 1345 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1346 "invalid PythonFile"); 1347 1348 int fd = PyObject_AsFileDescriptor(m_py_obj); 1349 if (fd < 0) { 1350 PyErr_Clear(); 1351 return ConvertToFileForcingUseOfScriptingIOMethods(borrowed); 1352 } 1353 auto options = GetOptionsForPyObject(*this); 1354 if (!options) 1355 return options.takeError(); 1356 1357 // LLDB and python will not share I/O buffers. We should probably 1358 // flush the python buffers now. 1359 auto r = CallMethod("flush"); 1360 if (!r) 1361 return r.takeError(); 1362 1363 FileSP file_sp; 1364 if (borrowed) { 1365 // In this case we we don't need to retain the python 1366 // object at all. 1367 file_sp = std::make_shared<NativeFile>(fd, options.get(), false); 1368 } else { 1369 file_sp = std::static_pointer_cast<File>( 1370 std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get())); 1371 } 1372 if (!file_sp->IsValid()) 1373 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1374 "invalid File"); 1375 1376 return file_sp; 1377 } 1378 1379 llvm::Expected<FileSP> 1380 PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) { 1381 1382 assert(!PyErr_Occurred()); 1383 1384 if (!IsValid()) 1385 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1386 "invalid PythonFile"); 1387 1388 #if PY_MAJOR_VERSION < 3 1389 1390 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1391 "not supported on python 2"); 1392 1393 #else 1394 1395 int fd = PyObject_AsFileDescriptor(m_py_obj); 1396 if (fd < 0) { 1397 PyErr_Clear(); 1398 fd = File::kInvalidDescriptor; 1399 } 1400 1401 auto io_module = PythonModule::Import("io"); 1402 if (!io_module) 1403 return io_module.takeError(); 1404 auto textIOBase = io_module.get().Get("TextIOBase"); 1405 if (!textIOBase) 1406 return textIOBase.takeError(); 1407 auto rawIOBase = io_module.get().Get("RawIOBase"); 1408 if (!rawIOBase) 1409 return rawIOBase.takeError(); 1410 auto bufferedIOBase = io_module.get().Get("BufferedIOBase"); 1411 if (!bufferedIOBase) 1412 return bufferedIOBase.takeError(); 1413 1414 FileSP file_sp; 1415 1416 auto isTextIO = IsInstance(textIOBase.get()); 1417 if (!isTextIO) 1418 return isTextIO.takeError(); 1419 if (isTextIO.get()) 1420 file_sp = std::static_pointer_cast<File>( 1421 std::make_shared<TextPythonFile>(fd, *this, borrowed)); 1422 1423 auto isRawIO = IsInstance(rawIOBase.get()); 1424 if (!isRawIO) 1425 return isRawIO.takeError(); 1426 auto isBufferedIO = IsInstance(bufferedIOBase.get()); 1427 if (!isBufferedIO) 1428 return isBufferedIO.takeError(); 1429 1430 if (isRawIO.get() || isBufferedIO.get()) { 1431 file_sp = std::static_pointer_cast<File>( 1432 std::make_shared<BinaryPythonFile>(fd, *this, borrowed)); 1433 } 1434 1435 if (!file_sp) 1436 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1437 "python file is neither text nor binary"); 1438 1439 if (!file_sp->IsValid()) 1440 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1441 "invalid File"); 1442 1443 return file_sp; 1444 1445 #endif 1446 } 1447 1448 Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) { 1449 if (!file.IsValid()) 1450 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1451 "invalid file"); 1452 1453 if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file)) 1454 return Retain<PythonFile>(simple->GetPythonObject()); 1455 #if PY_MAJOR_VERSION >= 3 1456 if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file)) 1457 return Retain<PythonFile>(pythonio->GetPythonObject()); 1458 #endif 1459 1460 if (!mode) { 1461 auto m = file.GetOpenMode(); 1462 if (!m) 1463 return m.takeError(); 1464 mode = m.get(); 1465 } 1466 1467 PyObject *file_obj; 1468 #if PY_MAJOR_VERSION >= 3 1469 file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr, 1470 "ignore", nullptr, 0); 1471 #else 1472 // Read through the Python source, doesn't seem to modify these strings 1473 char *cmode = const_cast<char *>(mode); 1474 // We pass ::flush instead of ::fclose here so we borrow the FILE* -- 1475 // the lldb_private::File still owns it. 1476 file_obj = 1477 PyFile_FromFile(file.GetStream(), const_cast<char *>(""), cmode, ::fflush); 1478 #endif 1479 1480 if (!file_obj) 1481 return exception(); 1482 1483 return Take<PythonFile>(file_obj); 1484 } 1485 1486 #endif 1487