Second attempt at getting the PyCallable changes in trunk

Thanks to Daniel Malea for helping test this patch for Linux happiness!



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@185965 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/lldb/API/SBCommandReturnObject.h b/include/lldb/API/SBCommandReturnObject.h
index cf04a3e..f2d2748 100644
--- a/include/lldb/API/SBCommandReturnObject.h
+++ b/include/lldb/API/SBCommandReturnObject.h
@@ -98,6 +98,13 @@
     const char *
     GetError (bool only_if_no_immediate);
     
+    void
+    SetError (lldb::SBError &error,
+              const char *fallback_error_cstr = NULL);
+    
+    void
+    SetError (const char* error_cstr);
+    
 protected:
     friend class SBCommandInterpreter;
     friend class SBOptions;
diff --git a/include/lldb/API/SBError.h b/include/lldb/API/SBError.h
index fbfb34d..a6d3dac 100644
--- a/include/lldb/API/SBError.h
+++ b/include/lldb/API/SBError.h
@@ -66,6 +66,7 @@
 
 protected:
 
+    friend class SBCommandReturnObject;
     friend class SBData;
     friend class SBDebugger;
     friend class SBCommunication;
diff --git a/include/lldb/Interpreter/CommandReturnObject.h b/include/lldb/Interpreter/CommandReturnObject.h
index aa92e14..acd0399 100644
--- a/include/lldb/Interpreter/CommandReturnObject.h
+++ b/include/lldb/Interpreter/CommandReturnObject.h
@@ -142,8 +142,11 @@
     AppendErrorWithFormat (const char *format, ...)  __attribute__ ((format (printf, 2, 3)));
 
     void
-    SetError (const Error &error, 
-              const char *fallback_error_cstr);
+    SetError (const Error &error,
+              const char *fallback_error_cstr = NULL);
+    
+    void
+    SetError (const char *error_cstr);
 
     lldb::ReturnStatus
     GetStatus();
diff --git a/include/lldb/Interpreter/ScriptInterpreter.h b/include/lldb/Interpreter/ScriptInterpreter.h
index 2566c3f..9a66c77 100644
--- a/include/lldb/Interpreter/ScriptInterpreter.h
+++ b/include/lldb/Interpreter/ScriptInterpreter.h
@@ -120,7 +120,6 @@
                                                                      const char *session_dictionary_name,
                                                                      lldb::DebuggerSP& debugger,
                                                                      const char* args,
-                                                                     std::string& err_msg,
                                                                      lldb_private::CommandReturnObject& cmd_retobj);
     
     typedef bool           (*SWIGPythonCallModuleInit)              (const char *python_module_name,
diff --git a/scripts/Python/interface/SBCommandReturnObject.i b/scripts/Python/interface/SBCommandReturnObject.i
index 7cec069..5ade97b 100644
--- a/scripts/Python/interface/SBCommandReturnObject.i
+++ b/scripts/Python/interface/SBCommandReturnObject.i
@@ -58,6 +58,13 @@
 
     void
     SetStatus (lldb::ReturnStatus status);
+    
+    void
+    SetError (lldb::SBError &error,
+              const char *fallback_error_cstr = NULL);
+    
+    void
+    SetError (const char *error_cstr);
 
     lldb::ReturnStatus
     GetStatus();
diff --git a/scripts/Python/python-swigsafecast.swig b/scripts/Python/python-swigsafecast.swig
index 7ee658f..da7444c 100644
--- a/scripts/Python/python-swigsafecast.swig
+++ b/scripts/Python/python-swigsafecast.swig
@@ -1,7 +1,3 @@
-#ifndef __cplusplus
-#error needs C++ to build these
-#endif
-
 // leaving this undefined ensures we will get a linker error if we try to use SBTypeToSWIGWrapper()
 // for a type for which we did not specialze this function
 template <typename SBClass>
@@ -15,6 +11,38 @@
     return SBTypeToSWIGWrapper(&sb_object);
 }
 
