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

Message ID 20190418152337.6376-7-jan.vrany@fit.cvut.cz
State New
Headers show
Series
  • Create MI commands using python
Related show

Commit Message

Jan Vrany April 18, 2019, 3:23 p.m.
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(-)

-- 
2.20.1

Comments

Tom Tromey April 25, 2019, 7:46 p.m. | #1
>>>>> "Jan" == Jan Vrany <jan.vrany@fit.cvut.cz> writes:


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

Jan> gdb/Changelog:

Jan> 	* python/py-micmd.c (py_mi_invoke): Handle exceptions thrown in Python
Jan> 	code.

This seems reasonable but it also seems like it should just be part of
the patch adding this new function.  If you're worried about joint
authorship, it's pretty common to just list two names in the ChangeLog.

thanks,
Tom
Jan Vrany April 26, 2019, 10:19 a.m. | #2
> This seems reasonable but it also seems like it should just be part of

> the patch adding this new function.  If you're worried about joint

> authorship, it's pretty common to just list two names in the ChangeLog.

> 


Yes, that's the reason why commits are structured this way. I did not want
to change Didier's commuts without him. 

I'll do as you suggested and squash commits. 

Thanks for reviewing! 

Jan

> thanks,

> Tom

Patch

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.