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