aboutsummaryrefslogtreecommitdiff
path: root/source/Interpreter/PythonDataObjects.cpp
blob: 1e2bd2391191bb86eee1fb1dd8e15a0696529eb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// In order to guarantee correct working with Python, Python.h *MUST* be
// the *FIRST* header file included here.
#ifdef LLDB_DISABLE_PYTHON

// Python is disabled in this build

#else

#include "lldb/lldb-python.h"

#include <stdio.h>

#include "lldb/Core/Stream.h"
#include "lldb/Host/File.h"
#include "lldb/Interpreter/PythonDataObjects.h"
#include "lldb/Interpreter/ScriptInterpreter.h"

using namespace lldb_private;
using namespace lldb;

//----------------------------------------------------------------------
// PythonObject
//----------------------------------------------------------------------
PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
    m_py_obj (NULL)
{
    if (script_object_sp)
        Reset ((PyObject *)script_object_sp->GetObject());
}

void
PythonObject::Dump (Stream &strm) const
{
    if (m_py_obj)
    {
        FILE *file = ::tmpfile();
        if (file)
        {
            ::PyObject_Print (m_py_obj, file, 0);
            const long length = ftell (file);
            if (length)
            {
                ::rewind(file);
                std::vector<char> file_contents (length,'\0');
                const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
                if (length_read > 0)
                    strm.Write (file_contents.data(), length_read);
            }
            ::fclose (file);
        }
    }
    else
        strm.PutCString ("NULL");
}

PythonString
PythonObject::Repr ()
{
    if (!m_py_obj)
        return PythonString ();
    PyObject *repr = PyObject_Repr(m_py_obj);
    if (!repr)
        return PythonString ();
    return PythonString(repr);
}

PythonString
PythonObject::Str ()
{
    if (!m_py_obj)
        return PythonString ();
    PyObject *str = PyObject_Str(m_py_obj);
    if (!str)
        return PythonString ();
    return PythonString(str);
}

//----------------------------------------------------------------------
// PythonString
//----------------------------------------------------------------------

PythonString::PythonString (PyObject *py_obj) :
    PythonObject()
{
    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string
}

PythonString::PythonString (const PythonObject &object) :
    PythonObject()
{
    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a string
}

PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
    PythonObject()
{
    if (script_object_sp)
        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
}

PythonString::PythonString (const char* string) :
    PythonObject(PyString_FromString(string))
{
}

PythonString::PythonString () :
    PythonObject()
{
}

PythonString::~PythonString ()
{
}

bool
PythonString::Reset (PyObject *py_obj)
{
    if (py_obj && PyString_Check(py_obj))
        return PythonObject::Reset(py_obj);
    
    PythonObject::Reset(NULL);
    return py_obj == NULL;
}

const char*
PythonString::GetString() const
{
    if (m_py_obj)
        return PyString_AsString(m_py_obj);
    return NULL;
}

size_t
PythonString::GetSize() const
{
    if (m_py_obj)
        return PyString_Size(m_py_obj);
    return 0;
}

void
PythonString::SetString (const char* string)
{
    PythonObject::Reset(PyString_FromString(string));
}

//----------------------------------------------------------------------
// PythonInteger
//----------------------------------------------------------------------

PythonInteger::PythonInteger (PyObject *py_obj) :
    PythonObject()
{
    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type
}

PythonInteger::PythonInteger (const PythonObject &object) :
    PythonObject()
{
    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a integer type
}

PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
    PythonObject()
{
    if (script_object_sp)
        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
}

PythonInteger::PythonInteger (int64_t value) :
    PythonObject()
{
    SetInteger (value);
}


PythonInteger::~PythonInteger ()
{
}

bool
PythonInteger::Reset (PyObject *py_obj)
{
    if (py_obj)
    {
        if (PyInt_Check (py_obj) || PyLong_Check(py_obj))
            return PythonObject::Reset(py_obj);
    }
    
    PythonObject::Reset(NULL);
    return py_obj == NULL;
}

int64_t
PythonInteger::GetInteger()
{
    if (m_py_obj)
    {
        if (PyInt_Check(m_py_obj))
            return PyInt_AsLong(m_py_obj);
        else if (PyLong_Check(m_py_obj))
            return PyLong_AsLongLong(m_py_obj);
    }
    return UINT64_MAX;
}

