[3/3] Translate PyExc_KeyboardInterrupt to gdb "quit"

Message ID 20181225195358.19399-4-tom@tromey.com
State New
Headers show
Series
  • Python exception handling improvements
Related show

Commit Message

Tom Tromey Dec. 25, 2018, 7:53 p.m.
A while back I typed "info pretty-printers" with a large number of
printers installed, and I typed "q" to stop the pagination.  I noticed
that gdb printed a Python exception in this case.

It seems to me that, instead, quitting pagination (or control-c'ing a
Python command generally) should be handled the same way that gdb
normally handles a quit.

This patch implements this idea by changing gdbpy_handle_exception to
treat PyExc_KeyboardInterrupt specially.

gdb/ChangeLog
2018-12-25  Tom Tromey  <tom@tromey.com>

	* python/py-utils.c (gdbpy_handle_exception): Translate
	PyExc_KeyboardInterrupt to quit.

gdb/testsuite/ChangeLog
2018-12-25  Tom Tromey  <tom@tromey.com>

	* gdb.python/py-cmd.exp (test_python_inline_or_multiline): Add
	pagination test.
---
 gdb/ChangeLog                       |  5 ++++
 gdb/python/py-utils.c               |  4 ++-
 gdb/testsuite/ChangeLog             |  5 ++++
 gdb/testsuite/gdb.python/py-cmd.exp | 40 +++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 1 deletion(-)

-- 
2.17.2

Patch

diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 2b4779097b..5c2409db75 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -423,7 +423,9 @@  gdbpy_handle_exception ()
      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)
+  if (PyErr_GivenExceptionMatches (ptype, PyExc_KeyboardInterrupt))
+    throw_quit ("Quit");
+  else if (! PyErr_GivenExceptionMatches (ptype, gdbpy_gdberror_exc)
       || msg == NULL || *msg == '\0')
     {
       PyErr_Restore (ptype, pvalue, ptraceback);
diff --git a/gdb/testsuite/gdb.python/py-cmd.exp b/gdb/testsuite/gdb.python/py-cmd.exp
index 33a5b39842..473fce23e8 100644
--- a/gdb/testsuite/gdb.python/py-cmd.exp
+++ b/gdb/testsuite/gdb.python/py-cmd.exp
@@ -261,3 +261,43 @@  if { [readline_is_used] } {
 	}
     }
 }
+
+
+# Test that interrupting pagination throws a gdb quit.
+gdb_test_no_output "set height 10"
+
+gdb_py_test_multiple "input multi-line-output command" \
+  "python" "" \
+  "class test_mline (gdb.Command):" "" \
+  "  \"\"\"Docstring\"\"\"" "" \
+  "  def __init__ (self):" "" \
+  "    super (test_mline, self).__init__ (\"test_multiline\", gdb.COMMAND_USER)" "" \
+  "  def invoke (self, arg, from_tty):" "" \
+  "    for f in range(20):" "" \
+  "      print (\"test_multiline output\")" "" \
+  "test_mline ()" "" \
+  "end" ""
+
+set test "verify pagination from test_multiline"
+gdb_test_multiple "test_multiline" $test {
+    -re "--Type <RET>" {
+	exp_continue
+    }
+    -re " for more, q to quit" {
+	exp_continue
+    }
+    -re ", c to continue without paging--$" {
+	pass $test
+    }
+}
+
+send_gdb "q\n"
+set test "verify pagination from test_multiline: q"
+gdb_test_multiple "test_multiline" $test {
+    -re "Error occurred in Python" {
+	fail $test
+    }
+    -re "Quit" {
+	pass $test
+    }
+}