+template <typename SBClass>
+PyObject*
+SBTypeToSWIGWrapper (const SBClass& sb_object)
+{
+    return SBTypeToSWIGWrapper(&sb_object);
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (PyObject* py_object)
+{
+    return py_object;
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (const char* c_str)
+{
+    if (c_str)
+        return PyString_FromString(c_str);
+    return NULL;
+}
+
+template <>
+PyObject*
+SBTypeToSWIGWrapper (unsigned int* c_int)
+{
+    if (!c_int)
+        return NULL;
+    return PyInt_FromLong(*c_int);
+}
+
 template <>
 PyObject*
 SBTypeToSWIGWrapper (lldb::SBProcess* process_sb)
diff --git a/scripts/Python/python-wrapper.swig b/scripts/Python/python-wrapper.swig
index 05b0921..f3cbf5f 100644
--- a/scripts/Python/python-wrapper.swig
+++ b/scripts/Python/python-wrapper.swig
@@ -1,1403 +1,985 @@
-%wrapper %{
-
-class PyErr_Cleaner
-{
-public:
-    PyErr_Cleaner(bool print=false) :
-        m_print(print)
-    {
-    }
-
-    ~PyErr_Cleaner()
-    {
-        if (PyErr_Occurred())
-        {
-            if(m_print)
-                PyErr_Print();
-            PyErr_Clear();
-        }
-    }
-
-private:
-    bool m_print;
-};
-
-// resolve a dotted Python name in the form
-// foo.bar.baz.Foobar to an actual Python object
-// if pmodule is NULL, the __main__ module will be used
-// as the starting point for the search
-
-static PyObject*
-ResolvePythonName(const char* name,
-                  PyObject* pmodule = NULL)
-{
-    if (!name)
-        return pmodule;
-
-    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
-
-    PyObject* main_dict;
-
-    if (!pmodule)
-    {
-        pmodule = PyImport_AddModule ("__main__");
-        if (!pmodule)
-            return NULL;
-    }
-
-    if (PyType_Check(pmodule))
-    {
-        main_dict = ((PyTypeObject*)pmodule)->tp_dict;
-        if (!main_dict)
-            return NULL;
-    }
-    else if (!PyDict_Check(pmodule))
-    {
-        main_dict = PyModule_GetDict (pmodule);
-        if (!main_dict)
-            return NULL;
-    }
-    else
-        main_dict = pmodule;
-
-    const char* dot_pos = ::strchr(name, '.');
-
-    PyObject *dest_object;
-    PyObject *key, *value;
-    Py_ssize_t pos = 0;
-
-    if (!dot_pos)
-    {
-        dest_object = NULL;   
-        while (PyDict_Next (main_dict, &pos, &key, &value))
-        {
-            // We have stolen references to the key and value objects in the dictionary; we need to increment 
-            // them now so that Python's garbage collector doesn't collect them out from under us.
-            Py_INCREF (key);
-            Py_INCREF (value);
-            if (strcmp (PyString_AsString (key), name) == 0)
-            {
-                dest_object = value;
-                break;
-            }
-        }        
-        if (!dest_object || dest_object == Py_None)
-            return NULL;
-        return dest_object;
-    }
-    else
-    {
-        size_t len = dot_pos - name;
-        std::string piece(name,len);
-        pmodule = ResolvePythonName(piece.c_str(), main_dict);
-        if (!pmodule)
-            return NULL;
-        name = dot_pos+1;
-        return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler
-    }
-}
-
-static PyObject*
-FindSessionDictionary(const char *session_dictionary_name)
-{
-    return ResolvePythonName(session_dictionary_name, NULL);
-}
-
-// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)
-// and is used when a script command is attached to a breakpoint for execution.
-
-SWIGEXPORT bool
-LLDBSwigPythonBreakpointCallbackFunction 
-(
-    const char *python_function_name,
-    const char *session_dictionary_name,
-    const lldb::StackFrameSP& frame_sp, 
-    const lldb::BreakpointLocationSP& bp_loc_sp
-)
-{
-    lldb::SBFrame sb_frame (frame_sp);
-    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
-
-    bool stop_at_breakpoint = true;
-    PyObject *Frame_PyObj = SBTypeToSWIGWrapper(sb_frame);
-    PyObject *Bp_Loc_PyObj = SBTypeToSWIGWrapper(sb_bp_loc);
-    
-    if (Frame_PyObj == NULL || Bp_Loc_PyObj == NULL)
-        return stop_at_breakpoint;
-        
-    if (!python_function_name || !session_dictionary_name)
-        return stop_at_breakpoint;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        if (pfunc != NULL)
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (3);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return stop_at_breakpoint;
-                }
-                
-                PyTuple_SetItem (pargs, 0, Frame_PyObj);  // This "steals" a reference to Frame_PyObj
-                PyTuple_SetItem (pargs, 1, Bp_Loc_PyObj); // This "steals" a reference to Bp_Loc_PyObj
-                PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (pvalue != NULL)
-                {
-                    // be very conservative here and only refuse to stop if the user
-                    // actually returned False - anything else, just stop
-                    if (pvalue == Py_False)
-                        stop_at_breakpoint = false;
-                    Py_XDECREF (pvalue);
-                }
-                else if (PyErr_Occurred ())
-                {
-                    PyErr_Clear();
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Clear();
-            }
-        }
-        else if (PyErr_Occurred())
-        {
-            PyErr_Clear();
-        }
-    }
-    else if (PyErr_Occurred ())
-    {
-        PyErr_Clear ();
-    }
-    return stop_at_breakpoint;
-}
-
-// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)
-// and is used when a script command is attached to a watchpoint for execution.
-
-SWIGEXPORT bool
-LLDBSwigPythonWatchpointCallbackFunction 
-(
-    const char *python_function_name,
-    const char *session_dictionary_name,
-    const lldb::StackFrameSP& frame_sp, 
-    const lldb::WatchpointSP& wp_sp
-)
-{
-    lldb::SBFrame sb_frame (frame_sp);
-    lldb::SBWatchpoint sb_wp(wp_sp);
-
-    bool stop_at_watchpoint = true;
-    PyObject *Frame_PyObj = SBTypeToSWIGWrapper(sb_frame);
-    PyObject *Wp_PyObj = SBTypeToSWIGWrapper(sb_wp);
-    
-    if (Frame_PyObj == NULL || Wp_PyObj == NULL)
-        return stop_at_watchpoint;
-        
-    if (!python_function_name || !session_dictionary_name)
-        return stop_at_watchpoint;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        if (pfunc != NULL)
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (3);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return stop_at_watchpoint;
-                }
-                
-                PyTuple_SetItem (pargs, 0, Frame_PyObj);  // This "steals" a reference to Frame_PyObj
-                PyTuple_SetItem (pargs, 1, Wp_PyObj);     // This "steals" a reference to Wp_PyObj
-                PyTuple_SetItem (pargs, 2, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (pvalue != NULL)
-                {
-                    Py_XDECREF (pvalue);
-                }
-                else if (PyErr_Occurred ())
-                {
-                    PyErr_Clear();
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Clear();
-            }
-        }
-        else if (PyErr_Occurred())
-        {
-            PyErr_Clear();
-        }
-    }
-    else if (PyErr_Occurred ())
-    {
-        PyErr_Clear ();
-    }
-    return stop_at_watchpoint;
-}
-
-SWIGEXPORT bool
-LLDBSwigPythonCallTypeScript 
-(
-    const char *python_function_name,
-    const void *session_dictionary,
-    const lldb::ValueObjectSP& valobj_sp,
-    void** pyfunct_wrapper,
-    std::string& retval
-)
-{
-    lldb::SBValue sb_value (valobj_sp);
-
-    retval.clear();
-
-    PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
-    
-    if (ValObj_PyObj == NULL)
-        return false;
-        
-    if (!python_function_name || !session_dictionary)
-        return false;
-
-    PyObject *session_dict = (PyObject*)session_dictionary, *pfunc = NULL, *pargs = NULL, *pvalue = NULL;
-    
-    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))
-    {
-        pfunc = (PyObject*)(*pyfunct_wrapper);
-        if (pfunc->ob_refcnt == 1)
-        {
-            Py_XDECREF(pfunc);
-            pfunc = NULL;
-        }
-    }
-
-    if (PyDict_Check(session_dict))
-    {
-        PyErr_Cleaner pyerr_cleanup(true);  // show Python errors
-        
-        if (!pfunc)
-        {
-            pfunc = ResolvePythonName (python_function_name, session_dict);
-            if (!pfunc || !PyFunction_Check (pfunc))
-                return false;
-            else
-            {
-                if (pyfunct_wrapper)
-                    *pyfunct_wrapper = pfunc;
-            }
-        }
-        /*else
-            printf("caching works!!!!\n");*/
-        
-        pargs = PyTuple_Pack(2, ValObj_PyObj, session_dict);
-        if (pargs == NULL)
-            return false;
-        
-        pvalue = PyObject_CallObject (pfunc, pargs);
-        Py_XDECREF (pargs);
-        
-        if (pvalue != NULL && pvalue != Py_None)
-        {
-            if (PyString_Check(pvalue))
-                retval.assign(PyString_AsString(pvalue));
-            else
-            {
-                PyObject* value_as_string = PyObject_Str(pvalue);
-                if (value_as_string && value_as_string != Py_None && PyString_Check(value_as_string))
-                    retval.assign(PyString_AsString(value_as_string));
-                Py_XDECREF(value_as_string);
-            }
-        }
-        Py_XDECREF (pvalue);
-        Py_INCREF (session_dict);
-    }
-    return true;
-}
-
-SWIGEXPORT void*
-LLDBSwigPythonCreateSyntheticProvider 
-(
-    const char *python_class_name,
-    const char *session_dictionary_name,
-    const lldb::ValueObjectSP& valobj_sp
-)
-{
-    PyObject* retval = NULL;
-
-    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
-        Py_RETURN_NONE;
-
-    // I do not want the SBValue to be deallocated when going out of scope because python
-    // has ownership of it and will manage memory for this object by itself
-    lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);
-    sb_value->SetPreferSyntheticValue(false);
-
-    PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);
-
-    if (ValObj_PyObj == NULL)
-        Py_RETURN_NONE;
-
-    const char* python_function_name = python_class_name;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        if (pfunc != NULL)
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                PyObject *argList = Py_BuildValue("SS", ValObj_PyObj, session_dict);
-
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                    return retval;
-                }
-
-                if (argList == NULL)
-                {
-                    return retval;
-                }
-
-                Py_INCREF(ValObj_PyObj);
-
-                pvalue = PyObject_CallObject(pfunc, argList);
-
-                Py_XDECREF(argList);
-
-                if (pvalue != NULL)
-                {
-                    if (pvalue != Py_None)
-                        retval = pvalue;
-                    else
-                    {
-                        retval = Py_None;
-                        Py_INCREF(retval);
-                    }
-                }
-                else if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-        else if (PyErr_Occurred())
-        {
-            PyErr_Print();
-            PyErr_Clear();
-        }
-    }
-    else if (PyErr_Occurred ())
-    {
-        PyErr_Print();
-        PyErr_Clear ();
-    }
-    if (retval)
-        return retval;
-    else
-        Py_RETURN_NONE;
-}
-
-// wrapper that calls an optional instance member of an object taking no arguments
-static PyObject*
-LLDBSwigPython_CallOptionalMember
-(
-    PyObject* self,
-    char* callee_name,
-    PyObject* ret_if_not_found = Py_None,
-    bool* was_found = NULL
-)
-{
-    if (self == NULL || self == Py_None)
-    {
-        if (was_found)
-            *was_found = false;
-        Py_XINCREF(ret_if_not_found);
-        return ret_if_not_found;
-    }
-
-    PyObject* pmeth  = PyObject_GetAttrString(self, callee_name);
-
-    if (PyErr_Occurred())
-    {
-        PyErr_Clear();
-    }
-
-    if (pmeth == NULL || pmeth == Py_None)
-    {
-        if (was_found)
-            *was_found = false;
-        Py_XDECREF(pmeth);
-        Py_XINCREF(ret_if_not_found);
-        return ret_if_not_found;
-    }
-
-    if (PyCallable_Check(pmeth) == 0)
-    {
-        if (PyErr_Occurred())
-        {
-            PyErr_Clear();
-        }
-
-        Py_XDECREF(pmeth);
-        if (was_found)
-            *was_found = false;
-        Py_XINCREF(ret_if_not_found);
-        return ret_if_not_found;
-    }
-
-    if (was_found)
-        *was_found = true;
-
-    if (PyErr_Occurred())
-    {
-        PyErr_Clear();
-    }
-
-    Py_XDECREF(pmeth);
-
-    // right now we know this function exists and is callable..
-    PyObject* py_return = PyObject_CallMethod(self, callee_name, NULL);
-    
-    // if it fails, print the error but otherwise go on
-    if (PyErr_Occurred())
-    {
-        PyErr_Print();
-        PyErr_Clear();
-    }
-    
-    return py_return;
-}
-
-SWIGEXPORT uint32_t
-LLDBSwigPython_CalculateNumChildren
-(
-    PyObject *implementor
-)
-{
-    uint32_t ret_val = UINT32_MAX;
-
-    static char callee_name[] = "num_children";
-
-    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);
-
-    if (!py_return)
-        return ret_val;
-
-    if (PyInt_Check(py_return))
-        ret_val = PyInt_AsLong(py_return);
-
-    Py_XDECREF(py_return);
-    
-    if (PyErr_Occurred())
-    {
-        PyErr_Print();
-        PyErr_Clear();
-    }
-    
-    return ret_val;
-}
-
-SWIGEXPORT PyObject*
-LLDBSwigPython_GetChildAtIndex
-(
-    PyObject *implementor,
-    uint32_t idx
-)
-{
-
-    static char callee_name[] = "get_child_at_index";
-    static char param_format[] = "i";
-
-    if (implementor == NULL || implementor == Py_None)
-        return NULL;
-    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, idx);
-    if (PyErr_Occurred())
-    {
-        PyErr_Print();
-        PyErr_Clear();
-    }
-    
-    if (py_return == NULL || py_return == Py_None)
-    {
-        Py_XDECREF(py_return);
-        return NULL;
-    }
-    
-    lldb::SBValue* sbvalue_ptr = NULL;
-
-    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)
-    {
-        Py_XDECREF(py_return);
-        return NULL;
-    }
-    
-    if (sbvalue_ptr == NULL)
-        return NULL;
-
-    return py_return;
-}
-
-SWIGEXPORT int
-LLDBSwigPython_GetIndexOfChildWithName
-(
-    PyObject *implementor,
-    const char* child_name
-)
-{
-    static char callee_name[] = "get_child_index";
-    static char param_format[] = "s";
-
-    if (implementor == NULL || implementor == Py_None)
-        return 0;
-    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, child_name);
-    if (PyErr_Occurred())
-    {
-        PyErr_Print();
-        PyErr_Clear();
-    }
-    
-    if (py_return == NULL || py_return == Py_None)
-    {
-        Py_XDECREF(py_return);
-        return UINT32_MAX;
-    }
-    long retval = PyInt_AsLong(py_return);
-    Py_XDECREF(py_return);
-    if (retval >= 0)
-        return (uint32_t)retval;
-    if (PyErr_Occurred())
-    {
-        PyErr_Print();
-        PyErr_Clear();
-    }
-    return 0;
-}
-
-SWIGEXPORT bool
-LLDBSwigPython_UpdateSynthProviderInstance
-(
-    PyObject *implementor
-)
-{
-    bool ret_val = false;
-
-    static char callee_name[] = "update";
-
-    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);
-
-    if (py_return == Py_True)
-        ret_val = true;
-
-    Py_XDECREF(py_return);
-    
-    return ret_val;
-}
-
-SWIGEXPORT bool
-LLDBSwigPython_MightHaveChildrenSynthProviderInstance
-(
-    PyObject *implementor
-)
-{
-    bool ret_val = false;
-
-    static char callee_name[] = "has_children";
-
-    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);
-
-    if (py_return == Py_True)
-        ret_val = true;
-
-    Py_XDECREF(py_return);
-    
-    return ret_val;
-}
-
-SWIGEXPORT void*
-LLDBSWIGPython_CastPyObjectToSBValue
-(
-    PyObject* data
-)
-{
-    lldb::SBValue* sb_ptr = NULL;
-    
-    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);
-
-    if (valid_cast == -1)
-        return NULL;
-
-    return sb_ptr;
-}
-
-// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
-// lldb_private::CommandReturnObject. This means that the destructor for the
-// SB object will deallocate its contained CommandReturnObject. Because that
-// object is used as the real return object for Python-based commands, we want
-// it to stay around. Thus, we release the unique pointer before returning from
-// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
-// matter how we exit from the function, we have a releaser object whose
-// destructor does the right thing for us
-class SBCommandReturnObjectReleaser
-{
-public:
-    SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :
-        m_command_return_object_ref (obj)
-    {
-    }
-
-    ~SBCommandReturnObjectReleaser ()
-    {
-        m_command_return_object_ref.Release();
-    }
-private:
-    lldb::SBCommandReturnObject &m_command_return_object_ref;
-};
-
-SWIGEXPORT bool
-LLDBSwigPythonCallCommand 
-(
-    const char *python_function_name,
-    const char *session_dictionary_name,
-    lldb::DebuggerSP& debugger,
-    const char* args,
-    std::string& err_msg,
-    lldb_private::CommandReturnObject& cmd_retobj
-)
-{
-
-    lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);
-    SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);
-    lldb::SBDebugger debugger_sb(debugger);
-
-    bool retval = false;
-
-    PyObject *DebuggerObj_PyObj = SBTypeToSWIGWrapper(debugger_sb);
-    PyObject *CmdRetObj_PyObj = SBTypeToSWIGWrapper(cmd_retobj_sb);
-
-    if (DebuggerObj_PyObj == NULL)
-        return retval;
-        
-    if (CmdRetObj_PyObj == NULL)
-        return retval;
-
-    if (!python_function_name || !session_dictionary_name)
-        return retval;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        if (pfunc != NULL)
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (4);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj);  // This "steals" a reference to DebuggerObj_PyObj
-                PyTuple_SetItem (pargs, 1, PyString_FromString(args));
-                PyTuple_SetItem (pargs, 2, CmdRetObj_PyObj);  // This "steals" a reference to CmdRetObj_PyObj
-                PyTuple_SetItem (pargs, 3, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (pvalue != NULL)
-                {
-                    if (pvalue == Py_None) // no error
-                    {
-                        err_msg.clear();
-                        retval = true;
-                    }
-                    else
-                    {
-                        // return value is an error string
-                        if (PyString_CheckExact(pvalue))
-                            err_msg.assign(PyString_AsString(pvalue));
-                        retval = false;
-                    }
-                    Py_XDECREF (pvalue);
-                }
-                else if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-        else if (PyErr_Occurred())
-        {
-            PyErr_Print();
-            PyErr_Clear();
-        }
-    }
-    else if (PyErr_Occurred ())
-    {
-        PyErr_Print();
-        PyErr_Clear ();
-    }
-    return retval;
-}
-
-SWIGEXPORT void*
-LLDBSWIGPythonCreateOSPlugin
-(
-    const char *python_class_name,
-    const char *session_dictionary_name,
-    const lldb::ProcessSP& process_sp
-)
-{
-    PyObject* retval = NULL;
-
-    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)
-        Py_RETURN_NONE;
-
-    // I do not want the SBProcess to be deallocated when going out of scope because python
-    // has ownership of it and will manage memory for this object by itself
-    lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);
-
-    PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);
-
-    if (SBProc_PyObj == NULL)
-        Py_RETURN_NONE;
-
-    const char* python_function_name = python_class_name;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        if (pfunc != NULL)
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                PyObject *argList = Py_BuildValue("(O)", SBProc_PyObj);
-
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                    return retval;
-                }
-
-                if (argList == NULL)
-                {
-                    return retval;
-                }
-
-                Py_INCREF(SBProc_PyObj);
-
-                pvalue = PyObject_CallObject(pfunc, argList);
-
-                Py_XDECREF(argList);
-
-                if (pvalue != NULL)
-                {
-                    if (pvalue != Py_None)
-                        retval = pvalue;
-                    else
-                    {
-                        retval = Py_None;
-                        Py_INCREF(retval);
-                    }
-                }
-                else if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-        else if (PyErr_Occurred())
-        {
-            PyErr_Print();
-            PyErr_Clear();
-        }
-    }
-    else if (PyErr_Occurred ())
-    {
-        PyErr_Print();
-        PyErr_Clear ();
-    }
-    if (retval)
-        return retval;
-    else
-        Py_RETURN_NONE;
-}
-
-SWIGEXPORT bool
-LLDBSWIGPythonRunScriptKeywordProcess
-(const char* python_function_name,
-const char* session_dictionary_name,
-lldb::ProcessSP& process,
-std::string& output)
-
-{
-    bool retval = false;
-
-    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
-        return retval;
-
-    lldb::SBProcess process_sb(process);
-    PyObject *ProcessObj_PyObj = SBTypeToSWIGWrapper(process_sb);
-
-    if (ProcessObj_PyObj == NULL)
-        return retval;
-        
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-        
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        
-        if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
-        {
-            PyErr_Clear();
-            return true;
-        }
-
-        if (pfunc == NULL)
-            return true;
-        else
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (2);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, ProcessObj_PyObj);  // This "steals" a reference to ProcessObj_PyObj
-                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                else
-                {
-                    if (PyString_Check(pvalue))
-                    {
-                        output.assign(PyString_AsString(pvalue));
-                        retval = true;
-                    }
-                    else
-                    {
-                        output.clear();
-                        retval = false;
-                    }
-                    Py_XDECREF (pvalue);
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-    }
-    return retval;
-}
-
-SWIGEXPORT bool
-LLDBSWIGPythonRunScriptKeywordThread
-(const char* python_function_name,
-const char* session_dictionary_name,
-lldb::ThreadSP& thread,
-std::string& output)
-
-{
-    bool retval = false;
-
-    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
-        return retval;
-
-    lldb::SBThread thread_sb(thread);
-    PyObject *ThreadObj_PyObj = SBTypeToSWIGWrapper(thread_sb);
-
-    if (ThreadObj_PyObj == NULL)
-        return retval;
-        
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-        
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        
-        if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
-        {
-            PyErr_Clear();
-            return true;
-        }
-
-        if (pfunc == NULL)
-            return true;
-        else
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (2);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, ThreadObj_PyObj);  // This "steals" a reference to ThreadObj_PyObj
-                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                else
-                {
-                    if (PyString_Check(pvalue))
-                    {
-                        output.assign(PyString_AsString(pvalue));
-                        retval = true;
-                    }
-                    else
-                    {
-                        output.clear();
-                        retval = false;
-                    }
-                    Py_XDECREF (pvalue);
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-    }
-    return retval;
-}
-
-SWIGEXPORT bool
-LLDBSWIGPythonRunScriptKeywordTarget
-(const char* python_function_name,
-const char* session_dictionary_name,
-lldb::TargetSP& target,
-std::string& output)
-
-{
-    bool retval = false;
-
-    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
-        return retval;
-
-    lldb::SBTarget target_sb(target);
-    PyObject *TargetObj_PyObj = SBTypeToSWIGWrapper(target_sb);
-
-    if (TargetObj_PyObj == NULL)
-        return retval;
-        
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-        
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        
-        if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
-        {
-            PyErr_Clear();
-            return true;
-        }
-
-        if (pfunc == NULL)
-            return true;
-        else
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (2);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, TargetObj_PyObj);  // This "steals" a reference to TargetObj_PyObj
-                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                else
-                {
-                    if (PyString_Check(pvalue))
-                    {
-                        output.assign(PyString_AsString(pvalue));
-                        retval = true;
-                    }
-                    else
-                    {
-                        output.clear();
-                        retval = false;
-                    }
-                    Py_XDECREF (pvalue);
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-    }
-    return retval;
-}
-
-SWIGEXPORT bool
-LLDBSWIGPythonRunScriptKeywordFrame
-(const char* python_function_name,
-const char* session_dictionary_name,
-lldb::StackFrameSP& frame,
-std::string& output)
-
-{
-    bool retval = false;
-
-    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)
-        return retval;
-
-    lldb::SBFrame frame_sb(frame);
-    PyObject *FrameObj_PyObj = SBTypeToSWIGWrapper(frame_sb);
-
-    if (FrameObj_PyObj == NULL)
-        return retval;
-        
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-        
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        
-        if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
-        {
-            PyErr_Clear();
-            return true;
-        }
-
-        if (pfunc == NULL)
-            return true;
-        else
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (2);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, FrameObj_PyObj);  // This "steals" a reference to FrameObj_PyObj
-                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                else
-                {
-                    if (PyString_Check(pvalue))
-                    {
-                        output.assign(PyString_AsString(pvalue));
-                        retval = true;
-                    }
-                    else
-                    {
-                        output.clear();
-                        retval = false;
-                    }
-                    Py_XDECREF (pvalue);
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-    }
-    return retval;
-}
-
-SWIGEXPORT bool
-LLDBSwigPythonCallModuleInit 
-(
-    const char *python_module_name,
-    const char *session_dictionary_name,
-    lldb::DebuggerSP& debugger
-)
-{
-
-    lldb::SBDebugger debugger_sb(debugger);
-
-    bool retval = false;
-
-    PyObject *DebuggerObj_PyObj = SBTypeToSWIGWrapper(debugger_sb);
-
-    if (DebuggerObj_PyObj == NULL)
-        return retval;
-        
-    if (python_module_name == NULL || python_module_name[0] == '\0' || !session_dictionary_name)
-        return retval;
-
-    PyObject *session_dict, *pfunc;
-    PyObject *pargs, *pvalue;
-    
-    session_dict = FindSessionDictionary (session_dictionary_name);
-    
-    std::string python_function_name_string = python_module_name;
-    python_function_name_string += ".__lldb_init_module";
-    const char* python_function_name = python_function_name_string.c_str();
-    
-    if (session_dict != NULL)
-    {
-        pfunc = ResolvePythonName (python_function_name, session_dict);
-        
-        if (PyErr_Occurred()) // this might not exist.. let's make sure we handle that
-        {
-            PyErr_Clear();
-            return true;
-        }
-
-        if (pfunc == NULL)
-            return true;
-        else
-        {
-            // Set up the arguments and call the function.
-                
-            if (PyCallable_Check (pfunc))
-            {
-                pargs = PyTuple_New (2);
-                if (pargs == NULL)
-                {
-                    if (PyErr_Occurred())
-                        PyErr_Clear();
-                    return retval;
-                }
-                
-                PyTuple_SetItem (pargs, 0, DebuggerObj_PyObj);  // This "steals" a reference to DebuggerObj_PyObj
-                PyTuple_SetItem (pargs, 1, session_dict); // This "steals" a reference to session_dict
-                pvalue = PyObject_CallObject (pfunc, pargs);
-                Py_XDECREF (pargs);
-                
-                if (PyErr_Occurred ())
-                {
-                    PyErr_Print();
-                    PyErr_Clear();
-                }
-                else
-                {
-                    retval = true;
-                    Py_XDECREF (pvalue);
-                }
-                Py_INCREF (session_dict);
-            }
-            else if (PyErr_Occurred())
-            {
-                PyErr_Print();
-                PyErr_Clear();
-            }
-        }
-    }
-    return retval;
-}
-%}
-
-
-%runtime %{
-// Forward declaration to be inserted at the start of LLDBWrapPython.h
-// I used runtime as a hack to make SWIG place it where it's needed.
-// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the
-// typemaps and in the extensions (SBInputReader.__del__()).
-#include "lldb/API/SBInputReader.h"
-#include "lldb/API/SBDebugger.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-size_t
-LLDBSwigPythonCallSBInputReaderCallback(void *baton,
-                                        lldb::SBInputReader *reader,
-                                        lldb::InputReaderAction notification,
-                                        const char*bytes,
-                                        size_t bytes_len);
-
-void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);
-
-#ifdef __cplusplus
-}
-#endif
-%}
-
-%wrapper %{
-// For the InputReader Callback functions
-SWIGEXPORT size_t
-LLDBSwigPythonCallSBInputReaderCallback(void *baton,
-                                        lldb::SBInputReader *reader,
-                                        lldb::InputReaderAction notification,
-                                        const char*bytes,
-                                        size_t bytes_len) {
-    if (baton != Py_None) {
-        SWIG_PYTHON_THREAD_BEGIN_BLOCK;
-    
-        PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);
-        PyObject *py_Notification = PyInt_FromLong(notification);
-        PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);
-    
-        PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);
-        PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);
-        Py_XDECREF(tuple);
-        Py_XDECREF(py_InputReader);
-        Py_XDECREF(py_Notification);
-        Py_XDECREF(py_Bytes);
-    
-        if (res == NULL) {
-          PyObject *exc = PyErr_Occurred();
-          if (exc) {
-            ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");
-            PyErr_Print();
-          }
-          return 0;
-        }
-    
-        size_t result = 0;
-        // If the callback misbehaves and returns Py_None, assume it returned 0
-        if (res != Py_None)
-          result = static_cast<size_t>(PyInt_AsSsize_t(res));
-    
-        Py_XDECREF(res);
-        SWIG_PYTHON_THREAD_END_BLOCK;
-        return result;
-    }
-    return 0;
-}
-
-// For the LogOutputCallback functions
-void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {
-    if (baton != Py_None) {
-      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
-      PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);
-      SWIG_PYTHON_THREAD_END_BLOCK;
-    }
-}
-%}
+%header %{

+

+template <typename T>

+PyObject *

+SBTypeToSWIGWrapper (T* item);

+

+class PyErr_Cleaner

+{

+public:

+    PyErr_Cleaner(bool print=false) :

+    m_print(print)

+    {

+    }

+    

+    ~PyErr_Cleaner()

+    {

+        if (PyErr_Occurred())

+        {

+            if(m_print)

+                PyErr_Print();

+            PyErr_Clear();

+        }

+    }

+    

+private:

+    bool m_print;

+};

+

+static PyObject*

+ResolvePythonName(const char* name,

+                  PyObject* pmodule)

+{

+    if (!name)

+        return pmodule;

+

+    PyErr_Cleaner pyerr_cleanup(true);  // show Python errors

+

+    PyObject* main_dict;

+

+    if (!pmodule)

+    {

+        pmodule = PyImport_AddModule ("__main__");

+        if (!pmodule)

+            return NULL;

+    }

+

+    if (PyType_Check(pmodule))

+    {

+        main_dict = ((PyTypeObject*)pmodule)->tp_dict;

+        if (!main_dict)

+            return NULL;

+    }

+    else if (!PyDict_Check(pmodule))

+    {

+        main_dict = PyModule_GetDict (pmodule);

+        if (!main_dict)

+            return NULL;

+    }

+    else

+        main_dict = pmodule;

+

+    const char* dot_pos = ::strchr(name, '.');

+

+    PyObject *dest_object;

+    PyObject *key, *value;

+    Py_ssize_t pos = 0;

+

+    if (!dot_pos)

+    {

+        dest_object = NULL;   

+        while (PyDict_Next (main_dict, &pos, &key, &value))

+        {

+            // We have stolen references to the key and value objects in the dictionary; we need to increment 

+            // them now so that Python's garbage collector doesn't collect them out from under us.

+            Py_INCREF (key);

+            Py_INCREF (value);

+            if (strcmp (PyString_AsString (key), name) == 0)

+            {

+                dest_object = value;

+                break;

+            }

+        }        

+        if (!dest_object || dest_object == Py_None)

+            return NULL;

+        return dest_object;

+    }

+    else

+    {

+        size_t len = dot_pos - name;

+        std::string piece(name,len);

+        pmodule = ResolvePythonName(piece.c_str(), main_dict);

+        if (!pmodule)

+            return NULL;

+        name = dot_pos+1;

+        return ResolvePythonName(dot_pos+1,pmodule); // tail recursion.. should be optimized by the compiler

+    }

+}

+

+static PyObject*

+FindSessionDictionary(const char *session_dictionary_name)

+{

+    return ResolvePythonName(session_dictionary_name, NULL);

+}

+

+class PyCallable

+{

+public:

+    

+    operator

+    bool ()

+    {

+        return m_callable != NULL;

+    }

+    

+    template<typename ...Args>

+    PyObject*

+    operator () (Args... args)

+    {

+        return (*this)({SBTypeToSWIGWrapper(args)...});

+    }

+    

+    PyObject*

+    operator () (std::initializer_list<PyObject*> args)

+    {

+        PyObject* retval = NULL;

+        PyObject* pargs = PyTuple_New (args.size());

+        if (pargs == NULL)

+        {

+            if (PyErr_Occurred())

+                PyErr_Clear();

+            return retval;

+        }

+        size_t idx = 0;

+        for (auto arg : args)

+        {

+            if (!arg)

+                return retval;

+            PyTuple_SetItem(pargs,idx,arg);

+            idx++;

+        }

+        retval = PyObject_CallObject (m_callable, pargs);

+        Py_XDECREF (pargs);

+        return retval;

+    }

+    

+    static PyCallable

+    FindWithPythonObject (PyObject* pfunc)

+    {

+        return PyCallable(pfunc);

+    }

+    

+    static PyCallable

+    FindWithFunctionName (const char *python_function_name,

+                          const char *session_dictionary_name)

+    {

+        if (!python_function_name || !session_dictionary_name)

+            return PyCallable();

+        if ( (python_function_name[0] == 0) || (session_dictionary_name[0] == 0) )

+            return PyCallable();

+        return FindWithFunctionName(python_function_name,FindSessionDictionary (session_dictionary_name));

+    }

+    

+    static PyCallable

+    FindWithFunctionName (const char *python_function_name,

+                          PyObject *session_dict)

+    {

+        if (!python_function_name || !session_dict)

+            return PyCallable();

+        if ( (python_function_name[0] == 0))

+            return PyCallable();

+        return PyCallable(ResolvePythonName (python_function_name, session_dict));

+    }

+    

+    static PyCallable

+    FindWithMemberFunction (PyObject *self,

+                            const char *python_function_name)

+    {

+        if (self == NULL || self == Py_None)

+            return PyCallable();

+        if (!python_function_name || (python_function_name[0] == 0))

+            return PyCallable();

+        return PyCallable(PyObject_GetAttrString(self, python_function_name));

+    }

+    

+private:

+    PyObject* m_callable;

+    

+    PyCallable (PyObject *callable = NULL) :

+    m_callable(callable)

+    {

+        if (m_callable && PyCallable_Check(m_callable) == false)

+            m_callable = NULL;

+    }

+};

+

+%}

