Coverage Report

Created: 2022-01-22 13:19

/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
557
  GIL() {
77
557
    m_state = PyGILState_Ensure();
78
557
    assert(!PyErr_Occurred());
79
557
  }
80
557
  ~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
116k
template <typename T> T Take(PyObject *obj) {
121
116k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
116k
  assert(thing.IsValid());
125
0
  return thing;
126
116k
}
lldb_private::python::PythonObject lldb_private::python::Take<lldb_private::python::PythonObject>(_object*)
Line
Count
Source
120
12.9k
template <typename T> T Take(PyObject *obj) {
121
12.9k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
12.9k
  assert(thing.IsValid());
125
0
  return thing;
126
12.9k
}
lldb_private::python::PythonString lldb_private::python::Take<lldb_private::python::PythonString>(_object*)
Line
Count
Source
120
61.8k
template <typename T> T Take(PyObject *obj) {
121
61.8k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
61.8k
  assert(thing.IsValid());
125
0
  return thing;
126
61.8k
}
lldb_private::python::PythonBytes lldb_private::python::Take<lldb_private::python::PythonBytes>(_object*)
Line
Count
Source
120
16
template <typename T> T Take(PyObject *obj) {
121
16
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
16
  assert(thing.IsValid());
125
0
  return thing;
126
16
}
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.42k
template <typename T> T Take(PyObject *obj) {
121
1.42k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
1.42k
  assert(thing.IsValid());
125
0
  return thing;
126
1.42k
}
Unexecuted instantiation: lldb_private::python::PythonBoolean lldb_private::python::Take<lldb_private::python::PythonBoolean>(_object*)
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
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
2
  assert(thing.IsValid());
125
0
  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
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
3
  assert(thing.IsValid());
125
0
  return thing;
126
3
}
lldb_private::python::PythonDictionary lldb_private::python::Take<lldb_private::python::PythonDictionary>(_object*)
Line
Count
Source
120
60
template <typename T> T Take(PyObject *obj) {
121
60
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
60
  assert(thing.IsValid());
125
0
  return thing;
126
60
}
lldb_private::python::PythonModule lldb_private::python::Take<lldb_private::python::PythonModule>(_object*)
Line
Count
Source
120
21.5k
template <typename T> T Take(PyObject *obj) {
121
21.5k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
21.5k
  assert(thing.IsValid());
125
0
  return thing;
126
21.5k
}
lldb_private::python::PythonFile lldb_private::python::Take<lldb_private::python::PythonFile>(_object*)
Line
Count
Source
120
18.6k
template <typename T> T Take(PyObject *obj) {
121
18.6k
  assert(obj);
122
0
  assert(!PyErr_Occurred());
123
0
  T thing(PyRefType::Owned, obj);
124
18.6k
  assert(thing.IsValid());
125
0
  return thing;
126
18.6k
}
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
47.7k
template <typename T> T Retain(PyObject *obj) {
139
47.7k
  assert(obj);
140
0
  assert(!PyErr_Occurred());
141
0
  T thing(PyRefType::Borrowed, obj);
142
47.7k
  assert(thing.IsValid());
143
0
  return thing;
144
47.7k
}
lldb_private::python::PythonObject lldb_private::python::Retain<lldb_private::python::PythonObject>(_object*)
Line
Count
Source
138
41.0k
template <typename T> T Retain(PyObject *obj) {
139
41.0k
  assert(obj);
140
0
  assert(!PyErr_Occurred());
141
0
  T thing(PyRefType::Borrowed, obj);
142
41.0k
  assert(thing.IsValid());
143
0
  return thing;
144
41.0k
}
lldb_private::python::PythonDictionary lldb_private::python::Retain<lldb_private::python::PythonDictionary>(_object*)
Line
Count
Source
138
6.70k
template <typename T> T Retain(PyObject *obj) {
139
6.70k
  assert(obj);
140
0
  assert(!PyErr_Occurred());
141
0
  T thing(PyRefType::Borrowed, obj);
142
6.70k
  assert(thing.IsValid());
143
0
  return thing;
144
6.70k
}
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
0
  assert(!PyErr_Occurred());
141
0
  T thing(PyRefType::Borrowed, obj);
142
7
  assert(thing.IsValid());
143
0
  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
48.2k
  NullTerminated(const llvm::Twine &twine) {
164
48.2k
    llvm::StringRef ref = twine.toNullTerminatedStringRef(storage);
165
48.2k
    str = ref.begin();
166
48.2k
  }
167
48.2k
  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
23
inline llvm::Error exception(const char *s = nullptr) {
176
23
  return llvm::make_error<PythonException>(s);
177
23
}
178
179
110
inline llvm::Error keyError() {
180
110
  return llvm::createStringError(llvm::inconvertibleErrorCode(),
181
110
                                 "key not in dict");
182
110
}
183
184
#if PY_MAJOR_VERSION < 3
185
// The python 2 API declares some arguments as char* that should
186
// be const char *, but it doesn't actually modify them.
187
inline char *py2_const_cast(const char *s) { return const_cast<char *>(s); }
188
#else
189
2.12k
inline const char *py2_const_cast(const char *s) { return s; }
190
#endif
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
7
  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
21
  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
725
  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
215
102
  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
215
21
  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
215
9
  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
215
196
  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
215
397
  static auto get(const T &value) { return value.get(); }
