<div dir="ltr">This breaks the build on Linux.  Working on a fix right now.</div><br><div class="gmail_quote"><div dir="ltr">On Fri, Oct 9, 2015 at 12:47 PM Zachary Turner via lldb-commits <<a href="mailto:lldb-commits@lists.llvm.org">lldb-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: zturner<br>
Date: Fri Oct  9 14:45:41 2015<br>
New Revision: 249886<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=249886&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=249886&view=rev</a><br>
Log:<br>
Port native Python-API to 3.x<br>
<br>
With this change, liblldb is 95% of the way towards being able<br>
to work under both Python 2.x and Python 3.x.  This should<br>
introduce no functional change for Python 2.x, but for Python<br>
3.x there are some important changes.  Primarily, these are:<br>
<br>
1) PyString doesn't exist in Python 3.  Everything is a PyUnicode.<br>
   To account for this, PythonString now stores a PyBytes instead<br>
   of a PyString.  In Python 2, this is equivalent to a PyUnicode,<br>
   and in Python 3, we do a conversion from PyUnicode to PyBytes<br>
   and store the PyBytes.<br>
2) PyInt doesn't exist in Python 3.  Everything is a PyLong.  To<br>
   account for this, PythonInteger stores a PyLong instead of a<br>
   PyInt.  In Python 2.x, this requires doing a conversion to<br>
   PyLong when creating a PythonInteger from a PyInt.  In 3.x,<br>
   there is no PyInt anyway, so we can assume everything is a<br>
   PyLong.<br>
3) PyFile_FromFile doesn't exist in Python 3.  Instead there is a<br>
   PyFile_FromFd.  This is not addressed in this patch because it<br>
   will require quite a large change to plumb fd's all the way<br>
   through the system into the ScriptInterpreter.  This is the only<br>
   remaining piece of the puzzle to get LLDB supporting Python 3.x.<br>
<br>
Being able to run the test suite is not addressed in this patch.<br>
After the extension module can compile and you can enter an embedded<br>
3.x interpreter, the test suite will be addressed in a followup.<br>
<br>
Added:<br>
    lldb/trunk/unittests/ScriptInterpreter/<br>
    lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt<br>
    lldb/trunk/unittests/ScriptInterpreter/Python/<br>
    lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt<br>
    lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp<br>
Modified:<br>
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp<br>
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h<br>
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp<br>
    lldb/trunk/unittests/CMakeLists.txt<br>
<br>
Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=249886&r1=249885&r2=249886&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=249886&r1=249885&r2=249886&view=diff</a><br>
==============================================================================<br>
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (original)<br>
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp Fri Oct  9 14:45:41 2015<br>
@@ -71,10 +71,18 @@ PythonObject::GetObjectType() const<br>
         return PyObjectType::List;<br>
     if (PyDict_Check(m_py_obj))<br>
         return PyObjectType::Dictionary;<br>
+    if (PyUnicode_Check(m_py_obj))<br>
+        return PyObjectType::String;<br>
+    if (PyLong_Check(m_py_obj))<br>
+        return PyObjectType::Integer;<br>
+#if PY_MAJOR_VERSION < 3<br>
+    // These functions don't exist in Python 3.x.  PyString is PyUnicode<br>
+    // and PyInt is PyLong.<br>
     if (PyString_Check(m_py_obj))<br>
         return PyObjectType::String;<br>
-    if (PyInt_Check(m_py_obj) || PyLong_Check(m_py_obj))<br>
+    if (PyInt_Check(m_py_obj))<br>
         return PyObjectType::Integer;<br>
+#endif<br>
     return PyObjectType::Unknown;<br>
 }<br>
<br>
@@ -142,14 +150,16 @@ PythonString::PythonString (const Python<br>
     Reset(object.get()); // Use "Reset()" to ensure that py_obj is a string<br>
 }<br>
<br>
-PythonString::PythonString (llvm::StringRef string) :<br>
-    PythonObject(PyString_FromStringAndSize(string.data(), string.size()))<br>
+PythonString::PythonString(llvm::StringRef string)<br>
+    : PythonObject()<br>
 {<br>
+    SetString(string);<br>
 }<br>
<br>
-PythonString::PythonString(const char *string) :<br>
-    PythonObject(PyString_FromString(string))<br>
+PythonString::PythonString(const char *string)<br>
+    : PythonObject()<br>
 {<br>
+    SetString(llvm::StringRef(string));<br>
 }<br>
<br>
 PythonString::PythonString () :<br>
@@ -162,20 +172,53 @@ PythonString::~PythonString ()<br>
 }<br>
<br>
 bool<br>
-PythonString::Reset (PyObject *py_obj)<br>
+PythonString::Check(PyObject *py_obj)<br>
+{<br>
+    if (!py_obj)<br>
+        return false;<br>
+#if PY_MAJOR_VERSION >= 3<br>
+    // Python 3 does not have PyString objects, only PyUnicode.<br>
+    return PyUnicode_Check(py_obj);<br>
+#else<br>
+    return PyUnicode_Check(py_obj) || PyString_Check(py_obj);<br>
+#endif<br>
+}<br>
+<br>
+bool<br>
+PythonString::Reset(PyObject *py_obj)<br>
 {<br>
-    if (py_obj && PyString_Check(py_obj))<br>
-        return PythonObject::Reset(py_obj);<br>
-<br>
-    PythonObject::Reset(nullptr);<br>
-    return py_obj == nullptr;<br>
+    if (!PythonString::Check(py_obj))<br>
+    {<br>
+        PythonObject::Reset(nullptr);<br>
+        return false;<br>
+    }<br>
+<br>
+// Convert this to a PyBytes object, and only store the PyBytes.  Note that in<br>
+// Python 2.x, PyString and PyUnicode are interchangeable, and PyBytes is an alias<br>
+// of PyString.  So on 2.x, if we get into this branch, we already have a PyBytes.<br>
+    //#if PY_MAJOR_VERSION >= 3<br>
+    if (PyUnicode_Check(py_obj))<br>
+    {<br>
+        PyObject *unicode = py_obj;<br>
+        py_obj = PyUnicode_AsUTF8String(py_obj);<br>
+        Py_XDECREF(unicode);<br>
+    }<br>
+    //#endif<br>
+<br>
+    assert(PyBytes_Check(py_obj) && "PythonString::Reset received a non-string");<br>
+    return PythonObject::Reset(py_obj);<br>
 }<br>
