[Lldb-commits] [lldb] r162504 - in /lldb/trunk: include/lldb/Interpreter/PythonDataObjects.h source/Interpreter/PythonDataObjects.cpp

Greg Clayton gclayton at apple.com
Thu Aug 23 16:49:47 PDT 2012


Author: gclayton
Date: Thu Aug 23 18:49:47 2012
New Revision: 162504

URL: http://llvm.org/viewvc/llvm-project?rev=162504&view=rev
Log:
Switch over to using object instances for all PythonData objects.


Modified:
    lldb/trunk/include/lldb/Interpreter/PythonDataObjects.h
    lldb/trunk/source/Interpreter/PythonDataObjects.cpp

Modified: lldb/trunk/include/lldb/Interpreter/PythonDataObjects.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/PythonDataObjects.h?rev=162504&r1=162503&r2=162504&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/PythonDataObjects.h (original)
+++ lldb/trunk/include/lldb/Interpreter/PythonDataObjects.h Thu Aug 23 18:49:47 2012
@@ -19,13 +19,24 @@
 #include "lldb/Core/ConstString.h"
 #include "lldb/Core/Flags.h"
 #include "lldb/Interpreter/OptionValue.h"
+#if defined (__APPLE__)
+#include <Python/Python.h>
+#else
+#include <Python.h>
+#endif
 
 namespace lldb_private {
     
     class PythonRefCountedObject
     {
     public:
-        PythonRefCountedObject (PyObject* obj) : m_object(obj)
+        PythonRefCountedObject (PyObject* obj = NULL) : m_object(obj)
+        {
+            Py_XINCREF(m_object);
+        }
+        
+        PythonRefCountedObject (const PythonRefCountedObject &rhs) :
+            m_object(rhs.m_object)
         {
             Py_XINCREF(m_object);
         }
@@ -34,22 +45,33 @@
         {
             Py_XDECREF(m_object);
         }
-        
+
+        const PythonRefCountedObject &
+        operator = (const PythonRefCountedObject &rhs)
+        {
+            if (this != &rhs)
+                Reset (rhs.m_object);
+            return *this;
+        }
+
         void
         Reset (PyObject* object = NULL)
         {
-            Py_XDECREF(m_object);
-            m_object = object;
-            Py_XINCREF(m_object);
+            if (object != m_object)
+            {
+                Py_XDECREF(m_object);
+                m_object = object;
+                Py_XINCREF(m_object);
+            }
         }
         
         PyObject*
-        GetPyhonObject ()
+        GetPyhonObject () const
         {
             return m_object;
         }
         
-        operator bool ()
+        operator bool () const
         {
             return m_object != NULL;
         }
@@ -62,7 +84,8 @@
     {
     public:
         
-        PythonDataString (PyObject* object);
+        PythonDataString (bool create_empty);
+        PythonDataString (PyObject* object = NULL);
         PythonDataString (const char* string);
         ~PythonDataString ();
         
@@ -72,13 +95,16 @@
         void
         SetString (const char* string);
         
-        operator bool ()
+        operator bool () const
         {
             return m_object.operator bool();
         }
         
         PyObject*
-        GetPythonObject() { return m_object.GetPyhonObject(); }
+        GetPythonObject() const
+        {
+            return m_object.GetPyhonObject();
+        }
     private:
         PythonRefCountedObject m_object;
     };
@@ -87,6 +113,7 @@
     {
     public:
         
+        PythonDataInteger (bool create_empty = true);
         PythonDataInteger (PyObject* object);
         PythonDataInteger (int64_t value);
         ~PythonDataInteger ();
@@ -97,13 +124,16 @@
         void
         SetInteger (int64_t value);
         
-        operator bool ()
+        operator bool () const
         {
             return m_object.operator bool();
         }
         
         PyObject*
-        GetPythonObject() { return m_object.GetPyhonObject(); }
+        GetPythonObject() const
+        {
+            return m_object.GetPyhonObject();
+        }
     private:
         PythonRefCountedObject m_object;
     };
@@ -112,29 +142,33 @@
     {
     public:
         
-        PythonDataArray (uint32_t count);
+        PythonDataArray (bool create_empty = true);
         PythonDataArray (PyObject* object);
+        PythonDataArray (uint32_t count);
         ~PythonDataArray ();
         
         uint32_t
         GetSize();
         
-        PythonDataObject*
+        PythonDataObject
         GetItemAtIndex (uint32_t index);
         
         void
-        SetItemAtIndex (uint32_t index, PythonDataObject* object);
+        SetItemAtIndex (uint32_t index, const PythonDataObject &object);
         
         void
-        AppendItem (PythonDataObject* object);
+        AppendItem (const PythonDataObject &object);
         
-        operator bool ()
+        operator bool () const
         {
             return m_object.operator bool();
         }
         
         PyObject*
-        GetPythonObject() { return m_object.GetPyhonObject(); }
+        GetPythonObject() const
+        {
+            return m_object.GetPyhonObject();
+        }
     private:
         PythonRefCountedObject m_object;
     };
@@ -143,36 +177,42 @@
     {
     public:
         
-        PythonDataDictionary ();
+        PythonDataDictionary (bool create_empty = true);
         PythonDataDictionary (PyObject* object);
         ~PythonDataDictionary ();
         
         uint32_t GetSize();
         
-        PythonDataObject*
-        GetItemForKey (PythonDataString* key);
-        
+        PythonDataObject
+        GetItemForKey (const PythonDataString &key);
+
+        PythonDataObject
+        GetItemForKey (const char *key);
+
         typedef bool (*DictionaryIteratorCallback)(PythonDataString* key, PythonDataDictionary* dict);
         
-        PythonDataArray*
+        PythonDataArray
         GetKeys ();
         
-        PythonDataString*
+        PythonDataString
         GetKeyAtPosition (uint32_t pos);
         
-        PythonDataObject*
+        PythonDataObject
         GetValueAtPosition (uint32_t pos);
         
         void
-        SetItemForKey (PythonDataString* key, PythonDataObject* value);
+        SetItemForKey (const PythonDataString &key, const PythonDataObject& value);
         
-        operator bool ()
+        operator bool () const
         {
             return m_object.operator bool();
         }
         
         PyObject*
-        GetPythonObject() { return m_object.GetPyhonObject(); }
+        GetPythonObject() const
+        {
+            return m_object.GetPyhonObject();
+        }
     private:
         PythonRefCountedObject m_object;
     };
@@ -181,29 +221,33 @@
     {
     public:
         
+        PythonDataObject ();
         PythonDataObject (PyObject* object);
         
         ~PythonDataObject ();
         
-        PythonDataString*
+        PythonDataString
         GetStringObject ();
         
-        PythonDataInteger*
+        PythonDataInteger
         GetIntegerObject ();
         
-        PythonDataArray*
+        PythonDataArray
         GetArrayObject();
         
-        PythonDataDictionary*
+        PythonDataDictionary
         GetDictionaryObject();
         
-        operator bool ()
+        operator bool () const
         {
             return m_object.operator bool();
         }
         
         PyObject*
-        GetPythonObject() { return m_object.GetPyhonObject(); }
+        GetPythonObject() const
+        {
+            return m_object.GetPyhonObject();
+        }
         
     private:
         PythonRefCountedObject m_object;

Modified: lldb/trunk/source/Interpreter/PythonDataObjects.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/PythonDataObjects.cpp?rev=162504&r1=162503&r2=162504&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/PythonDataObjects.cpp (original)
+++ lldb/trunk/source/Interpreter/PythonDataObjects.cpp Thu Aug 23 18:49:47 2012
@@ -26,45 +26,63 @@
 using namespace lldb_private;
 using namespace lldb;
 
-PythonDataObject::PythonDataObject (PyObject* object) : m_object(object)
+PythonDataObject::PythonDataObject (PyObject* object) :
+    m_object(object)
 {
 }
 
-PythonDataString*
+PythonDataObject::PythonDataObject () :
+    m_object()
+{
+}
+
+PythonDataObject::~PythonDataObject ()
+{
+}
+
+PythonDataString
 PythonDataObject::GetStringObject ()
 {
-    return new PythonDataString(GetPythonObject());
+    return PythonDataString(GetPythonObject());
 }
     
-PythonDataInteger*
+PythonDataInteger
 PythonDataObject::GetIntegerObject ()
 {
-    return new PythonDataInteger(GetPythonObject());
+    return PythonDataInteger(GetPythonObject());
 }
 
-PythonDataArray*
+PythonDataArray
 PythonDataObject::GetArrayObject()
 {
-    return new PythonDataArray(GetPythonObject());
+    return PythonDataArray(GetPythonObject());
 }
 
-PythonDataDictionary*
+PythonDataDictionary
 PythonDataObject::GetDictionaryObject()
 {
-    return new PythonDataDictionary(GetPythonObject());
+    return PythonDataDictionary(GetPythonObject());
+}
+
+PythonDataInteger::PythonDataInteger (bool create_empty) :
+    m_object(create_empty ? PyInt_FromLong(0) : NULL)
+{
 }
 
-PythonDataInteger::PythonDataInteger (PyObject* object) : m_object(object)
+PythonDataInteger::PythonDataInteger (PyObject* object) :
+    m_object(object)
 {
-    if (!PyInt_Check(GetPythonObject()))
+    if (object && !PyInt_Check(GetPythonObject()))
         m_object.Reset();
 }
 
-PythonDataInteger::~PythonDataInteger ()
+PythonDataInteger::PythonDataInteger (int64_t value) :
+    m_object(PyInt_FromLong(value))
 {
 }
 
-PythonDataInteger::PythonDataInteger (int64_t value) : m_object(PyInt_FromLong(value))
+
+PythonDataInteger::~PythonDataInteger ()
 {
 }
 
@@ -83,12 +101,20 @@
     m_object.Reset(PyInt_FromLong(value));
 }
 
-PythonDataString::PythonDataString (PyObject* object) : m_object(object)
+PythonDataString::PythonDataString (bool create_empty) :
+    m_object(create_empty ? PyString_FromString("") : NULL)
+{
+}
+
+PythonDataString::PythonDataString (PyObject* object) :
+    m_object(object)
 {
-    if (!PyString_Check(GetPythonObject()))
-        m_object.Reset();}
+    if (object && !PyString_Check(GetPythonObject()))
+        m_object.Reset();
+}
 
-PythonDataString::PythonDataString (const char* string) : m_object(PyString_FromString(string))
+PythonDataString::PythonDataString (const char* string) :
+    m_object(PyString_FromString(string))
 {
 }
 
@@ -110,13 +136,20 @@
     m_object.Reset(PyString_FromString(string));
 }
 
