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
17 // Other libraries and framework includes
19 #include "lldb/lldb-defines.h"
20 #include "lldb/Core/ConstString.h"
21 #include "lldb/Core/StructuredData.h"
22 #include "lldb/Core/Flags.h"
23 #include "lldb/Host/File.h"
24 #include "lldb/Interpreter/OptionValue.h"
26 #include "llvm/ADT/ArrayRef.h"
28 namespace lldb_private {
33 class PythonDictionary;
36 class StructuredPythonObject : public StructuredData::Generic
39 StructuredPythonObject()
40 : StructuredData::Generic()
44 StructuredPythonObject(void *obj)
45 : StructuredData::Generic(obj)
47 Py_XINCREF(GetValue());
50 ~StructuredPythonObject() override
52 if (Py_IsInitialized())
53 Py_XDECREF(GetValue());
58 IsValid() const override
60 return GetValue() && GetValue() != Py_None;
63 void Dump(Stream &s) const override;
66 DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
69 enum class PyObjectType
86 Borrowed, // We are not given ownership of the incoming PyObject.
87 // We cannot safely hold it without calling Py_INCREF.
88 Owned // We have ownership of the incoming PyObject. We should
89 // not call Py_INCREF.
92 enum class PyInitialValue
106 PythonObject(PyRefType type, PyObject *py_obj)
112 PythonObject(const PythonObject &rhs)
118 virtual ~PythonObject()
126 // Avoid calling the virtual method since it's not necessary
127 // to actually validate the type of the PyObject if we're
128 // just setting to null.
129 if (Py_IsInitialized())
130 Py_XDECREF(m_py_obj);
135 Reset(const PythonObject &rhs)
137 // Avoid calling the virtual method if it's not necessary
138 // to actually validate the type of the PyObject.
142 Reset(PyRefType::Borrowed, rhs.m_py_obj);
145 // PythonObject is implicitly convertible to PyObject *, which will call the
146 // wrong overload. We want to explicitly disallow this, since a PyObject
147 // *always* owns its reference. Therefore the overload which takes a
148 // PyRefType doesn't make sense, and the copy constructor should be used.
150 Reset(PyRefType type, const PythonObject &ref) = delete;
153 Reset(PyRefType type, PyObject *py_obj)
155 if (py_obj == m_py_obj)
158 if (Py_IsInitialized())
159 Py_XDECREF(m_py_obj);
163 // If this is a borrowed reference, we need to convert it to
164 // an owned reference by incrementing it. If it is an owned
165 // reference (for example the caller allocated it with PyDict_New()
166 // then we must *not* increment it.
167 if (Py_IsInitialized() && type == PyRefType::Borrowed)
168 Py_XINCREF(m_py_obj);
175 _PyObject_Dump (m_py_obj);
181 Dump (Stream &strm) const;
192 PyObject *result = m_py_obj;
198 operator=(const PythonObject &other)
200 Reset(PyRefType::Borrowed, other.get());
205 GetObjectType() const;
214 ResolveNameWithDictionary(llvm::StringRef name, const PythonDictionary &dict);
218 ResolveNameWithDictionary(llvm::StringRef name, const PythonDictionary &dict)
220 return ResolveNameWithDictionary(name, dict).AsType<T>();
224 ResolveName(llvm::StringRef name) const;
228 ResolveName(llvm::StringRef name) const
230 return ResolveName(name).AsType<T>();
234 HasAttribute(llvm::StringRef attribute) const;
237 GetAttributeValue(llvm::StringRef attribute) const;
251 if (!T::Check(m_py_obj))
253 return T(PyRefType::Borrowed, m_py_obj);
256 StructuredData::ObjectSP
257 CreateStructuredObject() const;
263 class PythonBytes : public PythonObject
267 explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
268 PythonBytes(const uint8_t *bytes, size_t length);
269 PythonBytes(PyRefType type, PyObject *o);
270 PythonBytes(const PythonBytes &object);
272 ~PythonBytes() override;
275 Check(PyObject *py_obj);
277 // Bring in the no-argument base class version
278 using PythonObject::Reset;
281 Reset(PyRefType type, PyObject *py_obj) override;
283 llvm::ArrayRef<uint8_t>
290 SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
292 StructuredData::StringSP
293 CreateStructuredString() const;
296 class PythonString : public PythonObject
300 explicit PythonString(llvm::StringRef string);
301 explicit PythonString(const char *string);
302 PythonString(PyRefType type, PyObject *o);
303 PythonString(const PythonString &object);
305 ~PythonString() override;
307 static bool Check(PyObject *py_obj);
309 // Bring in the no-argument base class version
310 using PythonObject::Reset;
312 void Reset(PyRefType type, PyObject *py_obj) override;
320 void SetString(llvm::StringRef string);
322 StructuredData::StringSP CreateStructuredString() const;
325 class PythonInteger : public PythonObject
329 explicit PythonInteger(int64_t value);
330 PythonInteger(PyRefType type, PyObject *o);
331 PythonInteger(const PythonInteger &object);
333 ~PythonInteger() override;
335 static bool Check(PyObject *py_obj);
337 // Bring in the no-argument base class version
338 using PythonObject::Reset;
340 void Reset(PyRefType type, PyObject *py_obj) override;
342 int64_t GetInteger() const;
345 SetInteger (int64_t value);
347 StructuredData::IntegerSP CreateStructuredInteger() const;
350 class PythonList : public PythonObject
354 explicit PythonList(PyInitialValue value);
355 explicit PythonList(int list_size);
356 PythonList(PyRefType type, PyObject *o);
357 PythonList(const PythonList &list);
359 ~PythonList() override;
361 static bool Check(PyObject *py_obj);
363 // Bring in the no-argument base class version
364 using PythonObject::Reset;
366 void Reset(PyRefType type, PyObject *py_obj) override;
368 uint32_t GetSize() const;
370 PythonObject GetItemAtIndex(uint32_t index) const;
372 void SetItemAtIndex(uint32_t index, const PythonObject &object);
374 void AppendItem(const PythonObject &object);
376 StructuredData::ArraySP CreateStructuredArray() const;
379 class PythonTuple : public PythonObject
383 explicit PythonTuple(PyInitialValue value);
384 explicit PythonTuple(int tuple_size);
385 PythonTuple(PyRefType type, PyObject *o);
386 PythonTuple(const PythonTuple &tuple);
387 PythonTuple(std::initializer_list<PythonObject> objects);
388 PythonTuple(std::initializer_list<PyObject*> objects);
390 ~PythonTuple() override;
392 static bool Check(PyObject *py_obj);
394 // Bring in the no-argument base class version
395 using PythonObject::Reset;
397 void Reset(PyRefType type, PyObject *py_obj) override;
399 uint32_t GetSize() const;
401 PythonObject GetItemAtIndex(uint32_t index) const;
403 void SetItemAtIndex(uint32_t index, const PythonObject &object);
405 StructuredData::ArraySP CreateStructuredArray() const;
408 class PythonDictionary : public PythonObject
411 PythonDictionary() {}
412 explicit PythonDictionary(PyInitialValue value);
413 PythonDictionary(PyRefType type, PyObject *o);
414 PythonDictionary(const PythonDictionary &dict);
416 ~PythonDictionary() override;
418 static bool Check(PyObject *py_obj);
420 // Bring in the no-argument base class version
421 using PythonObject::Reset;
423 void Reset(PyRefType type, PyObject *py_obj) override;
425 uint32_t GetSize() const;
427 PythonList GetKeys() const;
429 PythonObject GetItemForKey(const PythonObject &key) const;
430 void SetItemForKey(const PythonObject &key, const PythonObject &value);
432 StructuredData::DictionarySP CreateStructuredDictionary() const;
435 class PythonModule : public PythonObject
439 PythonModule(PyRefType type, PyObject *o);
440 PythonModule(const PythonModule &dict);
442 ~PythonModule() override;
444 static bool Check(PyObject *py_obj);
453 AddModule(llvm::StringRef module);
456 ImportModule(llvm::StringRef module);
458 // Bring in the no-argument base class version
459 using PythonObject::Reset;
461 void Reset(PyRefType type, PyObject *py_obj) override;
463 PythonDictionary GetDictionary() const;
466 class PythonCallable : public PythonObject
471 bool has_varargs : 1;
476 PythonCallable(PyRefType type, PyObject *o);
477 PythonCallable(const PythonCallable &dict);
479 ~PythonCallable() override;
482 Check(PyObject *py_obj);
484 // Bring in the no-argument base class version
485 using PythonObject::Reset;
488 Reset(PyRefType type, PyObject *py_obj) override;
491 GetNumArguments() const;
497 operator ()(std::initializer_list<PyObject*> args);
500 operator ()(std::initializer_list<PythonObject> args);
502 template<typename Arg, typename... Args>
504 operator ()(const Arg &arg, Args... args)
506 return operator()({ arg, args... });
511 class PythonFile : public PythonObject
515 PythonFile(File &file, const char *mode);
516 PythonFile(const char *path, const char *mode);
517 PythonFile(PyRefType type, PyObject *o);
519 ~PythonFile() override;
521 static bool Check(PyObject *py_obj);
523 using PythonObject::Reset;
525 void Reset(PyRefType type, PyObject *py_obj) override;
526 void Reset(File &file, const char *mode);
528 bool GetUnderlyingFile(File &file) const;
531 } // namespace lldb_private
535 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H