gdb/python: reformat an error string
authorAndrew Burgess <andrew.burgess@embecosm.com>
Mon, 18 Jan 2021 10:03:21 +0000 (10:03 +0000)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Mon, 8 Feb 2021 11:03:54 +0000 (11:03 +0000)
While working on another patch I noticed an oddly formatted error
message in the Python code.

When 'set python print-stack message' is in effect then consider this
Python script:

  class TestCommand (gdb.Command):
      def __init__ (self):
          gdb.Command.__init__ (self, "test-cmd", gdb.COMMAND_DATA)
      def invoke(self, args, from_tty):
          raise RuntimeError ("bad")
  TestCommand ()

And this GDB session:

  (gdb) source path/to/python/script.py
  (gdb) test-cmd
  Python Exception <class 'RuntimeError'> bad:
  Error occurred in Python: bad

The line 'Python Exception <class 'RuntimeError'> bad:' doesn't look
terrible in this situation, the colon at the end of the first line
makes sense given the second line.

However, there are places in GDB where there is no second line
printed, for example consider this python script:

  def stop_listener (e):
      raise RuntimeError ("bad")
  gdb.events.stop.connect (stop_listener)

Then this GDB session:

  (gdb) file helloworld.exe
  (gdb) start
  Temporary breakpoint 1 at 0x40112a: file hello.c, line 6.
  Starting program: helloworld.exe

  Temporary breakpoint 1, main () at hello.c:6
  6   printf ("Hello World\n");
  Python Exception <class 'RuntimeError'> bad:
  (gdb) si
  0x000000000040112f 6   printf ("Hello World\n");
  Python Exception <class 'RuntimeError'> bad:

In this case there is no auxiliary information displayed after the
warning, and the line ending in the colon looks weird to me.

A quick survey of the code seems to indicate that it is not uncommon
for there to be no auxiliary information line printed, its not just
the one case I found above.

I propose that the line that currently looks like this:

  Python Exception <class 'RuntimeError'> bad:

Be reformatted like this:

  Python Exception <class 'RuntimeError'>: bad

I think this looks fine then in either situation.  The first now looks
like this:

  (gdb) test-cmd
  Python Exception <class 'RuntimeError'>: bad
  Error occurred in Python: bad

And the second like this:

  (gdb) si
  0x000000000040112f 6   printf ("Hello World\n");
  Python Exception <class 'RuntimeError'>: bad

There's just two tests that needed updating.  Errors are checked for
in many more tests, but most of the time the pattern doesn't care
about the colon.

gdb/ChangeLog:

* python/python.c (gdbpy_print_stack): Reformat an error message.

gdb/testsuite/ChangeLog:

* gdb.python/py-framefilter.exp: Update expected results.
* gdb.python/python.exp: Update expected results.

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

index 634d272dbdffc94ca1b67f7132f6012b82d21407..a45c2c7499b932756826b572e93e94c26e43b3c1 100644 (file)
@@ -1,3 +1,7 @@
+2021-02-08  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * python/python.c (gdbpy_print_stack): Reformat an error message.
+
 2021-02-08  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * tui/tui-interp.c (tui_command_line_handler): New function.
index fd760f6a98b1e2b9f1cd85722431b7fb2bf82b0e..009c0c4c6d7b5cc60bcb3e7064ddf9f942d9ad6c 100644 (file)
@@ -1320,7 +1320,7 @@ gdbpy_print_stack (void)
              PyErr_Clear ();
            }
          else
-           fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
+           fprintf_filtered (gdb_stderr, "Python Exception %s: %s\n",
                              type.get (), msg.get ());
        }
       catch (const gdb_exception &except)
index ed496e4305ef78e71923170d5d14412454e9fe22..8fb69c0bacf627153f99c440576c4eaac7d4895e 100644 (file)
@@ -1,3 +1,8 @@
+2021-02-08  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * gdb.python/py-framefilter.exp: Update expected results.
+       * gdb.python/python.exp: Update expected results.
+
 2021-02-08  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * gdb.tui/scroll.exp: Tighten expected results.  Remove comment
index 346898950aa03ca376091128d7348333fef21685..a9c6ca642bef058c71963bb9239ac2ab8892c5b1 100644 (file)
@@ -269,7 +269,7 @@ gdb_test_no_output  "enable frame-filter global Error" \
     "enable frame-filter global Error"
 set test "bt 1 with Error filter"
 gdb_test_multiple "bt 1" $test {
-    -re "Python Exception .*whoops:.*$gdb_prompt $" {
+    -re "Python Exception .*: whoops\r\n.*$gdb_prompt $" {
        pass $test
     }
 }
index 83e83b49f75445e439bbb2fb3f2a2f5d47391147..c7d879efe634c4d97edb21a71ba4f86fe733bcab 100644 (file)
@@ -463,7 +463,7 @@ gdb_test_multiline "prompt substitution readline error_prompt" \
   "end" ""
 
 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook error_prompt" {
-    -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*$gdb_prompt $" {
+    -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>): Python exception called\r\n$gdb_prompt $" {
        pass $gdb_test_name
     }
 }
This page took 0.036745 seconds and 4 git commands to generate.