216
};
217
218
class PythonObject {
219
public:
220
200k
  PythonObject() = default;
221
222
201k
  PythonObject(PyRefType type, PyObject *py_obj) {
223
201k
    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
201k
    if (m_py_obj && 
Py_IsInitialized()201k
&&
type == PyRefType::Borrowed201k
)
229
74.9k
      Py_XINCREF(m_py_obj);
230
201k
  }
231
232
  PythonObject(const PythonObject &rhs)
233
12.3k
      : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {}
lldb_private::python::PythonObject::PythonObject(lldb_private::python::PythonObject const&)
Line
Count
Source
233
5.90k
      : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {}
lldb_private::python::PythonObject::PythonObject(lldb_private::python::PythonObject const&)
Line
Count
Source
233
6.47k
      : PythonObject(PyRefType::Borrowed, rhs.m_py_obj) {}
234
235
249k
  PythonObject(PythonObject &&rhs) {
236
249k
    m_py_obj = rhs.m_py_obj;
237
249k
    rhs.m_py_obj = nullptr;
238
249k
  }
239
240
651k
  ~PythonObject() { Reset(); }
241
242
  void Reset();
243
244
0
  void Dump() const {
245
0
    if (m_py_obj)
246
0
      _PyObject_Dump(m_py_obj);
247
0
    else
248
0
      puts("NULL");
249
0
  }
250
251
  void Dump(Stream &strm) const;
252
253
154k
  PyObject *get() const { return m_py_obj; }
254
255
2.44k
  PyObject *release() {
256
2.44k
    PyObject *result = m_py_obj;
257
2.44k
    m_py_obj = nullptr;
258
2.44k
    return result;
259
2.44k
  }
260
261
207k
  PythonObject &operator=(PythonObject other) {
262
207k
    Reset();
263
207k
    m_py_obj = std::exchange(other.m_py_obj, nullptr);
264
207k
    return *this;
265
207k
  }
266
267
  PyObjectType GetObjectType() const;
268
269
  PythonString Repr() const;
270
271
  PythonString Str() const;
272
273
  static PythonObject ResolveNameWithDictionary(llvm::StringRef name,
274
                                                const PythonDictionary &dict);
275
276
  template <typename T>
277
  static T ResolveNameWithDictionary(llvm::StringRef name,
278
490
                                     const PythonDictionary &dict) {
279
490
    return ResolveNameWithDictionary(name, dict).AsType<T>();
280
490
  }
281
282
  PythonObject ResolveName(llvm::StringRef name) const;
283
284
3.87k
  template <typename T> T ResolveName(llvm::StringRef name) const {
285
3.87k
    return ResolveName(name).AsType<T>();
286
3.87k
  }
lldb_private::python::PythonDictionary lldb_private::python::PythonObject::ResolveName<lldb_private::python::PythonDictionary>(llvm::StringRef) const
Line
Count
Source
284
446
  template <typename T> T ResolveName(llvm::StringRef name) const {
285
446
    return ResolveName(name).AsType<T>();
286
446
  }
lldb_private::python::PythonCallable lldb_private::python::PythonObject::ResolveName<lldb_private::python::PythonCallable>(llvm::StringRef) const
Line
Count
Source
284
3.42k
  template <typename T> T ResolveName(llvm::StringRef name) const {
285
3.42k
    return ResolveName(name).AsType<T>();
286
3.42k
  }
287
288
  bool HasAttribute(llvm::StringRef attribute) const;
289
290
  PythonObject GetAttributeValue(llvm::StringRef attribute) const;
291
292
11.0k
  bool IsNone() const { return m_py_obj == Py_None; }
293
294
441k
  bool IsValid() const { return m_py_obj != nullptr; }
295
296
11.9k
  bool IsAllocated() const { return IsValid() && 
!IsNone()10.5k
; }
297
298
476
  explicit operator bool() const { return IsValid() && 
!IsNone()475
; }
299
300
4.57k
  template <typename T> T AsType() const {
301
4.57k
    if (!T::Check(m_py_obj))
302
1.02k
      return T();
303
3.55k
    return T(PyRefType::Borrowed, m_py_obj);
304
4.57k
  }
lldb_private::python::PythonString lldb_private::python::PythonObject::AsType<lldb_private::python::PythonString>() const
Line
Count
Source
300
205
  template <typename T> T AsType() const {
301
205
    if (!T::Check(m_py_obj))
302
0
      return T();
303
205
    return T(PyRefType::Borrowed, m_py_obj);
304
205
  }
lldb_private::python::PythonDictionary lldb_private::python::PythonObject::AsType<lldb_private::python::PythonDictionary>() const
Line
Count
Source
300
446
  template <typename T> T AsType() const {
301
446
    if (!T::Check(m_py_obj))
302
0
      return T();
303
446
    return T(PyRefType::Borrowed, m_py_obj);
304
446
  }
lldb_private::python::PythonCallable lldb_private::python::PythonObject::AsType<lldb_private::python::PythonCallable>() const
Line
Count
Source
300
3.92k
  template <typename T> T AsType() const {
301
3.92k
    if (!T::Check(m_py_obj))
302
1.02k
      return T();
303
2.89k
    return T(PyRefType::Borrowed, m_py_obj);
304
3.92k
  }
305
306
  StructuredData::ObjectSP CreateStructuredObject() const;
307
308
  template <typename... T>
309
  llvm::Expected<PythonObject> CallMethod(const char *name,
310
789
                                          const T &... t) const {
311
789
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
312
789
    PyObject *obj =
313
789
        PyObject_CallMethod(m_py_obj, py2_const_cast(name),
314
789
                            py2_const_cast(format), PythonFormat<T>::get(t)...);
315
789
    if (!obj)
316
11
      return exception();
317
778
    return python::Take<PythonObject>(obj);
318
789
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::CallMethod<unsigned long long>(char const*, unsigned long long const&) const
Line
Count
Source
310
7
                                          const T &... t) const {
311
7
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
312
7
    PyObject *obj =
313
7
        PyObject_CallMethod(m_py_obj, py2_const_cast(name),
314
7
                            py2_const_cast(format), PythonFormat<T>::get(t)...);
315
7
    if (!obj)
316
1
      return exception();
317
6
    return python::Take<PythonObject>(obj);
318
7
  }
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
310
185
                                          const T &... t) const {
311
185
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
312
185
    PyObject *obj =
313
185
        PyObject_CallMethod(m_py_obj, py2_const_cast(name),
314
185
                            py2_const_cast(format), PythonFormat<T>::get(t)...);
315
185
    if (!obj)
316
2
      return exception();
317
183
    return python::Take<PythonObject>(obj);
318
185
  }
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
310
56
                                          const T &... t) const {
311
56
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
312
56
    PyObject *obj =
313
56
        PyObject_CallMethod(m_py_obj, py2_const_cast(name),
314
56
                            py2_const_cast(format), PythonFormat<T>::get(t)...);
315
56
    if (!obj)
316
0
      return exception();
317
56
    return python::Take<PythonObject>(obj);
318
56
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::CallMethod<>(char const*) const
Line
Count
Source
310
541
                                          const T &... t) const {
311
541
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
312
541
    PyObject *obj =
313
541
        PyObject_CallMethod(m_py_obj, py2_const_cast(name),
314
541
                            py2_const_cast(format), PythonFormat<T>::get(t)...);
315
541
    if (!obj)
316
8
      return exception();
317
533
    return python::Take<PythonObject>(obj);
318
541
  }