void
PythonInteger::SetInteger (int64_t value)
{
    PythonObject::Reset(PyLong_FromLongLong(value));
}

//----------------------------------------------------------------------
// PythonList
//----------------------------------------------------------------------

PythonList::PythonList () :
    PythonObject(PyList_New(0))
{
}

PythonList::PythonList (uint32_t count) :
    PythonObject(PyList_New(count))
{
}

PythonList::PythonList (PyObject *py_obj) :
    PythonObject()
{
    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list
}


PythonList::PythonList (const PythonObject &object) :
    PythonObject()
{
    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a list
}

PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
    PythonObject()
{
    if (script_object_sp)
        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list
}

PythonList::~PythonList ()
{
}

bool
PythonList::Reset (PyObject *py_obj)
{
    if (py_obj && PyList_Check(py_obj))
        return PythonObject::Reset(py_obj);
    
    PythonObject::Reset(NULL);
    return py_obj == NULL;
}

uint32_t
PythonList::GetSize()
{
    if (m_py_obj)
        return PyList_GET_SIZE(m_py_obj);
    return 0;
}

PythonObject
PythonList::GetItemAtIndex (uint32_t index)
{
    if (m_py_obj)
        return PythonObject(PyList_GetItem(m_py_obj, index));
    return NULL;
}

void
PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
{
    if (m_py_obj && object)
        PyList_SetItem(m_py_obj, index, object.GetPythonObject());
}

void
PythonList::AppendItem (const PythonObject &object)
{
    if (m_py_obj && object)
        PyList_Append(m_py_obj, object.GetPythonObject());
}

//----------------------------------------------------------------------
// PythonDictionary
//----------------------------------------------------------------------

PythonDictionary::PythonDictionary () :
    PythonObject(PyDict_New())
{
}

PythonDictionary::PythonDictionary (PyObject *py_obj) :
    PythonObject(py_obj)
{
    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
}


PythonDictionary::PythonDictionary (const PythonObject &object) :
    PythonObject()
{
    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a dictionary
}

PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
    PythonObject ()
{
    if (script_object_sp)
        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary
}

PythonDictionary::~PythonDictionary ()
{
}

bool
PythonDictionary::Reset (PyObject *py_obj)
{
    if (py_obj && PyDict_Check(py_obj))
        return PythonObject::Reset(py_obj);
    
    PythonObject::Reset(NULL);
    return py_obj == NULL;
}

uint32_t
PythonDictionary::GetSize()
{
    if (m_py_obj)
        return PyDict_Size(m_py_obj);
    return 0;
}

PythonObject
PythonDictionary::GetItemForKey (const char *key) const
{
    if (key && key[0])
    {
        PythonString python_key(key);
        return GetItemForKey(python_key);
    }
    return NULL;
}


PythonObject
PythonDictionary::GetItemForKey (const PythonString &key) const
{
    if (m_py_obj && key)
        return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
    return PythonObject();
}


const char *
PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
{
    if (m_py_obj && key)
    {
        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
        if (py_obj && PyString_Check(py_obj))
            return PyString_AsString(py_obj);
    }
    return fail_value;
}

int64_t
PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
{
    if (m_py_obj && key)
    {
        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
        if (py_obj)
        {
            if (PyInt_Check(py_obj))
                return PyInt_AsLong(py_obj);

            if (PyLong_Check(py_obj))
                return PyLong_AsLong(py_obj);
        }
    }
    return fail_value;
}

PythonList
PythonDictionary::GetKeys () const
{
    if (m_py_obj)
        return PythonList(PyDict_Keys(m_py_obj));
    return PythonList();
}

PythonString
PythonDictionary::GetKeyAtPosition (uint32_t pos) const
{
    PyObject *key, *value;
    Py_ssize_t pos_iter = 0;
    
    if (m_py_obj)
    {
        while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
        {
            if (pos-- == 0)
                return PythonString(key);
        }
    }
    return PythonString();
}

PythonObject
PythonDictionary::GetValueAtPosition (uint32_t pos) const
{
    PyObject *key, *value;
    Py_ssize_t pos_iter = 0;
    
    if (!m_py_obj)
        return NULL;
    
    while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
        if (pos-- == 0)
            return PythonObject(value);
    }
    return PythonObject();
}

void
PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
{
    if (m_py_obj && key && value)
        PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());
}

#endif