+

+%wrapper %{

+

+// resolve a dotted Python name in the form

+// foo.bar.baz.Foobar to an actual Python object

+// if pmodule is NULL, the __main__ module will be used

+// as the starting point for the search

+

+

+// This function is called by lldb_private::ScriptInterpreterPython::BreakpointCallbackFunction(...)

+// and is used when a script command is attached to a breakpoint for execution.

+

+SWIGEXPORT bool

+LLDBSwigPythonBreakpointCallbackFunction 

+(

+    const char *python_function_name,

+    const char *session_dictionary_name,

+    const lldb::StackFrameSP& frame_sp, 

+    const lldb::BreakpointLocationSP& bp_loc_sp

+)

+{

+    lldb::SBFrame sb_frame (frame_sp);

+    lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);

+    

+    bool stop_at_breakpoint = true;

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return stop_at_breakpoint;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(sb_frame, sb_bp_loc, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (pvalue == Py_False)

+            stop_at_breakpoint = false;

+        

+        Py_XDECREF (pvalue);

+    }

+    

+    return stop_at_breakpoint;

+}

+

+// This function is called by lldb_private::ScriptInterpreterPython::WatchpointCallbackFunction(...)

+// and is used when a script command is attached to a watchpoint for execution.

+

+SWIGEXPORT bool