319
320
  template <typename... T>
321
547
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
547
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
547
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
547
                                          PythonFormat<T>::get(t)...);
325
547
    if (!obj)
326
3
      return exception();
327
544
    return python::Take<PythonObject>(obj);
328
547
  }
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
321
17
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
17
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
17
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
17
                                          PythonFormat<T>::get(t)...);
325
17
    if (!obj)
326
1
      return exception();
327
16
    return python::Take<PythonObject>(obj);
328
17
  }
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
321
4
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
4
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
4
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
4
                                          PythonFormat<T>::get(t)...);
325
4
    if (!obj)
326
0
      return exception();
327
4
    return python::Take<PythonObject>(obj);
328
4
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::Call<>() const
Line
Count
Source
321
103
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
103
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
103
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
103
                                          PythonFormat<T>::get(t)...);
325
103
    if (!obj)
326
2
      return exception();
327
101
    return python::Take<PythonObject>(obj);
328
103
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::Call<lldb_private::python::PythonInteger>(lldb_private::python::PythonInteger const&) const
Line
Count
Source
321
9
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
9
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
9
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
9
                                          PythonFormat<T>::get(t)...);
325
9
    if (!obj)
326
0
      return exception();
327
9
    return python::Take<PythonObject>(obj);
328
9
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::Call<lldb_private::python::PythonString>(lldb_private::python::PythonString const&) const
Line
Count
Source
321
9
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
9
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
9
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
9
                                          PythonFormat<T>::get(t)...);
325
9
    if (!obj)
326
0
      return exception();
327
9
    return python::Take<PythonObject>(obj);
328
9
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::Call<lldb_private::python::PythonCallable>(lldb_private::python::PythonCallable const&) const
Line
Count
Source
321
397
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
397
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
397
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
397
                                          PythonFormat<T>::get(t)...);
325
397
    if (!obj)
326
0
      return exception();
327
397
    return python::Take<PythonObject>(obj);
328
397
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonObject::Call<_object*, _object*, _object*>(_object* const&, _object* const&, _object* const&) const
Line
Count
Source
321
7
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
7
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
7
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
7
                                          PythonFormat<T>::get(t)...);
325
7
    if (!obj)
326
0
      return exception();
327
7
    return python::Take<PythonObject>(obj);
328
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
321
1
  llvm::Expected<PythonObject> Call(const T &... t) const {
322
1
    const char format[] = {'(', PythonFormat<T>::format..., ')', 0};
323
1
    PyObject *obj = PyObject_CallFunction(m_py_obj, py2_const_cast(format),
324
1
                                          PythonFormat<T>::get(t)...);
325
1
    if (!obj)
326
0
      return exception();
327
1
    return python::Take<PythonObject>(obj);
328
1
  }
329
330
918
  llvm::Expected<PythonObject> GetAttribute(const llvm::Twine &name) const {
331
918
    if (!m_py_obj)
332
0
      return nullDeref();
333
918
    PyObject *obj = PyObject_GetAttrString(m_py_obj, NullTerminated(name));
334
918
    if (!obj)
335
0
      return exception();
336
918
    return python::Take<PythonObject>(obj);
337
918
  }
338
339
853
  llvm::Expected<bool> IsTrue() {
340
853
    if (!m_py_obj)
341
0
      return nullDeref();
342
853
    int r = PyObject_IsTrue(m_py_obj);
343
853
    if (r < 0)
344
0
      return exception();
345
853
    return !!r;
346
853
  }
