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