+LLDBSwigPythonWatchpointCallbackFunction 

+(

+    const char *python_function_name,

+    const char *session_dictionary_name,

+    const lldb::StackFrameSP& frame_sp, 

+    const lldb::WatchpointSP& wp_sp

+)

+{

+    lldb::SBFrame sb_frame (frame_sp);

+    lldb::SBWatchpoint sb_wp(wp_sp);

+

+    bool stop_at_watchpoint = true;

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return stop_at_watchpoint;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(sb_frame, sb_wp, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (pvalue == Py_False)

+            stop_at_watchpoint = false;

+        

+        Py_XDECREF (pvalue);

+    }

+    

+    return stop_at_watchpoint;

+}

+

+bool

+PyObjectToString (PyObject* object,

+                  std::string& retval)

+{

+    retval.clear();

+    bool was_ok = false;

+    if (object != NULL && object != Py_None)

+    {

+        if (PyString_Check(object))

+        {

+            retval.assign(PyString_AsString(object));

+            was_ok = true;

+        }

+        else

+        {

+            PyObject* value_as_string = PyObject_Str(object);

+            if (value_as_string && value_as_string != Py_None && PyString_Check(value_as_string))

+            {

+                retval.assign(PyString_AsString(value_as_string));

+                was_ok = true;

+            }

+            Py_XDECREF(value_as_string);

+        }

+    }

+    return was_ok;

+}