347
348
  llvm::Expected<long long> AsLongLong() const;
349
350
  llvm::Expected<long long> AsUnsignedLongLong() const;
351
352
  // wraps on overflow, instead of raising an error.
353
  llvm::Expected<unsigned long long> AsModuloUnsignedLongLong() const;
354
355
96
  llvm::Expected<bool> IsInstance(const PythonObject &cls) {
356
96
    if (!m_py_obj || !cls.IsValid())
357
0
      return nullDeref();
358
96
    int r = PyObject_IsInstance(m_py_obj, cls.get());
359
96
    if (r < 0)
360
0
      return exception();
361
96
    return !!r;
362
96
  }
363
364
protected:
365
  PyObject *m_py_obj = nullptr;
366
};
367
368
369
// This is why C++ needs monads.
370
1.31k
template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) {
371
1.31k
  if (!obj)
372
1
    return obj.takeError();
373
1.31k
  if (!T::Check(obj.get().get()))
374
0
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
375
0
                                   "type error");
376
1.31k
  return T(PyRefType::Borrowed, std::move(obj.get().get()));
377
1.31k
}
llvm::Expected<lldb_private::python::PythonString> lldb_private::python::As<lldb_private::python::PythonString>(llvm::Expected<lldb_private::python::PythonObject>&&)
Line
Count
Source
370
4
template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) {
371
4
  if (!obj)
372
1
    return obj.takeError();
373
3
  if (!T::Check(obj.get().get()))
374
0
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
375
0
                                   "type error");
376
3
  return T(PyRefType::Borrowed, std::move(obj.get().get()));
377
3
}
llvm::Expected<lldb_private::python::PythonCallable> lldb_private::python::As<lldb_private::python::PythonCallable>(llvm::Expected<lldb_private::python::PythonObject>&&)
Line
Count
Source
370
59
template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) {
371
59
  if (!obj)
372
0
    return obj.takeError();
373
59
  if (!T::Check(obj.get().get()))
374
0
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
375
0
                                   "type error");
376
59
  return T(PyRefType::Borrowed, std::move(obj.get().get()));
377
59
}
llvm::Expected<lldb_private::python::PythonDictionary> lldb_private::python::As<lldb_private::python::PythonDictionary>(llvm::Expected<lldb_private::python::PythonObject>&&)
Line
Count
Source
370
1.25k
template <typename T> llvm::Expected<T> As(llvm::Expected<PythonObject> &&obj) {
371
1.25k
  if (!obj)
372
0
    return obj.takeError();
373
1.25k
  if (!T::Check(obj.get().get()))
374
0
    return llvm::createStringError(llvm::inconvertibleErrorCode(),
375
0
                                   "type error");
376
1.25k
  return T(PyRefType::Borrowed, std::move(obj.get().get()));
377
1.25k
}
378
379
template <> llvm::Expected<bool> As<bool>(llvm::Expected<PythonObject> &&obj);
380
381
template <>
382
llvm::Expected<long long> As<long long>(llvm::Expected<PythonObject> &&obj);
383
384
template <>
385
llvm::Expected<unsigned long long>
386
As<unsigned long long>(llvm::Expected<PythonObject> &&obj);
387
388
template <>
389
llvm::Expected<std::string> As<std::string>(llvm::Expected<PythonObject> &&obj);
390
391
392
template <class T> class TypedPythonObject : public PythonObject {
393
public:
394
  // override to perform implicit type conversions on Reset
395
  // This can be eliminated once we drop python 2 support.
396
54.5k
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonDictionary>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
10.0k
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonCallable>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
3.04k
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonFile>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
18.8k
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonByteArray>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
8
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonBytes>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
32
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonList>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
358
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonBoolean>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
17
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonModule>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
22.1k
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
lldb_private::python::TypedPythonObject<lldb_private::python::PythonTuple>::Convert(lldb_private::python::PyRefType&, _object*&)
Line
Count
Source
396
3
  static void Convert(PyRefType &type, PyObject *&py_obj) {}
397
398
121k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
121k
    if (!py_obj)
400
44
      return;
401
121k
    T::Convert(type, py_obj);
402
121k
    if (T::Check(py_obj))
403
121k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
121k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonDictionary>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
10.0k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
10.0k
    if (!py_obj)
400
0
      return;
401
10.0k
    T::Convert(type, py_obj);
402
10.0k
    if (T::Check(py_obj))
403
10.0k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
10.0k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonCallable>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
3.08k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
3.08k
    if (!py_obj)
400
44
      return;
401
3.04k
    T::Convert(type, py_obj);
402
3.04k
    if (T::Check(py_obj))
403
3.04k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
3.04k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonFile>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
18.8k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
18.8k
    if (!py_obj)
400
0
      return;
401
18.8k
    T::Convert(type, py_obj);
402
18.8k
    if (T::Check(py_obj))
403
18.8k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
18.8k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonString>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
64.4k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
64.4k
    if (!py_obj)
400
0
      return;
401
64.4k
    T::Convert(type, py_obj);
402
64.4k
    if (T::Check(py_obj))
403
64.4k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
64.4k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonByteArray>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
8
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
8
    if (!py_obj)
400
0
      return;
401
8
    T::Convert(type, py_obj);
402
8
    if (T::Check(py_obj))
403
8
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
8
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonBytes>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
32
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
32
    if (!py_obj)
400
0
      return;
401
32
    T::Convert(type, py_obj);
402
32
    if (T::Check(py_obj))
403
32
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
32
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonList>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
358
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
358
    if (!py_obj)
400
0
      return;
401
358
    T::Convert(type, py_obj);
402
358
    if (T::Check(py_obj))
403
358
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
358
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonBoolean>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
17
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
17
    if (!py_obj)
400
0
      return;
401
17
    T::Convert(type, py_obj);
402
17
    if (T::Check(py_obj))
403
17
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
17
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonInteger>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
2.20k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
2.20k
    if (!py_obj)
400
0
      return;
401
2.20k
    T::Convert(type, py_obj);
402
2.20k
    if (T::Check(py_obj))
403
2.20k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
2.20k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonModule>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
22.1k
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
22.1k
    if (!py_obj)
400
0
      return;
401
22.1k
    T::Convert(type, py_obj);
402
22.1k
    if (T::Check(py_obj))
403
22.1k
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
22.1k
  }
