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