+

+SWIGEXPORT bool

+LLDBSwigPythonCallTypeScript 

+(

+    const char *python_function_name,

+    const void *session_dictionary,

+    const lldb::ValueObjectSP& valobj_sp,

+    void** pyfunct_wrapper,

+    std::string& retval

+)

+{

+    lldb::SBValue sb_value (valobj_sp);

+

+    retval.clear();

+

+    if (!python_function_name || !session_dictionary)

+        return false;

+

+    PyObject *session_dict = (PyObject*)session_dictionary, *pfunc_impl = NULL, *pvalue = NULL;

+    

+    if (pyfunct_wrapper && *pyfunct_wrapper && PyFunction_Check (*pyfunct_wrapper))

+    {

+        pfunc_impl = (PyObject*)(*pyfunct_wrapper);

+        if (pfunc_impl->ob_refcnt == 1)

+        {

+            Py_XDECREF(pfunc_impl);

+            pfunc_impl = NULL;

+        }

+    }

+

+    if (PyDict_Check(session_dict))

+    {

+        PyErr_Cleaner pyerr_cleanup(true);  // show Python errors

+        

+        if (!pfunc_impl)

+        {

+            pfunc_impl = ResolvePythonName (python_function_name, session_dict);

+            if (!pfunc_impl || !PyCallable_Check (pfunc_impl))

+                return false;

+            else

+            {

+                if (pyfunct_wrapper)

+                    *pyfunct_wrapper = pfunc_impl;

+            }

+        }

+        /*else

+            printf("caching works!!!!\n");*/

+        

+        PyCallable pfunc = PyCallable::FindWithPythonObject(pfunc_impl);

+        

+        if (!pfunc)

+            return false;

+        

+        pvalue = pfunc(sb_value,session_dict);

+

+        Py_INCREF (session_dict);

+        

+        PyObjectToString(pvalue,retval);

+        

+        Py_XDECREF (pvalue);

+    }

+    return true;

+}

