From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 6/7] Consolidate gdb.GdbError handling
Date: Sat, 15 Sep 2018 07:25:00 -0000 [thread overview]
Message-ID: <20180915072459.14934-7-tom@tromey.com> (raw)
In-Reply-To: <20180915072459.14934-1-tom@tromey.com>
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 <tom@tromey.com>
* 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<char>
- 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<char>
- 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<char>
+ 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<char> unicode_to_target_string (PyObject *unicode_str);
--
2.17.1
next prev parent reply other threads:[~2018-09-15 7:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-15 7:25 [PATCH 0/7] some small python fixes (one including a doc patch) Tom Tromey
2018-09-15 7:25 ` [PATCH 5/7] Check for negative argument in Type.template_argument Tom Tromey
2018-09-15 7:25 ` Tom Tromey [this message]
2018-09-15 7:25 ` [PATCH 3/7] Allow conversion of pointers to Python int Tom Tromey
2018-09-15 7:25 ` [PATCH 4/7] Report Python errors coming from gdb.post_event Tom Tromey
2018-09-15 7:25 ` [PATCH 7/7] Allow setting a parameter to raise gdb.GdbError Tom Tromey
2018-09-15 9:16 ` Eli Zaretskii
2018-09-24 2:39 ` Simon Marchi
2018-09-24 4:58 ` Tom Tromey
2018-09-24 5:14 ` Tom Tromey
2018-09-24 7:10 ` Eli Zaretskii
2018-09-24 10:24 ` Tom Tromey
2018-09-24 12:39 ` Eli Zaretskii
2018-09-15 7:25 ` [PATCH 1/7] Allow more Python scalar conversions Tom Tromey
2018-09-24 2:06 ` Simon Marchi
2018-09-24 4:58 ` Tom Tromey
2018-09-15 7:25 ` [PATCH 2/7] Preserve sign when converting gdb.Value to Python int Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180915072459.14934-7-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox