[v2,2/3,gdb/python] Normalize exceptions in gdbpy_err_fetch

Message ID 20240308152938.18166-2-tdevries@suse.de
State Committed
Headers
Series [v2,1/3,gdb/python] Use gdbpy_err_fetch::{type, value} as getters |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom de Vries March 8, 2024, 3:29 p.m. UTC
  With python 3.12, I run into:
...
(gdb) PASS: gdb.python/py-block.exp: check variable access
python print (block['nonexistent'])^M
Python Exception <class 'KeyError'>: 'nonexistent'^M
Error occurred in Python: 'nonexistent'^M
(gdb) FAIL: gdb.python/py-block.exp: check nonexistent variable
...

The problem is that that PyErr_Fetch returns a normalized exception, while the
test-case matches the output for an unnormalized exception.

With python 3.6, PyErr_Fetch returns an unnormalized exception, and the
test passes.

Fix this by:
- updating the test-case to match the output for a normalized exception, and
- lazily forcing normalized exceptions using PyErr_NormalizeException.

Tested on aarch64-linux.
---
 gdb/python/python-internal.h          | 15 ++++++++++++++-
 gdb/testsuite/gdb.python/py-block.exp |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)
  

Comments

Tom Tromey March 8, 2024, 8:21 p.m. UTC | #1
>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> With python 3.12, I run into:
Tom> ...
Tom> (gdb) PASS: gdb.python/py-block.exp: check variable access
Tom> python print (block['nonexistent'])^M
Tom> Python Exception <class 'KeyError'>: 'nonexistent'^M
Tom> Error occurred in Python: 'nonexistent'^M
Tom> (gdb) FAIL: gdb.python/py-block.exp: check nonexistent variable
Tom> ...

Tom> The problem is that that PyErr_Fetch returns a normalized exception, while the
Tom> test-case matches the output for an unnormalized exception.

Tom> With python 3.6, PyErr_Fetch returns an unnormalized exception, and the
Tom> test passes.

Tom> Fix this by:
Tom> - updating the test-case to match the output for a normalized exception, and
Tom> - lazily forcing normalized exceptions using PyErr_NormalizeException.

Tom> Tested on aarch64-linux.

Thanks.

Tom> -  gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
Tom> +  mutable gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
Tom> +  mutable bool m_normalized = false;

Maybe it's better not to add 'const'.
Not sure though.

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Tom de Vries March 9, 2024, 3:11 p.m. UTC | #2
On 3/8/24 21:21, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> With python 3.12, I run into:
> Tom> ...
> Tom> (gdb) PASS: gdb.python/py-block.exp: check variable access
> Tom> python print (block['nonexistent'])^M
> Tom> Python Exception <class 'KeyError'>: 'nonexistent'^M
> Tom> Error occurred in Python: 'nonexistent'^M
> Tom> (gdb) FAIL: gdb.python/py-block.exp: check nonexistent variable
> Tom> ...
> 
> Tom> The problem is that that PyErr_Fetch returns a normalized exception, while the
> Tom> test-case matches the output for an unnormalized exception.
> 
> Tom> With python 3.6, PyErr_Fetch returns an unnormalized exception, and the
> Tom> test passes.
> 
> Tom> Fix this by:
> Tom> - updating the test-case to match the output for a normalized exception, and
> Tom> - lazily forcing normalized exceptions using PyErr_NormalizeException.
> 
> Tom> Tested on aarch64-linux.
> 
> Thanks.
> 
> Tom> -  gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
> Tom> +  mutable gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
> Tom> +  mutable bool m_normalized = false;
> 
> Maybe it's better not to add 'const'.
> Not sure though.
> 

It's either use mutable, or drop the const on to_string.  I decided the 
former is less weird, but agreed, it's debatable.

Thanks,
- Tom

> Approved-By: Tom Tromey <tom@tromey.com>
> 
> Tom
  

Patch

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 9ceb4aa7ed4..30802ae2480 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -683,6 +683,18 @@  class gdbpy_err_fetch
 
   gdbpy_ref<> value () const
   {
+    if (!m_normalized)
+      {
+	PyObject *error_type, *error_value, *error_traceback;
+	error_type = m_error_type.release ();
+	error_value = m_error_value.release ();
+	error_traceback = m_error_traceback.release ();
+	PyErr_NormalizeException (&error_type, &error_value, &error_traceback);
+	m_error_type.reset (error_type);
+	m_error_value.reset (error_value);
+	m_error_traceback.reset (error_traceback);
+	m_normalized = true;
+      }
     return m_error_value;
   }
 
@@ -695,7 +707,8 @@  class gdbpy_err_fetch
 
 private:
 
-  gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
+  mutable gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;
+  mutable bool m_normalized = false;
 };
 
 /* Called before entering the Python interpreter to install the
diff --git a/gdb/testsuite/gdb.python/py-block.exp b/gdb/testsuite/gdb.python/py-block.exp
index 942611af305..99642a546a7 100644
--- a/gdb/testsuite/gdb.python/py-block.exp
+++ b/gdb/testsuite/gdb.python/py-block.exp
@@ -44,7 +44,7 @@  gdb_test "python print (block.function)" "None" "first anonymous block"
 gdb_test "python print (block.start)" "${decimal}" "check start not None"
 gdb_test "python print (block.end)" "${decimal}" "check end not None"
 gdb_test "python print (block\['f'\].name == 'f')" "True" "check variable access"
-gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: nonexistent.*" \
+gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
          "check nonexistent variable"
 gdb_test "python print (block\[42\])" ".*TypeError.*: Expected a string.*" \
          "check non-string key"