+

+SWIGEXPORT void*

+LLDBSwigPythonCreateSyntheticProvider 

+(

+    const char *python_class_name,

+    const char *session_dictionary_name,

+    const lldb::ValueObjectSP& valobj_sp

+)

+{

+    PyObject* retval = NULL;

+

+    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)

+        Py_RETURN_NONE;

+

+    // I do not want the SBValue to be deallocated when going out of scope because python

+    // has ownership of it and will manage memory for this object by itself

+    lldb::SBValue *sb_value = new lldb::SBValue(valobj_sp);

+    sb_value->SetPreferSyntheticValue(false);

+    PyObject *ValObj_PyObj = SBTypeToSWIGWrapper(sb_value);

+

+    if (ValObj_PyObj == NULL)

+        Py_RETURN_NONE;

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        Py_INCREF(ValObj_PyObj);

+        

+        PyObject* session_dict = NULL;

+        session_dict = FindSessionDictionary(session_dictionary_name);

+        retval = pfunc(sb_value, session_dict);

+        

+        Py_XINCREF (session_dict);

+        

+        Py_XINCREF(retval);

+    }

+    

+    if (retval)

+        return retval;

+    else

+        Py_RETURN_NONE;

+}

+

+// wrapper that calls an optional instance member of an object taking no arguments

+static PyObject*

+LLDBSwigPython_CallOptionalMember

+(

+    PyObject* self,

+    char* callee_name,

+    PyObject* ret_if_not_found = Py_None,

+    bool* was_found = NULL

+)

+{

+    PyErr_Cleaner py_err_cleaner(false);

+    

+    PyCallable pfunc = PyCallable::FindWithMemberFunction(self,callee_name);

+    

+    if (!pfunc)

+    {

+        if (was_found)

+            *was_found = false;

+        Py_XINCREF(ret_if_not_found);

+        return ret_if_not_found;

+    }

+    

+    if (was_found)

+        *was_found = true;

+    

+    PyObject* py_return = pfunc();

+    return py_return;

+}

+

+SWIGEXPORT uint32_t

+LLDBSwigPython_CalculateNumChildren

+(

+    PyObject *implementor

+)

+{

+    uint32_t ret_val = UINT32_MAX;

+

+    static char callee_name[] = "num_children";

+

+    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, NULL);

+

+    if (!py_return)

+        return ret_val;

+

+    if (PyInt_Check(py_return))

+        ret_val = PyInt_AsLong(py_return);

+

+    Py_XDECREF(py_return);

+    

+    if (PyErr_Occurred())

+    {

+        PyErr_Print();

+        PyErr_Clear();

+    }

+    

+    return ret_val;

+}

+

+SWIGEXPORT PyObject*

+LLDBSwigPython_GetChildAtIndex

+(

+    PyObject *implementor,

+    uint32_t idx

+)

+{

+    PyErr_Cleaner py_err_cleaner(true);

+    

+    PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_at_index");

+    

+    if (!pfunc)

+        return NULL;

+    

+    PyObject *py_return = NULL;

+    py_return = pfunc(idx);

+    

+    if (py_return == NULL || py_return == Py_None)

+    {

+        Py_XDECREF(py_return);

+        return NULL;

+    }

+    

+    lldb::SBValue* sbvalue_ptr = NULL;

+    

+    if (SWIG_ConvertPtr(py_return, (void**)&sbvalue_ptr, SWIGTYPE_p_lldb__SBValue, 0) == -1)

+    {

+        Py_XDECREF(py_return);

+        return NULL;

+    }

+    

+    if (sbvalue_ptr == NULL)

+        return NULL;

+    

+    return py_return;    

+}

+

+SWIGEXPORT int

+LLDBSwigPython_GetIndexOfChildWithName

