PythonDataObjects.cpp revision c66662664c44f34f891f39e4e416f766c7347f3c
16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===// 26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// 36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// The LLVM Compiler Infrastructure 46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// 56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// License. See LICENSE.TXT for details. 76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// 86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)//===----------------------------------------------------------------------===// 96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// In order to guarantee correct working with Python, Python.h *MUST* be 116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// the *FIRST* header file included here. 126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#ifdef LLDB_DISABLE_PYTHON 136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Python is disabled in this build 156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else 176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined (__APPLE__) 196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include <Python/Python.h> 206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else 216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include <Python.h> 226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif 236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "PythonDataObjects.h" 256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)using namespace lldb_private; 276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)using namespace lldb; 286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)PythonDataObject::PythonDataObject (PyObject* object) : m_object(object) 306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles){ 316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)PythonDataString* 346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)PythonDataObject::GetStringObject () 356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles){ 366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) return new PythonDataString(GetPythonObject()); 376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 39PythonDataInteger* 40PythonDataObject::GetIntegerObject () 41{ 42 return new PythonDataInteger(GetPythonObject()); 43} 44 45PythonDataArray* 46PythonDataObject::GetArrayObject() 47{ 48 return new PythonDataArray(GetPythonObject()); 49} 50 51PythonDataDictionary* 52PythonDataObject::GetDictionaryObject() 53{ 54 return new PythonDataDictionary(GetPythonObject()); 55} 56 57PythonDataInteger::PythonDataInteger (PyObject* object) : m_object(object) 58{ 59 if (!PyInt_Check(GetPythonObject())) 60 m_object.Reset(); 61} 62 63PythonDataInteger::~PythonDataInteger () 64{ 65} 66 67PythonDataInteger::PythonDataInteger (int64_t value) : m_object(PyInt_FromLong(value)) 68{ 69} 70 71int64_t 72PythonDataInteger::GetInteger() 73{ 74 if (m_object) 75 return PyInt_AsLong(GetPythonObject()); 76 else 77 return UINT64_MAX; 78} 79 80void 81PythonDataInteger::SetInteger (int64_t value) 82{ 83 m_object.Reset(PyInt_FromLong(value)); 84} 85 86PythonDataString::PythonDataString (PyObject* object) : m_object(object) 87{ 88 if (!PyString_Check(GetPythonObject())) 89 m_object.Reset();} 90 91PythonDataString::PythonDataString (const char* string) : m_object(PyString_FromString(string)) 92{ 93} 94 95PythonDataString::~PythonDataString () 96{ 97} 98 99const char* 100PythonDataString::GetString() 101{ 102 if (m_object) 103 return PyString_AsString(GetPythonObject()); 104 return NULL; 105} 106 107void 108PythonDataString::SetString (const char* string) 109{ 110 m_object.Reset(PyString_FromString(string)); 111} 112 113PythonDataArray::PythonDataArray (uint32_t count) : m_object(PyList_New(count)) 114{ 115} 116 117PythonDataArray::PythonDataArray (PyObject* object) : m_object(object) 118{ 119 if (!PyList_Check(GetPythonObject())) 120 m_object.Reset(); 121} 122 123PythonDataArray::~PythonDataArray () 124{ 125} 126 127uint32_t 128PythonDataArray::GetSize() 129{ 130 if (m_object) 131 return PyList_GET_SIZE(GetPythonObject()); 132 return 0; 133} 134 135PythonDataObject* 136PythonDataArray::GetItemAtIndex (uint32_t index) 137{ 138 if (m_object) 139 return new PythonDataObject(PyList_GetItem(GetPythonObject(), index)); 140 return NULL; 141} 142 143void 144PythonDataArray::SetItemAtIndex (uint32_t index, PythonDataObject* object) 145{ 146 if (m_object && object && *object) 147 PyList_SetItem(GetPythonObject(), index, object->GetPythonObject()); 148} 149 150void 151PythonDataArray::AppendItem (PythonDataObject* object) 152{ 153 if (m_object && object && *object) 154 PyList_Append(GetPythonObject(), object->GetPythonObject()); 155} 156 157PythonDataDictionary::PythonDataDictionary () : m_object(PyDict_New()) 158{ 159} 160 161PythonDataDictionary::PythonDataDictionary (PyObject* object) : m_object(object) 162{ 163 if (!PyDict_Check(GetPythonObject())) 164 m_object.Reset(); 165} 166 167PythonDataDictionary::~PythonDataDictionary () 168{ 169} 170 171uint32_t 172PythonDataDictionary::GetSize() 173{ 174 if (m_object) 175 return PyDict_Size(GetPythonObject()); 176 return 0; 177} 178 179PythonDataObject* 180PythonDataDictionary::GetItemForKey (PythonDataString* key) 181{ 182 if (m_object && key && *key) 183 return new PythonDataObject(PyDict_GetItem(GetPythonObject(), key->GetPythonObject())); 184 return NULL; 185} 186 187PythonDataArray* 188PythonDataDictionary::GetKeys () 189{ 190 if (m_object) 191 return new PythonDataArray(PyDict_Keys(GetPythonObject())); 192 return NULL; 193} 194 195PythonDataString* 196PythonDataDictionary::GetKeyAtPosition (uint32_t pos) 197{ 198 PyObject *key, *value; 199 Py_ssize_t pos_iter = 0; 200 201 if (!m_object) 202 return NULL; 203 204 while (PyDict_Next(GetPythonObject(), &pos_iter, &key, &value)) { 205 if (pos-- == 0) 206 return new PythonDataString(key); 207 } 208 return NULL; 209} 210 211PythonDataObject* 212PythonDataDictionary::GetValueAtPosition (uint32_t pos) 213{ 214 PyObject *key, *value; 215 Py_ssize_t pos_iter = 0; 216 217 if (!m_object) 218 return NULL; 219 220 while (PyDict_Next(GetPythonObject(), &pos_iter, &key, &value)) { 221 if (pos-- == 0) 222 return new PythonDataObject(value); 223 } 224 return NULL; 225} 226 227void 228PythonDataDictionary::SetItemForKey (PythonDataString* key, PythonDataObject* value) 229{ 230 if (m_object && key && value && *key && *value) 231 PyDict_SetItem(GetPythonObject(), key->GetPythonObject(), value->GetPythonObject()); 232} 233 234#endif 235