1c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//===-- PythonDataObjects.cpp ------------------------------------*- C++ -*-===//
2c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//
3c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//                     The LLVM Compiler Infrastructure
4c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//
5c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata// This file is distributed under the University of Illinois Open Source
6c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata// License. See LICENSE.TXT for details.
7c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//
8c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata//===----------------------------------------------------------------------===//
9c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
10c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata// In order to guarantee correct working with Python, Python.h *MUST* be
11c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata// the *FIRST* header file included here.
12c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#ifdef LLDB_DISABLE_PYTHON
13c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
14c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata// Python is disabled in this build
15c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
16c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#else
17c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
18c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#if defined (__APPLE__)
19c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#include <Python/Python.h>
20c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#else
21c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#include <Python.h>
22c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata#endif
23884288bcb6824452a3c64eb772c0976501acc47aMatt Kopec
24884288bcb6824452a3c64eb772c0976501acc47aMatt Kopec#include <stdio.h>
25884288bcb6824452a3c64eb772c0976501acc47aMatt Kopec
26e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton#include "lldb/Core/Stream.h"
27e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton#include "lldb/Host/File.h"
28b2e953d1d753bc725ea78f573c150493b99e2205Filipe Cabecinhas#include "lldb/Interpreter/PythonDataObjects.h"
2952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton#include "lldb/Interpreter/ScriptInterpreter.h"
30c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
31c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatausing namespace lldb_private;
32c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatausing namespace lldb;
33c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
3452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
3552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton// PythonObject
3652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
3752ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonObject::PythonObject (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
3852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    m_py_obj (NULL)
39c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
4052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (script_object_sp)
4152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        Reset ((PyObject *)script_object_sp->GetObject());
42c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
43c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
44e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Claytonvoid
45e15e58facd4814a2be1cc1aa385e9f9125b92993Greg ClaytonPythonObject::Dump (Stream &strm) const
46e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton{
47e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton    if (m_py_obj)
48e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton    {
499565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton        FILE *file = ::tmpfile();
50e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton        if (file)
51e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton        {
529565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton            ::PyObject_Print (m_py_obj, file, 0);
53e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton            const long length = ftell (file);
54e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton            if (length)
55e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton            {
569565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton                ::rewind(file);
57e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton                std::vector<char> file_contents (length,'\0');
589565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton                const size_t length_read = ::fread (file_contents.data(), 1, file_contents.size(), file);
59e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton                if (length_read > 0)
609565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton                    strm.Write (file_contents.data(), length_read);
61e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton            }
629565e9dde3e33483f127815b10d6e9d5b8f46049Greg Clayton            ::fclose (file);
63e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton        }
64e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton    }
65e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton    else
66e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton        strm.PutCString ("NULL");
67e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton}
68e15e58facd4814a2be1cc1aa385e9f9125b92993Greg Clayton
6995eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico GranataPythonString
7095eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico GranataPythonObject::Repr ()
7195eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata{
7295eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    if (!m_py_obj)
7395eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata        return PythonString ();
7495eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    PyObject *repr = PyObject_Repr(m_py_obj);
7595eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    if (!repr)
7695eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata        return PythonString ();
7795eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    return PythonString(repr);
7895eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata}
7995eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata
8095eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico GranataPythonString
8195eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico GranataPythonObject::Str ()
8295eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata{
8395eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    if (!m_py_obj)
8495eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata        return PythonString ();
8595eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    PyObject *str = PyObject_Str(m_py_obj);
8695eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    if (!str)
8795eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata        return PythonString ();
8895eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata    return PythonString(str);
8995eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata}
9095eeb63432ac232f81cf9ee3301abddd7ce2f817Enrico Granata
9152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
9252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton// PythonString
9352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
9452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
9552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::PythonString (PyObject *py_obj) :
9652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(py_obj)
9712477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
9812477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
9912477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
10052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::PythonString (const PythonObject &object) :
10152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(object.GetPythonObject())
10212477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
10312477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
10412477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
10552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::PythonString (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
10652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject (script_object_sp)
107c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
108c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
10952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
11052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::PythonString (const char* string) :
11152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(PyString_FromString(string))
11252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton{
11352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton}
11452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
11552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::PythonString () :
11652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject()
117c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
118c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
119c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
12052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::~PythonString ()
121c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
122c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
123c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
12452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonbool
12552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::Reset (PyObject *py_obj)
126c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
12752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (py_obj && PyString_Check(py_obj))
12852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject::Reset(py_obj);
12952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
13052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(NULL);
13152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return py_obj == NULL;
13212477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
13312477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
13452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonconst char*
13552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::GetString() const
13612477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
13752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
13852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PyString_AsString(m_py_obj);
13952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return NULL;
140c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
141c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
14252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonsize_t
14352ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::GetSize() const
144c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
14552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
14652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PyString_Size(m_py_obj);
14752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return 0;
148c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
149c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
15052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonvoid
15152ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonString::SetString (const char* string)
152c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
15352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(PyString_FromString(string));
154c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
155c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
15652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
15752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton// PythonInteger
15852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
15912477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
16052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::PythonInteger (PyObject *py_obj) :
16152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(py_obj)
162c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
163c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
164c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
16552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::PythonInteger (const PythonObject &object) :
16652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(object.GetPythonObject())
167c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
168c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
169c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
17052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::PythonInteger (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
17152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject (script_object_sp)
172c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
173c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
174c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
17552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::PythonInteger (int64_t value) :
17652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(PyInt_FromLong(value))
17712477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
17812477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
17912477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
18052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
18152ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::~PythonInteger ()
182c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
18312477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
184c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
18552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonbool
18652ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::Reset (PyObject *py_obj)
187c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
18852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (py_obj && PyInt_Check(py_obj))
18952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject::Reset(py_obj);
19052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
19152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(NULL);
19252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return py_obj == NULL;
193c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
194c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
19552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonint64_t
19652ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::GetInteger()
197c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
19852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
19952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PyInt_AsLong(m_py_obj);
20052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    else
20152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return UINT64_MAX;
202c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
203c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
20452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonvoid
20552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonInteger::SetInteger (int64_t value)
206c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
20752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(PyInt_FromLong(value));
208c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
209c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
21052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
21152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton// PythonList
21252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
21352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
21452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::PythonList () :
21552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(PyList_New(0))
216b8f126a807c00e5c2d0e7faaf764c38072cd6764Greg Clayton{
217b8f126a807c00e5c2d0e7faaf764c38072cd6764Greg Clayton}
218b8f126a807c00e5c2d0e7faaf764c38072cd6764Greg Clayton
21952ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::PythonList (uint32_t count) :
22052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(PyList_New(count))
221c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
222c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
223c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
22452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::PythonList (PyObject *py_obj) :
22552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(py_obj)
226c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
227c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
228c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
22952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
23052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::PythonList (const PythonObject &object) :
23152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(object.GetPythonObject())
232c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
23312477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
23412477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
23552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::PythonList (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
23652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject (script_object_sp)
23712477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
238c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
239c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
24052ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::~PythonList ()
241c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
242c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
243c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
24452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonbool
24552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::Reset (PyObject *py_obj)
24652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton{
24752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (py_obj && PyList_Check(py_obj))
24852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject::Reset(py_obj);
24952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
25052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(NULL);
25152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return py_obj == NULL;
25252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton}
25352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
254c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatauint32_t
25552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::GetSize()
256c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
25752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
25852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PyList_GET_SIZE(m_py_obj);
259c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata    return 0;
260c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
261c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
26252ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonObject
26352ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::GetItemAtIndex (uint32_t index)
264c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
26552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
26652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject(PyList_GetItem(m_py_obj, index));
267c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata    return NULL;
268c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
269c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
270c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatavoid
27152ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::SetItemAtIndex (uint32_t index, const PythonObject & object)
272c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
27352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj && object)
27452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        PyList_SetItem(m_py_obj, index, object.GetPythonObject());
275c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
276c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
277c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatavoid
27852ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonList::AppendItem (const PythonObject &object)
27952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton{
28052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj && object)
28152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        PyList_Append(m_py_obj, object.GetPythonObject());
28252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton}
28352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
28452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
28552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton// PythonDictionary
28652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton//----------------------------------------------------------------------
28752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
28852ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::PythonDictionary () :
28952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(PyDict_New())
290c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
291c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
292c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
29352ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::PythonDictionary (PyObject *py_obj) :
29452ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(py_obj)
295c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
296c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
297c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
29852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
29952ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::PythonDictionary (const PythonObject &object) :
30052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject(object.GetPythonObject())
301c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
302c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
303c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
30452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::PythonDictionary (const lldb::ScriptInterpreterObjectSP &script_object_sp) :
30552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject (script_object_sp)
306c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
307c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
308c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
30952ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::~PythonDictionary ()
31052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton{
31152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton}
31252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
31352ebc0aab1fdecb634801deceeddd71a14c2148cGreg Claytonbool
31452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::Reset (PyObject *py_obj)
31552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton{
31652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (py_obj && PyDict_Check(py_obj))
31752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject::Reset(py_obj);
31852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
31952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    PythonObject::Reset(NULL);
32052ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return py_obj == NULL;
32152ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton}
32252ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton
323c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granatauint32_t
32452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::GetSize()
325c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
32652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj)
32752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PyDict_Size(m_py_obj);
328c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata    return 0;
329c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
330c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
33152ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonObject
33252ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::GetItemForKey (const char *key) const
333c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata{
33412477554ee676b775ec419575e1b091fa95bdc80Greg Clayton    if (key && key[0])
33512477554ee676b775ec419575e1b091fa95bdc80Greg Clayton    {
33652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        PythonString python_key(key);
33712477554ee676b775ec419575e1b091fa95bdc80Greg Clayton        return GetItemForKey(python_key);
33812477554ee676b775ec419575e1b091fa95bdc80Greg Clayton    }
339c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata    return NULL;
340c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata}
341c66662664c44f34f891f39e4e416f766c7347f3cEnrico Granata
34212477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
34352ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonObject
34452ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::GetItemForKey (const PythonString &key) const
34512477554ee676b775ec419575e1b091fa95bdc80Greg Clayton{
34652ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj && key)
34752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        return PythonObject(PyDict_GetItem(m_py_obj, key.GetPythonObject()));
34852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    return PythonObject();
34912477554ee676b775ec419575e1b091fa95bdc80Greg Clayton}
35012477554ee676b775ec419575e1b091fa95bdc80Greg Clayton
35188b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton
35288b980bfd735c4691b7f0771a45a78cb75033564Greg Claytonconst char *
35352ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::GetItemForKeyAsString (const PythonString &key, const char *fail_value) const
35488b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton{
35552ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj && key)
35688b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton    {
35752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
35852ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        if (py_obj && PyString_Check(py_obj))
35952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton            return PyString_AsString(py_obj);
36088b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton    }
36188b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton    return fail_value;
36288b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton}
36388b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton
36488b980bfd735c4691b7f0771a45a78cb75033564Greg Claytonint64_t
36552ebc0aab1fdecb634801deceeddd71a14c2148cGreg ClaytonPythonDictionary::GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value) const
36688b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton{
36752ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton    if (m_py_obj && key)
36888b980bfd735c4691b7f0771a45a78cb75033564Greg Clayton    {
36952ebc0aab1fdecb634801deceeddd71a14c2148cGreg Clayton        PyObject *py_obj = PyDict_GetItem(m_py_obj, key.GetPythonObject());
37036da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton        if (py_obj)
37136da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton        {
37236da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton            if (PyInt_Check(py_obj))
37336da2aa6dc5ad9994b638ed09eb81c44cc05540b