lldb_private::python::TypedPythonObject<lldb_private::python::PythonTuple>::TypedPythonObject(lldb_private::python::PyRefType, _object*)
Line
Count
Source
398
3
  TypedPythonObject(PyRefType type, PyObject *py_obj) {
399
3
    if (!py_obj)
400
0
      return;
401
3
    T::Convert(type, py_obj);
402
3
    if (T::Check(py_obj))
403
3
      PythonObject::operator=(PythonObject(type, py_obj));
404
0
    else if (type == PyRefType::Owned)
405
0
      Py_DECREF(py_obj);
406
3
  }
407
408
71.0k
  TypedPythonObject() = default;
Unexecuted instantiation: lldb_private::python::TypedPythonObject<lldb_private::python::PythonFile>::TypedPythonObject()
lldb_private::python::TypedPythonObject<lldb_private::python::PythonString>::TypedPythonObject()
Line
Count
Source
408
61.6k
  TypedPythonObject() = default;
lldb_private::python::TypedPythonObject<lldb_private::python::PythonDictionary>::TypedPythonObject()
Line
Count
Source
408
2.92k
  TypedPythonObject() = default;
lldb_private::python::TypedPythonObject<lldb_private::python::PythonCallable>::TypedPythonObject()
Line
Count
Source
408
1.07k
  TypedPythonObject() = default;
lldb_private::python::TypedPythonObject<lldb_private::python::PythonBytes>::TypedPythonObject()
Line
Count
Source
408
16
  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
408
1.42k
  TypedPythonObject() = default;
Unexecuted instantiation: lldb_private::python::TypedPythonObject<lldb_private::python::PythonBoolean>::TypedPythonObject()
lldb_private::python::TypedPythonObject<lldb_private::python::PythonList>::TypedPythonObject()
Line
Count
Source
408
2
  TypedPythonObject() = default;
lldb_private::python::TypedPythonObject<lldb_private::python::PythonTuple>::TypedPythonObject()
Line
Count
Source
408
2.37k
  TypedPythonObject() = default;
lldb_private::python::TypedPythonObject<lldb_private::python::PythonModule>::TypedPythonObject()
Line
Count
Source
408
1.54k
  TypedPythonObject() = default;
409
};
410
411
class PythonBytes : public TypedPythonObject<PythonBytes> {
412
public:
413
  using TypedPythonObject::TypedPythonObject;
414
  explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
415
  PythonBytes(const uint8_t *bytes, size_t length);
416
417
  static bool Check(PyObject *py_obj);
418
419
  llvm::ArrayRef<uint8_t> GetBytes() const;
420
421
  size_t GetSize() const;
422
423
  void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
424
425
  StructuredData::StringSP CreateStructuredString() const;
426
};
427
428
class PythonByteArray : public TypedPythonObject<PythonByteArray> {
429
public:
430
  using TypedPythonObject::TypedPythonObject;
431
  explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes);
432
  PythonByteArray(const uint8_t *bytes, size_t length);
433
  PythonByteArray(const PythonBytes &object);
434
435
  static bool Check(PyObject *py_obj);
436
437
  llvm::ArrayRef<uint8_t> GetBytes() const;
438
439
  size_t GetSize() const;
440
441
  void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
442
443
  StructuredData::StringSP CreateStructuredString() const;