+(

+    PyObject *implementor,

+    const char* child_name

+)

+{

+    PyErr_Cleaner py_err_cleaner(true);

+    

+    PyCallable pfunc = PyCallable::FindWithMemberFunction(implementor,"get_child_index");

+    

+    if (!pfunc)

+        return UINT32_MAX;

+    

+    PyObject *py_return = NULL;

+    py_return = pfunc(child_name);

+    

+    if (py_return == NULL || py_return == Py_None)

+    {

+        Py_XDECREF(py_return);

+        return UINT32_MAX;

+    }

+    

+    long retval = PyInt_AsLong(py_return);

+    Py_XDECREF(py_return);

+

+    if (retval >= 0)

+        return (uint32_t)retval;

+    

+    return UINT32_MAX;

+}

+

+SWIGEXPORT bool

+LLDBSwigPython_UpdateSynthProviderInstance

+(

+    PyObject *implementor

+)

+{

+    bool ret_val = false;

+

+    static char callee_name[] = "update";

+

+    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name);

+

+    if (py_return == Py_True)

+        ret_val = true;

+

+    Py_XDECREF(py_return);

+    

+    return ret_val;

+}

+

+SWIGEXPORT bool

+LLDBSwigPython_MightHaveChildrenSynthProviderInstance

+(

+    PyObject *implementor

+)

+{

+    bool ret_val = false;

+

+    static char callee_name[] = "has_children";

+

+    PyObject* py_return = LLDBSwigPython_CallOptionalMember(implementor,callee_name, Py_True);

+

+    if (py_return == Py_True)

+        ret_val = true;

+

+    Py_XDECREF(py_return);

+    

+    return ret_val;

+}

+

+SWIGEXPORT void*

+LLDBSWIGPython_CastPyObjectToSBValue

+(

+    PyObject* data

+)

+{

+    lldb::SBValue* sb_ptr = NULL;

+    

+    int valid_cast = SWIG_ConvertPtr(data, (void**)&sb_ptr, SWIGTYPE_p_lldb__SBValue, 0);

+

+    if (valid_cast == -1)

+        return NULL;

+

+    return sb_ptr;

+}

+

+// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an

+// lldb_private::CommandReturnObject. This means that the destructor for the

+// SB object will deallocate its contained CommandReturnObject. Because that

+// object is used as the real return object for Python-based commands, we want

+// it to stay around. Thus, we release the unique pointer before returning from

+// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no

+// matter how we exit from the function, we have a releaser object whose

+// destructor does the right thing for us

+class SBCommandReturnObjectReleaser

+{

+public:

+    SBCommandReturnObjectReleaser (lldb::SBCommandReturnObject &obj) :

+        m_command_return_object_ref (obj)

+    {

+    }

+

+    ~SBCommandReturnObjectReleaser ()

+    {

+        m_command_return_object_ref.Release();

+    }

+private:

+    lldb::SBCommandReturnObject &m_command_return_object_ref;

+};

+

+SWIGEXPORT bool

+LLDBSwigPythonCallCommand 

+(

+    const char *python_function_name,

+    const char *session_dictionary_name,

+    lldb::DebuggerSP& debugger,

+    const char* args,

+    lldb_private::CommandReturnObject& cmd_retobj

+)

+{

+

+    lldb::SBCommandReturnObject cmd_retobj_sb(&cmd_retobj);

+    SBCommandReturnObjectReleaser cmd_retobj_sb_releaser(cmd_retobj_sb);

+    lldb::SBDebugger debugger_sb(debugger);

+

+    bool retval = false;

+

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+

+        PyObject* session_dict = NULL;

+        // pass the pointer-to cmd_retobj_sb or watch the underlying object disappear from under you

+        // see comment above for SBCommandReturnObjectReleaser for further details

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(debugger_sb, args, &cmd_retobj_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        Py_XDECREF (pvalue);

+        

+        retval = true;

+    }

+    

+    return retval;

+}

+

+SWIGEXPORT void*

+LLDBSWIGPythonCreateOSPlugin

+(

+    const char *python_class_name,

+    const char *session_dictionary_name,

+    const lldb::ProcessSP& process_sp

+)

+{

+    PyObject* retval = NULL;

+

+    if (python_class_name == NULL || python_class_name[0] == '\0' || !session_dictionary_name)

+        Py_RETURN_NONE;

+

+    // I do not want the SBProcess to be deallocated when going out of scope because python

+    // has ownership of it and will manage memory for this object by itself

+    lldb::SBProcess *process_sb = new lldb::SBProcess(process_sp);

+

+    PyObject *SBProc_PyObj = SBTypeToSWIGWrapper(process_sb);

+

+    if (SBProc_PyObj == NULL)

+        Py_RETURN_NONE;

+

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_class_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        Py_INCREF(SBProc_PyObj);

+        

+        PyObject* session_dict = NULL;

+        session_dict = session_dict = FindSessionDictionary(session_dictionary_name);

+        retval = pfunc(SBProc_PyObj);

+        

+        Py_XINCREF (session_dict);

+        

+        Py_XINCREF(retval);

+    }

+    

+    if (retval)

+        return retval;

+    else

+        Py_RETURN_NONE;

+}

+

+SWIGEXPORT bool

+LLDBSWIGPythonRunScriptKeywordProcess

+(const char* python_function_name,

+const char* session_dictionary_name,

+lldb::ProcessSP& process,

+std::string& output)

+

+{

+    bool retval = false;

+

+    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)

+        return retval;

+

+    lldb::SBProcess process_sb(process);

+

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(process_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (PyObjectToString(pvalue,output))

+            retval = true;

+        

+        Py_XDECREF(pvalue);

+    }

+    

+    return retval;

+}

+

+SWIGEXPORT bool

+LLDBSWIGPythonRunScriptKeywordThread

+(const char* python_function_name,

+const char* session_dictionary_name,

+lldb::ThreadSP& thread,

+std::string& output)

+

+{

+    bool retval = false;

+

+    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)

+        return retval;

+

+    lldb::SBThread thread_sb(thread);

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(thread_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (PyObjectToString(pvalue,output))

+            retval = true;

+        

+        Py_XDECREF(pvalue);

+    }

+    

+    return retval;

+}

+

+SWIGEXPORT bool

+LLDBSWIGPythonRunScriptKeywordTarget

+(const char* python_function_name,

+const char* session_dictionary_name,

+lldb::TargetSP& target,

+std::string& output)

+

+{

+    bool retval = false;

+

+    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)

+        return retval;

+

+    lldb::SBTarget target_sb(target);

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(target_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (PyObjectToString(pvalue,output))

+            retval = true;

+        

+        Py_XDECREF(pvalue);

+    }

+    

+    return retval;

+}

+

+SWIGEXPORT bool

+LLDBSWIGPythonRunScriptKeywordFrame

+(const char* python_function_name,

+const char* session_dictionary_name,

+lldb::StackFrameSP& frame,

+std::string& output)

+

+{

+    bool retval = false;

+

+    if (python_function_name == NULL || python_function_name[0] == '\0' || !session_dictionary_name)

+        return retval;

+

+    lldb::SBFrame frame_sb(frame);

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return retval;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(frame_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        if (PyObjectToString(pvalue,output))

+            retval = true;

+        

+        Py_XDECREF(pvalue);

+    }

+    

+    return retval;

+}

+

+SWIGEXPORT bool

+LLDBSwigPythonCallModuleInit 

+(

+    const char *python_module_name,

+    const char *session_dictionary_name,

+    lldb::DebuggerSP& debugger

+)

