1 //===-- PythonDataObjects.h--------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
11 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
13 #ifndef LLDB_DISABLE_PYTHON
15 // LLDB Python header must be included first
16 #include "lldb-python.h"
18 #include "lldb/Utility/Flags.h"
20 #include "lldb/Host/File.h"
21 #include "lldb/Interpreter/OptionValue.h"
22 #include "lldb/Utility/ConstString.h"
23 #include "lldb/Utility/StructuredData.h"
24 #include "lldb/lldb-defines.h"
26 #include "llvm/ADT/ArrayRef.h"
28 namespace lldb_private {
33 class PythonDictionary;
36 class StructuredPythonObject : public StructuredData::Generic {
38 StructuredPythonObject() : StructuredData::Generic() {}
40 StructuredPythonObject(void *obj) : StructuredData::Generic(obj) {
41 Py_XINCREF(GetValue());
44 ~StructuredPythonObject() override {
45 if (Py_IsInitialized())
46 Py_XDECREF(GetValue());
50 bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
52 void Dump(Stream &s, bool pretty_print = true) const override;
55 DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
58 enum class PyObjectType {
73 enum class PyRefType {
74 Borrowed, // We are not given ownership of the incoming PyObject.
75 // We cannot safely hold it without calling Py_INCREF.
76 Owned // We have ownership of the incoming PyObject. We should
77 // not call Py_INCREF.
80 enum class PyInitialValue { Invalid, Empty };
84 PythonObject() : m_py_obj(nullptr) {}
86 PythonObject(PyRefType type, PyObject *py_obj) : m_py_obj(nullptr) {
90 PythonObject(const PythonObject &rhs) : m_py_obj(nullptr) { Reset(rhs); }
92 virtual ~PythonObject() { Reset(); }
95 // Avoid calling the virtual method since it's not necessary
96 // to actually validate the type of the PyObject if we're
97 // just setting to null.
98 if (Py_IsInitialized())
103 void Reset(const PythonObject &rhs) {
104 // Avoid calling the virtual method if it's not necessary
105 // to actually validate the type of the PyObject.
109 Reset(PyRefType::Borrowed, rhs.m_py_obj);
112 // PythonObject is implicitly convertible to PyObject *, which will call the
113 // wrong overload. We want to explicitly disallow this, since a PyObject
114 // *always* owns its reference. Therefore the overload which takes a
115 // PyRefType doesn't make sense, and the copy constructor should be used.
116 void Reset(PyRefType type, const PythonObject &ref) = delete;
118 virtual void Reset(PyRefType type, PyObject *py_obj) {
119 if (py_obj == m_py_obj)
122 if (Py_IsInitialized())
123 Py_XDECREF(m_py_obj);
127 // If this is a borrowed reference, we need to convert it to
128 // an owned reference by incrementing it. If it is an owned
129 // reference (for example the caller allocated it with PyDict_New()
130 // then we must *not* increment it.
131 if (Py_IsInitialized() && type == PyRefType::Borrowed)
132 Py_XINCREF(m_py_obj);
137 _PyObject_Dump(m_py_obj);
142 void Dump(Stream &strm) const;
144 PyObject *get() const { return m_py_obj; }
146 PyObject *release() {
147 PyObject *result = m_py_obj;
152 PythonObject &operator=(const PythonObject &other) {
153 Reset(PyRefType::Borrowed, other.get());
157 PyObjectType GetObjectType() const;
159 PythonString Repr() const;
161 PythonString Str() const;
163 static PythonObject ResolveNameWithDictionary(llvm::StringRef name,
164 const PythonDictionary &dict);
166 template <typename T>
167 static T ResolveNameWithDictionary(llvm::StringRef name,
168 const PythonDictionary &dict) {
169 return ResolveNameWithDictionary(name, dict).AsType<T>();
172 PythonObject ResolveName(llvm::StringRef name) const;
174 template <typename T> T ResolveName(llvm::StringRef name) const {
175 return ResolveName(name).AsType<T>();
178 bool HasAttribute(llvm::StringRef attribute) const;
180 PythonObject GetAttributeValue(llvm::StringRef attribute) const;
182 bool IsValid() const;
184 bool IsAllocated() const;
188 template <typename T> T AsType() const {
189 if (!T::Check(m_py_obj))
191 return T(PyRefType::Borrowed, m_py_obj);
194 StructuredData::ObjectSP CreateStructuredObject() const;
200 class PythonBytes : public PythonObject {
203 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
204 PythonBytes(const uint8_t *bytes, size_t length);
205 PythonBytes(PyRefType type, PyObject *o);
206 PythonBytes(const PythonBytes &object);
208 ~PythonBytes() override;
210 static bool Check(PyObject *py_obj);
212 // Bring in the no-argument base class version
213 using PythonObject::Reset;
215 void Reset(PyRefType type, PyObject *py_obj) override;
217 llvm::ArrayRef<uint8_t> GetBytes() const;
219 size_t GetSize() const;
221 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
223 StructuredData::StringSP CreateStructuredString() const;
226 class PythonByteArray : public PythonObject {
229 explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes);
230 PythonByteArray(const uint8_t *bytes, size_t length);
231 PythonByteArray(PyRefType type, PyObject *o);
232 PythonByteArray(const PythonBytes &object);
234 ~PythonByteArray() override;
236 static bool Check(PyObject *py_obj);
238 // Bring in the no-argument base class version
239 using PythonObject::Reset;
241 void Reset(PyRefType type, PyObject *py_obj) override;
243 llvm::ArrayRef<uint8_t> GetBytes() const;
245 size_t GetSize() const;
247 void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
249 StructuredData::StringSP CreateStructuredString() const;
252 class PythonString : public PythonObject {
255 explicit PythonString(llvm::StringRef string);
256 explicit PythonString(const char *string);
257 PythonString(PyRefType type, PyObject *o);
258 PythonString(const PythonString &object);
260 ~PythonString() override;
262 static bool Check(PyObject *py_obj);
264 // Bring in the no-argument base class version
265 using PythonObject::Reset;
267 void Reset(PyRefType type, PyObject *py_obj) override;
269 llvm::StringRef GetString() const;
271 size_t GetSize() const;
273 void SetString(llvm::StringRef string);
275 StructuredData::StringSP CreateStructuredString() const;
278 class PythonInteger : public PythonObject {
281 explicit PythonInteger(int64_t value);
282 PythonInteger(PyRefType type, PyObject *o);
283 PythonInteger(const PythonInteger &object);
285 ~PythonInteger() override;
287 static bool Check(PyObject *py_obj);
289 // Bring in the no-argument base class version
290 using PythonObject::Reset;
292 void Reset(PyRefType type, PyObject *py_obj) override;
294 int64_t GetInteger() const;
296 void SetInteger(int64_t value);
298 StructuredData::IntegerSP CreateStructuredInteger() const;
301 class PythonList : public PythonObject {
304 explicit PythonList(PyInitialValue value);
305 explicit PythonList(int list_size);
306 PythonList(PyRefType type, PyObject *o);
307 PythonList(const PythonList &list);
309 ~PythonList() override;
311 static bool Check(PyObject *py_obj);
313 // Bring in the no-argument base class version
314 using PythonObject::Reset;
316 void Reset(PyRefType type, PyObject *py_obj) override;
318 uint32_t GetSize() const;
320 PythonObject GetItemAtIndex(uint32_t index) const;
322 void SetItemAtIndex(uint32_t index, const PythonObject &object);
324 void AppendItem(const PythonObject &object);
326 StructuredData::ArraySP CreateStructuredArray() const;
329 class PythonTuple : public PythonObject {
332 explicit PythonTuple(PyInitialValue value);
333 explicit PythonTuple(int tuple_size);
334 PythonTuple(PyRefType type, PyObject *o);
335 PythonTuple(const PythonTuple &tuple);
336 PythonTuple(std::initializer_list<PythonObject> objects);
337 PythonTuple(std::initializer_list<PyObject *> objects);
339 ~PythonTuple() override;
341 static bool Check(PyObject *py_obj);
343 // Bring in the no-argument base class version
344 using PythonObject::Reset;
346 void Reset(PyRefType type, PyObject *py_obj) override;
348 uint32_t GetSize() const;
350 PythonObject GetItemAtIndex(uint32_t index) const;
352 void SetItemAtIndex(uint32_t index, const PythonObject &object);
354 StructuredData::ArraySP CreateStructuredArray() const;
357 class PythonDictionary : public PythonObject {
359 PythonDictionary() {}
360 explicit PythonDictionary(PyInitialValue value);
361 PythonDictionary(PyRefType type, PyObject *o);
362 PythonDictionary(const PythonDictionary &dict);
364 ~PythonDictionary() override;
366 static bool Check(PyObject *py_obj);
368 // Bring in the no-argument base class version
369 using PythonObject::Reset;
371 void Reset(PyRefType type, PyObject *py_obj) override;
373 uint32_t GetSize() const;
375 PythonList GetKeys() const;
377 PythonObject GetItemForKey(const PythonObject &key) const;
378 void SetItemForKey(const PythonObject &key, const PythonObject &value);
380 StructuredData::DictionarySP CreateStructuredDictionary() const;
383 class PythonModule : public PythonObject {
386 PythonModule(PyRefType type, PyObject *o);
387 PythonModule(const PythonModule &dict);
389 ~PythonModule() override;
391 static bool Check(PyObject *py_obj);
393 static PythonModule BuiltinsModule();
395 static PythonModule MainModule();
397 static PythonModule AddModule(llvm::StringRef module);
399 static PythonModule ImportModule(llvm::StringRef module);
401 // Bring in the no-argument base class version
402 using PythonObject::Reset;
404 void Reset(PyRefType type, PyObject *py_obj) override;
406 PythonDictionary GetDictionary() const;
409 class PythonCallable : public PythonObject {
413 bool is_bound_method : 1;
414 bool has_varargs : 1;
419 PythonCallable(PyRefType type, PyObject *o);
420 PythonCallable(const PythonCallable &dict);
422 ~PythonCallable() override;
424 static bool Check(PyObject *py_obj);
426 // Bring in the no-argument base class version
427 using PythonObject::Reset;
429 void Reset(PyRefType type, PyObject *py_obj) override;
431 ArgInfo GetNumArguments() const;
433 PythonObject operator()();
435 PythonObject operator()(std::initializer_list<PyObject *> args);
437 PythonObject operator()(std::initializer_list<PythonObject> args);
439 template <typename Arg, typename... Args>
440 PythonObject operator()(const Arg &arg, Args... args) {
441 return operator()({arg, args...});
445 class PythonFile : public PythonObject {
448 PythonFile(File &file, const char *mode);
449 PythonFile(const char *path, const char *mode);
450 PythonFile(PyRefType type, PyObject *o);
452 ~PythonFile() override;
454 static bool Check(PyObject *py_obj);
456 using PythonObject::Reset;
458 void Reset(PyRefType type, PyObject *py_obj) override;
459 void Reset(File &file, const char *mode);
461 static uint32_t GetOptionsFromMode(llvm::StringRef mode);
463 bool GetUnderlyingFile(File &file) const;
466 } // namespace lldb_private
470 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H