PythonDataObjects.cpp revision 52ebc0aab1fdecb634801deceeddd71a14c2148c
145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//                     The LLVM Compiler Infrastructure
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// This file is distributed under the University of Illinois Open Source
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// License. See LICENSE.TXT for details.
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//===----------------------------------------------------------------------===//
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// In order to guarantee correct working with Python, Python.h *MUST* be
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// the *FIRST* header file included here.
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef LLDB_DISABLE_PYTHON
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// Python is disabled in this build
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#if defined (__APPLE__)
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <Python/Python.h>
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <Python.h>
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "lldb/Interpreter/PythonDataObjects.h"
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "lldb/Interpreter/ScriptInterpreter.h"
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgusing namespace lldb_private;
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgusing namespace lldb;
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// PythonObject
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    m_py_obj (NULL)
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (script_object_sp)
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        Reset ((PyObject *)script_object_sp->GetObject());
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// PythonString
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::PythonString (PyObject *py_obj) :
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(py_obj)
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::PythonString (const PythonObject &object) :
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(object.GetPythonObject())
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject (script_object_sp)
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::PythonString (const char* string) :
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(PyString_FromString(string))
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::PythonString () :
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject()
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::~PythonString ()
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbool
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::Reset (PyObject *py_obj)
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (py_obj && PyString_Check(py_obj))
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PythonObject::Reset(py_obj);
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject::Reset(NULL);
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return py_obj == NULL;
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgconst char*
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::GetString() const
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj)
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PyString_AsString(m_py_obj);
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return NULL;
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsize_t
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::GetSize() const
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj)
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PyString_Size(m_py_obj);
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonString::SetString (const char* string)
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject::Reset(PyString_FromString(string));
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// PythonInteger
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::PythonInteger (PyObject *py_obj) :
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(py_obj)
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::PythonInteger (const PythonObject &object) :
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(object.GetPythonObject())
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject (script_object_sp)
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::PythonInteger (int64_t value) :
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(PyInt_FromLong(value))
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::~PythonInteger ()
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbool
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::Reset (PyObject *py_obj)
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (py_obj && PyInt_Check(py_obj))
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PythonObject::Reset(py_obj);
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject::Reset(NULL);
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return py_obj == NULL;
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint64_t
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::GetInteger()
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj)
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PyInt_AsLong(m_py_obj);
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    else
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return UINT64_MAX;
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonInteger::SetInteger (int64_t value)
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject::Reset(PyInt_FromLong(value));
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// PythonList
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::PythonList () :
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(PyList_New(0))
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::PythonList (uint32_t count) :
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(PyList_New(count))
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::PythonList (PyObject *py_obj) :
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(py_obj)
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::PythonList (const PythonObject &object) :
18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(object.GetPythonObject())
18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject (script_object_sp)
18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::~PythonList ()
19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbool
19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::Reset (PyObject *py_obj)
19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (py_obj && PyList_Check(py_obj))
19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PythonObject::Reset(py_obj);
19845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
19945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject::Reset(NULL);
20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return py_obj == NULL;
20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orguint32_t
20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::GetSize()
20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj)
20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PyList_GET_SIZE(m_py_obj);
20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonObject
21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::GetItemAtIndex (uint32_t index)
21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj)
21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return PythonObject(PyList_GetItem(m_py_obj, index));
21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return NULL;
21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid
22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj && object)
22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        PyList_SetItem(m_py_obj, index, object.GetPythonObject());
22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid
22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonList::AppendItem (const PythonObject &object)
22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (m_py_obj && object)
23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        PyList_Append(m_py_obj, object.GetPythonObject());
23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org// PythonDictionary
23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org//----------------------------------------------------------------------
23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonDictionary::PythonDictionary () :
23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(PyDict_New())
23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonDictionary::PythonDictionary (PyObject *py_obj) :
24345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    PythonObject(py_obj)
24445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
24545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
24645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgPythonDictionary::PythonDictionary (const PythonObject &object) :
249    PythonObject(object.GetPythonObject())
250{
251}
252
253PythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
254    PythonObject (script_object_sp)
255{
256}
257
258PythonDictionary::~PythonDictionary ()
259{
260}
261
262bool
263PythonDictionary::Reset (PyObject *py_obj)
264{
265    if (py_obj && PyDict_Check(py_obj))
266        return PythonObject::Reset(py_obj);
267
268    PythonObject::Reset(NULL);
269    return py_obj == NULL;
270}
271
272uint32_t
273PythonDictionary::GetSize()
274{
275    if (m_py_obj)
276        return PyDict_Size(m_py_obj);
277    return 0;
278}
279
280PythonObject
281PythonDictionary::GetItemForKey (const char *key) const
282{
283    if (key && key[0])
284    {
285        PythonString python_key(key);
286        return GetItemForKey(python_key);
287    }
288    return NULL;
289}
290
291
292PythonObject
293PythonDictionary::GetItemForKey (const PythonString &key) const
294{
295    if (m_py_obj && key)
296        return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
297    return PythonObject();
298}
299
300
301const char *
302PythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
303{
304    if (m_py_obj && key)
305    {
306        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
307        if (py_obj && PyString_Check(py_obj))
308            return PyString_AsString(py_obj);
309    }
310    return fail_value;
311}
312
313int64_t
314PythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
315{
316    if (m_py_obj && key)
317    {
318        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
319        if (py_obj && PyInt_Check(py_obj))
320            return PyInt_AsLong(py_obj);
321    }
322    return fail_value;
323}
324
325PythonList
326PythonDictionary::GetKeys () const
327{
328    if (m_py_obj)
329        return PythonList(PyDict_Keys(m_py_obj));
330    return PythonList();
331}
332
333PythonString
334PythonDictionary::GetKeyAtPosition (uint32_t pos) const
335{
336    PyObject *key, *value;
337    Py_ssize_t pos_iter = 0;
338
339    if (m_py_obj)
340    {
341        while (PyDict_Next(m_py_obj, &pos_iter, &key, &value))
342        {
343            if (pos-- == 0)
344                return PythonString(key);
345        }
346    }
347    return PythonString();
348}
349
350PythonObject
351PythonDictionary::GetValueAtPosition (uint32_t pos) const
352{
353    PyObject *key, *value;
354    Py_ssize_t pos_iter = 0;
355
356    if (!m_py_obj)
357        return NULL;
358
359    while (PyDict_Next(m_py_obj, &pos_iter, &key, &value)) {
360        if (pos-- == 0)
361            return PythonObject(value);
362    }
363    return PythonObject();
364}
365
366void
367PythonDictionary::SetItemForKey (const PythonString &key, const PythonObject &value)
368{
369    if (m_py_obj && key && value)
370        PyDict_SetItem(m_py_obj, key.GetPythonObject(), value.GetPythonObject());
371}
372
373#endif
374