Author: enrico
Date: Thu Jun 23 21:07:15 2016
New Revision: 273640

URL: http://llvm.org/viewvc/llvm-project?rev=273640&view=rev
Log:
Fix an issue where the @lldb.command marker would not work with the new 
5-argument version of the Python command function

This:
a) teaches PythonCallable to look inside a callable object
b) teaches PythonCallable to discover whether a callable method is bound
c) teaches lldb.command to dispatch to either the older 4 argument version or 
the newer 5 argument version


Modified:
    lldb/trunk/scripts/Python/python-extensions.swig
    lldb/trunk/scripts/Python/python-wrapper.swig
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: lldb/trunk/scripts/Python/python-extensions.swig
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-extensions.swig?rev=273640&r1=273639&r2=273640&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-extensions.swig (original)
+++ lldb/trunk/scripts/Python/python-extensions.swig Thu Jun 23 21:07:15 2016
@@ -810,6 +810,7 @@
 
 def command(*args, **kwargs):
     import lldb
+    import inspect
     """A decorator function that registers an LLDB command line
         command that is bound to the function it is attached to."""
     class obj(object):
@@ -821,11 +822,15 @@ def command(*args, **kwargs):
             command = "command script add -f %s.%s %s" % (function.__module__, 
function.__name__, command_name)
             lldb.debugger.HandleCommand(command)
             self.function = function
-        def __call__(self, *args, **kwargs):
-            self.function(*args, **kwargs)
+        def __call__(self, debugger, command, exe_ctx, result, dict):
+            if len(inspect.getargspec(self.function).args) == 5:
+                self.function(debugger, command, exe_ctx, result, dict)
+            else:
+                self.function(debugger, command, result, dict)
     def callable(function):
         """Creates a callable object that gets used."""
-        return obj(function, *args, **kwargs)
+        f = obj(function, *args, **kwargs)
+        return f.__call__
     return callable
 
 class declaration(object):
@@ -1129,4 +1134,4 @@ def is_numeric_type(basic_type):
     #if basic_type == eBasicTypeOther:
     return (False,False)
 
-%}
\ No newline at end of file
+%}

Modified: lldb/trunk/scripts/Python/python-wrapper.swig
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-wrapper.swig?rev=273640&r1=273639&r2=273640&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-wrapper.swig (original)
+++ lldb/trunk/scripts/Python/python-wrapper.swig Thu Jun 23 21:07:15 2016
@@ -610,7 +610,7 @@ LLDBSwigPythonCallCommand
     PythonObject exe_ctx_arg(PyRefType::Owned, 
SBTypeToSWIGWrapper(exe_ctx_sb));
     PythonObject cmd_retobj_arg(PyRefType::Owned, 
SBTypeToSWIGWrapper(&cmd_retobj_sb));
 
-    if (argc.count == 5 || argc.has_varargs)
+    if (argc.count == 5 || argc.is_bound_method || argc.has_varargs)
         pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg, 
dict);
     else
         pfunc(debugger_arg, PythonString(args), cmd_retobj_arg, dict);

Modified: 
lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=273640&r1=273639&r2=273640&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
Thu Jun 23 21:07:15 2016
@@ -1109,13 +1109,37 @@ PythonCallable::Reset(PyRefType type, Py
 PythonCallable::ArgInfo
 PythonCallable::GetNumArguments() const
 {
-    ArgInfo result = { 0, false, false };
+    ArgInfo result = { 0, false, false, false };
     if (!IsValid())
         return result;
 
     PyObject *py_func_obj = m_py_obj;
     if (PyMethod_Check(py_func_obj))
+    {
         py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
+        PythonObject im_self = GetAttributeValue("im_self");
+        if (im_self.IsValid() && !im_self.IsNone())
+            result.is_bound_method = true;
+    }
+    else
+    {
+        // see if this is a callable object with an __call__ method
+        if (!PyFunction_Check(py_func_obj))
+        {
+            PythonObject __call__ = GetAttributeValue("__call__");
+            if (__call__.IsValid())
+            {
+                auto __callable__ = __call__.AsType<PythonCallable>();
+                if (__callable__.IsValid())
+                {
+                    py_func_obj = PyMethod_GET_FUNCTION(__callable__.get());
+                    PythonObject im_self = GetAttributeValue("im_self");
+                    if (im_self.IsValid() && !im_self.IsNone())
+                        result.is_bound_method = true;
+                }
+            }
+        }
+    }
 
     if (!py_func_obj)
         return result;

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=273640&r1=273639&r2=273640&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h 
(original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Thu 
Jun 23 21:07:15 2016
@@ -502,6 +502,7 @@ class PythonCallable : public PythonObje
 public:
     struct ArgInfo {
         size_t count;
+        bool is_bound_method : 1;
         bool has_varargs : 1;
         bool has_kwargs : 1;
     };


_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to