Enrico Granata | c666626 | 2012-08-23 22:02:23 +0000 | [diff] [blame^] | 1 | //===-- PythonDataObjects.cpp ------------------------------------*- 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 | // In order to guarantee correct working with Python, Python.h *MUST* be |
| 11 | // the *FIRST* header file included here. |
| 12 | #ifdef LLDB_DISABLE_PYTHON |
| 13 | |
| 14 | // Python is disabled in this build |
| 15 | |
| 16 | #else |
| 17 | |
| 18 | #if defined (__APPLE__) |
| 19 | #include <Python/Python.h> |
| 20 | #else |
| 21 | #include <Python.h> |
| 22 | #endif |
| 23 | |
| 24 | #include "PythonDataObjects.h" |
| 25 | |
| 26 | using namespace lldb_private; |
| 27 | using namespace lldb; |
| 28 | |
| 29 | PythonDataObject::PythonDataObject (PyObject* object) : m_object(object) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | PythonDataString* |
| 34 | PythonDataObject::GetStringObject () |
| 35 | { |
| 36 | return new PythonDataString(GetPythonObject()); |
| 37 | } |
| 38 | |
| 39 | PythonDataInteger* |
| 40 | PythonDataObject::GetIntegerObject () |
| 41 | { |
| 42 | return new PythonDataInteger(GetPythonObject()); |
| 43 | } |
| 44 | |
| 45 | PythonDataArray* |
| 46 | PythonDataObject::GetArrayObject() |
| 47 | { |
| 48 | return new PythonDataArray(GetPythonObject()); |
| 49 | } |
| 50 | |
| 51 | PythonDataDictionary* |
| 52 | PythonDataObject::GetDictionaryObject() |
| 53 | { |
| 54 | return new PythonDataDictionary(GetPythonObject()); |
| 55 | } |
| 56 | |
| 57 | PythonDataInteger::PythonDataInteger (PyObject* object) : m_object(object) |
| 58 | { |
| 59 | if (!PyInt_Check(GetPythonObject())) |
| 60 | m_object.Reset(); |
| 61 | } |
| 62 | |
| 63 | PythonDataInteger::~PythonDataInteger () |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | PythonDataInteger::PythonDataInteger (int64_t value) : m_object(PyInt_FromLong(value)) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | int64_t |
| 72 | PythonDataInteger::GetInteger() |
| 73 | { |
| 74 | if (m_object) |
| 75 | return PyInt_AsLong(GetPythonObject()); |
| 76 | else |
| 77 | return UINT64_MAX; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | PythonDataInteger::SetInteger (int64_t value) |
| 82 | { |
| 83 | m_object.Reset(PyInt_FromLong(value)); |
| 84 | } |
| 85 | |
| 86 | PythonDataString::PythonDataString (PyObject* object) : m_object(object) |
| 87 | { |
| 88 | if (!PyString_Check(GetPythonObject())) |
| 89 | m_object.Reset();} |
| 90 | |
| 91 | PythonDataString::PythonDataString (const char* string) : m_object(PyString_FromString(string)) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | PythonDataString::~PythonDataString () |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | const char* |
| 100 | PythonDataString::GetString() |
| 101 | { |
| 102 | if (m_object) |
| 103 | return PyString_AsString(GetPythonObject()); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | PythonDataString::SetString (const char* string) |
| 109 | { |
| 110 | m_object.Reset(PyString_FromString(string)); |
| 111 | } |
| 112 | |
| 113 | PythonDataArray::PythonDataArray (uint32_t count) : m_object(PyList_New(count)) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | PythonDataArray::PythonDataArray (PyObject* object) : m_object(object) |
| 118 | { |
| 119 | if (!PyList_Check(GetPythonObject())) |
| 120 | m_object.Reset(); |
| 121 | } |
| 122 | |
| 123 | PythonDataArray::~PythonDataArray () |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | uint32_t |
| 128 | PythonDataArray::GetSize() |
| 129 | { |
| 130 | if (m_object) |
| 131 | return PyList_GET_SIZE(GetPythonObject()); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | PythonDataObject* |
| 136 | PythonDataArray::GetItemAtIndex (uint32_t index) |
| 137 | { |
| 138 | if (m_object) |
| 139 | return new PythonDataObject(PyList_GetItem(GetPythonObject(), index)); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | PythonDataArray::SetItemAtIndex (uint32_t index, PythonDataObject* object) |
| 145 | { |
| 146 | if (m_object && object && *object) |
| 147 | PyList_SetItem(GetPythonObject(), index, object->GetPythonObject()); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | PythonDataArray::AppendItem (PythonDataObject* object) |
| 152 | { |
| 153 | if (m_object && object && *object) |
| 154 | PyList_Append(GetPythonObject(), object->GetPythonObject()); |
| 155 | } |
| 156 | |
| 157 | PythonDataDictionary::PythonDataDictionary () : m_object(PyDict_New()) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | PythonDataDictionary::PythonDataDictionary (PyObject* object) : m_object(object) |
| 162 | { |
| 163 | if (!PyDict_Check(GetPythonObject())) |
| 164 | m_object.Reset(); |
| 165 | } |
| 166 | |
| 167 | PythonDataDictionary::~PythonDataDictionary () |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | uint32_t |
| 172 | PythonDataDictionary::GetSize() |
| 173 | { |
| 174 | if (m_object) |
| 175 | return PyDict_Size(GetPythonObject()); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | PythonDataObject* |
| 180 | PythonDataDictionary::GetItemForKey (PythonDataString* key) |
| 181 | { |
| 182 | if (m_object && key && *key) |
| 183 | return new PythonDataObject(PyDict_GetItem(GetPythonObject(), key->GetPythonObject())); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | PythonDataArray* |
| 188 | PythonDataDictionary::GetKeys () |
| 189 | { |
| 190 | if (m_object) |
| 191 | return new PythonDataArray(PyDict_Keys(GetPythonObject())); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | PythonDataString* |
| 196 | PythonDataDictionary::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 | |
| 211 | PythonDataObject* |
| 212 | PythonDataDictionary::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 | |
| 227 | void |
| 228 | PythonDataDictionary::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 |