[RFC 6/8] mi/python: Handle python exception when executiong python-defined MI commands

Jan Vrany
2019-04-18T15:24:35Z

Respond with ^error,msg="..." when an unhandled python exception is thrown
while invoking python-defined MI command. The error message is taken from
python exception object.

gdb/Changelog:

* python/py-micmd.c (py_mi_invoke): Handle exceptions thrown in Python
code.
---
gdb/ChangeLog | 5 +++++
gdb/python/py-micmd.c | 18 ++++++++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 8bbaaba5ad..1fd49e703f 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2018-12-10 Jan Vrany <jan.vrany@fit.cvut.cz>
+
+ * python/py-micmd.c (py_mi_invoke): Handle exceptions thrown in Python
+ code.
+
2018-12-10 Jan Vrany <jan.vrany@fit.cvut.cz>

* python/py-micmd.c (parse_mi_result): Polish to make the output
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index bbd746fa32..a099bb48cd 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -115,14 +115,28 @@ py_mi_invoke (void *py_obj, char **argv, int argc)
error (_("Failed to create the python arguments list."));
}
}
-
+
+ gdb_assert (PyErr_Occurred () == NULL);
gdbpy_ref<> result (PyObject_CallMethodObjArgs ((PyObject *) obj, invoke_cst, argobj.get (),
NULL));
+ if (PyErr_Occurred () != NULL)
+ {
+ gdbpy_err_fetch ex;
+ gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string());

- if (result != nullptr)
+ if (ex_msg == NULL || *ex_msg == '\0')
+ error (_("Failed to execute command"));
+ else
+ error (_(ex_msg.get ()));
+ }
+ else if (result != nullptr)
{
if (Py_None != result) parse_mi_result (result.get (), "result");
}
+ else
+ {
+ error (_("Command invoke() method returned NULL but no python exception is set"));
+ }
}

/* Parse the name of the MI command to register.
--
2.20.1