<br>
 llvm::StringRef<br>
 PythonString::GetString() const<br>
 {<br>
     if (m_py_obj)<br>
-        return llvm::StringRef(PyString_AsString(m_py_obj), GetSize());<br>
+    {<br>
+        Py_ssize_t size;<br>
+        char *c;<br>
+        PyBytes_AsStringAndSize(m_py_obj, &c, &size);<br>
+        return llvm::StringRef(c, size);<br>
+    }<br>
     return llvm::StringRef();<br>
 }<br>
<br>
@@ -183,14 +226,21 @@ size_t<br>
 PythonString::GetSize() const<br>
 {<br>
     if (m_py_obj)<br>
-        return PyString_Size(m_py_obj);<br>
+        return PyBytes_Size(m_py_obj);<br>
     return 0;<br>
 }<br>
<br>
 void<br>
 PythonString::SetString (llvm::StringRef string)<br>
 {<br>
+#if PY_MAJOR_VERSION >= 3<br>
+    PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size());<br>
+    PyObject *bytes = PyUnicode_AsUTF8String(unicode);<br>
+    PythonObject::Reset(bytes);<br>
+    Py_XDECREF(unicode);<br>
+#else<br>
     PythonObject::Reset(PyString_FromStringAndSize(string.data(), string.size()));<br>
+#endif<br>
 }<br>
<br>
 StructuredData::StringSP<br>
@@ -229,16 +279,44 @@ PythonInteger::~PythonInteger ()<br>
 }<br>
<br>
 bool<br>
-PythonInteger::Reset (PyObject *py_obj)<br>
+PythonInteger::Check(PyObject *py_obj)<br>
 {<br>
-    if (py_obj)<br>
+    if (!py_obj)<br>
+        return false;<br>
+<br>
+#if PY_MAJOR_VERSION >= 3<br>
+    // Python 3 does not have PyInt_Check.  There is only one type of<br>
+    // integral value, long.<br>
+    return PyLong_Check(py_obj);<br>
+#else<br>
+    return PyLong_Check(py_obj) || PyInt_Check(py_obj);<br>
+#endif<br>
+}<br>
+<br>
+bool<br>
+PythonInteger::Reset(PyObject *py_obj)<br>
+{<br>
+    if (!PythonInteger::Check(py_obj))<br>
     {<br>
-        if (PyInt_Check (py_obj) || PyLong_Check(py_obj))<br>
-            return PythonObject::Reset(py_obj);<br>
+        PythonObject::Reset(nullptr);<br>
+        return false;<br>
     }<br>
-<br>
-    PythonObject::Reset(nullptr);<br>
-    return py_obj == nullptr;<br>
+<br>
+#if PY_MAJOR_VERSION < 3<br>
+    // Always store this as a PyLong, which makes interoperability between<br>
+    // Python 2.x and Python 3.x easier.  This is only necessary in 2.x,<br>
+    // since 3.x doesn't even have a PyInt.<br>
+    if (PyInt_Check(py_obj))<br>
+    {<br>
+        PyObject *py_long = PyLong_FromLongLong(PyInt_AsLong(py_obj));<br>
+        Py_XDECREF(py_obj);<br>
+        py_obj = py_long;<br>
+    }<br>
+#endif<br>
+<br>
+    assert(PyLong_Check(py_obj) && "Couldn't get a PyLong from this PyObject");<br>
+<br>
+    return PythonObject::Reset(py_obj);<br>
 }<br>
<br>
 int64_t<br>
@@ -246,10 +324,9 @@ PythonInteger::GetInteger() const<br>
 {<br>
     if (m_py_obj)<br>
     {<br>
-        if (PyInt_Check(m_py_obj))<br>
-            return PyInt_AsLong(m_py_obj);<br>
-        else if (PyLong_Check(m_py_obj))<br>
-            return PyLong_AsLongLong(m_py_obj);<br>
+        assert(PyLong_Check(m_py_obj) && "PythonInteger::GetInteger has a PyObject that isn't a PyLong");<br>
+<br>
+        return PyLong_AsLongLong(m_py_obj);<br>
     }<br>
     return UINT64_MAX;<br>
 }<br>
@@ -272,13 +349,8 @@ PythonInteger::CreateStructuredInteger()<br>
 // PythonList<br>
 //----------------------------------------------------------------------<br>
<br>
-PythonList::PythonList (bool create_empty) :<br>
-    PythonObject(create_empty ? PyList_New(0) : nullptr)<br>
-{<br>
-}<br>
-<br>
-PythonList::PythonList (uint32_t count) :<br>
-    PythonObject(PyList_New(count))<br>
+PythonList::PythonList()<br>
+    : PythonObject(PyList_New(0))<br>
 {<br>
 }<br>
<br>
@@ -300,13 +372,23 @@ PythonList::~PythonList ()<br>
 }<br>
<br>
 bool<br>
-PythonList::Reset (PyObject *py_obj)<br>
+PythonList::Check(PyObject *py_obj)<br>
+{<br>
+    if (!py_obj)<br>
+        return false;<br>
+    return PyList_Check(py_obj);<br>
+}<br>
+<br>
+bool<br>
+PythonList::Reset(PyObject *py_obj)<br>
 {<br>
-    if (py_obj && PyList_Check(py_obj))<br>
-        return PythonObject::Reset(py_obj);<br>
-<br>
-    PythonObject::Reset(nullptr);<br>
-    return py_obj == nullptr;<br>
+    if (!PythonList::Check(py_obj))<br>
+    {<br>
+        PythonObject::Reset(nullptr);<br>
+        return false;<br>
+    }<br>
+<br>
+    return PythonObject::Reset(py_obj);<br>
 }<br>
<br>
 uint32_t<br>
@@ -356,8 +438,8 @@ PythonList::CreateStructuredArray() cons<br>
 // PythonDictionary<br>
 //----------------------------------------------------------------------<br>
<br>
-PythonDictionary::PythonDictionary (bool create_empty) :<br>
-PythonObject(create_empty ? PyDict_New() : nullptr)<br>
+PythonDictionary::PythonDictionary()<br>
+    : PythonObject(PyDict_New())<br>
 {<br>
 }<br>
<br>
@@ -379,13 +461,24 @@ PythonDictionary::~PythonDictionary ()<br>
 }<br>
<br>
 bool<br>
