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/Host/File.h" 23 #include "lldb/Interpreter/OptionValue.h" 24 25 namespace lldb_private { 26 class PythonString; 27 class PythonList; 28 class PythonDictionary; 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 File 73 }; 74 75 enum class PyRefType 76 { 77 Borrowed, // We are not given ownership of the incoming PyObject. 78 // We cannot safely hold it without calling Py_INCREF. 79 Owned // We have ownership of the incoming PyObject. We should 80 // not call Py_INCREF. 81 }; 82 83 enum class PyInitialValue 84 { 85 Invalid, 86 Empty 87 }; 88 89 class PythonObject 90 { 91 public: 92 PythonObject() 93 : m_py_obj(nullptr) 94 { 95 } 96 97 PythonObject(PyRefType type, PyObject *py_obj) 98 : m_py_obj(nullptr) 99 { 100 Reset(type, py_obj); 101 } 102 103 PythonObject(const PythonObject &rhs) 104 : m_py_obj(nullptr) 105 { 106 Reset(rhs); 107 } 108 109 virtual ~PythonObject() { Reset(); } 110 111 void 112 Reset() 113 { 114 // Avoid calling the virtual method since it's not necessary 115 // to actually validate the type of the PyObject if we're 116 // just setting to null. 117 if (Py_IsInitialized()) 118 Py_XDECREF(m_py_obj); 119 m_py_obj = nullptr; 120 } 121 122 void 123 Reset(const PythonObject &rhs) 124 { 125 // Avoid calling the virtual method if it's not necessary 126 // to actually validate the type of the PyObject. 127 if (!rhs.IsValid()) 128 Reset(); 129 else 130 Reset(PyRefType::Borrowed, rhs.m_py_obj); 131 } 132 133 // PythonObject is implicitly convertible to PyObject *, which will call the 134 // wrong overload. We want to explicitly disallow this, since a PyObject 135 // *always* owns its reference. Therefore the overload which takes a 136 // PyRefType doesn't make sense, and the copy constructor should be used. 137 void 138 Reset(PyRefType type, const PythonObject &ref) = delete; 139 140 virtual void 141 Reset(PyRefType type, PyObject *py_obj) 142 { 143 if (py_obj == m_py_obj) 144 return; 145 146 if (Py_IsInitialized()) 147 Py_XDECREF(m_py_obj); 148 149 m_py_obj = py_obj; 150 151 // If this is a borrowed reference, we need to convert it to 152 // an owned reference by incrementing it. If it is an owned 153 // reference (for example the caller allocated it with PyDict_New() 154 // then we must *not* increment it. 155 if (Py_IsInitialized() && type == PyRefType::Borrowed) 156 Py_XINCREF(m_py_obj); 157 } 158 159 void 160 Dump () const 161 { 162 if (m_py_obj) 163 _PyObject_Dump (m_py_obj); 164 else 165 puts ("NULL"); 166 } 167 168 void 169 Dump (Stream &strm) const; 170 171 PyObject* 172 get() const 173 { 174 return m_py_obj; 175 } 176 177 PyObject* 178 release() 179 { 180 PyObject *result = m_py_obj; 181 m_py_obj = nullptr; 182 return result; 183 } 184 185 PyObjectType 186 GetObjectType() const; 187 188 PythonString 189 Repr (); 190 191 PythonString 192 Str (); 193 194 PythonObject & 195 operator=(const PythonObject &other) 196 { 197 Reset(PyRefType::Borrowed, other.get()); 198 return *this; 199 } 200 201 bool 202 HasAttribute(llvm::StringRef attribute) const; 203 204 PythonObject 205 GetAttributeValue(llvm::StringRef attribute) const; 206 207 bool 208 IsValid() const; 209 210 bool 211 IsAllocated() const; 212 213 bool 214 IsNone() const; 215 216 template<typename T> 217 T AsType() const 218 { 219 if (!T::Check(m_py_obj)) 220 return T(); 221 return T(PyRefType::Borrowed, m_py_obj); 222 } 223 224 StructuredData::ObjectSP CreateStructuredObject() const; 225 226 protected: 227 PyObject* m_py_obj; 228 }; 229 230 class PythonString : public PythonObject 231 { 232 public: 233 PythonString(); 234 explicit PythonString(llvm::StringRef string); 235 explicit PythonString(const char *string); 236 PythonString(PyRefType type, PyObject *o); 237 PythonString(const PythonString &object); 238 ~PythonString() override; 239 240 static bool Check(PyObject *py_obj); 241 242 // Bring in the no-argument base class version 243 using PythonObject::Reset; 244 245 void Reset(PyRefType type, PyObject *py_obj) override; 246 247 llvm::StringRef 248 GetString() const; 249 250 size_t 251 GetSize() const; 252 253 void SetString(llvm::StringRef string); 254 255 StructuredData::StringSP CreateStructuredString() const; 256 }; 257 258 class PythonInteger : public PythonObject 259 { 260 public: 261 PythonInteger(); 262 explicit PythonInteger(int64_t value); 263 PythonInteger(PyRefType type, PyObject *o); 264 PythonInteger(const PythonInteger &object); 265 ~PythonInteger() override; 266 267 static bool Check(PyObject *py_obj); 268 269 // Bring in the no-argument base class version 270 using PythonObject::Reset; 271 272 void Reset(PyRefType type, PyObject *py_obj) override; 273 274 int64_t GetInteger() const; 275 276 void 277 SetInteger (int64_t value); 278 279 StructuredData::IntegerSP CreateStructuredInteger() const; 280 }; 281 282 class PythonList : public PythonObject 283 { 284 public: 285 explicit PythonList(PyInitialValue value); 286 explicit PythonList(int list_size); 287 PythonList(PyRefType type, PyObject *o); 288 PythonList(const PythonList &list); 289 ~PythonList() override; 290 291 static bool Check(PyObject *py_obj); 292 293 // Bring in the no-argument base class version 294 using PythonObject::Reset; 295 296 void Reset(PyRefType type, PyObject *py_obj) override; 297 298 uint32_t GetSize() const; 299 300 PythonObject GetItemAtIndex(uint32_t index) const; 301 302 void SetItemAtIndex(uint32_t index, const PythonObject &object); 303 304 void AppendItem(const PythonObject &object); 305 306 StructuredData::ArraySP CreateStructuredArray() const; 307 }; 308 309 class PythonDictionary : public PythonObject 310 { 311 public: 312 explicit PythonDictionary(PyInitialValue value); 313 PythonDictionary(PyRefType type, PyObject *o); 314 PythonDictionary(const PythonDictionary &dict); 315 ~PythonDictionary() override; 316 317 static bool Check(PyObject *py_obj); 318 319 // Bring in the no-argument base class version 320 using PythonObject::Reset; 321 322 void Reset(PyRefType type, PyObject *py_obj) override; 323 324 uint32_t GetSize() const; 325 326 PythonList GetKeys() const; 327 328 PythonObject GetItemForKey(const PythonObject &key) const; 329 void SetItemForKey(const PythonObject &key, const PythonObject &value); 330 331 StructuredData::DictionarySP CreateStructuredDictionary() const; 332 }; 333 334 class PythonFile : public PythonObject 335 { 336 public: 337 PythonFile(); 338 PythonFile(File &file, const char *mode); 339 PythonFile(const char *path, const char *mode); 340 PythonFile(PyRefType type, PyObject *o); 341 ~PythonFile() override; 342 343 static bool Check(PyObject *py_obj); 344 345 using PythonObject::Reset; 346 347 void Reset(PyRefType type, PyObject *py_obj) override; 348 void Reset(File &file, const char *mode); 349 350 bool GetUnderlyingFile(File &file) const; 351 }; 352 353 } // namespace lldb_private 354 355 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H 356