+{

+    bool retval = false;

+

+    lldb::SBDebugger debugger_sb(debugger);

+

+    std::string python_function_name_string = python_module_name;

+    python_function_name_string += ".__lldb_init_module";

+    const char* python_function_name = python_function_name_string.c_str();

+    

+    {

+        PyErr_Cleaner py_err_cleaner(true);

+        

+        PyCallable pfunc = PyCallable::FindWithFunctionName(python_function_name,session_dictionary_name);

+        

+        if (!pfunc)

+            return true;

+        

+        PyObject* session_dict = NULL;

+        PyObject* pvalue = NULL;

+        pvalue = pfunc(debugger_sb, session_dict = FindSessionDictionary(session_dictionary_name));

+        

+        Py_XINCREF (session_dict);

+        

+        retval = true;

+        

+        Py_XDECREF(pvalue);

+    }

+    

+    return retval;

+}

+%}

+

+

+%runtime %{

+// Forward declaration to be inserted at the start of LLDBWrapPython.h

+// I used runtime as a hack to make SWIG place it where it's needed.

+// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the

+// typemaps and in the extensions (SBInputReader.__del__()).

+#include "lldb/API/SBInputReader.h"

+#include "lldb/API/SBDebugger.h"

+

+#ifdef __cplusplus

+extern "C" {

+#endif

+

+size_t

+LLDBSwigPythonCallSBInputReaderCallback(void *baton,

+                                        lldb::SBInputReader *reader,

+                                        lldb::InputReaderAction notification,

+                                        const char*bytes,

+                                        size_t bytes_len);

+

+void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton);

+

+#ifdef __cplusplus

+}

+#endif

+%}

+

+%wrapper %{

+// For the InputReader Callback functions

+SWIGEXPORT size_t

+LLDBSwigPythonCallSBInputReaderCallback(void *baton,

+                                        lldb::SBInputReader *reader,

+                                        lldb::InputReaderAction notification,

+                                        const char*bytes,

+                                        size_t bytes_len) {

+    if (baton != Py_None) {

+        SWIG_PYTHON_THREAD_BEGIN_BLOCK;

+    

+        PyObject *py_InputReader = SBTypeToSWIGWrapper(reader);

+        PyObject *py_Notification = PyInt_FromLong(notification);

+        PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len);

+    

+        PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes);

+        PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL);

+        Py_XDECREF(tuple);

+        Py_XDECREF(py_InputReader);

+        Py_XDECREF(py_Notification);

+        Py_XDECREF(py_Bytes);

+    

+        if (res == NULL) {

+          PyObject *exc = PyErr_Occurred();

+          if (exc) {

+            ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback");

+            PyErr_Print();

+          }

+          return 0;

+        }

+    

+        size_t result = 0;

+        // If the callback misbehaves and returns Py_None, assume it returned 0

+        if (res != Py_None)

+          result = static_cast<size_t>(PyInt_AsSsize_t(res));

+    

+        Py_XDECREF(res);

+        SWIG_PYTHON_THREAD_END_BLOCK;

+        return result;

+    }

+    return 0;

+}

+

+// For the LogOutputCallback functions

+void LLDBSwigPythonCallPythonLogOutputCallback(const char *str, void *baton) {

+    if (baton != Py_None) {

+      SWIG_PYTHON_THREAD_BEGIN_BLOCK;

+      PyObject_CallFunction(reinterpret_cast<PyObject*>(baton), const_cast<char*>("s"), str);

+      SWIG_PYTHON_THREAD_END_BLOCK;

+    }

+}

+%}

diff --git a/source/API/SBCommandReturnObject.cpp b/source/API/SBCommandReturnObject.cpp
index b0533e1..83d6563 100644
--- a/source/API/SBCommandReturnObject.cpp
+++ b/source/API/SBCommandReturnObject.cpp
@@ -8,8 +8,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/API/SBCommandReturnObject.h"
+#include "lldb/API/SBError.h"
 #include "lldb/API/SBStream.h"
 
+#include "lldb/Core/Error.h"
 #include "lldb/Core/Log.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
 
@@ -329,3 +331,22 @@
     return 0;
 }
 
+void
+SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
+{
+    if (m_opaque_ap.get())
+    {
+        if (error.IsValid())
+            m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
+        else if (fallback_error_cstr)
+            m_opaque_ap->SetError(Error(), fallback_error_cstr);
+    }
+}
+
+void
+SBCommandReturnObject::SetError (const char *error_cstr)
+{
+    if (m_opaque_ap.get() && error_cstr)
+        m_opaque_ap->SetError(error_cstr);
+}
+
diff --git a/source/Interpreter/CommandReturnObject.cpp b/source/Interpreter/CommandReturnObject.cpp
index 2b93a54..9c63753 100644
--- a/source/Interpreter/CommandReturnObject.cpp
+++ b/source/Interpreter/CommandReturnObject.cpp
@@ -143,9 +143,19 @@
     const char *error_cstr = error.AsCString();
     if (error_cstr == NULL)
         error_cstr = fallback_error_cstr;
-    AppendError (error_cstr);
-    SetStatus (eReturnStatusFailed);
+    SetError(error_cstr);
 }
+
+void
+CommandReturnObject::SetError (const char *error_cstr)
+{
+    if (error_cstr)
+    {
+        AppendError (error_cstr);
+        SetStatus (eReturnStatusFailed);
+    }
+}
+
 // Similar to AppendError, but do not prepend 'Error: ' to message, and
 // don't append "\n" to the end of it.
 
diff --git a/source/Interpreter/ScriptInterpreterPython.cpp b/source/Interpreter/ScriptInterpreterPython.cpp
index e42ca68..6841497 100644
--- a/source/Interpreter/ScriptInterpreterPython.cpp
+++ b/source/Interpreter/ScriptInterpreterPython.cpp
@@ -115,8 +115,7 @@
                            const char *session_dictionary_name,
                            lldb::DebuggerSP& debugger,
                            const char* args,
-                           std::string& err_msg,
-                           lldb_private::CommandReturnObject& cmd_retobj);
+                           lldb_private::CommandReturnObject &cmd_retobj);
 
 extern "C" bool
 LLDBSwigPythonCallModuleInit (const char *python_module_name,
@@ -2963,25 +2962,25 @@
     }
     
     lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
-
+    
     if (!debugger_sp.get())
     {
         error.SetErrorString("invalid Debugger pointer");
         return false;
     }
     
-    bool ret_val;
+    bool ret_val = false;
     
     std::string err_msg;
-
+    
     {
         Locker py_lock(this,
                        Locker::AcquireLock | Locker::InitSession,
                        Locker::FreeLock    | Locker::TearDownSession);
-
+        
         SynchronicityHandler synch_handler(debugger_sp,
                                            synchronicity);
-
+        
         // we need to save the thread state when we first start the command
         // because we might decide to interrupt it while some action is taking
         // place outside of Python (e.g. printing to screen, waiting for the network, ...)
@@ -2995,12 +2994,11 @@
                                              m_dictionary_name.c_str(),
                                              debugger_sp,
                                              args,
-                                             err_msg,
                                              cmd_retobj);
     }
-
+    
     if (!ret_val)
-        error.SetErrorString(err_msg.c_str());
+        error.SetErrorString("unable to execute script function");
     else
         error.Clear();
     
diff --git a/test/functionalities/command_script/welcome.py b/test/functionalities/command_script/welcome.py
index b7e8072..b634099 100644
--- a/test/functionalities/command_script/welcome.py
+++ b/test/functionalities/command_script/welcome.py
@@ -13,9 +13,7 @@
     file = target.GetExecutable()
     print >>result,  ('Current target ' + file.GetFilename())
     if args == 'fail':
-        return 'a test for error in command'
-    else:
-        return None
+        result.SetError('a test for error in command')
 
 def print_wait_impl(debugger, args, result, dict):
     result.SetImmediateOutputFile(sys.stdout)
@@ -25,11 +23,10 @@
     print >>result,  ('Still doing long task..')
     time.sleep(1)
     print >>result,  ('Done; if you saw the delays I am doing OK')
-    return None
 
 def check_for_synchro(debugger, args, result, dict):
     if debugger.GetAsync() == True:
         print >>result,  ('I am running async')
     if debugger.GetAsync() == False:
         print >>result,  ('I am running sync')
-    return None
+