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