1 //===-- PythonDataObjects.h----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 11 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 12 13 // C Includes 14 // C++ Includes 15 16 // Other libraries and framework includes 17 // Project includes 18 #include "lldb/lldb-defines.h" 19 #include "lldb/Core/ConstString.h" 20 #include "lldb/Core/StructuredData.h" 21 #include "lldb/Core/Flags.h" 22 #include "lldb/Interpreter/OptionValue.h" 23 24 namespace lldb_private { 25 class PythonString; 26 class PythonList; 27 class PythonDictionary; 28 class PythonObject; 29 class PythonInteger; 30 31 class StructuredPythonObject : public StructuredData::Generic 32 { 33 public: 34 StructuredPythonObject() 35 : StructuredData::Generic() 36 { 37 } 38 39 StructuredPythonObject(void *obj) 40 : StructuredData::Generic(obj) 41 { 42 Py_XINCREF(GetValue()); 43 } 44 45 virtual ~StructuredPythonObject() 46 { 47 if (Py_IsInitialized()) 48 Py_XDECREF(GetValue()); 49 SetValue(nullptr); 50 } 51 52 bool 53 IsValid() const override 54 { 55 return GetValue() && GetValue() != Py_None; 56 } 57 58 void Dump(Stream &s) const override; 59 60 private: 61 DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject); 62 }; 63 64 enum class PyObjectType 65 { 66 Unknown, 67 None, 68 Integer, 69 Dictionary, 70 List, 71 String 72 }; 73 74 class PythonObject 75 { 76 public: 77 PythonObject () : 78 m_py_obj(NULL) 79 { 80 } 81 82 explicit PythonObject (PyObject* py_obj) : 83 m_py_obj(NULL) 84 { 85 Reset (py_obj); 86 } 87 88 PythonObject (const PythonObject &rhs) : 89 m_py_obj(NULL) 90 { 91 Reset (rhs.m_py_obj); 92 } 93 94 virtual 95 ~PythonObject () 96 { 97 Reset (NULL); 98 } 99 100 bool 101 Reset (const PythonObject &object) 102 { 103 return Reset(object.get()); 104 } 105 106 virtual bool 107 Reset (PyObject* py_obj = NULL) 108 { 109 if (py_obj != m_py_obj) 110 { 111 if (Py_IsInitialized()) 112 Py_XDECREF(m_py_obj); 113 m_py_obj = py_obj; 114 if (Py_IsInitialized()) 115 Py_XINCREF(m_py_obj); 116 } 117 return true; 118 } 119 120 void 121 Dump () const 122 { 123 if (m_py_obj) 124 _PyObject_Dump (m_py_obj); 125 else 126 puts ("NULL"); 127 } 128 129 void 130 Dump (Stream &strm) const; 131 132 PyObject* 133 get () const 134 { 135 return m_py_obj; 136 } 137 138 PyObjectType GetObjectType() const; 139 140 PythonString 141 Repr (); 142 143 PythonString 144 Str (); 145 146 explicit operator bool () const 147 { 148 return m_py_obj != NULL; 149 } 150 151 bool 152 IsNULLOrNone () const; 153 154 StructuredData::ObjectSP CreateStructuredObject() const; 155 156 protected: 157 PyObject* m_py_obj; 158 }; 159 160 class PythonString: public PythonObject 161 { 162 public: 163 PythonString (); 164 PythonString (PyObject *o); 165 PythonString (const PythonObject &object); 166 PythonString (llvm::StringRef string); 167 PythonString (const char *string); 168 virtual ~PythonString (); 169 170 virtual bool 171 Reset (PyObject* py_obj = NULL); 172 173 llvm::StringRef 174 GetString() const; 175 176 size_t 177 GetSize() const; 178 179 void SetString(llvm::StringRef string); 180 181 StructuredData::StringSP CreateStructuredString() const; 182 }; 183 184 class PythonInteger: public PythonObject 185 { 186 public: 187 188 PythonInteger (); 189 PythonInteger (PyObject* py_obj); 190 PythonInteger (const PythonObject &object); 191 PythonInteger (int64_t value); 192 virtual ~PythonInteger (); 193 194 virtual bool 195 Reset (PyObject* py_obj = NULL); 196 197 int64_t GetInteger() const; 198 199 void 200 SetInteger (int64_t value); 201 202 StructuredData::IntegerSP CreateStructuredInteger() const; 203 }; 204 205 class PythonList: public PythonObject 206 { 207 public: 208 209 PythonList (bool create_empty); 210 PythonList (PyObject* py_obj); 211 PythonList (const PythonObject &object); 212 PythonList (uint32_t count); 213 virtual ~PythonList (); 214 215 virtual bool 216 Reset (PyObject* py_obj = NULL); 217 218 uint32_t GetSize() const; 219 220 PythonObject GetItemAtIndex(uint32_t index) const; 221 222 void 223 SetItemAtIndex (uint32_t index, const PythonObject &object); 224 225 void 226 AppendItem (const PythonObject &object); 227 228 StructuredData::ArraySP CreateStructuredArray() const; 229 }; 230 231 class PythonDictionary: public PythonObject 232 { 233 public: 234 235 explicit PythonDictionary (bool create_empty); 236 PythonDictionary (PyObject* object); 237 PythonDictionary (const PythonObject &object); 238 virtual ~PythonDictionary (); 239 240 virtual bool 241 Reset (PyObject* object = NULL); 242 243 uint32_t GetSize() const; 244 245 PythonObject 246 GetItemForKey (const PythonString &key) const; 247 248 const char * 249 GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const; 250 251 int64_t 252 GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const; 253 254 PythonObject 255 GetItemForKey (const char *key) const; 256 257 typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict); 258 259 PythonList 260 GetKeys () const; 261 262 PythonString 263 GetKeyAtPosition (uint32_t pos) const; 264 265 PythonObject 266 GetValueAtPosition (uint32_t pos) const; 267 268 void 269 SetItemForKey (const PythonString &key, PyObject *value); 270 271 void 272 SetItemForKey (const PythonString &key, const PythonObject& value); 273 274 StructuredData::DictionarySP CreateStructuredDictionary() const; 275 }; 276 277 } // namespace lldb_private 278 279 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 280