-PythonDataArray::PythonDataArray (uint32_t count) : m_object(PyList_New(count))
+PythonDataArray::PythonDataArray (bool create_empty) :
+    m_object(create_empty ? PyList_New(0) : NULL)
 {
 }
 
-PythonDataArray::PythonDataArray (PyObject* object) : m_object(object)
+PythonDataArray::PythonDataArray (uint32_t count) :
+    m_object(PyList_New(count))
 {
-    if (!PyList_Check(GetPythonObject()))
+}
+
+PythonDataArray::PythonDataArray (PyObject* object) :
+    m_object(object)
+{
+    if (object && !PyList_Check(GetPythonObject()))
         m_object.Reset();
 }
 
@@ -132,35 +165,37 @@
     return 0;
 }
 
-PythonDataObject*
+PythonDataObject
 PythonDataArray::GetItemAtIndex (uint32_t index)
 {
     if (m_object)
-        return new PythonDataObject(PyList_GetItem(GetPythonObject(), index));
+        return PythonDataObject(PyList_GetItem(GetPythonObject(), index));
     return NULL;
 }
 
 void
-PythonDataArray::SetItemAtIndex (uint32_t index, PythonDataObject* object)
+PythonDataArray::SetItemAtIndex (uint32_t index, const PythonDataObject & object)
 {
-    if (m_object && object && *object)
-        PyList_SetItem(GetPythonObject(), index, object->GetPythonObject());
+    if (m_object && object)
+        PyList_SetItem(GetPythonObject(), index, object.GetPythonObject());
 }
 
 void
