1 //===-- PythonDataObjects.h--------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // 10 // !! FIXME FIXME FIXME !! 11 // 12 // Python APIs nearly all can return an exception. They do this 13 // by returning NULL, or -1, or some such value and setting 14 // the exception state with PyErr_Set*(). Exceptions must be 15 // handled before further python API functions are called. Failure 16 // to do so will result in asserts on debug builds of python. 17 // It will also sometimes, but not usually result in crashes of 18 // release builds. 19 // 20 // Nearly all the code in this header does not handle python exceptions 21 // correctly. It should all be converted to return Expected<> or 22 // Error types to capture the exception. 23 // 24 // Everything in this file except functions that return Error or 25 // Expected<> is considered deprecated and should not be 26 // used in new code. If you need to use it, fix it first. 27 // 28 // 29 // TODOs for this file 30 // 31 // * Make all methods safe for exceptions. 32 // 33 // * Eliminate method signatures that must translate exceptions into 34 // empty objects or NULLs. Almost everything here should return 35 // Expected<>. It should be acceptable for certain operations that 36 // can never fail to assert instead, such as the creation of 37 // PythonString from a string literal. 38 // 39 // * Eliminate Reset(), and make all non-default constructors private. 40 // Python objects should be created with Retain<> or Take<>, and they 41 // should be assigned with operator= 42 // 43 // * Eliminate default constructors, make python objects always 44 // nonnull, and use optionals where necessary. 45 // 46 47 48 #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 49 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 50 51 #include "lldb/Host/Config.h" 52 53 #if LLDB_ENABLE_PYTHON 54 55 // LLDB Python header must be included first 56 #include "lldb-python.h" 57 58 #include "lldb/Host/File.h" 59 #include "lldb/Utility/StructuredData.h" 60 61 #include "llvm/ADT/ArrayRef.h" 62 63 namespace lldb_private { 64 namespace python { 65 66 class PythonObject; 67 class PythonBytes; 68 class PythonString; 69 class PythonList; 70 class PythonDictionary; 71 class PythonInteger; 72 class PythonException; 73 74 class StructuredPythonObject : public StructuredData::Generic { 75 public: 76 StructuredPythonObject() : StructuredData::Generic() {} 77 78 StructuredPythonObject(void *obj) : StructuredData::Generic(obj) { 79 Py_XINCREF(GetValue()); 80 } 81 82 ~StructuredPythonObject() override { 83 if (Py_IsInitialized()) 84 Py_XDECREF(GetValue()); 85 SetValue(nullptr); 86 } 87 88 bool IsValid() const override { return GetValue() && GetValue() != Py_None; } 89 90 void Serialize(llvm::json::OStream &s) const override; 91 92 private: 93 DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject); 94 }; 95 96 enum class PyObjectType { 97 Unknown, 98 None, 99 Boolean, 100 Integer, 101 Dictionary, 102 List, 103 String, 104 Bytes, 105 ByteArray, 106 Module, 107 Callable, 108 Tuple, 109 File 110 }; 111 112 enum class PyRefType { 113 Borrowed, // We are not given ownership of the incoming PyObject. 114 // We cannot safely hold it without calling Py_INCREF. 115 Owned // We have ownership of the incoming PyObject. We should 116 // not call Py_INCREF. 117 }; 118 119 120 // Take a reference that you already own, and turn it into 121 // a PythonObject. 122 // 123 // Most python API methods will return a +1 reference 124 // if they succeed or NULL if and only if 125 // they set an exception. Use this to collect such return 126 // values, after checking for NULL. 127 // 128 // If T is not just PythonObject, then obj must be already be 129 // checked to be of the correct type. 130 template <typename T> T Take(PyObject *obj) { 131 assert(obj); 132 assert(!PyErr_Occurred()); 133 T thing(PyRefType::Owned, obj); 134 assert(thing.IsValid()); 135 return thing; 136 } 137 138 // Retain a reference you have borrowed, and turn it into 139 // a PythonObject. 140 // 141 // A minority of python APIs return a borrowed reference 142 // instead of a +1. They will also return NULL if and only 143 // if they set an exception. Use this to collect such return 144 // values, after checking for NULL. 145 // 146 // If T is not just PythonObject, then obj must be already be 147 // checked to be of the correct type. 148 template <typename T> T Retain(PyObject *obj) { 149 assert(obj); 150 assert(!PyErr_Occurred()); 151 T thing(PyRefType::Borrowed, obj); 152 assert(thing.IsValid()); 153 return thing; 154 } 155 156 // This class can be used like a utility function to convert from 157 // a llvm-friendly Twine into a null-terminated const char *, 158 // which is the form python C APIs want their strings in. 159 // 160 // Example: 161 // const llvm::Twine &some_twine; 162 // PyFoo_Bar(x, y, z, NullTerminated(some_twine)); 163 // 164 // Why a class instead of a function? If the twine isn't already null 165 // terminated, it will need a temporary buffer to copy the string 166 // into. We need that buffer to stick around for the lifetime of the 167 // statement. 168 class NullTerminated { 169 const char *str; 170 llvm::SmallString<32> storage; 171 172 public: 173 NullTerminated(const llvm::Twine &twine) { 174 llvm::StringRef ref = twine.toNullTerminatedStringRef(storage); 175 str = ref.begin(); 176 } 177 operator const char *() { return str; } 178 }; 179 180 inline llvm::Error nullDeref() { 181 return llvm::createStringError(llvm::inconvertibleErrorCode(), 182 "A NULL PyObject* was dereferenced"); 183 } 184 185 inline llvm::Error exception(const char *s = nullptr) { 186 return llvm::make_error<PythonException>(s); 187 } 188 189 inline llvm::Error keyError() { 190 return llvm::createStringError(llvm::inconvertibleErrorCode(), 191 "key not in dict"); 192 } 193 194 #if PY_MAJOR_VERSION < 3 195 // The python 2 API declares some arguments as char* that should 196 // be const char *, but it doesn't actually modify them. 197 inline char *py2_const_cast(const char *s) { return const_cast<char *>(s); } 198 #else 199 inline const char *py2_const_cast(const char *s) { return s; } 200 #endif 201 202 enum class PyInitialValue { Invalid, Empty }; 203 204 template <typename T, typename Enable = void> struct PythonFormat; 205 206 template <> struct PythonFormat<unsigned long long> { 207 static constexpr char format = 'K'; 208 static auto get(unsigned long long value) { return value; } 209 }; 210 211 template <> struct PythonFormat<long long> { 212 static constexpr char format = 'L'; 213 static auto get(long long value) { return value; } 214 }; 215 216 template <> struct PythonFormat<PyObject *> { 217 static constexpr char format = 'O'; 218 static auto get(PyObject *value) { return value; } 219 }; 220 221 template <typename T> 222 struct PythonFormat< 223 T, typename std::enable_if<std::is_base_of<PythonObject, T>::value>::type> { 224 static constexpr char format = 'O'; 225 static auto get(const T &value) { return value.get(); } 226 }; 227 228 class PythonObject { 229 public: 230 PythonObject() : m_py_obj(nullptr) {} 231 232 PythonObject(PyRefType type, PyObject *py_obj) { 233 m_py_obj = py_obj; 234 // If this is a borrowed reference, we need to convert it to 235 // an owned reference by incrementing it. If it is an owned 236 // reference (for example the caller allocated it with PyDict_New() 237 // then we must *not* increment it. 238 if (m_py_obj && Py_IsInitialized() && type == PyRefType::Borrowed) 239 Py_XINCREF(m_py_obj); 240 } 241 242 PythonObject(const PythonObject &rhs) 243 : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {} 244 245 PythonObject(PythonObject &&rhs) { 246 m_py_obj = rhs.m_py_obj; 247 rhs.m_py_obj = nullptr; 248 } 249 250 ~PythonObject() { Reset(); } 251 252 void Reset() { 253 if (m_py_obj && Py_IsInitialized()) 254 Py_DECREF(m_py_obj); 255 m_py_obj = nullptr; 256 } 257 258 void Dump() const { 259 if (m_py_obj) 260 _PyObject_Dump(m_py_obj); 261 else 262 puts("NULL"); 263 } 264 265 void Dump(Stream &strm) const; 266 267 PyObject *get() const { return m_py_obj; } 268 269 PyObject *release() { 270 PyObject *result = m_py_obj; 271 m_py_obj = nullptr; 272 return result; 273 } 274 275 PythonObject &operator=(PythonObject other) { 276 Reset(); 277 m_py_obj = std::exchange(other.m_py_obj, nullptr); 278 return *this; 279 } 280 281 PyObjectType GetObjectType() const; 282 283 PythonString Repr() const; 284 285 PythonString Str() const; 286 287 static PythonObject ResolveNameWithDictionary(llvm::StringRef name, 288 const PythonDictionary &dict); 289 290 template <typename T> 291 static T ResolveNameWithDictionary(llvm::StringRef name, 292 const PythonDictionary &dict) { 293 return ResolveNameWithDictionary(name, dict).AsType<T>(); 294 } 295 296 PythonObject ResolveName(llvm::StringRef name) const; 297 298 template <typename T> T ResolveName(llvm::StringRef name) const { 299 return ResolveName(name).AsType<T>(); 300 } 301 302 bool HasAttribute(llvm::StringRef attribute) const; 303 304 PythonObject GetAttributeValue(llvm::StringRef attribute) const; 305 306 bool IsNone() const { return m_py_obj == Py_None; } 307 308 bool IsValid() const { return m_py_obj != nullptr; } 309 310 bool IsAllocated() const { return IsValid() && !IsNone(); } 311 312 explicit operator bool() const { return IsValid() && !IsNone(); } 313 314 template <typename T> T AsType() const { 315 if (!T::Check(m_py_obj)) 316 return T(); 317 return T(PyRefType::Borrowed, m_py_obj); 318 } 319 320 StructuredData::ObjectSP CreateStructuredObject() const; 321 322 public: 323 template <typename... T> 324 llvm::Expected<PythonObject> CallMethod(const char *name, 325 const T &... t) const { 326 const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; 327 PyObject *obj = 328 PyObject_CallMethod(m_py_obj, py2_const_cast(name), 329 py2_const_cast(format), PythonFormat<T>::get(t)...); 330 if (!obj) 331 return exception(); 332 return python::Take<PythonObject>(obj); 333 } 334 335 template <typename... T> 336 llvm::Expected<PythonObject> Call(const T &... t) const { 337 const char format[] = {'(', PythonFormat<T>::format..., ')', 0}; 338 PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format), 339 PythonFormat<T>::get(t)...); 340 if (!obj) 341 return exception(); 342 return python::Take<PythonObject>(obj); 343 } 344 345 llvm::Expected<PythonObject> GetAttribute(const llvm::Twine &name) const { 346 if (!m_py_obj) 347 return nullDeref(); 348 PyObject *obj = PyObject_GetAttrString(m_py_obj, NullTerminated(name)); 349 if (!obj) 350 return exception(); 351 return python::Take<PythonObject>(obj); 352 } 353 354 llvm::Expected<bool> IsTrue() { 355 if (!m_py_obj) 356 return nullDeref(); 357 int r = PyObject_IsTrue(m_py_obj); 358 if (r < 0) 359 return exception(); 360 return !!r; 361 } 362 363 llvm::Expected<long long> AsLongLong() { 364 if (!m_py_obj) 365 return nullDeref(); 366 assert(!PyErr_Occurred()); 367 long long r = PyLong_AsLongLong(m_py_obj); 368 if (PyErr_Occurred()) 369 return exception(); 370 return r; 371 } 372 373 llvm::Expected<long long> AsUnsignedLongLong() { 374 if (!m_py_obj) 375 return nullDeref(); 376 assert(!PyErr_Occurred()); 377 long long r = PyLong_AsUnsignedLongLong(m_py_obj); 378 if (PyErr_Occurred()) 379 return exception(); 380 return r; 381 } 382 383 llvm::Expected<unsigned long long> AsModuloUnsignedLongLong() const { 384 // wraps on overflow, instead of raising an error. 385 if (!m_py_obj) 386 return nullDeref(); 387 assert(!PyErr_Occurred()); 388 unsigned long long r = PyLong_AsUnsignedLongLongMask(m_py_obj); 389 if (PyErr_Occurred()) 390 return exception(); 391 return r; 392 } 393 394 llvm::Expected<bool> IsInstance(const PythonObject &cls) { 395 if (!m_py_obj || !cls.IsValid()) 396 return nullDeref(); 397 int r = PyObject_IsInstance(m_py_obj, cls.get()); 398 if (r < 0) 399 return exception(); 400 return !!r; 401 } 402 403 protected: 404 PyObject *m_py_obj; 405 }; 406 407 408 // This is why C++ needs monads. 409 template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) { 410 if (!obj) 411 return obj.takeError(); 412 if (!T::Check(obj.get().get())) 413 return llvm::createStringError(llvm::inconvertibleErrorCode(), 414 "type error"); 415 return T(PyRefType::Borrowed, std::move(obj.get().get())); 416 } 417 418 template <> llvm::Expected<bool> As<bool>(llvm::Expected<PythonObject> &&obj); 419 420 template <> 421 llvm::Expected<long long> As<long long>(llvm::Expected<PythonObject> &&obj); 422 423 template <> 424 llvm::Expected<unsigned long long> 425 As<unsigned long long>(llvm::Expected<PythonObject> &&obj); 426 427 template <> 428 llvm::Expected<std::string> As<std::string>(llvm::Expected<PythonObject> &&obj); 429 430 431 template <class T> class TypedPythonObject : public PythonObject { 432 public: 433 // override to perform implicit type conversions on Reset 434 // This can be eliminated once we drop python 2 support. 435 static void Convert(PyRefType &type, PyObject *&py_obj) {} 436 437 TypedPythonObject(PyRefType type, PyObject *py_obj) { 438 if (!py_obj) 439 return; 440 T::Convert(type, py_obj); 441 if (T::Check(py_obj)) 442 PythonObject::operator=(PythonObject(type, py_obj)); 443 else if (type == PyRefType::Owned) 444 Py_DECREF(py_obj); 445 } 446 447 TypedPythonObject() {} 448 }; 449 450 class PythonBytes : public TypedPythonObject<PythonBytes> { 451 public: 452 using TypedPythonObject::TypedPythonObject; 453 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes); 454 PythonBytes(const uint8_t *bytes, size_t length); 455 456 static bool Check(PyObject *py_obj); 457 458 llvm::ArrayRef<uint8_t> GetBytes() const; 459 460 size_t GetSize() const; 461 462 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); 463 464 StructuredData::StringSP CreateStructuredString() const; 465 }; 466 467 class PythonByteArray : public TypedPythonObject<PythonByteArray> { 468 public: 469 using TypedPythonObject::TypedPythonObject; 470 explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes); 471 PythonByteArray(const uint8_t *bytes, size_t length); 472 PythonByteArray(const PythonBytes &object); 473 474 static bool Check(PyObject *py_obj); 475 476 llvm::ArrayRef<uint8_t> GetBytes() const; 477 478 size_t GetSize() const; 479 480 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes); 481 482 StructuredData::StringSP CreateStructuredString() const; 483 }; 484 485 class PythonString : public TypedPythonObject<PythonString> { 486 public: 487 using TypedPythonObject::TypedPythonObject; 488 static llvm::Expected<PythonString> FromUTF8(llvm::StringRef string); 489 490 PythonString() : TypedPythonObject() {} // MSVC requires this for some reason 491 492 explicit PythonString(llvm::StringRef string); // safe, null on error 493 494 static bool Check(PyObject *py_obj); 495 static void Convert(PyRefType &type, PyObject *&py_obj); 496 497 llvm::StringRef GetString() const; // safe, empty string on error 498 499 llvm::Expected<llvm::StringRef> AsUTF8() const; 500 501 size_t GetSize() const; 502 503 void SetString(llvm::StringRef string); // safe, null on error 504 505 StructuredData::StringSP CreateStructuredString() const; 506 }; 507 508 class PythonInteger : public TypedPythonObject<PythonInteger> { 509 public: 510 using TypedPythonObject::TypedPythonObject; 511 512 PythonInteger() : TypedPythonObject() {} // MSVC requires this for some reason 513 514 explicit PythonInteger(int64_t value); 515 516 static bool Check(PyObject *py_obj); 517 static void Convert(PyRefType &type, PyObject *&py_obj); 518 519 void SetInteger(int64_t value); 520 521 StructuredData::IntegerSP CreateStructuredInteger() const; 522 }; 523 524 class PythonBoolean : public TypedPythonObject<PythonBoolean> { 525 public: 526 using TypedPythonObject::TypedPythonObject; 527 528 explicit PythonBoolean(bool value); 529 530 static bool Check(PyObject *py_obj); 531 532 bool GetValue() const; 533 534 void SetValue(bool value); 535 536 StructuredData::BooleanSP CreateStructuredBoolean() const; 537 }; 538 539 class PythonList : public TypedPythonObject<PythonList> { 540 public: 541 using TypedPythonObject::TypedPythonObject; 542 543 PythonList() : TypedPythonObject() {} // MSVC requires this for some reason 544 545 explicit PythonList(PyInitialValue value); 546 explicit PythonList(int list_size); 547 548 static bool Check(PyObject *py_obj); 549 550 uint32_t GetSize() const; 551 552 PythonObject GetItemAtIndex(uint32_t index) const; 553 554 void SetItemAtIndex(uint32_t index, const PythonObject &object); 555 556 void AppendItem(const PythonObject &object); 557 558 StructuredData::ArraySP CreateStructuredArray() const; 559 }; 560 561 class PythonTuple : public TypedPythonObject<PythonTuple> { 562 public: 563 using TypedPythonObject::TypedPythonObject; 564 565 explicit PythonTuple(PyInitialValue value); 566 explicit PythonTuple(int tuple_size); 567 PythonTuple(std::initializer_list<PythonObject> objects); 568 PythonTuple(std::initializer_list<PyObject *> objects); 569 570 static bool Check(PyObject *py_obj); 571 572 uint32_t GetSize() const; 573 574 PythonObject GetItemAtIndex(uint32_t index) const; 575 576 void SetItemAtIndex(uint32_t index, const PythonObject &object); 577 578 StructuredData::ArraySP CreateStructuredArray() const; 579 }; 580 581 class PythonDictionary : public TypedPythonObject<PythonDictionary> { 582 public: 583 using TypedPythonObject::TypedPythonObject; 584 585 PythonDictionary() : TypedPythonObject() {} // MSVC requires this for some reason 586 587 explicit PythonDictionary(PyInitialValue value); 588 589 static bool Check(PyObject *py_obj); 590 591 uint32_t GetSize() const; 592 593 PythonList GetKeys() const; 594 595 PythonObject GetItemForKey(const PythonObject &key) const; // DEPRECATED 596 void SetItemForKey(const PythonObject &key, 597 const PythonObject &value); // DEPRECATED 598 599 llvm::Expected<PythonObject> GetItem(const PythonObject &key) const; 600 llvm::Expected<PythonObject> GetItem(const llvm::Twine &key) const; 601 llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const; 602 llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const; 603 604 StructuredData::DictionarySP CreateStructuredDictionary() const; 605 }; 606 607 class PythonModule : public TypedPythonObject<PythonModule> { 608 public: 609 using TypedPythonObject::TypedPythonObject; 610 611 static bool Check(PyObject *py_obj); 612 613 static PythonModule BuiltinsModule(); 614 615 static PythonModule MainModule(); 616 617 static PythonModule AddModule(llvm::StringRef module); 618 619 // safe, returns invalid on error; 620 static PythonModule ImportModule(llvm::StringRef name) { 621 std::string s = std::string(name); 622 auto mod = Import(s.c_str()); 623 if (!mod) { 624 llvm::consumeError(mod.takeError()); 625 return PythonModule(); 626 } 627 return std::move(mod.get()); 628 } 629 630 static llvm::Expected<PythonModule> Import(const llvm::Twine &name); 631 632 llvm::Expected<PythonObject> Get(const llvm::Twine &name); 633 634 PythonDictionary GetDictionary() const; 635 }; 636 637 class PythonCallable : public TypedPythonObject<PythonCallable> { 638 public: 639 using TypedPythonObject::TypedPythonObject; 640 641 struct ArgInfo { 642 /* the largest number of positional arguments this callable 643 * can accept, or UNBOUNDED, ie UINT_MAX if it's a varargs 644 * function and can accept an arbitrary number */ 645 unsigned max_positional_args; 646 static constexpr unsigned UNBOUNDED = UINT_MAX; // FIXME c++17 inline 647 }; 648 649 static bool Check(PyObject *py_obj); 650 651 llvm::Expected<ArgInfo> GetArgInfo() const; 652 653 PythonObject operator()(); 654 655 PythonObject operator()(std::initializer_list<PyObject *> args); 656 657 PythonObject operator()(std::initializer_list<PythonObject> args); 658 659 template <typename Arg, typename... Args> 660 PythonObject operator()(const Arg &arg, Args... args) { 661 return operator()({arg, args...}); 662 } 663 }; 664 665 class PythonFile : public TypedPythonObject<PythonFile> { 666 public: 667 using TypedPythonObject::TypedPythonObject; 668 669 PythonFile() : TypedPythonObject() {} // MSVC requires this for some reason 670 671 static bool Check(PyObject *py_obj); 672 673 static llvm::Expected<PythonFile> FromFile(File &file, 674 const char *mode = nullptr); 675 676 llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false); 677 llvm::Expected<lldb::FileSP> 678 ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false); 679 }; 680 681 class PythonException : public llvm::ErrorInfo<PythonException> { 682 private: 683 PyObject *m_exception_type, *m_exception, *m_traceback; 684 PyObject *m_repr_bytes; 685 686 public: 687 static char ID; 688 const char *toCString() const; 689 PythonException(const char *caller = nullptr); 690 void Restore(); 691 ~PythonException(); 692 void log(llvm::raw_ostream &OS) const override; 693 std::error_code convertToErrorCode() const override; 694 bool Matches(PyObject *exc) const; 695 std::string ReadBacktrace() const; 696 }; 697 698 // This extracts the underlying T out of an Expected<T> and returns it. 699 // If the Expected is an Error instead of a T, that error will be converted 700 // into a python exception, and this will return a default-constructed T. 701 // 702 // This is appropriate for use right at the boundary of python calling into 703 // C++, such as in a SWIG typemap. In such a context you should simply 704 // check if the returned T is valid, and if it is, return a NULL back 705 // to python. This will result in the Error being raised as an exception 706 // from python code's point of view. 707 // 708 // For example: 709 // ``` 710 // Expected<Foo *> efoop = some_cpp_function(); 711 // Foo *foop = unwrapOrSetPythonException(efoop); 712 // if (!foop) 713 // return NULL; 714 // do_something(*foop); 715 // 716 // If the Error returned was itself created because a python exception was 717 // raised when C++ code called into python, then the original exception 718 // will be restored. Otherwise a simple string exception will be raised. 719 template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) { 720 if (expected) 721 return expected.get(); 722 llvm::handleAllErrors( 723 expected.takeError(), [](PythonException &E) { E.Restore(); }, 724 [](const llvm::ErrorInfoBase &E) { 725 PyErr_SetString(PyExc_Exception, E.message().c_str()); 726 }); 727 return T(); 728 } 729 730 // This is only here to help incrementally migrate old, exception-unsafe 731 // code. 732 template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) { 733 if (expected) 734 return std::move(expected.get()); 735 llvm::consumeError(expected.takeError()); 736 return T(); 737 } 738 739 llvm::Expected<PythonObject> runStringOneLine(const llvm::Twine &string, 740 const PythonDictionary &globals, 741 const PythonDictionary &locals); 742 743 llvm::Expected<PythonObject> runStringMultiLine(const llvm::Twine &string, 744 const PythonDictionary &globals, 745 const PythonDictionary &locals); 746 747 // Sometimes the best way to interact with a python interpreter is 748 // to run some python code. You construct a PythonScript with 749 // script string. The script assigns some function to `_function_` 750 // and you get a C++ callable object that calls the python function. 751 // 752 // Example: 753 // 754 // const char script[] = R"( 755 // def main(x, y): 756 // .... 757 // )"; 758 // 759 // Expected<PythonObject> cpp_foo_wrapper(PythonObject x, PythonObject y) { 760 // // no need to synchronize access to this global, we already have the GIL 761 // static PythonScript foo(script) 762 // return foo(x, y); 763 // } 764 class PythonScript { 765 const char *script; 766 PythonCallable function; 767 768 llvm::Error Init(); 769 770 public: 771 PythonScript(const char *script) : script(script), function() {} 772 773 template <typename... Args> 774 llvm::Expected<PythonObject> operator()(Args &&... args) { 775 if (llvm::Error error = Init()) 776 return std::move(error); 777 return function.Call(std::forward<Args>(args)...); 778 } 779 }; 780 781 } // namespace python 782 } // namespace lldb_private 783 784 #endif 785 786 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 787