[v2,1/3,gdb/python] Use gdbpy_err_fetch::{type, value} as getters

Message ID 20240308152938.18166-1-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
  Similar to gdbpy_err_fetch::value, add a getter gdbpy_err_fetch::type, and use
both consistently to get gdbpy_err_fetch members m_error_value and
m_error_type.

Tested on aarch64-linux.
---
 gdb/python/py-utils.c        |  8 ++++----
 gdb/python/python-internal.h | 12 ++++++++++--
 2 files changed, 14 insertions(+), 6 deletions(-)


base-commit: cdabd12b186e8e794045372b753416a18c387d7b
  

Comments

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

Tom> -  if (m_error_value.get () != nullptr && m_error_value.get () != Py_None)
Tom> -    return gdbpy_obj_to_string (m_error_value.get ());
Tom> +  if (this->value ().get () != nullptr && this->value ().get () != Py_None)
Tom> +    return gdbpy_obj_to_string (this->value ().get ());
Tom>    else
Tom> -    return gdbpy_obj_to_string (m_error_type.get ());
Tom> +    return gdbpy_obj_to_string (this->type ().get ());

I think it'd be better to introduce a local variable for this->value() here.

Tom
  
Tom de Vries March 9, 2024, 3:09 p.m. UTC | #2
On 3/8/24 21:20, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> -  if (m_error_value.get () != nullptr && m_error_value.get () != Py_None)
> Tom> -    return gdbpy_obj_to_string (m_error_value.get ());
> Tom> +  if (this->value ().get () != nullptr && this->value ().get () != Py_None)
> Tom> +    return gdbpy_obj_to_string (this->value ().get ());
> Tom>    else
> Tom> -    return gdbpy_obj_to_string (m_error_type.get ());
> Tom> +    return gdbpy_obj_to_string (this->type ().get ());
> 
> I think it'd be better to introduce a local variable for this->value() here.


Done in attached patch.

I'll commit shortly.

Thanks,
- Tom
  

Patch

diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 9382eb62a5f..4fbdb695a50 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -195,10 +195,10 @@  gdbpy_err_fetch::to_string () const
      Using str (aka PyObject_Str) will fetch the error message from
      gdb.GdbError ("message").  */
 
-  if (m_error_value.get () != nullptr && m_error_value.get () != Py_None)
-    return gdbpy_obj_to_string (m_error_value.get ());
+  if (this->value ().get () != nullptr && this->value ().get () != Py_None)
+    return gdbpy_obj_to_string (this->value ().get ());
   else
-    return gdbpy_obj_to_string (m_error_type.get ());
+    return gdbpy_obj_to_string (this->type ().get ());
 }
 
 /* See python-internal.h.  */
@@ -206,7 +206,7 @@  gdbpy_err_fetch::to_string () const
 gdb::unique_xmalloc_ptr<char>
 gdbpy_err_fetch::type_to_string () const
 {
-  return gdbpy_obj_to_string (m_error_type.get ());
+  return gdbpy_obj_to_string (this->type ().get ());
 }
 
 /* Convert a GDB exception to the appropriate Python exception.
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index c68aff5340e..9ceb4aa7ed4 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -675,16 +675,24 @@  class gdbpy_err_fetch
 
   bool type_matches (PyObject *type) const
   {
-    return PyErr_GivenExceptionMatches (m_error_type.get (), type);
+    gdbpy_ref<> err_type = this->type ();
+    return PyErr_GivenExceptionMatches (err_type.get (), type);
   }
 
   /* Return a new reference to the exception value object.  */
 
-  gdbpy_ref<> value ()
+  gdbpy_ref<> value () const
   {
     return m_error_value;
   }
 
+  /* Return a new reference to the exception type object.  */
+
+  gdbpy_ref<> type () const
+  {
+    return m_error_type;
+  }
+
 private:
 
   gdbpy_ref<> m_error_type, m_error_value, m_error_traceback;