-PythonDictionary::Reset (PyObject *py_obj)<br>
+PythonDictionary::Check(PyObject *py_obj)<br>
 {<br>
-    if (py_obj && PyDict_Check(py_obj))<br>
-        return PythonObject::Reset(py_obj);<br>
-<br>
-    PythonObject::Reset(nullptr);<br>
-    return py_obj == nullptr;<br>
+    if (!py_obj)<br>
+        return false;<br>
+<br>
+    return PyDict_Check(py_obj);<br>
+}<br>
+<br>
+bool<br>
+PythonDictionary::Reset(PyObject *py_obj)<br>
+{<br>
+    if (!PythonDictionary::Check(py_obj))<br>
+    {<br>
+        PythonObject::Reset(nullptr);<br>
+        return false;<br>
+    }<br>
+<br>
+    return PythonObject::Reset(py_obj);<br>
 }<br>
<br>
 uint32_t<br>
@@ -423,8 +516,11 @@ PythonDictionary::GetItemForKeyAsString<br>
     if (m_py_obj && key)<br>
     {<br>
         PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get());<br>
-        if (py_obj && PyString_Check(py_obj))<br>
-            return PyString_AsString(py_obj);<br>
+        if (py_obj && PythonString::Check(py_obj))<br>
+        {<br>
+            PythonString str(py_obj);<br>
+            return str.GetString().data();<br>
+        }<br>
     }<br>
     return fail_value;<br>
 }<br>
@@ -435,13 +531,10 @@ PythonDictionary::GetItemForKeyAsInteger<br>
     if (m_py_obj && key)<br>
     {<br>
         PyObject *py_obj = PyDict_GetItem(m_py_obj, key.get());<br>
-        if (py_obj)<br>
+        if (PythonInteger::Check(py_obj))<br>
         {<br>
-            if (PyInt_Check(py_obj))<br>
-                return PyInt_AsLong(py_obj);<br>
-<br>
-            if (PyLong_Check(py_obj))<br>
-                return PyLong_AsLong(py_obj);<br>
+            PythonInteger int_obj(py_obj);<br>
+            return int_obj.GetInteger();<br>
         }<br>
     }<br>
     return fail_value;<br>
@@ -452,7 +545,7 @@ PythonDictionary::GetKeys () const<br>
 {<br>
     if (m_py_obj)<br>
         return PythonList(PyDict_Keys(m_py_obj));<br>
-    return PythonList(true);<br>
+    return PythonList();<br>
 }<br>
<br>
 PythonString<br>
<br>
Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=249886&r1=249885&r2=249886&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=249886&r1=249885&r2=249886&view=diff</a><br>
==============================================================================<br>
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (original)<br>
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Fri Oct  9 14:45:41 2015<br>
@@ -156,7 +156,7 @@ enum class PyObjectType<br>
     protected:<br>
         PyObject* m_py_obj;<br>
     };<br>
-<br>
+<br>
     class PythonString: public PythonObject<br>
     {<br>
     public:<br>
@@ -167,6 +167,8 @@ enum class PyObjectType<br>
         PythonString (const char *string);<br>
         virtual ~PythonString ();<br>
<br>
+        static bool Check(PyObject *py_obj);<br>
+<br>
         virtual bool<br>
         Reset (PyObject* py_obj = NULL);<br>
<br>
@@ -180,7 +182,7 @@ enum class PyObjectType<br>
<br>
         StructuredData::StringSP CreateStructuredString() const;<br>
     };<br>
