2011-03-21 Phil Muldoon <pmuldoon@redhat.com>
authorPhil Muldoon <pmuldoon@redhat.com>
Tue, 22 Mar 2011 09:38:16 +0000 (09:38 +0000)
committerPhil Muldoon <pmuldoon@redhat.com>
Tue, 22 Mar 2011 09:38:16 +0000 (09:38 +0000)
        PR python/12183

* python/py-function.c (fnpy_call): Treat GdbErrors differently to
other error classes.  Do not print stack trace.

2011-03-21  Phil Muldoon  <pmuldoon@redhat.com>

        PR python/12183

* gdb.python/py-function.exp: Add GdbError tests.

gdb/ChangeLog
gdb/python/py-function.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-function.exp

index 7aec9624c2085ad19a215e3483a292992c6a583c..5388eef6181c733ae899aa675c18413392c32fea 100644 (file)
@@ -1,3 +1,10 @@
+2011-03-22  Phil Muldoon  <pmuldoon@redhat.com>
+
+        PR python/12183
+
+       * python/py-function.c (fnpy_call): Treat GdbErrors differently to
+       other error classes.  Do not print stack trace.
+
 2011-03-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
        * dwarf2read.c (producer_is_gxx_lt_4_6): New function.
index 47d916b4b92f33db2cb8ab3da6da911bb04093a9..0c3e6f2a466e70d096d112d802dc5e1807837204 100644 (file)
@@ -79,8 +79,55 @@ fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language,
 
   if (!result)
     {
-      gdbpy_print_stack ();
-      error (_("Error while executing Python code."));
+      PyObject *ptype, *pvalue, *ptraceback;
+      char *msg;
+
+      PyErr_Fetch (&ptype, &pvalue, &ptraceback);
+
+      /* Try to fetch an error message contained within ptype, pvalue.
+        When fetching the error message we need to make our own copy,
+        we no longer own ptype, pvalue after the call to PyErr_Restore.  */
+
+      msg = gdbpy_exception_to_string (ptype, pvalue);
+      make_cleanup (xfree, msg);
+
+      if (msg == NULL)
+       {
+         /* An error occurred computing the string representation of the
+            error message.  This is rare, but we should inform the user.  */
+
+         printf_filtered (_("An error occurred in a Python "
+                            "convenience function\n"
+                            "and then another occurred computing the "
+                            "error message.\n"));
+         gdbpy_print_stack ();
+       }
+
+      /* Don't print the stack for gdb.GdbError exceptions.
+        It is generally used to flag user errors.
+
+        We also don't want to print "Error occurred in Python command"
+        for user errors.  However, a missing message for gdb.GdbError
+        exceptions is arguably a bug, so we flag it as such.  */
+
+      if (!PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
+         || msg == NULL || *msg == '\0')
+       {
+         PyErr_Restore (ptype, pvalue, ptraceback);
+         gdbpy_print_stack ();
+         if (msg != NULL && *msg != '\0')
+           error (_("Error occurred in Python convenience function: %s"),
+                  msg);
+         else
+           error (_("Error occurred in Python convenience function."));
+       }
+      else
+       {
+         Py_XDECREF (ptype);
+         Py_XDECREF (pvalue);
+         Py_XDECREF (ptraceback);
+         error ("%s", msg);
+       }
     }
 
   value = convert_value_from_python (result);
index 2abb0f9825c18a8ff803a7374d5e621cedf07ae5..55089012944a8d0502239a0aee77fb56b99c5f71 100644 (file)
@@ -1,3 +1,9 @@
+2011-03-22  Phil Muldoon  <pmuldoon@redhat.com>
+
+        PR python/12183
+
+       * gdb.python/py-function.exp: Add GdbError tests.
+
 2011-03-18  Pedro Alves  <pedro@codesourcery.com>
 
        * gdb.trace/unavailable.cc (args_test_func, local_test_func)
index bbbbf422bb5db14657dac3c6a97b67e13f3fc645..dfccdff29718119400bc7c9ae37e3cc3f3d1f867 100644 (file)
@@ -69,3 +69,29 @@ gdb_py_test_multiple "input int-returning function" \
 
 gdb_test "print \$yes() && \$yes()" " = 1" "call yes with &&"
 gdb_test "print \$yes() || \$yes()" " = 1" "call yes with ||"
+
+gdb_py_test_multiple "Test GDBError" \
+  "python" "" \
+  "class GDBError(gdb.Function):" "" \
+  "    def __init__(self):" "" \
+  "        gdb.Function.__init__(self, 'gdberror')" "" \
+  "    def invoke(self):" "" \
+  "        raise gdb.GdbError(\"This is a GdbError\")" "" \
+  "GDBError ()" "" \
+  "end" ""
+
+gdb_test "print \$gdberror()" "This is a GdbError.*" \
+    "Test GdbError.  There should not be a stack trace"
+
+gdb_py_test_multiple "Test Normal Error" \
+  "python" "" \
+  "class NormalError(gdb.Function):" "" \
+  "    def __init__(self):" "" \
+  "        gdb.Function.__init__(self, 'normalerror')" "" \
+  "    def invoke(self):" "" \
+  "        raise RuntimeError(\"This is a Normal Error\")" "" \
+  "NormalError ()" "" \
+  "end" ""
+
+gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \
+    "Test a Runtime error.  There should be a stack trace."
This page took 0.05114 seconds and 4 git commands to generate.