From dac97f31a38cd97c4c2c0465d8fcd462e7744145 Mon Sep 17 00:00:00 2001
From: Caroline Tice <ctice@apple.com>
Date: Wed, 22 Sep 2010 23:01:29 +0000
Subject: [PATCH] Remove all the __repr__ methods from the API/*.h files, and
 put them into python-extensions.swig, which gets included into lldb.swig, and
 adds them back into the classes when swig generates it's C++ file.  This
 keeps the Python stuff out of the general API classes.

Also fixed a small bug in the copy constructor for SBSymbolContext.

llvm-svn: 114602
---
 lldb/include/lldb/API/SBAddress.h             |   7 -
 lldb/include/lldb/API/SBBlock.h               |   7 -
 lldb/include/lldb/API/SBBreakpoint.h          |   7 -
 lldb/include/lldb/API/SBBreakpointLocation.h  |   7 -
 lldb/include/lldb/API/SBCommandReturnObject.h |   7 -
 lldb/include/lldb/API/SBCompileUnit.h         |   7 -
 lldb/include/lldb/API/SBDebugger.h            |   7 -
 lldb/include/lldb/API/SBDefines.h             |   5 -
 lldb/include/lldb/API/SBError.h               |   7 -
 lldb/include/lldb/API/SBEvent.h               |   7 -
 lldb/include/lldb/API/SBFileSpec.h            |   7 -
 lldb/include/lldb/API/SBFrame.h               |   7 -
 lldb/include/lldb/API/SBFunction.h            |   7 -
 lldb/include/lldb/API/SBInstruction.h         |   7 -
 lldb/include/lldb/API/SBLineEntry.h           |   7 -
 lldb/include/lldb/API/SBModule.h              |   7 -
 lldb/include/lldb/API/SBProcess.h             |   7 -
 lldb/include/lldb/API/SBSymbol.h              |   7 -
 lldb/include/lldb/API/SBSymbolContext.h       |  16 +--
 lldb/include/lldb/API/SBTarget.h              |   7 -
 lldb/include/lldb/API/SBThread.h              |   7 -
 lldb/include/lldb/API/SBType.h                |   7 -
 lldb/include/lldb/API/SBValue.h               |   7 -
 lldb/lldb.xcodeproj/project.pbxproj           |   3 +-
 lldb/scripts/Python/python-extensions.swig    | 135 ++++++++++++++++++
 lldb/scripts/lldb.swig                        |   2 +-
 lldb/source/API/SBAddress.cpp                 |  10 +-
 lldb/source/API/SBBlock.cpp                   |   9 --
 lldb/source/API/SBBreakpoint.cpp              |  11 +-
 lldb/source/API/SBBreakpointLocation.cpp      |  10 +-
 lldb/source/API/SBCommandReturnObject.cpp     |   8 --
 lldb/source/API/SBCompileUnit.cpp             |  10 +-
 lldb/source/API/SBDebugger.cpp                |   9 --
 lldb/source/API/SBError.cpp                   |   8 --
 lldb/source/API/SBEvent.cpp                   |  10 +-
 lldb/source/API/SBFileSpec.cpp                |   8 --
 lldb/source/API/SBFrame.cpp                   |  10 +-
 lldb/source/API/SBFunction.cpp                |  10 +-
 lldb/source/API/SBLineEntry.cpp               |   8 --
 lldb/source/API/SBModule.cpp                  |  10 +-
 lldb/source/API/SBProcess.cpp                 |   8 --
 lldb/source/API/SBSymbol.cpp                  |  13 +-
 lldb/source/API/SBSymbolContext.cpp           |  25 ++--
 lldb/source/API/SBTarget.cpp                  |  10 +-
 lldb/source/API/SBThread.cpp                  |   8 --
 lldb/source/API/SBType.cpp                    |   8 --
 lldb/source/API/SBValue.cpp                   |   8 --
 47 files changed, 171 insertions(+), 348 deletions(-)
 create mode 100644 lldb/scripts/Python/python-extensions.swig

diff --git a/lldb/include/lldb/API/SBAddress.h b/lldb/include/lldb/API/SBAddress.h
index d9d4ace706b6..ffafdf179bb6 100644
--- a/lldb/include/lldb/API/SBAddress.h
+++ b/lldb/include/lldb/API/SBAddress.h
@@ -47,13 +47,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
 
     friend class SBFrame;
diff --git a/lldb/include/lldb/API/SBBlock.h b/lldb/include/lldb/API/SBBlock.h
index c49c9a17a954..4e84aaee9005 100644
--- a/lldb/include/lldb/API/SBBlock.h
+++ b/lldb/include/lldb/API/SBBlock.h
@@ -52,13 +52,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject *, which contains a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBFrame;
     friend class SBSymbolContext;
diff --git a/lldb/include/lldb/API/SBBreakpoint.h b/lldb/include/lldb/API/SBBreakpoint.h
index b24515b6adca..2c0c256c847d 100644
--- a/lldb/include/lldb/API/SBBreakpoint.h
+++ b/lldb/include/lldb/API/SBBreakpoint.h
@@ -116,13 +116,6 @@ public:
     static lldb::SBBreakpointLocation
     GetBreakpointLocationAtIndexFromEvent (const lldb::SBEvent& event, uint32_t loc_idx);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBBreakpointLocation;
     friend class SBTarget;
diff --git a/lldb/include/lldb/API/SBBreakpointLocation.h b/lldb/include/lldb/API/SBBreakpointLocation.h
index 32e83f245bd2..529cd0570b6f 100644
--- a/lldb/include/lldb/API/SBBreakpointLocation.h
+++ b/lldb/include/lldb/API/SBBreakpointLocation.h
@@ -74,13 +74,6 @@ public:
     SBBreakpoint
     GetBreakpoint ();
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBBreakpoint;
 
diff --git a/lldb/include/lldb/API/SBCommandReturnObject.h b/lldb/include/lldb/API/SBCommandReturnObject.h
index 5d52dc1c2116..1533816367da 100644
--- a/lldb/include/lldb/API/SBCommandReturnObject.h
+++ b/lldb/include/lldb/API/SBCommandReturnObject.h
@@ -61,13 +61,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBCommandInterpreter;
     friend class SBOptions;
diff --git a/lldb/include/lldb/API/SBCompileUnit.h b/lldb/include/lldb/API/SBCompileUnit.h
index 5c7d6a45f288..e0bebd079c30 100644
--- a/lldb/include/lldb/API/SBCompileUnit.h
+++ b/lldb/include/lldb/API/SBCompileUnit.h
@@ -53,13 +53,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBFrame;
     friend class SBSymbolContext;
diff --git a/lldb/include/lldb/API/SBDebugger.h b/lldb/include/lldb/API/SBDebugger.h
index bef4cad14390..d05bb9fc572c 100644
--- a/lldb/include/lldb/API/SBDebugger.h
+++ b/lldb/include/lldb/API/SBDebugger.h
@@ -157,13 +157,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
     uint32_t
     GetTerminalWidth () const;
 
diff --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h
index 3a2ad79b704a..fb59cc645424 100644
--- a/lldb/include/lldb/API/SBDefines.h
+++ b/lldb/include/lldb/API/SBDefines.h
@@ -10,11 +10,6 @@
 #ifndef LLDB_SBDefines_h_
 #define LLDB_SBDefines_h_
 
-// In order to guarantee correct working with Python, Python.h *MUST* be
-// the *FIRST* header file included:
-
-#include <Python.h>
-
 // C Includes
 // C++ Includes
 // Other libraries and framework includes
diff --git a/lldb/include/lldb/API/SBError.h b/lldb/include/lldb/API/SBError.h
index 3fedd13ff87c..cbea6910ffa3 100644
--- a/lldb/include/lldb/API/SBError.h
+++ b/lldb/include/lldb/API/SBError.h
@@ -68,13 +68,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBArguments;
     friend class SBDebugger;
diff --git a/lldb/include/lldb/API/SBEvent.h b/lldb/include/lldb/API/SBEvent.h
index 9f6a425058c3..cab413a9ab37 100644
--- a/lldb/include/lldb/API/SBEvent.h
+++ b/lldb/include/lldb/API/SBEvent.h
@@ -57,13 +57,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-   PyObject *
-   __repr__ ();
-
 protected:
     friend class SBListener;
     friend class SBBroadcaster;
diff --git a/lldb/include/lldb/API/SBFileSpec.h b/lldb/include/lldb/API/SBFileSpec.h
index fa8564df2b2c..db76655795a8 100644
--- a/lldb/include/lldb/API/SBFileSpec.h
+++ b/lldb/include/lldb/API/SBFileSpec.h
@@ -54,13 +54,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBBlock;
     friend class SBCompileUnit;
diff --git a/lldb/include/lldb/API/SBFrame.h b/lldb/include/lldb/API/SBFrame.h
index ae12452e6f3a..67c4b60c20e5 100644
--- a/lldb/include/lldb/API/SBFrame.h
+++ b/lldb/include/lldb/API/SBFrame.h
@@ -115,13 +115,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBValue;
 
diff --git a/lldb/include/lldb/API/SBFunction.h b/lldb/include/lldb/API/SBFunction.h
index 4e7ae8081a43..643d5273471a 100644
--- a/lldb/include/lldb/API/SBFunction.h
+++ b/lldb/include/lldb/API/SBFunction.h
@@ -42,13 +42,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBFrame;
     friend class SBSymbolContext;
diff --git a/lldb/include/lldb/API/SBInstruction.h b/lldb/include/lldb/API/SBInstruction.h
index 0461ce34e420..d64a4d3da0d6 100644
--- a/lldb/include/lldb/API/SBInstruction.h
+++ b/lldb/include/lldb/API/SBInstruction.h
@@ -49,13 +49,6 @@ public:
     //bool
     //GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    //PyObject *
-    //__repr__ ();
-
 private:
 
     //lldb_private::Disassembler::Instruction::SharedPtr  m_opaque_sp;
diff --git a/lldb/include/lldb/API/SBLineEntry.h b/lldb/include/lldb/API/SBLineEntry.h
index 619884ed3100..851e63f7ea7f 100644
--- a/lldb/include/lldb/API/SBLineEntry.h
+++ b/lldb/include/lldb/API/SBLineEntry.h
@@ -61,13 +61,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBCompileUnit;
     friend class SBFrame;
diff --git a/lldb/include/lldb/API/SBModule.h b/lldb/include/lldb/API/SBModule.h
index 6231d13a319b..835386d3129d 100644
--- a/lldb/include/lldb/API/SBModule.h
+++ b/lldb/include/lldb/API/SBModule.h
@@ -52,13 +52,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBSymbolContext;
     friend class SBTarget;
diff --git a/lldb/include/lldb/API/SBProcess.h b/lldb/include/lldb/API/SBProcess.h
index 3543ee1e9e80..0e462ce3e929 100644
--- a/lldb/include/lldb/API/SBProcess.h
+++ b/lldb/include/lldb/API/SBProcess.h
@@ -155,13 +155,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It take no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBAddress;
     friend class SBBreakpoint;
diff --git a/lldb/include/lldb/API/SBSymbol.h b/lldb/include/lldb/API/SBSymbol.h
index a51cb424c62c..9c056251421b 100644
--- a/lldb/include/lldb/API/SBSymbol.h
+++ b/lldb/include/lldb/API/SBSymbol.h
@@ -43,13 +43,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     friend class SBSymbolContext;
 
diff --git a/lldb/include/lldb/API/SBSymbolContext.h b/lldb/include/lldb/API/SBSymbolContext.h
index 659683f91483..6744fa8ddabf 100644
--- a/lldb/include/lldb/API/SBSymbolContext.h
+++ b/lldb/include/lldb/API/SBSymbolContext.h
@@ -44,6 +44,9 @@ public:
     SBLineEntry     GetLineEntry ();
     SBSymbol        GetSymbol ();
 
+    bool
+    GetDescription (lldb::SBStream &description);
+
 protected:
     friend class SBFrame;
     friend class SBModule;
@@ -58,6 +61,9 @@ protected:
     lldb_private::SymbolContext&
     operator*();
 
+    lldb_private::SymbolContext&
+    ref();
+
     const lldb_private::SymbolContext&
     operator*() const;
 
@@ -71,16 +77,6 @@ protected:
     void
     SetSymbolContext (const lldb_private::SymbolContext *sc_ptr);
 
-    bool
-    GetDescription (lldb::SBStream &description);
-
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 private:
     std::auto_ptr<lldb_private::SymbolContext> m_opaque_ap;
 };
diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h
index 1b4c09ab38c1..1f9d0ce1b253 100644
--- a/lldb/include/lldb/API/SBTarget.h
+++ b/lldb/include/lldb/API/SBTarget.h
@@ -145,13 +145,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBAddress;
     friend class SBDebugger;
diff --git a/lldb/include/lldb/API/SBThread.h b/lldb/include/lldb/API/SBThread.h
index ead395ae5290..bf2cc4f2e97b 100644
--- a/lldb/include/lldb/API/SBThread.h
+++ b/lldb/include/lldb/API/SBThread.h
@@ -91,13 +91,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBBreakpoint;
     friend class SBBreakpointLocation;
diff --git a/lldb/include/lldb/API/SBType.h b/lldb/include/lldb/API/SBType.h
index c745989364b8..b89aca92e51d 100644
--- a/lldb/include/lldb/API/SBType.h
+++ b/lldb/include/lldb/API/SBType.h
@@ -57,13 +57,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It takes no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     void *m_ast;
     void *m_type;
diff --git a/lldb/include/lldb/API/SBValue.h b/lldb/include/lldb/API/SBValue.h
index 78b4ea9bbf04..c247339ef5b3 100644
--- a/lldb/include/lldb/API/SBValue.h
+++ b/lldb/include/lldb/API/SBValue.h
@@ -88,13 +88,6 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
 
-    // The following function gets called by Python when a user tries to print
-    // an object of this class.  It take no arguments and returns a
-    // PyObject * representing a char * (and it must be named "__repr__");
-
-    PyObject *
-    __repr__ ();
-
 protected:
     friend class SBValueList;
     friend class SBFrame;
diff --git a/lldb/lldb.xcodeproj/project.pbxproj b/lldb/lldb.xcodeproj/project.pbxproj
index 752dc86fc99e..ddeb11274af6 100644
--- a/lldb/lldb.xcodeproj/project.pbxproj
+++ b/lldb/lldb.xcodeproj/project.pbxproj
@@ -1046,6 +1046,7 @@
 		9A42976211861AA600FE05CD /* CommandObjectBreakpointCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommandObjectBreakpointCommand.cpp; path = source/Commands/CommandObjectBreakpointCommand.cpp; sourceTree = "<group>"; };
 		9A4633DA11F65D8600955CE1 /* UserSettingsController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UserSettingsController.h; path = include/lldb/Core/UserSettingsController.h; sourceTree = "<group>"; };
 		9A4633DC11F65D9A00955CE1 /* UserSettingsController.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UserSettingsController.cpp; path = source/Core/UserSettingsController.cpp; sourceTree = "<group>"; };
+		9A48A3A7124AAA5A00922451 /* python-extensions.swig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "python-extensions.swig"; sourceTree = "<group>"; };
 		9A633FE7112DCE3C001A7E43 /* SBFrame.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBFrame.cpp; path = source/API/SBFrame.cpp; sourceTree = "<group>"; };
 		9A633FE8112DCE3C001A7E43 /* SBFrame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBFrame.h; path = include/lldb/API/SBFrame.h; sourceTree = "<group>"; };
 		9A82010B10FFB49800182560 /* ScriptInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScriptInterpreter.cpp; path = source/Interpreter/ScriptInterpreter.cpp; sourceTree = "<group>"; };
@@ -1553,6 +1554,7 @@
 				266960601199F4230075C61A /* build-swig-Python.sh */,
 				266960611199F4230075C61A /* edit-swig-python-wrapper-file.py */,
 				266960621199F4230075C61A /* finish-swig-Python-lldb.sh */,
