From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29763 invoked by alias); 15 Sep 2018 07:25:21 -0000 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 Received: (qmail 29487 invoked by uid 89); 15 Sep 2018 07:25:09 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.8 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gateway33.websitewelcome.com Received: from gateway33.websitewelcome.com (HELO gateway33.websitewelcome.com) (192.185.145.4) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Sep 2018 07:25:06 +0000 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway33.websitewelcome.com (Postfix) with ESMTP id C6574A2690 for ; Sat, 15 Sep 2018 02:25:04 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 14wygjSxRRPoj14wyg6QAl; Sat, 15 Sep 2018 02:25:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=WpdRcEi9Zl0bLWHRgXoFf2jYlAajtQJ3LF0ICCDtC7g=; b=a7D29JUd1J1+SIRVzZRTRpOReR M7a4FDd2EAT6PClLEuo+4e3bV3G2LT3O+2fxP8WgYKyFlqbSMzs2Q/Xo5kG2brbgNuQMwK7fL8Bf2 1lP5ITIgzV7AAaJnXHwIk295n; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:41846 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1g14wy-00253W-Jk; Sat, 15 Sep 2018 02:25:04 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 6/7] Consolidate gdb.GdbError handling Date: Sat, 15 Sep 2018 07:25:00 -0000 Message-Id: <20180915072459.14934-7-tom@tromey.com> In-Reply-To: <20180915072459.14934-1-tom@tromey.com> References: <20180915072459.14934-1-tom@tromey.com> X-SW-Source: 2018-09/txt/msg00482.txt.bz2 I noticed two nearly identical copies of the same code for handling gdb.GdbError. The only differences were in some error messages. These differences didn't seem very important, so this patch pulls the code out into a new function. gdb/ChangeLog 2018-09-15 Tom Tromey * python/py-function.c (fnpy_call): Use gdbpy_handle_exception. * python/py-cmd.c (cmdpy_function): Use gdbpy_handle_exception. * python/python-internal.h (gdbpy_handle_exception): Declare. * python/py-utils.c (gdbpy_handle_exception): New function. --- gdb/ChangeLog | 7 +++++ gdb/python/py-cmd.c | 48 +------------------------------ gdb/python/py-function.c | 51 +------------------------------- gdb/python/py-utils.c | 56 ++++++++++++++++++++++++++++++++++++ gdb/python/python-internal.h | 1 + 5 files changed, 66 insertions(+), 97 deletions(-) diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 27c4689413a..e7eb66f515c 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -145,53 +145,7 @@ cmdpy_function (struct cmd_list_element *command, NULL)); if (result == NULL) - { - PyObject *ptype, *pvalue, *ptraceback; - - 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. */ - - gdb::unique_xmalloc_ptr - msg (gdbpy_exception_to_string (ptype, pvalue)); - - 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 command\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 command: %s"), msg.get ()); - else - error (_("Error occurred in Python command.")); - } - else - { - Py_XDECREF (ptype); - Py_XDECREF (pvalue); - Py_XDECREF (ptraceback); - error ("%s", msg.get ()); - } - } + gdbpy_handle_exception (); } /* Helper function for the Python command completers (both "pure" diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c index c4612316df6..1900f0ff0c0 100644 --- a/gdb/python/py-function.c +++ b/gdb/python/py-function.c @@ -83,56 +83,7 @@ fnpy_call (struct gdbarch *gdbarch, const struct language_defn *language, } if (result == NULL) - { - PyObject *ptype, *pvalue, *ptraceback; - - 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. */ - - gdb::unique_xmalloc_ptr - msg (gdbpy_exception_to_string (ptype, pvalue)); - - 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.get ()); - else - error (_("Error occurred in Python convenience function.")); - } - else - { - Py_XDECREF (ptype); - Py_XDECREF (pvalue); - Py_XDECREF (ptraceback); - error ("%s", msg.get ()); - } - } + gdbpy_handle_exception (); value = convert_value_from_python (result.get ()); if (value == NULL) diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 01fd6ade8d5..6ef0d7efd39 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -384,3 +384,59 @@ gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object) Py_DECREF (object); return result; } + +/* Handle a Python exception when the special gdb.GdbError treatment + is desired. This should only be called when an exception is set. + If the exception is a gdb.GdbError, throw a gdb exception with the + exception text. For other exceptions, print the Python stack and + then throw a gdb exception. */ + +void +gdbpy_handle_exception () +{ + PyObject *ptype, *pvalue, *ptraceback; + + 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. */ + + gdb::unique_xmalloc_ptr + msg (gdbpy_exception_to_string (ptype, pvalue)); + + 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 Python " + "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: %s"), msg.get ()); + else + error (_("Error occurred in Python.")); + } + else + { + Py_XDECREF (ptype); + Py_XDECREF (pvalue); + Py_XDECREF (ptraceback); + error ("%s", msg.get ()); + } +} diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 785ad171511..d395adc26a3 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -674,6 +674,7 @@ extern const struct language_defn *python_language; int gdbpy_print_python_errors_p (void); void gdbpy_print_stack (void); +void gdbpy_handle_exception () ATTRIBUTE_NORETURN; PyObject *python_string_to_unicode (PyObject *obj); gdb::unique_xmalloc_ptr unicode_to_target_string (PyObject *unicode_str); -- 2.17.1