444
};
445
446
class PythonString : public TypedPythonObject<PythonString> {
447
public:
448
  using TypedPythonObject::TypedPythonObject;
449
  static llvm::Expected<PythonString> FromUTF8(llvm::StringRef string);
450
451
2
  PythonString() : TypedPythonObject() {} // MSVC requires this for some reason
452
453
  explicit PythonString(llvm::StringRef string); // safe, null on error
454
455
  static bool Check(PyObject *py_obj);
456
  static void Convert(PyRefType &type, PyObject *&py_obj);
457
458
  llvm::StringRef GetString() const; // safe, empty string on error
459
460
  llvm::Expected<llvm::StringRef> AsUTF8() const;
461
462
  size_t GetSize() const;
463
464
  void SetString(llvm::StringRef string); // safe, null on error
465
466
  StructuredData::StringSP CreateStructuredString() const;
467
};
468
469
class PythonInteger : public TypedPythonObject<PythonInteger> {
470
public:
471
  using TypedPythonObject::TypedPythonObject;
472
473
0
  PythonInteger() : TypedPythonObject() {} // MSVC requires this for some reason
474
475
  explicit PythonInteger(int64_t value);
476
477
  static bool Check(PyObject *py_obj);
478
  static void Convert(PyRefType &type, PyObject *&py_obj);
479
480
  void SetInteger(int64_t value);
481
482
  StructuredData::IntegerSP CreateStructuredInteger() const;
483
};
484
485
class PythonBoolean : public TypedPythonObject<PythonBoolean> {
486
public:
487
  using TypedPythonObject::TypedPythonObject;
488
489
  explicit PythonBoolean(bool value);
490
491
  static bool Check(PyObject *py_obj);
492
493
  bool GetValue() const;
494
495
  void SetValue(bool value);
496
497
  StructuredData::BooleanSP CreateStructuredBoolean() const;
498
};
499
500
class PythonList : public TypedPythonObject<PythonList> {
501
public:
502
  using TypedPythonObject::TypedPythonObject;
503
504
0
  PythonList() : TypedPythonObject() {} // MSVC requires this for some reason
505
506
  explicit PythonList(PyInitialValue value);
507
  explicit PythonList(int list_size);
508
509
  static bool Check(PyObject *py_obj);
510
511
  uint32_t GetSize() const;
512
513
  PythonObject GetItemAtIndex(uint32_t index) const;
514
515
  void SetItemAtIndex(uint32_t index, const PythonObject &object);
516
517
  void AppendItem(const PythonObject &object);
518
519
  StructuredData::ArraySP CreateStructuredArray() const;
520
};
521
522
class PythonTuple : public TypedPythonObject<PythonTuple> {
523
public:
524
  using TypedPythonObject::TypedPythonObject;
525
526
  explicit PythonTuple(PyInitialValue value);
527
  explicit PythonTuple(int tuple_size);
528
  PythonTuple(std::initializer_list<PythonObject> objects);
529
  PythonTuple(std::initializer_list<PyObject *> objects);
530
531
  static bool Check(PyObject *py_obj);
532
533
  uint32_t GetSize() const;
534
535
  PythonObject GetItemAtIndex(uint32_t index) const;
536
537
  void SetItemAtIndex(uint32_t index, const PythonObject &object);
538
539
  StructuredData::ArraySP CreateStructuredArray() const;
540
};
541
542
class PythonDictionary : public TypedPythonObject<PythonDictionary> {
543
public:
544
  using TypedPythonObject::TypedPythonObject;
545
546
0
  PythonDictionary() : TypedPythonObject() {} // MSVC requires this for some reason
547
548
  explicit PythonDictionary(PyInitialValue value);
549
550
  static bool Check(PyObject *py_obj);
551
552
  uint32_t GetSize() const;
553
554
  PythonList GetKeys() const;
555
556
  PythonObject GetItemForKey(const PythonObject &key) const; // DEPRECATED
557
  void SetItemForKey(const PythonObject &key,
558
                     const PythonObject &value); // DEPRECATED
559
560
  llvm::Expected<PythonObject> GetItem(const PythonObject &key) const;
561
  llvm::Expected<PythonObject> GetItem(const llvm::Twine &key) const;
562
  llvm::Error SetItem(const PythonObject &key, const PythonObject &value) const;
563
  llvm::Error SetItem(const llvm::Twine &key, const PythonObject &value) const;
564
565
  StructuredData::DictionarySP CreateStructuredDictionary() const;
566
};
567
568
class PythonModule : public TypedPythonObject<PythonModule> {
569
public:
570
  using TypedPythonObject::TypedPythonObject;
571
572
  static bool Check(PyObject *py_obj);
573
574
  static PythonModule BuiltinsModule();
575
576
  static PythonModule MainModule();
577
578
  static PythonModule AddModule(llvm::StringRef module);
579
580
  // safe, returns invalid on error;
581
0
  static PythonModule ImportModule(llvm::StringRef name) {
582
0
    std::string s = std::string(name);
583
0
    auto mod = Import(s.c_str());
584
0
    if (!mod) {
585
0
      llvm::consumeError(mod.takeError());
586
0
      return PythonModule();
587
0
    }
588
0
    return std::move(mod.get());
589
0
  }
590
591
  static llvm::Expected<PythonModule> Import(const llvm::Twine &name);
592
593
  llvm::Expected<PythonObject> Get(const llvm::Twine &name);
594
595
  PythonDictionary GetDictionary() const;
596
};
597
598
class PythonCallable : public TypedPythonObject<PythonCallable> {
599
public:
600
  using TypedPythonObject::TypedPythonObject;
601
602
  struct ArgInfo {
603
    /* the largest number of positional arguments this callable
604
     * can accept, or UNBOUNDED, ie UINT_MAX if it's a varargs
605
     * function and can accept an arbitrary number */
606
    unsigned max_positional_args;
607
    static constexpr unsigned UNBOUNDED = UINT_MAX; // FIXME c++17 inline
608
  };
609
610
  static bool Check(PyObject *py_obj);
611
612
  llvm::Expected<ArgInfo> GetArgInfo() const;
613
614
  PythonObject operator()();
615
616
  PythonObject operator()(std::initializer_list<PyObject *> args);
617
618
  PythonObject operator()(std::initializer_list<PythonObject> args);
619
620
  template <typename Arg, typename... Args>
621
2.36k
  PythonObject operator()(const Arg &arg, Args... args) {
622
2.36k
    return operator()({arg, args...});
623
2.36k
  }
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
621
42
  PythonObject operator()(const Arg &arg, Args... args) {
622
42
    return operator()({arg, args...});
623
42
  }
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
621
297
  PythonObject operator()(const Arg &arg, Args... args) {
622
297
    return operator()({arg, args...});
623
297
  }
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
621
3
  PythonObject operator()(const Arg &arg, Args... args) {
622
3
    return operator()({arg, args...});
623
3
  }
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
621
54
  PythonObject operator()(const Arg &arg, Args... args) {
622
54
    return operator()({arg, args...});
623
54
  }
lldb_private::python::PythonObject lldb_private::python::PythonCallable::operator()<lldb_private::python::PythonObject>(lldb_private::python::PythonObject const&)
Line
Count
Source
621
503
  PythonObject operator()(const Arg &arg, Args... args) {
622
503
    return operator()({arg, args...});
623
503
  }
lldb_private::python::PythonObject lldb_private::python::PythonCallable::operator()<lldb_private::python::PythonInteger>(lldb_private::python::PythonInteger const&)
Line
Count
Source
621
1.39k
  PythonObject operator()(const Arg &arg, Args... args) {
622
1.39k
    return operator()({arg, args...});
623
1.39k
  }
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
621
30
  PythonObject operator()(const Arg &arg, Args... args) {
622
30
    return operator()({arg, args...});
623
30
  }
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
621
20
  PythonObject operator()(const Arg &arg, Args... args) {
622
20
    return operator()({arg, args...});
623
20
  }
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
621
17
  PythonObject operator()(const Arg &arg, Args... args) {
622
17
    return operator()({arg, args...});
623
17
  }
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)
624
};
625
626
class PythonFile : public TypedPythonObject<PythonFile> {
627
public:
628
  using TypedPythonObject::TypedPythonObject;
629
630
0
  PythonFile() : TypedPythonObject() {} // MSVC requires this for some reason
631
632
  static bool Check(PyObject *py_obj);
633
634
  static llvm::Expected<PythonFile> FromFile(File &file,
635
                                             const char *mode = nullptr);
636
637
  llvm::Expected<lldb::FileSP> ConvertToFile(bool borrowed = false);
638
  llvm::Expected<lldb::FileSP>
639
  ConvertToFileForcingUseOfScriptingIOMethods(bool borrowed = false);
640
};
641
642
class PythonException : public llvm::ErrorInfo<PythonException> {
643
private:
644
  PyObject *m_exception_type, *m_exception, *m_traceback;
645
  PyObject *m_repr_bytes;
646
647
public:
648
  static char ID;
649
  const char *toCString() const;
650
  PythonException(const char *caller = nullptr);
651
  void Restore();
652
  ~PythonException();
653
  void log(llvm::raw_ostream &OS) const override;
654
  std::error_code convertToErrorCode() const override;
655
  bool Matches(PyObject *exc) const;
656
  std::string ReadBacktrace() const;
657
};
658
659
// This extracts the underlying T out of an Expected<T> and returns it.
660
// If the Expected is an Error instead of a T, that error will be converted
661
// into a python exception, and this will return a default-constructed T.
662
//
663
// This is appropriate for use right at the boundary of python calling into
664
// C++, such as in a SWIG typemap.   In such a context you should simply
665
// check if the returned T is valid, and if it is, return a NULL back
666
// to python.   This will result in the Error being raised as an exception
667
// from python code's point of view.
668
//
669
// For example:
670
// ```
671
// Expected<Foo *> efoop = some_cpp_function();
672
// Foo *foop = unwrapOrSetPythonException(efoop);
673
// if (!foop)
674
//    return NULL;
675
// do_something(*foop);
676
//
677
// If the Error returned was itself created because a python exception was
678
// raised when C++ code called into python, then the original exception
679
// will be restored.   Otherwise a simple string exception will be raised.
680
665
template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
681
665
  if (expected)
