PythonDataObjects.cpp revision 263363
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 <stdio.h>
25
26#include "lldb/Core/Stream.h"
27#include "lldb/Host/File.h"
28#include "lldb/Interpreter/PythonDataObjects.h"
29#include "lldb/Interpreter/ScriptInterpreter.h"
30
31using namespace lldb_private;
32using namespace lldb;
33
34//----------------------------------------------------------------------
35// PythonObject
36//----------------------------------------------------------------------
37PythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
38    m_py_obj (NULL)
39{
40    if (script_object_sp)
41        Reset ((PyObject *)script_object_sp->GetObject());
42}
43
44void
45PythonObject::Dump (Stream &strm) const
46{
47    if (m_py_obj)
48    {
49        FILE *file = ::tmpfile();
50        if (file)
51        {
52            ::PyObject_Print (m_py_obj, file, 0);
53            const long length = ftell (file);
54            if (length)
55            {
56                ::rewind(file);
57                std::vector<char> file_contents (length,'\0');
58                const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
59                if (length_read > 0)
60                    strm.Write (file_contents.data(), length_read);
61            }
62            ::fclose (file);
63        }
64    }
65    else
66        strm.PutCString ("NULL");
67}
68
69PythonString
70PythonObject::Repr ()
71{
72    if (!m_py_obj)
73        return PythonString ();
74    PyObject *repr = PyObject_Repr(m_py_obj);
75    if (!repr)
76        return PythonString ();
77    return PythonString(repr);
78}
79
80PythonString
81PythonObject::Str ()
82{
83    if (!m_py_obj)
84        return PythonString ();
85    PyObject *str = PyObject_Str(m_py_obj);
86    if (!str)
87        return PythonString ();
88    return PythonString(str);
89}
90
91//----------------------------------------------------------------------
92// PythonString
93//----------------------------------------------------------------------
94
95PythonString::PythonString (PyObject *py_obj) :
96    PythonObject()
97{
98    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a string
99}
100
101PythonString::PythonString (const PythonObject &object) :
102    PythonObject()
103{
104    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a string
105}
106
107PythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
108    PythonObject()
109{
110    if (script_object_sp)
111        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
112}
113
114PythonString::PythonString (const char* string) :
115    PythonObject(PyString_FromString(string))
116{
117}
118
119PythonString::PythonString () :
120    PythonObject()
121{
122}
123
124PythonString::~PythonString ()
125{
126}
127
128bool
129PythonString::Reset (PyObject *py_obj)
130{
131    if (py_obj && PyString_Check(py_obj))
132        return PythonObject::Reset(py_obj);
133
134    PythonObject::Reset(NULL);
135    return py_obj == NULL;
136}
137
138const char*
139PythonString::GetString() const
140{
141    if (m_py_obj)
142        return PyString_AsString(m_py_obj);
143    return NULL;
144}
145
146size_t
147PythonString::GetSize() const
148{
149    if (m_py_obj)
150        return PyString_Size(m_py_obj);
151    return 0;
152}
153
154void
155PythonString::SetString (const char* string)
156{
157    PythonObject::Reset(PyString_FromString(string));
158}
159
160//----------------------------------------------------------------------
161// PythonInteger
162//----------------------------------------------------------------------
163
164PythonInteger::PythonInteger (PyObject *py_obj) :
165    PythonObject()
166{
167    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a integer type
168}
169
170PythonInteger::PythonInteger (const PythonObject &object) :
171    PythonObject()
172{
173    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a integer type
174}
175
176PythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
177    PythonObject()
178{
179    if (script_object_sp)
180        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a string
181}
182
183PythonInteger::PythonInteger (int64_t value) :
184    PythonObject()
185{
186    SetInteger (value);
187}
188
189
190PythonInteger::~PythonInteger ()
191{
192}
193
194bool
195PythonInteger::Reset (PyObject *py_obj)
196{
197    if (py_obj)
198    {
199        if (PyInt_Check (py_obj) || PyLong_Check(py_obj))
200            return PythonObject::Reset(py_obj);
201    }
202
203    PythonObject::Reset(NULL);
204    return py_obj == NULL;
205}
206
207int64_t
208PythonInteger::GetInteger()
209{
210    if (m_py_obj)
211    {
212        if (PyInt_Check(m_py_obj))
213            return PyInt_AsLong(m_py_obj);
214        else if (PyLong_Check(m_py_obj))
215            return PyLong_AsLongLong(m_py_obj);
216    }
217    return UINT64_MAX;
218}
219
220void
221PythonInteger::SetInteger (int64_t value)
222{
223    PythonObject::Reset(PyLong_FromLongLong(value));
224}
225
226//----------------------------------------------------------------------
227// PythonList
228//----------------------------------------------------------------------
229
230PythonList::PythonList () :
231    PythonObject(PyList_New(0))
232{
233}
234
235PythonList::PythonList (uint32_t count) :
236    PythonObject(PyList_New(count))
237{
238}
239
240PythonList::PythonList (PyObject *py_obj) :
241    PythonObject()
242{
243    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a list
244}
245
246
247PythonList::PythonList (const PythonObject &object) :
248    PythonObject()
249{
250    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a list
251}
252
253PythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
254    PythonObject()
255{
256    if (script_object_sp)
257        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a list
258}
259
260PythonList::~PythonList ()
261{
262}
263
264bool
265PythonList::Reset (PyObject *py_obj)
266{
267    if (py_obj && PyList_Check(py_obj))
268        return PythonObject::Reset(py_obj);
269
270    PythonObject::Reset(NULL);
271    return py_obj == NULL;
272}
273
274uint32_t
275PythonList::GetSize()
276{
277    if (m_py_obj)
278        return PyList_GET_SIZE(m_py_obj);
279    return 0;
280}
281
282PythonObject
283PythonList::GetItemAtIndex (uint32_t index)
284{
285    if (m_py_obj)
286        return PythonObject(PyList_GetItem(m_py_obj, index));
287    return NULL;
288}
289
290void
291PythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
292{
293    if (m_py_obj && object)
294        PyList_SetItem(m_py_obj, index, object.GetPythonObject());
295}
296
297void
298PythonList::AppendItem (const PythonObject &object)
299{
300    if (m_py_obj && object)
301        PyList_Append(m_py_obj, object.GetPythonObject());
302}
303
304//----------------------------------------------------------------------
305// PythonDictionary
306//----------------------------------------------------------------------
307
308PythonDictionary::PythonDictionary () :
309    PythonObject(PyDict_New())
310{
311}
312
313PythonDictionary::PythonDictionary (PyObject *py_obj) :
314    PythonObject(py_obj)
315{
316    Reset(py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
317}
318
319
320PythonDictionary::PythonDictionary (const PythonObject &object) :
321    PythonObject()
322{
323    Reset(object.GetPythonObject()); // Use "Reset()" to ensure that py_obj is a dictionary
324}
325
326PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
327    PythonObject ()
328{
329    if (script_object_sp)
330        Reset((PyObject *)script_object_sp->GetObject()); // Use "Reset()" to ensure that py_obj is a dictionary
331}
332
333PythonDictionary::~PythonDictionary ()
334{
335}
336
337bool
338PythonDictionary::Reset (PyObject *py_obj)
339{
340    if (py_obj && PyDict_Check(py_obj))
341        return PythonObject::Reset(py_obj);
342
343    PythonObject::Reset(NULL);
344    return py_obj == NULL;
345}
346
347uint32_t
348PythonDictionary::GetSize()
349{
350    if (m_py_obj)
351        return PyDict_Size(m_py_obj);
352    return 0;
353}
354
355PythonObject
356PythonDictionary::GetItemForKey (const char *key) const
357{
358    if (key && key[0])
359    {
360        PythonString python_key(key);
361        return GetItemForKey(python_key);
362    }
363    return NULL;
364}
365
366
367PythonObject
368PythonDictionary::GetItemForKey (const PythonString &key) const
369{
370    if (m_py_obj && key)
371        return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
372    return PythonObject();
373}
374
375
376const char *
377PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
378{
379    if (m_py_obj && key)
380    {
381        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
382        if (py_obj && PyString_Check(py_obj))
383            return PyString_AsString(py_obj);
384    }
385    return fail_value;
386}
387
388int64_t
389PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
390{
391    if (m_py_obj && key)
392    {
393        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
394        if (py_obj)
395        {
396            if (PyInt_Check(py_obj))
397                return PyInt_AsLong(py_obj);
398
399            if (PyLong_Check(py_obj))
400                return PyLong_AsLong(py_obj);
401        }
402    }
403    return fail_value;
404}
405
406PythonList
407PythonDictionary::GetKeys () const
408{
409    if (m_py_obj)
410        return PythonList(PyDict_Keys(m_py_obj));
411    return PythonList();
412}
413
414PythonString
415PythonDictionary::GetKeyAtPosition (uint32_t pos) const
416{
417    PyObject *key, *value;
418    Py_ssize_t pos_iter = 0;
419
420    if (m_py_obj)
421    {
422        while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
423        {
424            if (pos-- == 0)
425                return PythonString(key);
426        }
427    }
428    return PythonString();
429}
430
431PythonObject
432PythonDictionary::GetValueAtPosition (uint32_t pos) const
433{
434    PyObject *key, *value;
435    Py_ssize_t pos_iter = 0;
436
437    if (!m_py_obj)
438        return NULL;
439
440    while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
441        if (pos-- == 0)
442            return PythonObject(value);
443    }
444    return PythonObject();
445}
446
447void
448PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
449{
450    if (m_py_obj && key && value)
451        PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());
452}
453
454#endif
455