-<br>
+<br>
     class PythonInteger: public PythonObject<br>
     {<br>
     public:<br>
@@ -190,7 +192,9 @@ enum class PyObjectType<br>
         PythonInteger (const PythonObject &object);<br>
         PythonInteger (int64_t value);<br>
         virtual ~PythonInteger ();<br>
-<br>
+<br>
+        static bool Check(PyObject *py_obj);<br>
+<br>
         virtual bool<br>
         Reset (PyObject* py_obj = NULL);<br>
<br>
@@ -205,13 +209,13 @@ enum class PyObjectType<br>
     class PythonList: public PythonObject<br>
     {<br>
     public:<br>
-<br>
-        PythonList (bool create_empty);<br>
+      PythonList();<br>
         PythonList (PyObject* py_obj);<br>
         PythonList (const PythonObject &object);<br>
-        PythonList (uint32_t count);<br>
         virtual ~PythonList ();<br>
-<br>
+<br>
+        static bool Check(PyObject *py_obj);<br>
+<br>
         virtual bool<br>
         Reset (PyObject* py_obj = NULL);<br>
<br>
@@ -231,12 +235,13 @@ enum class PyObjectType<br>
     class PythonDictionary: public PythonObject<br>
     {<br>
     public:<br>
-<br>
-        explicit PythonDictionary (bool create_empty);<br>
+      PythonDictionary();<br>
         PythonDictionary (PyObject* object);<br>
         PythonDictionary (const PythonObject &object);<br>
         virtual ~PythonDictionary ();<br>
-<br>
+<br>
+        static bool Check(PyObject *py_obj);<br>
+<br>
         virtual bool<br>
         Reset (PyObject* object = NULL);<br>
<br>
<br>
Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=249886&r1=249885&r2=249886&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=249886&r1=249885&r2=249886&view=diff</a><br>
==============================================================================<br>
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)<br>
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Fri Oct  9 14:45:41 2015<br>
@@ -46,6 +46,7 @@<br>
 #endif<br>
<br>
 #include "llvm/ADT/StringRef.h"<br>
+#include "llvm/ADT/STLExtras.h"<br>
<br>
 using namespace lldb;<br>
 using namespace lldb_private;<br>
@@ -79,6 +80,29 @@ static ScriptInterpreterPython::SWIGPyth<br>
<br>
 static bool g_initialized = false;<br>
<br>
+#if PY_MAJOR_VERSION >= 3 && defined(LLDB_PYTHON_HOME)<br>
+typedef wchar_t PythonHomeChar;<br>
+#else<br>
+typedef char PythonHomeChar;<br>
+#endif<br>
+<br>
+PythonHomeChar *<br>
+GetDesiredPythonHome()<br>
+{<br>
+#if defined(LLDB_PYTHON_HOME)<br>
+#if PY_MAJOR_VERSION >= 3<br>
+    size_t size = 0;<br>
+    static PythonHomeChar *g_python_home = Py_DecodeLocale(LLDB_PYTHON_HOME, &size);<br>
+    return g_python_home;<br>
+#else<br>
+    static PythonHomeChar *g_python_home = LLDB_PYTHON_HOME;<br>
+    return g_python_home;<br>
+#endif<br>
+#else<br>
+    return nullptr;<br>
+#endif<br>
+}<br>
+<br>
 static std::string<br>
 ReadPythonBacktrace (PyObject* py_backtrace);<br>
<br>
@@ -116,7 +140,7 @@ ScriptInterpreterPython::Locker::DoAcqui<br>
     // place outside of Python (e.g. printing to screen, waiting for the network, ...)<br>
     // in that case, _PyThreadState_Current will be NULL - and we would be unable<br>
     // to set the asynchronous exception - not a desirable situation<br>
-    m_python_interpreter->SetThreadState (_PyThreadState_Current);<br>
+    m_python_interpreter->SetThreadState(PyThreadState_Get());<br>
     m_python_interpreter->IncrementLockCount();<br>
     return true;<br>
 }<br>
@@ -908,13 +932,13 @@ ScriptInterpreterPython::Interrupt()<br>
<br>
     if (IsExecutingPython())<br>
     {<br>
-        PyThreadState* state = _PyThreadState_Current;<br>
+        PyThreadState *state = PyThreadState_Get();<br>
         if (!state)<br>
             state = GetThreadState();<br>
         if (state)<br>
         {<br>
             long tid = state->thread_id;<br>
-            _PyThreadState_Current = state;<br>
+            PyThreadState_Swap(state);<br>
             int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);<br>
             if (log)<br>
                 log->Printf("ScriptInterpreterPython::Interrupt() sending PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...", tid, num_threads);<br>
@@ -1124,15 +1148,16 @@ ScriptInterpreterPython::ExecuteMultiple<br>
<br>
     if (in_string != nullptr)<br>
     {<br>
-        struct _node *compiled_node = PyParser_SimpleParseString (in_string, Py_file_input);<br>
+#if PY_MAJOR_VERSION >= 3<br>
+        PyObject *code_object = Py_CompileString(in_string, "temp.py", Py_file_input);<br>
+#else<br>
+        PyCodeObject *code_object = nullptr;<br>
+        struct _node *compiled_node = PyParser_SimpleParseString(in_string, Py_file_input);<br>
         if (compiled_node)<br>
-        {<br>
-            PyCodeObject *compiled_code = PyNode_Compile (compiled_node, "temp.py");<br>
-            if (compiled_code)<br>
-            {<br>
-              return_value.Reset(PyEval_EvalCode (compiled_code, globals.get(), locals.get()));<br>
-            }<br>
-        }<br>
+            code_object = PyNode_Compile(compiled_node, "temp.py");<br>
+#endif<br>
+        if (code_object)<br>
+            return_value.Reset(PyEval_EvalCode(code_object, globals.get(), locals.get()));<br>
     }<br>
<br>
     py_error = PyErr_Occurred ();<br>
@@ -1152,7 +1177,11 @@ ScriptInterpreterPython::ExecuteMultiple<br>
         std::string bt = ReadPythonBacktrace(traceback);<br>
<br>
         if (value && value != Py_None)<br>
-            error.SetErrorStringWithFormat("%s\n%s", PyString_AsString(PyObject_Str(value)),bt.c_str());<br>
+        {<br>
+            PythonString str(value);<br>
+            llvm::StringRef value_str(str.GetString());<br>
+            error.SetErrorStringWithFormat("%s\n%s", value_str.str().c_str(), bt.c_str());<br>
+        }<br>
         else<br>
             error.SetErrorStringWithFormat("%s",bt.c_str());<br>
         Py_XDECREF(type);<br>
@@ -2342,8 +2371,12 @@ ReadPythonBacktrace (PyObject* py_backtr<br>
                         if (stringIO_getvalue && stringIO_getvalue != Py_None)<br>
                         {<br>
                             printTB_string = PyObject_CallObject (stringIO_getvalue,nullptr);<br>
-                            if (printTB_string && printTB_string != Py_None && PyString_Check(printTB_string))<br>
-                                retval.assign(PyString_AsString(printTB_string));<br>
+                            if (printTB_string && PythonString::Check(printTB_string))<br>
+                            {<br>
+                                PythonString str(printTB_string);<br>
+                                llvm::StringRef string_data(str.GetString());<br>
+                                retval.assign(string_data.data(), string_data.size());<br>
+                            }<br>
                         }<br>
                     }<br>
                 }<br>
@@ -2924,14 +2957,13 @@ ScriptInterpreterPython::GetShortHelpFor<br>
         PyErr_Print();<br>
         PyErr_Clear();<br>
     }<br>
-<br>
-    if (py_return != nullptr && py_return != Py_None)<br>
+<br>
+    if (py_return != Py_None && PythonString::Check(py_return))<br>
     {<br>
-        if (PyString_Check(py_return))<br>
-        {<br>
-            dest.assign(PyString_AsString(py_return));<br>
-            got_string = true;<br>
-        }<br>
+        PythonString py_string(py_return);<br>
+        llvm::StringRef return_data(py_string.GetString());<br>
+        dest.assign(return_data.data(), return_data.size());<br>
+        got_string = true;<br>
     }<br>
     Py_XDECREF(py_return);<br>
<br>
@@ -2997,13 +3029,11 @@ ScriptInterpreterPython::GetFlagsForComm<br>
         PyErr_Print();<br>
         PyErr_Clear();<br>
     }<br>
-<br>
-    if (py_return != nullptr && py_return != Py_None)<br>
+<br>
+    if (py_return != Py_None && PythonInteger::Check(py_return))<br>
     {<br>
-        if (PyInt_Check(py_return))<br>
-            result = (uint32_t)PyInt_AsLong(py_return);<br>
-        else if (PyLong_Check(py_return))<br>
-            result = (uint32_t)PyLong_AsLong(py_return);<br>
+        PythonInteger int_value(py_return);<br>
+        result = int_value.GetInteger();<br>
     }<br>
     Py_XDECREF(py_return);<br>
<br>
@@ -3071,14 +3101,13 @@ ScriptInterpreterPython::GetLongHelpForC<br>
         PyErr_Print();<br>
         PyErr_Clear();<br>
     }<br>