-PythonDataArray::AppendItem (PythonDataObject* object)
+PythonDataArray::AppendItem (const PythonDataObject &object)
 {
-    if (m_object && object && *object)
-        PyList_Append(GetPythonObject(), object->GetPythonObject());
+    if (m_object && object)
+        PyList_Append(GetPythonObject(), object.GetPythonObject());
 }
 
-PythonDataDictionary::PythonDataDictionary () : m_object(PyDict_New())
+PythonDataDictionary::PythonDataDictionary (bool create_empty) :
+    m_object(create_empty ? PyDict_New() : NULL)
 {
 }
 
-PythonDataDictionary::PythonDataDictionary (PyObject* object) : m_object(object)
+PythonDataDictionary::PythonDataDictionary (PyObject* object) :
+    m_object(object)
 {
-    if (!PyDict_Check(GetPythonObject()))
+    if (object && !PyDict_Check(GetPythonObject()))
         m_object.Reset();
 }
 
@@ -176,39 +211,52 @@
     return 0;
 }
 
-PythonDataObject*
-PythonDataDictionary::GetItemForKey (PythonDataString* key)
+PythonDataObject
+PythonDataDictionary::GetItemForKey (const char *key)
 {
-    if (m_object && key && *key)
-        return new PythonDataObject(PyDict_GetItem(GetPythonObject(), key->GetPythonObject()));
+    if (key && key[0])
+    {
+        PythonDataString python_key(key);
+        return GetItemForKey(python_key);
+    }
     return NULL;
 }
 