682
663
    return expected.get();
683
2
  llvm::handleAllErrors(
684
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
684
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
684
1
      expected.takeError(), [](PythonException &E) { E.Restore(); },
685
2
      [](const llvm::ErrorInfoBase &E) {
686
0
        PyErr_SetString(PyExc_Exception, E.message().c_str());
687
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
688
2
  return T();
689
665
}
unsigned long long lldb_private::python::unwrapOrSetPythonException<unsigned long long>(llvm::Expected<unsigned long long>)
Line
Count
Source
680
55
template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
681
55
  if (expected)
682
55
    return expected.get();
683
0
  llvm::handleAllErrors(
684
0
      expected.takeError(), [](PythonException &E) { E.Restore(); },
685
0
      [](const llvm::ErrorInfoBase &E) {
686
0
        PyErr_SetString(PyExc_Exception, E.message().c_str());
687
0
      });
688
0
  return T();
689
55
}
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
680
199
template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
681
199
  if (expected)
682
198
    return expected.get();
683
1
  llvm::handleAllErrors(
684
1
      expected.takeError(), [](PythonException &E) { E.Restore(); },
685
1
      [](const llvm::ErrorInfoBase &E) {
686
1
        PyErr_SetString(PyExc_Exception, E.message().c_str());
687
1
      });
688
1
  return T();
689
199
}
lldb_private::python::PythonFile lldb_private::python::unwrapOrSetPythonException<lldb_private::python::PythonFile>(llvm::Expected<lldb_private::python::PythonFile>)
Line
Count
Source
680
25
template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
681
25
  if (expected)
682
25
    return expected.get();
