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