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 using llvm::Twine; 38 39 template <> Expected<bool> python::As<bool>(Expected<PythonObject> &&obj) { 40 if (!obj) 41 return obj.takeError(); 42 return obj.get().IsTrue(); 43 } 44 45 template <> 46 Expected<long long> python::As<long long>(Expected<PythonObject> &&obj) { 47 if (!obj) 48 return obj.takeError(); 49 return obj.get().AsLongLong(); 50 } 51 52 template <> 53 Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) { 54 if (!obj) 55 return obj.takeError(); 56 PyObject *str_obj = PyObject_Str(obj.get().get()); 57 if (!obj) 58 return llvm::make_error<PythonException>(); 59 auto str = Take<PythonString>(str_obj); 60 auto utf8 = str.AsUTF8(); 61 if (!utf8) 62 return utf8.takeError(); 63 return utf8.get(); 64 } 65 66 void StructuredPythonObject::Serialize(llvm::json::OStream &s) const { 67 s.value(llvm::formatv("Python Obj: {0:X}", GetValue()).str()); 68 } 69 70 // PythonObject 71 72 void PythonObject::Dump(Stream &strm) const { 73 if (m_py_obj) { 74 FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile); 75 if (file) { 76 ::PyObject_Print(m_py_obj, file, 0); 77 const long length = ftell(file); 78 if (length) { 79 ::rewind(file); 80 std::vector<char> file_contents(length, '\0'); 81 const size_t length_read = 82 ::fread(file_contents.data(), 1, file_contents.size(), file); 83 if (length_read > 0) 84 strm.Write(file_contents.data(), length_read); 85 } 86 ::fclose(file); 87 } 88 } else 89 strm.PutCString("NULL"); 90 } 91 92 PyObjectType PythonObject::GetObjectType() const { 93 if (!IsAllocated()) 94 return PyObjectType::None; 95 96 if (PythonModule::Check(m_py_obj)) 97 return PyObjectType::Module; 98 if (PythonList::Check(m_py_obj)) 99 return PyObjectType::List; 100 if (PythonTuple::Check(m_py_obj)) 101 return PyObjectType::Tuple; 102 if (PythonDictionary::Check(m_py_obj)) 103 return PyObjectType::Dictionary; 104 if (PythonString::Check(m_py_obj)) 105 return PyObjectType::String; 106 #if PY_MAJOR_VERSION >= 3 107 if (PythonBytes::Check(m_py_obj)) 108 return PyObjectType::Bytes; 109 #endif 110 if (PythonByteArray::Check(m_py_obj)) 111 return PyObjectType::ByteArray; 112 if (PythonBoolean::Check(m_py_obj)) 113 return PyObjectType::Boolean; 114 if (PythonInteger::Check(m_py_obj)) 115 return PyObjectType::Integer; 116 if (PythonFile::Check(m_py_obj)) 117 return PyObjectType::File; 118 if (PythonCallable::Check(m_py_obj)) 119 return PyObjectType::Callable; 120 return PyObjectType::Unknown; 121 } 122 123 PythonString PythonObject::Repr() const { 124 if (!m_py_obj) 125 return PythonString(); 126 PyObject *repr = PyObject_Repr(m_py_obj); 127 if (!repr) 128 return PythonString(); 129 return PythonString(PyRefType::Owned, repr); 130 } 131 132 PythonString PythonObject::Str() const { 133 if (!m_py_obj) 134 return PythonString(); 135 PyObject *str = PyObject_Str(m_py_obj); 136 if (!str) 137 return PythonString(); 138 return PythonString(PyRefType::Owned, str); 139 } 140 141 PythonObject 142 PythonObject::ResolveNameWithDictionary(llvm::StringRef name, 143 const PythonDictionary &dict) { 144 size_t dot_pos = name.find('.'); 145 llvm::StringRef piece = name.substr(0, dot_pos); 146 PythonObject result = dict.GetItemForKey(PythonString(piece)); 147 if (dot_pos == llvm::StringRef::npos) { 148 // There was no dot, we're done. 149 return result; 150 } 151 152 // There was a dot. The remaining portion of the name should be looked up in 153 // the context of the object that was found in the dictionary. 154 return result.ResolveName(name.substr(dot_pos + 1)); 155 } 156 157 PythonObject PythonObject::ResolveName(llvm::StringRef name) const { 158 // Resolve the name in the context of the specified object. If, for example, 159 // `this` refers to a PyModule, then this will look for `name` in this 160 // module. If `this` refers to a PyType, then it will resolve `name` as an 161 // attribute of that type. If `this` refers to an instance of an object, 162 // then it will resolve `name` as the value of the specified field. 163 // 164 // This function handles dotted names so that, for example, if `m_py_obj` 165 // refers to the `sys` module, and `name` == "path.append", then it will find 166 // the function `sys.path.append`. 167 168 size_t dot_pos = name.find('.'); 169 if (dot_pos == llvm::StringRef::npos) { 170 // No dots in the name, we should be able to find the value immediately as 171 // an attribute of `m_py_obj`. 172 return GetAttributeValue(name); 173 } 174 175 // Look up the first piece of the name, and resolve the rest as a child of 176 // that. 177 PythonObject parent = ResolveName(name.substr(0, dot_pos)); 178 if (!parent.IsAllocated()) 179 return PythonObject(); 180 181 // Tail recursion.. should be optimized by the compiler 182 return parent.ResolveName(name.substr(dot_pos + 1)); 183 } 184 185 bool PythonObject::HasAttribute(llvm::StringRef attr) const { 186 if (!IsValid()) 187 return false; 188 PythonString py_attr(attr); 189 return !!PyObject_HasAttr(m_py_obj, py_attr.get()); 190 } 191 192 PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const { 193 if (!IsValid()) 194 return PythonObject(); 195 196 PythonString py_attr(attr); 197 if (!PyObject_HasAttr(m_py_obj, py_attr.get())) 198 return PythonObject(); 199 200 return PythonObject(PyRefType::Owned, 201 PyObject_GetAttr(m_py_obj, py_attr.get())); 202 } 203 204 StructuredData::ObjectSP PythonObject::CreateStructuredObject() const { 205 switch (GetObjectType()) { 206 case PyObjectType::Dictionary: 207 return PythonDictionary(PyRefType::Borrowed, m_py_obj) 208 .CreateStructuredDictionary(); 209 case PyObjectType::Boolean: 210 return PythonBoolean(PyRefType::Borrowed, m_py_obj) 211 .CreateStructuredBoolean(); 212 case PyObjectType::Integer: 213 return PythonInteger(PyRefType::Borrowed, m_py_obj) 214 .CreateStructuredInteger(); 215 case PyObjectType::List: 216 return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray(); 217 case PyObjectType::String: 218 return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 219 case PyObjectType::Bytes: 220 return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString(); 221 case PyObjectType::ByteArray: 222 return PythonByteArray(PyRefType::Borrowed, m_py_obj) 223 .CreateStructuredString(); 224 case PyObjectType::None: 225 return StructuredData::ObjectSP(); 226 default: 227 return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj)); 228 } 229 } 230 231 // PythonString 232 233 PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) { SetBytes(bytes); } 234 235 PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) { 236 SetBytes(llvm::ArrayRef<uint8_t>(bytes, length)); 237 } 238 239 bool PythonBytes::Check(PyObject *py_obj) { 240 if (!py_obj) 241 return false; 242 return PyBytes_Check(py_obj); 243 } 244 245 llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const { 246 if (!IsValid()) 247 return llvm::ArrayRef<uint8_t>(); 248 249 Py_ssize_t size; 250 char *c; 251 252 PyBytes_AsStringAndSize(m_py_obj, &c, &size); 253 return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size); 254 } 255 256 size_t PythonBytes::GetSize() const { 257 if (!IsValid()) 258 return 0; 259 return PyBytes_Size(m_py_obj); 260 } 261 262 void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) { 263 const char *data = reinterpret_cast<const char *>(bytes.data()); 264 *this = Take<PythonBytes>(PyBytes_FromStringAndSize(data, bytes.size())); 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 *this = Take<PythonByteArray>(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 *this = Take<PythonInteger>(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 *this = Take<PythonBoolean>(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 *this = Take<PythonList>(PyList_New(0)); 526 } 527 528 PythonList::PythonList(int list_size) { 529 *this = Take<PythonList>(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 *this = Take<PythonTuple>(PyTuple_New(0)); 582 } 583 584 PythonTuple::PythonTuple(int tuple_size) { 585 *this = Take<PythonTuple>(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 *this = Take<PythonDictionary>(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 Twine &key) const { 700 if (!IsValid()) 701 return nullDeref(); 702 PyObject *o = PyDict_GetItemString(m_py_obj, NullTerminated(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 Twine &key, 721 const PythonObject &value) const { 722 if (!IsValid() || !value.IsValid()) 723 return nullDeref(); 724 int r = PyDict_SetItemString(m_py_obj, NullTerminated(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 Twine &name) { 767 PyObject *mod = PyImport_ImportModule(NullTerminated(name)); 768 if (!mod) 769 return exception(); 770 return Take<PythonModule>(mod); 771 } 772 773 Expected<PythonObject> PythonModule::Get(const Twine &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, NullTerminated(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 if (!IsValid()) 794 return PythonDictionary(); 795 return Retain<PythonDictionary>(PyModule_GetDict(m_py_obj)); 796 } 797 798 bool PythonCallable::Check(PyObject *py_obj) { 799 if (!py_obj) 800 return false; 801 802 return PyCallable_Check(py_obj); 803 } 804 805 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 806 static const char get_arg_info_script[] = R"( 807 from inspect import signature, Parameter, ismethod 808 from collections import namedtuple 809 ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs']) 810 def main(f): 811 count = 0 812 varargs = False 813 for parameter in signature(f).parameters.values(): 814 kind = parameter.kind 815 if kind in (Parameter.POSITIONAL_ONLY, 816 Parameter.POSITIONAL_OR_KEYWORD): 817 count += 1 818 elif kind == Parameter.VAR_POSITIONAL: 819 varargs = True 820 elif kind in (Parameter.KEYWORD_ONLY, 821 Parameter.VAR_KEYWORD): 822 pass 823 else: 824 raise Exception(f'unknown parameter kind: {kind}') 825 return ArgInfo(count, varargs) 826 )"; 827 #endif 828 829 Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const { 830 ArgInfo result = {}; 831 if (!IsValid()) 832 return nullDeref(); 833 834 #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 835 836 // no need to synchronize access to this global, we already have the GIL 837 static PythonScript get_arg_info(get_arg_info_script); 838 Expected<PythonObject> pyarginfo = get_arg_info(*this); 839 if (!pyarginfo) 840 return pyarginfo.takeError(); 841 long long count = 842 cantFail(As<long long>(pyarginfo.get().GetAttribute("count"))); 843 bool has_varargs = 844 cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs"))); 845 result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count; 846 847 #else 848 PyObject *py_func_obj; 849 bool is_bound_method = false; 850 bool is_class = false; 851 852 if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) { 853 auto init = GetAttribute("__init__"); 854 if (!init) 855 return init.takeError(); 856 py_func_obj = init.get().get(); 857 is_class = true; 858 } else { 859 py_func_obj = m_py_obj; 860 } 861 862 if (PyMethod_Check(py_func_obj)) { 863 py_func_obj = PyMethod_GET_FUNCTION(py_func_obj); 864 PythonObject im_self = GetAttributeValue("im_self"); 865 if (im_self.IsValid() && !im_self.IsNone()) 866 is_bound_method = true; 867 } else { 868 // see if this is a callable object with an __call__ method 869 if (!PyFunction_Check(py_func_obj)) { 870 PythonObject __call__ = GetAttributeValue("__call__"); 871 if (__call__.IsValid()) { 872 auto __callable__ = __call__.AsType<PythonCallable>(); 873 if (__callable__.IsValid()) { 874 py_func_obj = PyMethod_GET_FUNCTION(__callable__.get()); 875 PythonObject im_self = __callable__.GetAttributeValue("im_self"); 876 if (im_self.IsValid() && !im_self.IsNone()) 877 is_bound_method = true; 878 } 879 } 880 } 881 } 882 883 if (!py_func_obj) 884 return result; 885 886 PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj); 887 if (!code) 888 return result; 889 890 auto count = code->co_argcount; 891 bool has_varargs = !!(code->co_flags & CO_VARARGS); 892 result.max_positional_args = 893 has_varargs ? ArgInfo::UNBOUNDED 894 : (count - (int)is_bound_method) - (int)is_class; 895 896 #endif 897 898 return result; 899 } 900 901 constexpr unsigned 902 PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17 903 904 PythonObject PythonCallable::operator()() { 905 return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr)); 906 } 907 908 PythonObject PythonCallable:: 909 operator()(std::initializer_list<PyObject *> args) { 910 PythonTuple arg_tuple(args); 911 return PythonObject(PyRefType::Owned, 912 PyObject_CallObject(m_py_obj, arg_tuple.get())); 913 } 914 915 PythonObject PythonCallable:: 916 operator()(std::initializer_list<PythonObject> args) { 917 PythonTuple arg_tuple(args); 918 return PythonObject(PyRefType::Owned, 919 PyObject_CallObject(m_py_obj, arg_tuple.get())); 920 } 921 922 bool PythonFile::Check(PyObject *py_obj) { 923 if (!py_obj) 924 return false; 925 #if PY_MAJOR_VERSION < 3 926 return PyFile_Check(py_obj); 927 #else 928 // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a 929 // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper 930 // over `io.open()`, which returns some object derived from `io.IOBase`. As a 931 // result, the only way to detect a file in Python 3 is to check whether it 932 // inherits from `io.IOBase`. 933 auto io_module = PythonModule::Import("io"); 934 if (!io_module) { 935 llvm::consumeError(io_module.takeError()); 936 return false; 937 } 938 auto iobase = io_module.get().Get("IOBase"); 939 if (!iobase) { 940 llvm::consumeError(iobase.takeError()); 941 return false; 942 } 943 int r = PyObject_IsInstance(py_obj, iobase.get().get()); 944 if (r < 0) { 945 llvm::consumeError(exception()); // clear the exception and log it. 946 return false; 947 } 948 return !!r; 949 #endif 950 } 951 952 namespace { 953 class GIL { 954 public: 955 GIL() { 956 m_state = PyGILState_Ensure(); 957 assert(!PyErr_Occurred()); 958 } 959 ~GIL() { PyGILState_Release(m_state); } 960 961 protected: 962 PyGILState_STATE m_state; 963 }; 964 } // namespace 965 966 const char *PythonException::toCString() const { 967 if (!m_repr_bytes) 968 return "unknown exception"; 969 return PyBytes_AS_STRING(m_repr_bytes); 970 } 971 972 PythonException::PythonException(const char *caller) { 973 assert(PyErr_Occurred()); 974 m_exception_type = m_exception = m_traceback = m_repr_bytes = NULL; 975 PyErr_Fetch(&m_exception_type, &m_exception, &m_traceback); 976 PyErr_NormalizeException(&m_exception_type, &m_exception, &m_traceback); 977 PyErr_Clear(); 978 if (m_exception) { 979 PyObject *repr = PyObject_Repr(m_exception); 980 if (repr) { 981 m_repr_bytes = PyUnicode_AsEncodedString(repr, "utf-8", nullptr); 982 if (!m_repr_bytes) { 983 PyErr_Clear(); 984 } 985 Py_XDECREF(repr); 986 } else { 987 PyErr_Clear(); 988 } 989 } 990 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT); 991 if (caller) 992 LLDB_LOGF(log, "%s failed with exception: %s", caller, toCString()); 993 else 994 LLDB_LOGF(log, "python exception: %s", toCString()); 995 } 996 void PythonException::Restore() { 997 if (m_exception_type && m_exception) { 998 PyErr_Restore(m_exception_type, m_exception, m_traceback); 999 } else { 1000 PyErr_SetString(PyExc_Exception, toCString()); 1001 } 1002 m_exception_type = m_exception = m_traceback = NULL; 1003 } 1004 1005 PythonException::~PythonException() { 1006 Py_XDECREF(m_exception_type); 1007 Py_XDECREF(m_exception); 1008 Py_XDECREF(m_traceback); 1009 Py_XDECREF(m_repr_bytes); 1010 } 1011 1012 void PythonException::log(llvm::raw_ostream &OS) const { OS << toCString(); } 1013 1014 std::error_code PythonException::convertToErrorCode() const { 1015 return llvm::inconvertibleErrorCode(); 1016 } 1017 1018 bool PythonException::Matches(PyObject *exc) const { 1019 return PyErr_GivenExceptionMatches(m_exception_type, exc); 1020 } 1021 1022 const char read_exception_script[] = R"( 1023 import sys 1024 from traceback import print_exception 1025 if sys.version_info.major < 3: 1026 from StringIO import StringIO 1027 else: 1028 from io import StringIO 1029 def main(exc_type, exc_value, tb): 1030 f = StringIO() 1031 print_exception(exc_type, exc_value, tb, file=f) 1032 return f.getvalue() 1033 )"; 1034 1035 std::string PythonException::ReadBacktrace() const { 1036 1037 if (!m_traceback) 1038 return toCString(); 1039 1040 // no need to synchronize access to this global, we already have the GIL 1041 static PythonScript read_exception(read_exception_script); 1042 1043 Expected<std::string> backtrace = As<std::string>( 1044 read_exception(m_exception_type, m_exception, m_traceback)); 1045 1046 if (!backtrace) { 1047 std::string message = 1048 std::string(toCString()) + "\n" + 1049 "Traceback unavailble, an error occurred while reading it:\n"; 1050 return (message + llvm::toString(backtrace.takeError())); 1051 } 1052 1053 return std::move(backtrace.get()); 1054 } 1055 1056 char PythonException::ID = 0; 1057 1058 llvm::Expected<File::OpenOptions> 1059 GetOptionsForPyObject(const PythonObject &obj) { 1060 #if PY_MAJOR_VERSION >= 3 1061 auto options = File::OpenOptions(0); 1062 auto readable = As<bool>(obj.CallMethod("readable")); 1063 if (!readable) 1064 return readable.takeError(); 1065 auto writable = As<bool>(obj.CallMethod("writable")); 1066 if (!writable) 1067 return writable.takeError(); 1068 if (readable.get()) 1069 options |= File::eOpenOptionRead; 1070 if (writable.get()) 1071 options |= File::eOpenOptionWrite; 1072 return options; 1073 #else 1074 PythonString py_mode = obj.GetAttributeValue("mode").AsType<PythonString>(); 1075 return File::GetOptionsFromMode(py_mode.GetString()); 1076 #endif 1077 } 1078 1079 // Base class template for python files. All it knows how to do 1080 // is hold a reference to the python object and close or flush it 1081 // when the File is closed. 1082 namespace { 1083 template <typename Base> class OwnedPythonFile : public Base { 1084 public: 1085 template <typename... Args> 1086 OwnedPythonFile(const PythonFile &file, bool borrowed, Args... args) 1087 : Base(args...), m_py_obj(file), m_borrowed(borrowed) { 1088 assert(m_py_obj); 1089 } 1090 1091 ~OwnedPythonFile() override { 1092 assert(m_py_obj); 1093 GIL takeGIL; 1094 Close(); 1095 // we need to ensure the python object is released while we still 1096 // hold the GIL 1097 m_py_obj.Reset(); 1098 } 1099 1100 bool IsPythonSideValid() const { 1101 GIL takeGIL; 1102 auto closed = As<bool>(m_py_obj.GetAttribute("closed")); 1103 if (!closed) { 1104 llvm::consumeError(closed.takeError()); 1105 return false; 1106 } 1107 return !closed.get(); 1108 } 1109 1110 bool IsValid() const override { 1111 return IsPythonSideValid() && Base::IsValid(); 1112 } 1113 1114 Status Close() override { 1115 assert(m_py_obj); 1116 Status py_error, base_error; 1117 GIL takeGIL; 1118 if (!m_borrowed) { 1119 auto r = m_py_obj.CallMethod("close"); 1120 if (!r) 1121 py_error = Status(r.takeError()); 1122 } 1123 base_error = Base::Close(); 1124 if (py_error.Fail()) 1125 return py_error; 1126 return base_error; 1127 }; 1128 1129 PyObject *GetPythonObject() const { 1130 assert(m_py_obj.IsValid()); 1131 return m_py_obj.get(); 1132 } 1133 1134 static bool classof(const File *file) = delete; 1135 1136 protected: 1137 PythonFile m_py_obj; 1138 bool m_borrowed; 1139 }; 1140 } // namespace 1141 1142 // A SimplePythonFile is a OwnedPythonFile that just does all I/O as 1143 // a NativeFile 1144 namespace { 1145 class SimplePythonFile : public OwnedPythonFile<NativeFile> { 1146 public: 1147 SimplePythonFile(const PythonFile &file, bool borrowed, int fd, 1148 File::OpenOptions options) 1149 : OwnedPythonFile(file, borrowed, fd, options, false) {} 1150 1151 static char ID; 1152 bool isA(const void *classID) const override { 1153 return classID == &ID || NativeFile::isA(classID); 1154 } 1155 static bool classof(const File *file) { return file->isA(&ID); } 1156 }; 1157 char SimplePythonFile::ID = 0; 1158 } // namespace 1159 1160 #if PY_MAJOR_VERSION >= 3 1161 1162 namespace { 1163 class PythonBuffer { 1164 public: 1165 PythonBuffer &operator=(const PythonBuffer &) = delete; 1166 PythonBuffer(const PythonBuffer &) = delete; 1167 1168 static Expected<PythonBuffer> Create(PythonObject &obj, 1169 int flags = PyBUF_SIMPLE) { 1170 Py_buffer py_buffer = {}; 1171 PyObject_GetBuffer(obj.get(), &py_buffer, flags); 1172 if (!py_buffer.obj) 1173 return llvm::make_error<PythonException>(); 1174 return PythonBuffer(py_buffer); 1175 } 1176 1177 PythonBuffer(PythonBuffer &&other) { 1178 m_buffer = other.m_buffer; 1179 other.m_buffer.obj = nullptr; 1180 } 1181 1182 ~PythonBuffer() { 1183 if (m_buffer.obj) 1184 PyBuffer_Release(&m_buffer); 1185 } 1186 1187 Py_buffer &get() { return m_buffer; } 1188 1189 private: 1190 // takes ownership of the buffer. 1191 PythonBuffer(const Py_buffer &py_buffer) : m_buffer(py_buffer) {} 1192 Py_buffer m_buffer; 1193 }; 1194 } // namespace 1195 1196 // Shared methods between TextPythonFile and BinaryPythonFile 1197 namespace { 1198 class PythonIOFile : public OwnedPythonFile<File> { 1199 public: 1200 PythonIOFile(const PythonFile &file, bool borrowed) 1201 : OwnedPythonFile(file, borrowed) {} 1202 1203 ~PythonIOFile() override { Close(); } 1204 1205 bool IsValid() const override { return IsPythonSideValid(); } 1206 1207 Status Close() override { 1208 assert(m_py_obj); 1209 GIL takeGIL; 1210 if (m_borrowed) 1211 return Flush(); 1212 auto r = m_py_obj.CallMethod("close"); 1213 if (!r) 1214 return Status(r.takeError()); 1215 return Status(); 1216 } 1217 1218 Status Flush() override { 1219 GIL takeGIL; 1220 auto r = m_py_obj.CallMethod("flush"); 1221 if (!r) 1222 return Status(r.takeError()); 1223 return Status(); 1224 } 1225 1226 Expected<File::OpenOptions> GetOptions() const override { 1227 GIL takeGIL; 1228 return GetOptionsForPyObject(m_py_obj); 1229 } 1230 1231 static char ID; 1232 bool isA(const void *classID) const override { 1233 return classID == &ID || File::isA(classID); 1234 } 1235 static bool classof(const File *file) { return file->isA(&ID); } 1236 }; 1237 char PythonIOFile::ID = 0; 1238 } // namespace 1239 1240 namespace { 1241 class BinaryPythonFile : public PythonIOFile { 1242 protected: 1243 int m_descriptor; 1244 1245 public: 1246 BinaryPythonFile(int fd, const PythonFile &file, bool borrowed) 1247 : PythonIOFile(file, borrowed), 1248 m_descriptor(File::DescriptorIsValid(fd) ? fd 1249 : File::kInvalidDescriptor) {} 1250 1251 int GetDescriptor() const override { return m_descriptor; } 1252 1253 Status Write(const void *buf, size_t &num_bytes) override { 1254 GIL takeGIL; 1255 PyObject *pybuffer_p = PyMemoryView_FromMemory( 1256 const_cast<char *>((const char *)buf), num_bytes, PyBUF_READ); 1257 if (!pybuffer_p) 1258 return Status(llvm::make_error<PythonException>()); 1259 auto pybuffer = Take<PythonObject>(pybuffer_p); 1260 num_bytes = 0; 1261 auto bytes_written = As<long long>(m_py_obj.CallMethod("write", pybuffer)); 1262 if (!bytes_written) 1263 return Status(bytes_written.takeError()); 1264 if (bytes_written.get() < 0) 1265 return Status(".write() method returned a negative number!"); 1266 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1267 num_bytes = bytes_written.get(); 1268 return Status(); 1269 } 1270 1271 Status Read(void *buf, size_t &num_bytes) override { 1272 GIL takeGIL; 1273 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1274 auto pybuffer_obj = 1275 m_py_obj.CallMethod("read", (unsigned long long)num_bytes); 1276 if (!pybuffer_obj) 1277 return Status(pybuffer_obj.takeError()); 1278 num_bytes = 0; 1279 if (pybuffer_obj.get().IsNone()) { 1280 // EOF 1281 num_bytes = 0; 1282 return Status(); 1283 } 1284 auto pybuffer = PythonBuffer::Create(pybuffer_obj.get()); 1285 if (!pybuffer) 1286 return Status(pybuffer.takeError()); 1287 memcpy(buf, pybuffer.get().get().buf, pybuffer.get().get().len); 1288 num_bytes = pybuffer.get().get().len; 1289 return Status(); 1290 } 1291 }; 1292 } // namespace 1293 1294 namespace { 1295 class TextPythonFile : public PythonIOFile { 1296 protected: 1297 int m_descriptor; 1298 1299 public: 1300 TextPythonFile(int fd, const PythonFile &file, bool borrowed) 1301 : PythonIOFile(file, borrowed), 1302 m_descriptor(File::DescriptorIsValid(fd) ? fd 1303 : File::kInvalidDescriptor) {} 1304 1305 int GetDescriptor() const override { return m_descriptor; } 1306 1307 Status Write(const void *buf, size_t &num_bytes) override { 1308 GIL takeGIL; 1309 auto pystring = 1310 PythonString::FromUTF8(llvm::StringRef((const char *)buf, num_bytes)); 1311 if (!pystring) 1312 return Status(pystring.takeError()); 1313 num_bytes = 0; 1314 auto bytes_written = 1315 As<long long>(m_py_obj.CallMethod("write", pystring.get())); 1316 if (!bytes_written) 1317 return Status(bytes_written.takeError()); 1318 if (bytes_written.get() < 0) 1319 return Status(".write() method returned a negative number!"); 1320 static_assert(sizeof(long long) >= sizeof(size_t), "overflow"); 1321 num_bytes = bytes_written.get(); 1322 return Status(); 1323 } 1324 1325 Status Read(void *buf, size_t &num_bytes) override { 1326 GIL takeGIL; 1327 size_t num_chars = num_bytes / 6; 1328 size_t orig_num_bytes = num_bytes; 1329 num_bytes = 0; 1330 if (orig_num_bytes < 6) { 1331 return Status("can't read less than 6 bytes from a utf8 text stream"); 1332 } 1333 auto pystring = As<PythonString>( 1334 m_py_obj.CallMethod("read", (unsigned long long)num_chars)); 1335 if (!pystring) 1336 return Status(pystring.takeError()); 1337 if (pystring.get().IsNone()) { 1338 // EOF 1339 return Status(); 1340 } 1341 auto stringref = pystring.get().AsUTF8(); 1342 if (!stringref) 1343 return Status(stringref.takeError()); 1344 num_bytes = stringref.get().size(); 1345 memcpy(buf, stringref.get().begin(), num_bytes); 1346 return Status(); 1347 } 1348 }; 1349 } // namespace 1350 1351 #endif 1352 1353 llvm::Expected<FileSP> PythonFile::ConvertToFile(bool borrowed) { 1354 if (!IsValid()) 1355 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1356 "invalid PythonFile"); 1357 1358 int fd = PyObject_AsFileDescriptor(m_py_obj); 1359 if (fd < 0) { 1360 PyErr_Clear(); 1361 return ConvertToFileForcingUseOfScriptingIOMethods(borrowed); 1362 } 1363 auto options = GetOptionsForPyObject(*this); 1364 if (!options) 1365 return options.takeError(); 1366 1367 if (options.get() & File::eOpenOptionWrite) { 1368 // LLDB and python will not share I/O buffers. We should probably 1369 // flush the python buffers now. 1370 auto r = CallMethod("flush"); 1371 if (!r) 1372 return r.takeError(); 1373 } 1374 1375 FileSP file_sp; 1376 if (borrowed) { 1377 // In this case we we don't need to retain the python 1378 // object at all. 1379 file_sp = std::make_shared<NativeFile>(fd, options.get(), false); 1380 } else { 1381 file_sp = std::static_pointer_cast<File>( 1382 std::make_shared<SimplePythonFile>(*this, borrowed, fd, options.get())); 1383 } 1384 if (!file_sp->IsValid()) 1385 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1386 "invalid File"); 1387 1388 return file_sp; 1389 } 1390 1391 llvm::Expected<FileSP> 1392 PythonFile::ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed) { 1393 1394 assert(!PyErr_Occurred()); 1395 1396 if (!IsValid()) 1397 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1398 "invalid PythonFile"); 1399 1400 #if PY_MAJOR_VERSION < 3 1401 1402 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1403 "not supported on python 2"); 1404 1405 #else 1406 1407 int fd = PyObject_AsFileDescriptor(m_py_obj); 1408 if (fd < 0) { 1409 PyErr_Clear(); 1410 fd = File::kInvalidDescriptor; 1411 } 1412 1413 auto io_module = PythonModule::Import("io"); 1414 if (!io_module) 1415 return io_module.takeError(); 1416 auto textIOBase = io_module.get().Get("TextIOBase"); 1417 if (!textIOBase) 1418 return textIOBase.takeError(); 1419 auto rawIOBase = io_module.get().Get("RawIOBase"); 1420 if (!rawIOBase) 1421 return rawIOBase.takeError(); 1422 auto bufferedIOBase = io_module.get().Get("BufferedIOBase"); 1423 if (!bufferedIOBase) 1424 return bufferedIOBase.takeError(); 1425 1426 FileSP file_sp; 1427 1428 auto isTextIO = IsInstance(textIOBase.get()); 1429 if (!isTextIO) 1430 return isTextIO.takeError(); 1431 if (isTextIO.get()) 1432 file_sp = std::static_pointer_cast<File>( 1433 std::make_shared<TextPythonFile>(fd, *this, borrowed)); 1434 1435 auto isRawIO = IsInstance(rawIOBase.get()); 1436 if (!isRawIO) 1437 return isRawIO.takeError(); 1438 auto isBufferedIO = IsInstance(bufferedIOBase.get()); 1439 if (!isBufferedIO) 1440 return isBufferedIO.takeError(); 1441 1442 if (isRawIO.get() || isBufferedIO.get()) { 1443 file_sp = std::static_pointer_cast<File>( 1444 std::make_shared<BinaryPythonFile>(fd, *this, borrowed)); 1445 } 1446 1447 if (!file_sp) 1448 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1449 "python file is neither text nor binary"); 1450 1451 if (!file_sp->IsValid()) 1452 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1453 "invalid File"); 1454 1455 return file_sp; 1456 1457 #endif 1458 } 1459 1460 Expected<PythonFile> PythonFile::FromFile(File &file, const char *mode) { 1461 if (!file.IsValid()) 1462 return llvm::createStringError(llvm::inconvertibleErrorCode(), 1463 "invalid file"); 1464 1465 if (auto *simple = llvm::dyn_cast<SimplePythonFile>(&file)) 1466 return Retain<PythonFile>(simple->GetPythonObject()); 1467 #if PY_MAJOR_VERSION >= 3 1468 if (auto *pythonio = llvm::dyn_cast<PythonIOFile>(&file)) 1469 return Retain<PythonFile>(pythonio->GetPythonObject()); 1470 #endif 1471 1472 if (!mode) { 1473 auto m = file.GetOpenMode(); 1474 if (!m) 1475 return m.takeError(); 1476 mode = m.get(); 1477 } 1478 1479 PyObject *file_obj; 1480 #if PY_MAJOR_VERSION >= 3 1481 file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr, 1482 "ignore", nullptr, /*closefd=*/0); 1483 #else 1484 // I'd like to pass ::fflush here if the file is writable, so that 1485 // when the python side destructs the file object it will be flushed. 1486 // However, this would be dangerous. It can cause fflush to be called 1487 // after fclose if the python program keeps a reference to the file after 1488 // the original lldb_private::File has been destructed. 1489 // 1490 // It's all well and good to ask a python program not to use a closed file 1491 // but asking a python program to make sure objects get released in a 1492 // particular order is not safe. 1493 // 1494 // The tradeoff here is that if a python 2 program wants to make sure this 1495 // file gets flushed, they'll have to do it explicitly or wait untill the 1496 // original lldb File itself gets flushed. 1497 file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""), 1498 py2_const_cast(mode), [](FILE *) { return 0; }); 1499 #endif 1500 1501 if (!file_obj) 1502 return exception(); 1503 1504 return Take<PythonFile>(file_obj); 1505 } 1506 1507 Error PythonScript::Init() { 1508 if (function.IsValid()) 1509 return Error::success(); 1510 1511 PythonDictionary globals(PyInitialValue::Empty); 1512 auto builtins = PythonModule::BuiltinsModule(); 1513 if (Error error = globals.SetItem("__builtins__", builtins)) 1514 return error; 1515 PyObject *o = 1516 PyRun_String(script, Py_file_input, globals.get(), globals.get()); 1517 if (!o) 1518 return exception(); 1519 Take<PythonObject>(o); 1520 auto f = As<PythonCallable>(globals.GetItem("main")); 1521 if (!f) 1522 return f.takeError(); 1523 function = std::move(f.get()); 1524 1525 return Error::success(); 1526 } 1527 1528 llvm::Expected<PythonObject> 1529 python::runStringOneLine(const llvm::Twine &string, 1530 const PythonDictionary &globals, 1531 const PythonDictionary &locals) { 1532 if (!globals.IsValid() || !locals.IsValid()) 1533 return nullDeref(); 1534 1535 PyObject *code = 1536 Py_CompileString(NullTerminated(string), "<string>", Py_eval_input); 1537 if (!code) { 1538 PyErr_Clear(); 1539 code = 1540 Py_CompileString(NullTerminated(string), "<string>", Py_single_input); 1541 } 1542 if (!code) 1543 return exception(); 1544 auto code_ref = Take<PythonObject>(code); 1545 1546 #if PY_MAJOR_VERSION < 3 1547 PyObject *result = 1548 PyEval_EvalCode((PyCodeObject *)code, globals.get(), locals.get()); 1549 #else 1550 PyObject *result = PyEval_EvalCode(code, globals.get(), locals.get()); 1551 #endif 1552 1553 if (!result) 1554 return exception(); 1555 1556 return Take<PythonObject>(result); 1557 } 1558 1559 llvm::Expected<PythonObject> 1560 python::runStringMultiLine(const llvm::Twine &string, 1561 const PythonDictionary &globals, 1562 const PythonDictionary &locals) { 1563 if (!globals.IsValid() || !locals.IsValid()) 1564 return nullDeref(); 1565 PyObject *result = PyRun_String(NullTerminated(string), Py_file_input, 1566 globals.get(), locals.get()); 1567 if (!result) 1568 return exception(); 1569 return Take<PythonObject>(result); 1570 } 1571 1572 #endif 1573