683
0
  llvm::handleAllErrors(
684
0
      expected.takeError(), [](PythonException &E) { E.Restore(); },
685
0
      [](const llvm::ErrorInfoBase &E) {
686
0
        PyErr_SetString(PyExc_Exception, E.message().c_str());
687
0
      });
688
0
  return T();
689
25
}
long long lldb_private::python::unwrapOrSetPythonException<long long>(llvm::Expected<long long>)
Line
Count
Source
680
386
template <typename T> T unwrapOrSetPythonException(llvm::Expected<T> expected) {
681
386
  if (expected)
682
385
    return expected.get();
683
1
  llvm::handleAllErrors(
684
1
      expected.takeError(), [](PythonException &E) { E.Restore(); },
685
1
      [](const llvm::ErrorInfoBase &E) {
686
1
        PyErr_SetString(PyExc_Exception, E.message().c_str());
687
1
      });
688
1
  return T();
689
386
}
690
691
// This is only here to help incrementally migrate old, exception-unsafe
692
// code.
693
3.81k
template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) {
694
3.81k
  if (expected)
695
3.81k
    return std::move(expected.get());
696
0
  llvm::consumeError(expected.takeError());
697
0
  return T();
698
3.81k
}
lldb_private::python::PythonDictionary lldb_private::python::unwrapIgnoringErrors<lldb_private::python::PythonDictionary>(llvm::Expected<lldb_private::python::PythonDictionary>)
Line
Count
Source
693
1.25k
template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) {
694
1.25k
  if (expected)
695
1.25k
    return std::move(expected.get());
696
0
  llvm::consumeError(expected.takeError());
697
0
  return T();
698
1.25k
}
lldb_private::python::PythonModule lldb_private::python::unwrapIgnoringErrors<lldb_private::python::PythonModule>(llvm::Expected<lldb_private::python::PythonModule>)
Line
Count
Source
693
2.56k
template <typename T> T unwrapIgnoringErrors(llvm::Expected<T> expected) {
694
2.56k
  if (expected)
695
2.56k
    return std::move(expected.get());
696
0
  llvm::consumeError(expected.takeError());
697
0
  return T();
698
2.56k
}
699
700
llvm::Expected<PythonObject> runStringOneLine(const llvm::Twine &string,
701
                                              const PythonDictionary &globals,
702
                                              const PythonDictionary &locals);
703
704
llvm::Expected<PythonObject> runStringMultiLine(const llvm::Twine &string,
705
                                                const PythonDictionary &globals,
706
                                                const PythonDictionary &locals);
707
708
// Sometimes the best way to interact with a python interpreter is
709
// to run some python code.   You construct a PythonScript with
710
// script string.   The script assigns some function to `_function_`
711
// and you get a C++ callable object that calls the python function.
712
//
713
// Example:
714
//
715
// const char script[] = R"(
716
// def main(x, y):
717
//    ....
718
// )";
719
//
720
// Expected<PythonObject> cpp_foo_wrapper(PythonObject x, PythonObject y) {
721
//   // no need to synchronize access to this global, we already have the GIL
722
//   static PythonScript foo(script)
723
//   return  foo(x, y);
724
// }
725
class PythonScript {
726
  const char *script;
727
  PythonCallable function;
728
729
  llvm::Error Init();
730
731
public:
732
51
  PythonScript(const char *script) : script(script), function() {}
733
734
  template <typename... Args>
735
405
  llvm::Expected<PythonObject> operator()(Args &&... args) {
736
405
    if (llvm::Error error = Init())
737
0
      return std::move(error);
738
405
    return function.Call(std::forward<Args>(args)...);
739
405
  }
llvm::Expected<lldb_private::python::PythonObject> lldb_private::python::PythonScript::operator()<lldb_private::python::PythonCallable const&>(lldb_private::python::PythonCallable const&)
Line
Count
Source
735
397
  llvm::Expected<PythonObject> operator()(Args &&... args) {
736
397
    if (llvm::Error error = Init())
737
0
      return std::move(error);
738
397
    return function.Call(std::forward<Args>(args)...);
739
397
  }
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
735
7
  llvm::Expected<PythonObject> operator()(Args &&... args) {
736
7
    if (llvm::Error error = Init())
737
0
      return std::move(error);
738
7
    return function.Call(std::forward<Args>(args)...);
739
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
735
1
  llvm::Expected<PythonObject> operator()(Args &&... args) {
736
1
    if (llvm::Error error = Init())
737
0
      return std::move(error);
738
1
    return function.Call(std::forward<Args>(args)...);
739
1
  }
740
};
741
742
class StructuredPythonObject : public StructuredData::Generic {
743
public:
744
0
  StructuredPythonObject() : StructuredData::Generic() {}
745
746
  // Take ownership of the object we received.
747
  StructuredPythonObject(PythonObject obj)
748
287
      : StructuredData::Generic(obj.release()) {}
749
750
149
  ~StructuredPythonObject() override {
751
    // Hand ownership back to a (temporary) PythonObject instance and let it
752
    // take care of releasing it.
753
149
    PythonObject(PyRefType::Owned, static_cast<PyObject *>(GetValue()));
754
149
  }
755
756
207
  bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
757
758
  void Serialize(llvm::json::OStream &s) const override;
759
760
private:
761
  StructuredPythonObject(const StructuredPythonObject &) = delete;
762
  const StructuredPythonObject &
763
  operator=(const StructuredPythonObject &) = delete;
764
};
765
766
} // namespace python
767
} // namespace lldb_private
768
769
#endif
770
771
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H