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