-<br>
-    if (py_return != nullptr && py_return != Py_None)<br>
+<br>
+    if (py_return != Py_None && PythonString::Check(py_return))<br>
     {<br>
-        if (PyString_Check(py_return))<br>
-        {<br>
-            dest.assign(PyString_AsString(py_return));<br>
-            got_string = true;<br>
-        }<br>
+        PythonString str(py_return);<br>
+        llvm::StringRef str_data(str.GetString());<br>
+        dest.assign(str_data.data(), str_data.size());<br>
+        got_string = true;<br>
     }<br>
     Py_XDECREF(py_return);<br>
<br>
@@ -3164,8 +3193,9 @@ ScriptInterpreterPython::InitializePriva<br>
     stdin_tty_state.Save(STDIN_FILENO, false);<br>
<br>
 #if defined(LLDB_PYTHON_HOME)<br>
-    Py_SetPythonHome(LLDB_PYTHON_HOME);<br>
+    Py_SetPythonHome(GetDesiredPythonHome());<br>
 #endif<br>
+<br>
     PyGILState_STATE gstate;<br>
     Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));<br>
     bool threads_already_initialized = false;<br>
<br>
Modified: lldb/trunk/unittests/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/CMakeLists.txt?rev=249886&r1=249885&r2=249886&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/CMakeLists.txt?rev=249886&r1=249885&r2=249886&view=diff</a><br>
==============================================================================<br>
--- lldb/trunk/unittests/CMakeLists.txt (original)<br>
+++ lldb/trunk/unittests/CMakeLists.txt Fri Oct  9 14:45:41 2015<br>
@@ -25,4 +25,5 @@ endfunction()<br>
<br>
 add_subdirectory(Host)<br>
 add_subdirectory(Interpreter)<br>
+add_subdirectory(ScriptInterpreter)<br>
 add_subdirectory(Utility)<br>
