From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32151 invoked by alias); 21 Mar 2011 12:45:26 -0000 Received: (qmail 32142 invoked by uid 22791); 21 Mar 2011 12:45:25 -0000 X-SWARE-Spam-Status: No, hits=-6.1 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 21 Mar 2011 12:45:13 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p2LCjBUa027294 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 21 Mar 2011 08:45:12 -0400 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p2LCjAjZ025950 for ; Mon, 21 Mar 2011 08:45:11 -0400 From: Phil Muldoon To: gdb-patches@sourceware.org Subject: [python] [patch] Fix PR/12183 Reply-to: pmuldoon@redhat.com X-URL: http://www.redhat.com Date: Mon, 21 Mar 2011 14:47:00 -0000 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-03/txt/msg00951.txt.bz2 This patch fixes PR python/12183. In this case GDB was printing a stack trace for a gdb.GdbError. This is incorrect, GdbErrors should not produce a stack trace. This fix is largely a duplicate of the code already in py-cmd. OK? Cheers, Phil 2011-03-21 Phil Muldoon 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 PR python/12183 * gdb.python/py-function.exp: Add GdbError tests. -- diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c index 47d916b..7ba2838 100644 --- a/gdb/python/py-function.c +++ b/gdb/python/py-function.c @@ -77,10 +77,57 @@ fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language, Py_DECREF (callable); Py_DECREF (args); - if (!result) + 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); diff --git a/gdb/testsuite/gdb.python/py-function.exp b/gdb/testsuite/gdb.python/py-function.exp index bbbbf42..dfccdff 100644 --- a/gdb/testsuite/gdb.python/py-function.exp +++ b/gdb/testsuite/gdb.python/py-function.exp @@ -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."