+				9A48A3A7124AAA5A00922451 /* python-extensions.swig */,
 			);
 			path = Python;
 			sourceTree = "<group>";
@@ -2349,7 +2351,6 @@
 			isa = PBXProject;
 			buildConfigurationList = 1DEB91EF08733DB70010E9CD /* Build configuration list for PBXProject "lldb" */;
 			compatibilityVersion = "Xcode 3.1";
-			developmentRegion = English;
 			hasScannedForEncodings = 1;
 			knownRegions = (
 				en,
diff --git a/lldb/scripts/Python/python-extensions.swig b/lldb/scripts/Python/python-extensions.swig
new file mode 100644
index 000000000000..7d706fd0d635
--- /dev/null
+++ b/lldb/scripts/Python/python-extensions.swig
@@ -0,0 +1,135 @@
+
+%extend lldb::SBAddress {
+        PyObject *lldb::SBAddress::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBBlock {
+        PyObject *lldb::SBBlock::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBBreakpoint {
+        PyObject *lldb::SBBreakpoint::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription ("full", description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBBreakpointLocation {
+        PyObject *lldb::SBBreakpointLocation::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription ("full", description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBCommandReturnObject {
+        PyObject *lldb::SBCommandReturnObject::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBCompileUnit {
+        PyObject *lldb::SBCompileUnit::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBDebugger {
+        PyObject *lldb::SBDebugger::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBError {
+        PyObject *lldb::SBError::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBFileSpec {
+        PyObject *lldb::SBFileSpec::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBFrame {
+        PyObject *lldb::SBFrame::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBFunction {
+        PyObject *lldb::SBFunction::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBLineEntry {
+        PyObject *lldb::SBLineEntry::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBModule {
+        PyObject *lldb::SBModule::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBProcess {
+        PyObject *lldb::SBProcess::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBSymbol {
+        PyObject *lldb::SBSymbol::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBSymbolContext {
+        PyObject *lldb::SBSymbolContext::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBTarget {
+        PyObject *lldb::SBTarget::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBThread {
+        PyObject *lldb::SBThread::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+%extend lldb::SBValue {
+        PyObject *lldb::SBValue::__repr__ (){
+                lldb::SBStream description;
+                $self->GetDescription (description);
+                return PyString_FromString (description.GetData());
+        }
+}
+
diff --git a/lldb/scripts/lldb.swig b/lldb/scripts/lldb.swig
index 6c505d2426fc..d4c0220d559f 100644
--- a/lldb/scripts/lldb.swig
+++ b/lldb/scripts/lldb.swig
@@ -156,4 +156,4 @@ typedef int StopReason;
 %include "lldb/API/SBValueList.h"
 %include "lldb/lldb-types.h"
 
-
+%include "./Python/python-extensions.swig"
diff --git a/lldb/source/API/SBAddress.cpp b/lldb/source/API/SBAddress.cpp
index 0afe914e6dfd..7f21cf79fcad 100644
--- a/lldb/source/API/SBAddress.cpp
+++ b/lldb/source/API/SBAddress.cpp
@@ -140,6 +140,7 @@ SBAddress::operator*() const
 bool
 SBAddress::GetDescription (SBStream &description)
 {
+    description.ref();
     if (m_opaque_ap.get())
     {
         m_opaque_ap->DumpDebug (description.get());
@@ -149,12 +150,3 @@ SBAddress::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBAddress::__repr__ ()
-{
-    SBStream description;
-    description.ref();            // Make sure it contains a valid StreamString.
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBBlock.cpp b/lldb/source/API/SBBlock.cpp
index 02db9b2237a9..b57936d9fe0a 100644
--- a/lldb/source/API/SBBlock.cpp
+++ b/lldb/source/API/SBBlock.cpp
@@ -158,12 +158,3 @@ SBBlock::GetDescription (SBStream &description)
     
     return true;
 }
-
-PyObject *
-SBBlock::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBBreakpoint.cpp b/lldb/source/API/SBBreakpoint.cpp
index aa84575be2e9..70a70458b364 100644
--- a/lldb/source/API/SBBreakpoint.cpp
+++ b/lldb/source/API/SBBreakpoint.cpp
@@ -337,7 +337,7 @@ SBBreakpoint::GetDescription (const char *description_level, SBStream &descripti
         else
             level = eDescriptionLevelBrief;
 
-
+        description.ref();
         m_opaque_sp->GetDescription (description.get(), level);
         description.get()->EOL();
     }
@@ -347,15 +347,6 @@ SBBreakpoint::GetDescription (const char *description_level, SBStream &descripti
     return true;
 }
 
-PyObject *
-SBBreakpoint::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription ("full", description);
-    return PyString_FromString (description.GetData());
-}
-
 bool
 SBBreakpoint::PrivateBreakpointHitCallback 
 (
diff --git a/lldb/source/API/SBBreakpointLocation.cpp b/lldb/source/API/SBBreakpointLocation.cpp
index f7e059ba9677..07300eabac44 100644
--- a/lldb/source/API/SBBreakpointLocation.cpp
+++ b/lldb/source/API/SBBreakpointLocation.cpp
@@ -212,6 +212,7 @@ SBBreakpointLocation::GetDescription (const char *description_level, SBStream &d
         else
             level = eDescriptionLevelBrief;
 
+        description.ref();
         m_opaque_sp->GetDescription (description.get(), level);
         description.get()->EOL();
     }
@@ -221,15 +222,6 @@ SBBreakpointLocation::GetDescription (const char *description_level, SBStream &d
     return true;
 }
 
-PyObject *
-SBBreakpointLocation::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription ("full", description);
-    return PyString_FromString (description.GetData());
-}
-
 SBBreakpoint
 SBBreakpointLocation::GetBreakpoint ()
 {
diff --git a/lldb/source/API/SBCommandReturnObject.cpp b/lldb/source/API/SBCommandReturnObject.cpp
index 458ce504e3d3..35195f6da239 100644
--- a/lldb/source/API/SBCommandReturnObject.cpp
+++ b/lldb/source/API/SBCommandReturnObject.cpp
@@ -188,11 +188,3 @@ SBCommandReturnObject::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBCommandReturnObject::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBCompileUnit.cpp b/lldb/source/API/SBCompileUnit.cpp
index 9e68e28c01db..42bbf82a7e54 100644
--- a/lldb/source/API/SBCompileUnit.cpp
+++ b/lldb/source/API/SBCompileUnit.cpp
@@ -125,6 +125,7 @@ SBCompileUnit::GetDescription (SBStream &description)
 {
     if (m_opaque_ptr)
     {
+        description.ref();
         m_opaque_ptr->Dump (description.get(), false);
     }
     else
@@ -132,12 +133,3 @@ SBCompileUnit::GetDescription (SBStream &description)
     
     return true;
 }
-
-PyObject *
-SBCompileUnit::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp
index 0a48e69cb79a..81658d5ab597 100644
--- a/lldb/source/API/SBDebugger.cpp
+++ b/lldb/source/API/SBDebugger.cpp
@@ -695,12 +695,3 @@ SBDebugger::GetDescription (SBStream &description)
     
     return true;
 }
-
-
-PyObject *
-SBDebugger::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBError.cpp b/lldb/source/API/SBError.cpp
index 136561981b6e..9553b6f41667 100644
--- a/lldb/source/API/SBError.cpp
+++ b/lldb/source/API/SBError.cpp
@@ -198,11 +198,3 @@ SBError::GetDescription (SBStream &description)
 
     return true;
 } 
-
-PyObject *
-SBError::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBEvent.cpp b/lldb/source/API/SBEvent.cpp
index d77f00208354..d1123e7f8588 100644
--- a/lldb/source/API/SBEvent.cpp
+++ b/lldb/source/API/SBEvent.cpp
@@ -156,6 +156,7 @@ SBEvent::GetDescription (SBStream &description)
 {
     if (m_opaque)
     {
+        description.ref();
         m_opaque->Dump (description.get());
     }
     else
@@ -163,12 +164,3 @@ SBEvent::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBEvent::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBFileSpec.cpp b/lldb/source/API/SBFileSpec.cpp
index 1edd9468b505..36fcf1bbf2e3 100644
--- a/lldb/source/API/SBFileSpec.cpp
+++ b/lldb/source/API/SBFileSpec.cpp
@@ -158,11 +158,3 @@ SBFileSpec::GetDescription (SBStream &description)
     
     return true;
 }
-
-PyObject *
-SBFileSpec::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBFrame.cpp b/lldb/source/API/SBFrame.cpp
index 90952691df0e..9e0f35d0a1dd 100644
--- a/lldb/source/API/SBFrame.cpp
+++ b/lldb/source/API/SBFrame.cpp
@@ -394,6 +394,7 @@ SBFrame::GetDescription (SBStream &description)
 {
     if (m_opaque_sp)
     {
+        description.ref();
         m_opaque_sp->Dump (description.get(), true, false);
     }
     else
@@ -401,12 +402,3 @@ SBFrame::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBFrame::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBFunction.cpp b/lldb/source/API/SBFunction.cpp
index 4c5f64e23ca5..38e349bd3f91 100644
--- a/lldb/source/API/SBFunction.cpp
+++ b/lldb/source/API/SBFunction.cpp
@@ -69,6 +69,7 @@ SBFunction::GetDescription (SBStream &description)
 {
     if (m_opaque_ptr)
     {
+        description.ref();
         m_opaque_ptr->Dump (description.get(), false);
     }
     else
@@ -76,12 +77,3 @@ SBFunction::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBFunction::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBLineEntry.cpp b/lldb/source/API/SBLineEntry.cpp
index 27f01d5a2da9..023afb94aad8 100644
--- a/lldb/source/API/SBLineEntry.cpp
+++ b/lldb/source/API/SBLineEntry.cpp
@@ -172,11 +172,3 @@ SBLineEntry::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBLineEntry::__repr__ ()
-{
-    SBStream description; 
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp
index a0b340a9cb7d..b59a60029251 100644
--- a/lldb/source/API/SBModule.cpp
+++ b/lldb/source/API/SBModule.cpp
@@ -133,6 +133,7 @@ SBModule::GetDescription (SBStream &description)
 {
     if (m_opaque_sp)
     {
+        description.ref();
         m_opaque_sp->Dump (description.get());
     }
     else
@@ -140,12 +141,3 @@ SBModule::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBModule::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBProcess.cpp b/lldb/source/API/SBProcess.cpp
index d2c85ba90312..2d6e6b03a92f 100644
--- a/lldb/source/API/SBProcess.cpp
+++ b/lldb/source/API/SBProcess.cpp
@@ -485,11 +485,3 @@ SBProcess::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBProcess::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBSymbol.cpp b/lldb/source/API/SBSymbol.cpp
index 3c700e170354..017df4394ca7 100644
--- a/lldb/source/API/SBSymbol.cpp
+++ b/lldb/source/API/SBSymbol.cpp
@@ -69,19 +69,12 @@ SBSymbol::GetDescription (SBStream &description)
 {
     if (m_opaque_ptr)
     {
-        m_opaque_ptr->GetDescription (description.get(), lldb::eDescriptionLevelFull, NULL);
+        description.ref();
+        m_opaque_ptr->GetDescription (description.get(), 
+                                      lldb::eDescriptionLevelFull, NULL);
     }
     else
         description.Printf ("No value");
     
     return true;
 }
-
-PyObject *
-SBSymbol::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBSymbolContext.cpp b/lldb/source/API/SBSymbolContext.cpp
index 4381994c70f1..ecd7d6677ff0 100644
--- a/lldb/source/API/SBSymbolContext.cpp
+++ b/lldb/source/API/SBSymbolContext.cpp
@@ -32,7 +32,12 @@ SBSymbolContext::SBSymbolContext (const SBSymbolContext& rhs) :
     m_opaque_ap ()
 {
     if (rhs.IsValid())
-        *m_opaque_ap = *rhs.m_opaque_ap;
+    {
+        if (m_opaque_ap.get())
+            *m_opaque_ap = *rhs.m_opaque_ap;
+        else
+            ref() = *rhs.m_opaque_ap;
+    }
 }
 
 SBSymbolContext::~SBSymbolContext ()
@@ -141,6 +146,14 @@ SBSymbolContext::operator*()
     return *m_opaque_ap.get();
 }
 
+lldb_private::SymbolContext&
+SBSymbolContext::ref()
+{
+    if (m_opaque_ap.get() == NULL)
+        m_opaque_ap.reset (new SymbolContext);
+    return *m_opaque_ap.get();
+}
+
 lldb_private::SymbolContext *
 SBSymbolContext::get() const
 {
@@ -152,6 +165,7 @@ SBSymbolContext::GetDescription (SBStream &description)
 {
     if (m_opaque_ap.get())
     {
+        description.ref();
         m_opaque_ap->GetDescription (description.get(), lldb::eDescriptionLevelFull, NULL);
     }
     else
@@ -159,12 +173,3 @@ SBSymbolContext::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBSymbolContext::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp
index cc544db90885..f34b0142cb22 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -507,6 +507,7 @@ SBTarget::GetDescription (SBStream &description)
 {
     if (m_opaque_sp)
     {
+        description.ref();
         m_opaque_sp->Dump (description.get());
     }
     else
@@ -514,12 +515,3 @@ SBTarget::GetDescription (SBStream &description)
     
     return true;
 }
-
-PyObject *
-SBTarget::__repr__ ()
-{
-    SBStream description;
-    description.ref();
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBThread.cpp b/lldb/source/API/SBThread.cpp
index aa621fb13c03..8c74a34cdbec 100644
--- a/lldb/source/API/SBThread.cpp
+++ b/lldb/source/API/SBThread.cpp
@@ -428,11 +428,3 @@ SBThread::GetDescription (SBStream &description)
     
     return true;
 }
-
-PyObject *
-SBThread::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp
index d34e94a14607..5c3e97381add 100644
--- a/lldb/source/API/SBType.cpp
+++ b/lldb/source/API/SBType.cpp
@@ -185,14 +185,6 @@ SBType::GetDescription (SBStream &description)
     return true;
 }
 
-PyObject *
-SBType::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}
-
 SBTypeMember::SBTypeMember () :
     m_ast (NULL),
     m_parent_type (NULL),
diff --git a/lldb/source/API/SBValue.cpp b/lldb/source/API/SBValue.cpp
index e2fdffab5929..4b437f4e1b57 100644
--- a/lldb/source/API/SBValue.cpp
+++ b/lldb/source/API/SBValue.cpp
@@ -293,11 +293,3 @@ SBValue::GetDescription (SBStream &description)
 
     return true;
 }
-
-PyObject *
-SBValue::__repr__ ()
-{
-    SBStream description;
-    GetDescription (description);
-    return PyString_FromString (description.GetData());
-}