diff options
author | Enrico Granata <egranata@apple.com> | 2015-03-13 22:22:28 +0000 |
---|---|---|
committer | Enrico Granata <egranata@apple.com> | 2015-03-13 22:22:28 +0000 |
commit | 6f79bb2d57218e2bd11bf26876d49a1276328bd3 (patch) | |
tree | 8e1121c0e0e91538662d0d8b34ac329053ebc14c /lldb/source/Interpreter/ScriptInterpreterPython.cpp | |
parent | e7ce9ec398103c59b18993ef637fd1aab32c0710 (diff) | |
download | bcm5719-llvm-6f79bb2d57218e2bd11bf26876d49a1276328bd3.tar.gz bcm5719-llvm-6f79bb2d57218e2bd11bf26876d49a1276328bd3.zip |
Add support for Python object commands to return custom short and long help by implementing
def get_short_help(self)
def get_long_help(self)
methods on the command object
Also, add a test case for this feature
llvm-svn: 232224
Diffstat (limited to 'lldb/source/Interpreter/ScriptInterpreterPython.cpp')
-rw-r--r-- | lldb/source/Interpreter/ScriptInterpreterPython.cpp | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp index aab837cc42a..4ef0bbefd54 100644 --- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp +++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp @@ -2793,6 +2793,156 @@ ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& } } +bool +ScriptInterpreterPython::GetShortHelpForCommandObject (lldb::ScriptInterpreterObjectSP cmd_obj_sp, + std::string& dest) +{ + bool got_string = false; + dest.clear(); + + Locker py_lock (this, + Locker::AcquireLock | Locker::NoSTDIN, + Locker::FreeLock); + + static char callee_name[] = "get_short_help"; + + if (!cmd_obj_sp) + return false; + + PyObject* implementor = (PyObject*)cmd_obj_sp->GetObject(); + + if (implementor == nullptr || implementor == Py_None) + return false; + + PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); + + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + if (pmeth == nullptr || pmeth == Py_None) + { + Py_XDECREF(pmeth); + return false; + } + + if (PyCallable_Check(pmeth) == 0) + { + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + Py_XDECREF(pmeth); + return false; + } + + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + Py_XDECREF(pmeth); + + // right now we know this function exists and is callable.. + PyObject* py_return = PyObject_CallMethod(implementor, callee_name, nullptr); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) + { + PyErr_Print(); + PyErr_Clear(); + } + + if (py_return != nullptr && py_return != Py_None) + { + if (PyString_Check(py_return)) + { + dest.assign(PyString_AsString(py_return)); + got_string = true; + } + } + Py_XDECREF(py_return); + + return got_string; +} + +bool +ScriptInterpreterPython::GetLongHelpForCommandObject (lldb::ScriptInterpreterObjectSP cmd_obj_sp, + std::string& dest) +{ + bool got_string = false; + dest.clear(); + + Locker py_lock (this, + Locker::AcquireLock | Locker::NoSTDIN, + Locker::FreeLock); + + static char callee_name[] = "get_long_help"; + + if (!cmd_obj_sp) + return false; + + PyObject* implementor = (PyObject*)cmd_obj_sp->GetObject(); + + if (implementor == nullptr || implementor == Py_None) + return false; + + PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); + + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + if (pmeth == nullptr || pmeth == Py_None) + { + Py_XDECREF(pmeth); + return false; + } + + if (PyCallable_Check(pmeth) == 0) + { + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + Py_XDECREF(pmeth); + return false; + } + + if (PyErr_Occurred()) + { + PyErr_Clear(); + } + + Py_XDECREF(pmeth); + + // right now we know this function exists and is callable.. + PyObject* py_return = PyObject_CallMethod(implementor, callee_name, nullptr); + + // if it fails, print the error but otherwise go on + if (PyErr_Occurred()) + { + PyErr_Print(); + PyErr_Clear(); + } + + if (py_return != nullptr && py_return != Py_None) + { + if (PyString_Check(py_return)) + { + dest.assign(PyString_AsString(py_return)); + got_string = true; + } + } + Py_XDECREF(py_return); + + return got_string; +} + std::unique_ptr<ScriptInterpreterLocker> ScriptInterpreterPython::AcquireInterpreterLock () { |