<br>
Added: lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt?rev=249886&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt?rev=249886&view=auto</a><br>
==============================================================================<br>
--- lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt (added)<br>
+++ lldb/trunk/unittests/ScriptInterpreter/CMakeLists.txt Fri Oct  9 14:45:41 2015<br>
@@ -0,0 +1,3 @@<br>
+if (NOT LLDB_DISABLE_PYTHON)<br>
+  add_subdirectory(Python)<br>
+endif()<br>
<br>
Added: lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt?rev=249886&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt?rev=249886&view=auto</a><br>
==============================================================================<br>
--- lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt (added)<br>
+++ lldb/trunk/unittests/ScriptInterpreter/Python/CMakeLists.txt Fri Oct  9 14:45:41 2015<br>
@@ -0,0 +1,6 @@<br>
+add_lldb_unittest(ScriptInterpreterPythonTests<br>
+  PythonDataObjectsTests.cpp<br>
+  )<br>
+<br>
+  target_link_libraries(ScriptInterpreterPythonTests lldbPluginScriptInterpreterPython ${PYTHON_LIBRARY})<br>
+<br>
\ No newline at end of file<br>
<br>
Added: lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp?rev=249886&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp?rev=249886&view=auto</a><br>
==============================================================================<br>
--- lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp (added)<br>
+++ lldb/trunk/unittests/ScriptInterpreter/Python/PythonDataObjectsTests.cpp Fri Oct  9 14:45:41 2015<br>
@@ -0,0 +1,411 @@<br>
+//===-- PythonDataObjectsTests.cpp ------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "gtest/gtest.h"<br>
+<br>
+#include "lldb/Host/HostInfo.h"<br>
+#include "Plugins/ScriptInterpreter/Python/lldb-python.h"<br>
+#include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"<br>
+#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"<br>
+<br>
+using namespace lldb_private;<br>
+<br>
+class PythonDataObjectsTest : public testing::Test<br>
+{<br>
+  public:<br>
+    void<br>
+    SetUp() override<br>
+    {<br>
+        HostInfoBase::Initialize();<br>
+        // ScriptInterpreterPython::Initialize() depends on things like HostInfo being initialized<br>
+        // so it can compute the python directory etc, so we need to do this after<br>
+        // SystemInitializerCommon::Initialize().<br>
+        ScriptInterpreterPython::Initialize();<br>
+    }<br>
+<br>
+    void<br>
+    TearDown() override<br>
+    {<br>
+        ScriptInterpreterPython::Terminate();<br>
+    }<br>
+};<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonInteger)<br>
+{<br>
+// Test that integers behave correctly when wrapped by a PythonInteger.<br>
+<br>
+#if PY_MAJOR_VERSION < 3<br>
+    // Verify that `PythonInt` works correctly when given a PyInt object.<br>
+    // Note that PyInt doesn't exist in Python 3.x, so this is only for 2.x<br>
+    PyObject *py_int = PyInt_FromLong(12);<br>
+    EXPECT_TRUE(PythonInteger::Check(py_int));<br>
+    PythonInteger python_int(py_int);<br>
+<br>
+    EXPECT_EQ(PyObjectType::Integer, python_int.GetObjectType());<br>
+    EXPECT_EQ(12, python_int.GetInteger());<br>
+#endif<br>
+<br>
+    // Verify that `PythonInt` works correctly when given a PyLong object.<br>
+    PyObject *py_long = PyLong_FromLong(12);<br>
+    EXPECT_TRUE(PythonInteger::Check(py_long));<br>
+    PythonInteger python_long(py_long);<br>
+    EXPECT_EQ(PyObjectType::Integer, python_long.GetObjectType());<br>
+<br>
+    // Verify that you can reset the value and that it is reflected properly.<br>
+    python_long.SetInteger(40);<br>
+    EXPECT_EQ(40, python_long.GetInteger());<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonString)<br>
+{<br>
+    // Test that strings behave correctly when wrapped by a PythonString.<br>
+<br>
+    static const char *test_string = "PythonDataObjectsTest::TestPythonString";<br>
+    static const char *test_string2 = "PythonDataObjectsTest::TestPythonString";<br>
+<br>
+#if PY_MAJOR_VERSION < 3<br>
+    // Verify that `PythonString` works correctly when given a PyString object.<br>
+    // Note that PyString doesn't exist in Python 3.x, so this is only for 2.x<br>
+    PyObject *py_string = PyString_FromString(test_string);<br>
+    EXPECT_TRUE(PythonString::Check(py_string));<br>
+    PythonString python_string(py_string);<br>
+<br>
+    EXPECT_EQ(PyObjectType::String, python_string.GetObjectType());<br>
+    EXPECT_STREQ(test_string, python_string.GetString().data());<br>
+#endif<br>
+<br>
+    // Verify that `PythonString` works correctly when given a PyUnicode object.<br>
+    PyObject *py_unicode = PyUnicode_FromString(test_string);<br>
+    EXPECT_TRUE(PythonString::Check(py_unicode));<br>
+    PythonString python_unicode(py_unicode);<br>
+<br>
+    EXPECT_EQ(PyObjectType::String, python_unicode.GetObjectType());<br>
+    EXPECT_STREQ(test_string, python_unicode.GetString().data());<br>
+<br>
+    // Verify that you can reset the value and that it is reflected properly.<br>
+    python_unicode.SetString(test_string2);<br>
+    EXPECT_STREQ(test_string2, python_unicode.GetString().data());<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonListPrebuilt)<br>
+{<br>
+    // Test that a list which is built through the native<br>
+    // Python API behaves correctly when wrapped by a PythonList.<br>
+    static const int list_size = 2;<br>
+    static const long long_idx0 = 5;<br>
+    static const char *const string_idx1 = "String Index 1";<br>
+<br>
+    PyObject *list_items[list_size];<br>
+<br>
+    PyObject *py_list = PyList_New(2);<br>
+    list_items[0] = PyLong_FromLong(long_idx0);<br>
+    list_items[1] = PyString_FromString(string_idx1);<br>
+<br>
+    for (int i = 0; i < list_size; ++i)<br>
+        PyList_SetItem(py_list, i, list_items[i]);<br>
+<br>
+    EXPECT_TRUE(PythonList::Check(py_list));<br>
+<br>
+    PythonList list(py_list);<br>
+    EXPECT_EQ(list_size, list.GetSize());<br>
+    EXPECT_EQ(PyObjectType::List, list.GetObjectType());<br>
+<br>
+    // PythonList doesn't yet support getting objects by type.<br>
+    // For now, we have to call CreateStructuredArray and use<br>
+    // those objects.  That will be in a different test.<br>
+    // TODO: Add the ability for GetItemByIndex() to return a<br>
+    // typed object.<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonDictionaryPrebuilt)<br>
+{<br>
+    // Test that a dictionary which is built through the native<br>
+    // Python API behaves correctly when wrapped by a PythonDictionary.<br>
+    static const int dict_entries = 2;<br>
+<br>
+    PyObject *keys[dict_entries];<br>
+    PyObject *values[dict_entries];<br>
+<br>
+    keys[0] = PyString_FromString("Key 0");<br>
+    keys[1] = PyLong_FromLong(1);<br>
+    values[0] = PyLong_FromLong(0);<br>
+    values[1] = PyString_FromString("Value 1");<br>
+<br>
+    PyObject *py_dict = PyDict_New();<br>
+    for (int i = 0; i < dict_entries; ++i)<br>
+        PyDict_SetItem(py_dict, keys[i], values[i]);<br>
+<br>
+    EXPECT_TRUE(PythonDictionary::Check(py_dict));<br>
+<br>
+    PythonDictionary dict(py_dict);<br>
+    EXPECT_EQ(dict.GetSize(), dict_entries);<br>
+    EXPECT_EQ(PyObjectType::Dictionary, dict.GetObjectType());<br>
+<br>
+    // PythonDictionary doesn't yet support getting objects by type.<br>
+    // For now, we have to call CreateStructuredDictionary and use<br>
+    // those objects.  That will be in a different test.<br>
+    // TODO: Add the ability for GetItemByKey() to return a<br>
+    // typed object.<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonListManipulation)<br>
+{<br>
+    // Test that manipulation of a PythonList behaves correctly when<br>
+    // wrapped by a PythonDictionary.<br>
+<br>
+    static const long long_idx0 = 5;<br>
+    static const char *const string_idx1 = "String Index 1";<br>
+<br>
+    PyObject *py_list = PyList_New(0);<br>
+    PythonList list(py_list);<br>
+    PythonInteger integer(long_idx0);<br>
+    PythonString string(string_idx1);<br>
+<br>
+    list.AppendItem(integer);<br>
+    list.AppendItem(string);<br>
+    EXPECT_EQ(2, list.GetSize());<br>
+<br>
+    // PythonList doesn't yet support getting typed objects out, so we<br>
+    // can't easily test that the first item is an integer with the correct<br>
+    // value, etc.<br>
+    // TODO: Add the ability for GetItemByIndex() to return a<br>
+    // typed object.<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonDictionaryManipulation)<br>
+{<br>
+    // Test that manipulation of a dictionary behaves correctly when wrapped<br>
+    // by a PythonDictionary.<br>
+    static const int dict_entries = 2;<br>
+<br>
+    PyObject *keys[dict_entries];<br>
+    PyObject *values[dict_entries];<br>
+<br>
+    keys[0] = PyString_FromString("Key 0");<br>
+    keys[1] = PyString_FromString("Key 1");<br>
+    values[0] = PyLong_FromLong(1);<br>
+    values[1] = PyString_FromString("Value 1");<br>
+<br>
+    PyObject *py_dict = PyDict_New();<br>
+<br>
+    PythonDictionary dict(py_dict);<br>
+    for (int i = 0; i < 2; ++i)<br>
+        dict.SetItemForKey(PythonString(keys[i]), values[i]);<br>
+<br>
+    EXPECT_EQ(dict_entries, dict.GetSize());<br>
+<br>
+    // PythonDictionary doesn't yet support getting objects by type.<br>
+    // For now, we have to call CreateStructuredDictionary and use<br>
+    // those objects.  That will be in a different test.<br>
+    // TODO: Add the ability for GetItemByKey() to return a<br>
+    // typed object.<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonListToStructuredObject)<br>
+{<br>
+    // Test that a PythonList is properly converted to a StructuredArray.<br>
+    // This includes verifying that a list can contain a nested list as<br>
+    // well as a nested dictionary.<br>
+<br>
+    static const int item_count = 4;<br>
+    static const long long_idx0 = 5;<br>
+    static const char *const string_idx1 = "String Index 1";<br>
+<br>
+    static const long nested_list_long_idx0 = 6;<br>
+    static const char *const nested_list_str_idx1 = "Nested String Index 1";<br>
+<br>
+    static const char *const nested_dict_key0 = "Nested Key 0";<br>
+    static const char *const nested_dict_value0 = "Nested Value 0";<br>
+    static const char *const nested_dict_key1 = "Nested Key 1";<br>
+    static const long nested_dict_value1 = 2;<br>
+<br>
+    PythonList list;<br>
+    PythonList nested_list;<br>
+    PythonDictionary nested_dict;<br>
+<br>
+    nested_list.AppendItem(PythonInteger(nested_list_long_idx0));<br>
+    nested_list.AppendItem(PythonString(nested_list_str_idx1));<br>
+    nested_dict.SetItemForKey(PythonString(nested_dict_key0), PythonString(nested_dict_value0));<br>
+    nested_dict.SetItemForKey(PythonString(nested_dict_key1), PythonInteger(nested_dict_value1));<br>
+<br>
+    list.AppendItem(PythonInteger(long_idx0));<br>
+    list.AppendItem(PythonString(string_idx1));<br>
+    list.AppendItem(nested_list);<br>
+    list.AppendItem(nested_dict);<br>
+<br>
+    EXPECT_EQ(item_count, list.GetSize());<br>
+<br>
+    StructuredData::ArraySP array_sp = list.CreateStructuredArray();<br>
+    EXPECT_EQ(list.GetSize(), array_sp->GetSize());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeInteger, array_sp->GetItemAtIndex(0)->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeString, array_sp->GetItemAtIndex(1)->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeArray, array_sp->GetItemAtIndex(2)->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeDictionary, array_sp->GetItemAtIndex(3)->GetType());<br>
+<br>
+    auto list_int_sp = std::static_pointer_cast<StructuredData::Integer>(array_sp->GetItemAtIndex(0));<br>
+    auto list_str_sp = std::static_pointer_cast<StructuredData::String>(array_sp->GetItemAtIndex(1));<br>
+    auto list_list_sp = std::static_pointer_cast<StructuredData::Array>(array_sp->GetItemAtIndex(2));<br>
+    auto list_dict_sp = std::static_pointer_cast<StructuredData::Dictionary>(array_sp->GetItemAtIndex(3));<br>
+<br>
+    // Verify that the first item (long) has the correct value<br>
+    EXPECT_EQ(long_idx0, list_int_sp->GetValue());<br>
+<br>
+    // Verify that the second item (string) has the correct value<br>
+    EXPECT_STREQ(string_idx1, list_str_sp->GetValue().c_str());<br>
+<br>
+    // Verify that the third item is a list with the correct length and element types<br>
+    EXPECT_EQ(nested_list.GetSize(), list_list_sp->GetSize());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeInteger, list_list_sp->GetItemAtIndex(0)->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeString, list_list_sp->GetItemAtIndex(1)->GetType());<br>
+    // Verify that the values of each element in the list are correct<br>
+    auto nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(list_list_sp->GetItemAtIndex(0));<br>
+    auto nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(list_list_sp->GetItemAtIndex(1));<br>
+    EXPECT_EQ(nested_list_long_idx0, nested_list_value_0->GetValue());<br>
+    EXPECT_STREQ(nested_list_str_idx1, nested_list_value_1->GetValue().c_str());<br>
+<br>
+    // Verify that the fourth item is a dictionary with the correct length<br>
+    EXPECT_EQ(nested_dict.GetSize(), list_dict_sp->GetSize());<br>
+    auto dict_keys = std::static_pointer_cast<StructuredData::Array>(list_dict_sp->GetKeys());<br>
+<br>
+    // Verify that all of the keys match the values and types of keys we inserted<br>
+    EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(0)->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys->GetItemAtIndex(1)->GetType());<br>
+    auto nested_key_0 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(0));<br>
+    auto nested_key_1 = std::static_pointer_cast<StructuredData::String>(dict_keys->GetItemAtIndex(1));<br>
+    EXPECT_STREQ(nested_dict_key0, nested_key_0->GetValue().c_str());<br>
+    EXPECT_STREQ(nested_dict_key1, nested_key_1->GetValue().c_str());<br>
+<br>
+    // Verify that for each key, the value has the correct type and value as what we inserted.<br>
+    auto nested_dict_value_0 = list_dict_sp->GetValueForKey(nested_key_0->GetValue());<br>
+    auto nested_dict_value_1 = list_dict_sp->GetValueForKey(nested_key_1->GetValue());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_value_0->GetType());<br>
+    EXPECT_EQ(StructuredData::Type::eTypeInteger, nested_dict_value_1->GetType());<br>
+    auto nested_dict_str_value_0 = std::static_pointer_cast<StructuredData::String>(nested_dict_value_0);<br>
+    auto nested_dict_int_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_dict_value_1);<br>
+    EXPECT_STREQ(nested_dict_value0, nested_dict_str_value_0->GetValue().c_str());<br>
+    EXPECT_EQ(nested_dict_value1, nested_dict_int_value_1->GetValue());<br>
+}<br>
+<br>
+TEST_F(PythonDataObjectsTest, TestPythonDictionaryToStructuredObject)<br>
+{<br>
+    // Test that a PythonDictionary is properly converted to a<br>
+    // StructuredDictionary.  This includes verifying that a dictionary<br>
+    // can contain a nested dictionary as well as a nested list.<br>
+<br>
+    static const int dict_item_count = 4;<br>
+    static const char *const dict_keys[dict_item_count] = {"Key 0 (str)", "Key 1 (long)", "Key 2 (dict)",<br>
+                                                           "Key 3 (list)"};<br>
+<br>
+    static const StructuredData::Type dict_value_types[dict_item_count] = {<br>
+        StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger, StructuredData::Type::eTypeDictionary,<br>
+        StructuredData::Type::eTypeArray};<br>
+<br>
+    static const char *const nested_dict_keys[2] = {"Nested Key 0 (str)", "Nested Key 1 (long)"};<br>
+<br>
+    static const StructuredData::Type nested_dict_value_types[2] = {<br>
+        StructuredData::Type::eTypeString, StructuredData::Type::eTypeInteger,<br>
+    };<br>
+<br>
+    static const StructuredData::Type nested_list_value_types[2] = {StructuredData::Type::eTypeInteger,<br>
+                                                                    StructuredData::Type::eTypeString};<br>
+<br>
+    static const char *const dict_value0 = "Value 0";<br>
+    static const long dict_value1 = 2;<br>
+<br>
+    static const long nested_list_value0 = 5;<br>
+    static const char *const nested_list_value1 = "Nested list string";<br>
+<br>
+    static const char *const nested_dict_value0 = "Nested Dict Value 0";<br>
+    static const long nested_dict_value1 = 7;<br>
+<br>
+    PythonDictionary dict;<br>
+    PythonDictionary nested_dict;<br>
+    PythonList nested_list;<br>
+<br>
+    nested_dict.SetItemForKey(PythonString(nested_dict_keys[0]), PythonString(nested_dict_value0));<br>
+    nested_dict.SetItemForKey(PythonString(nested_dict_keys[1]), PythonInteger(nested_dict_value1));<br>
+<br>
+    nested_list.AppendItem(PythonInteger(nested_list_value0));<br>
+    nested_list.AppendItem(PythonString(nested_list_value1));<br>
+<br>
+    dict.SetItemForKey(PythonString(dict_keys[0]), PythonString(dict_value0));<br>
+    dict.SetItemForKey(PythonString(dict_keys[1]), PythonInteger(dict_value1));<br>
+    dict.SetItemForKey(PythonString(dict_keys[2]), nested_dict);<br>
+    dict.SetItemForKey(PythonString(dict_keys[3]), nested_list);<br>
+<br>
+    StructuredData::DictionarySP dict_sp = dict.CreateStructuredDictionary();<br>
+    EXPECT_EQ(dict_item_count, dict_sp->GetSize());<br>
+    auto dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_sp->GetKeys());<br>
+<br>
+    std::vector<StructuredData::StringSP> converted_keys;<br>
+    std::vector<StructuredData::ObjectSP> converted_values;<br>
+    // Verify that all of the keys match the values and types of keys we inserted<br>
+    // (Keys are always strings, so this is easy)<br>
+    for (int i = 0; i < dict_sp->GetSize(); ++i)<br>
+    {<br>
+        EXPECT_EQ(StructuredData::Type::eTypeString, dict_keys_array->GetItemAtIndex(i)->GetType());<br>
+        auto converted_key = std::static_pointer_cast<StructuredData::String>(dict_keys_array->GetItemAtIndex(i));<br>
+        converted_keys.push_back(converted_key);<br>
+        converted_values.push_back(dict_sp->GetValueForKey(converted_key->GetValue().c_str()));<br>
+<br>
+        EXPECT_STREQ(dict_keys[i], converted_key->GetValue().c_str());<br>
+        EXPECT_EQ(dict_value_types[i], converted_values[i]->GetType());<br>
+    }<br>
+<br>
+    auto dict_string_value = std::static_pointer_cast<StructuredData::String>(converted_values[0]);<br>
+    auto dict_int_value = std::static_pointer_cast<StructuredData::Integer>(converted_values[1]);<br>
+    auto dict_dict_value = std::static_pointer_cast<StructuredData::Dictionary>(converted_values[2]);<br>
+    auto dict_list_value = std::static_pointer_cast<StructuredData::Array>(converted_values[3]);<br>
+<br>
+    // The first two dictionary values are easy to test, because they are just a string and an integer.<br>
+    EXPECT_STREQ(dict_value0, dict_string_value->GetValue().c_str());<br>
+    EXPECT_EQ(dict_value1, dict_int_value->GetValue());<br>
+<br>
+    // For the nested dictionary, repeat the same process as before.<br>
+    EXPECT_EQ(2, dict_dict_value->GetSize());<br>
+    auto nested_dict_keys_array = std::static_pointer_cast<StructuredData::Array>(dict_dict_value->GetKeys());<br>
+<br>
+    std::vector<StructuredData::StringSP> nested_converted_keys;<br>
+    std::vector<StructuredData::ObjectSP> nested_converted_values;<br>
+    // Verify that all of the keys match the values and types of keys we inserted<br>
+    // (Keys are always strings, so this is easy)<br>
+    for (int i = 0; i < dict_dict_value->GetSize(); ++i)<br>
+    {<br>
+        EXPECT_EQ(StructuredData::Type::eTypeString, nested_dict_keys_array->GetItemAtIndex(i)->GetType());<br>
+        auto converted_key =<br>
+            std::static_pointer_cast<StructuredData::String>(nested_dict_keys_array->GetItemAtIndex(i));<br>
+        nested_converted_keys.push_back(converted_key);<br>
+        nested_converted_values.push_back(dict_dict_value->GetValueForKey(converted_key->GetValue().c_str()));<br>
+<br>
+        EXPECT_STREQ(nested_dict_keys[i], converted_key->GetValue().c_str());<br>
+        EXPECT_EQ(nested_dict_value_types[i], converted_values[i]->GetType());<br>
+    }<br>
+<br>
+    auto converted_nested_dict_value_0 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[0]);<br>
+    auto converted_nested_dict_value_1 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[1]);<br>
+<br>
+    // The first two dictionary values are easy to test, because they are just a string and an integer.<br>
+    EXPECT_STREQ(nested_dict_value0, converted_nested_dict_value_0->GetValue().c_str());<br>
+    EXPECT_EQ(nested_dict_value1, converted_nested_dict_value_1->GetValue());<br>
+<br>
+    // For the nested list, just verify the size, type and value of each item<br>
+    nested_converted_values.clear();<br>
+    EXPECT_EQ(2, dict_list_value->GetSize());<br>
+    for (int i = 0; i < dict_list_value->GetSize(); ++i)<br>
+    {<br>
+        auto converted_value = dict_list_value->GetItemAtIndex(i);<br>
+        EXPECT_EQ(nested_list_value_types[i], converted_value->GetType());<br>
+        nested_converted_values.push_back(converted_value);<br>
+    }<br>
+<br>
+    auto converted_nested_list_value_0 = std::static_pointer_cast<StructuredData::Integer>(nested_converted_values[0]);<br>
+    auto converted_nested_list_value_1 = std::static_pointer_cast<StructuredData::String>(nested_converted_values[1]);<br>
+    EXPECT_EQ(nested_list_value0, converted_nested_list_value_0->GetValue());<br>
+    EXPECT_STREQ(nested_list_value1, converted_nested_list_value_1->GetValue().c_str());<br>
+}<br>
<br>
<br>
_______________________________________________<br>
lldb-commits mailing list<br>
<a href="mailto:lldb-commits@lists.llvm.org" target="_blank">lldb-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits</a><br>
</blockquote></div>