-PythonDataArray*
+
+PythonDataObject
+PythonDataDictionary::GetItemForKey (const PythonDataString &key)
+{
+    if (m_object && key)
+        return PythonDataObject(PyDict_GetItem(GetPythonObject(), key.GetPythonObject()));
+    return PythonDataObject();
+}
+
+PythonDataArray
 PythonDataDictionary::GetKeys ()
 {
     if (m_object)
-        return new PythonDataArray(PyDict_Keys(GetPythonObject()));
-    return NULL;
+        return PythonDataArray(PyDict_Keys(GetPythonObject()));
+    return PythonDataArray();
 }
 
-PythonDataString*
+PythonDataString
 PythonDataDictionary::GetKeyAtPosition (uint32_t pos)
 {
     PyObject *key, *value;
     Py_ssize_t pos_iter = 0;
     
-    if (!m_object)
-        return NULL;
-    
-    while (PyDict_Next(GetPythonObject(), &pos_iter, &key, &value)) {
-        if (pos-- == 0)
-            return new PythonDataString(key);
+    if (m_object)
+    {
+        while (PyDict_Next(GetPythonObject(), &pos_iter, &key, &value))
+        {
+            if (pos-- == 0)
+                return PythonDataString(key);
+        }
     }
-    return NULL;
+    return PythonDataString();
 }
 
-PythonDataObject*
+PythonDataObject
 PythonDataDictionary::GetValueAtPosition (uint32_t pos)
 {
     PyObject *key, *value;
@@ -219,16 +267,16 @@
     
     while (PyDict_Next(GetPythonObject(), &pos_iter, &key, &value)) {
         if (pos-- == 0)
-            return new PythonDataObject(value);
+            return PythonDataObject(value);
     }
-    return NULL;
+    return PythonDataObject();
 }
 
 void
-PythonDataDictionary::SetItemForKey (PythonDataString* key, PythonDataObject* value)
+PythonDataDictionary::SetItemForKey (const PythonDataString &key, const PythonDataObject &value)
 {
-    if (m_object && key && value && *key && *value)
-        PyDict_SetItem(GetPythonObject(), key->GetPythonObject(), value->GetPythonObject());
+    if (m_object && key && value)
+        PyDict_SetItem(GetPythonObject(), key.GetPythonObject(), value.GetPythonObject());
 }
 
 #endif





More information about the lldb-commits mailing list