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