[18/25] Add gdb.Value.assign method

Message ID 20230427-ada-catch-exception-v1-18-947caa9905e3@adacore.com
State New
Headers
Series Many updates to DAP implementation |

Commit Message

Tom Tromey May 24, 2023, 4:37 p.m. UTC
  This adds an 'assign' method to gdb.Value.  This allows for assignment
without requiring the use of parse_and_eval.
---
 gdb/NEWS                              |  2 ++
 gdb/doc/python.texi                   |  6 ++++++
 gdb/python/py-value.c                 | 30 ++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-value.exp | 14 ++++++++++++++
 4 files changed, 52 insertions(+)
  

Comments

Eli Zaretskii May 24, 2023, 4:54 p.m. UTC | #1
> Date: Wed, 24 May 2023 10:37:09 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> 
> This adds an 'assign' method to gdb.Value.  This allows for assignment
> without requiring the use of parse_and_eval.
> ---
>  gdb/NEWS                              |  2 ++
>  gdb/doc/python.texi                   |  6 ++++++
>  gdb/python/py-value.c                 | 30 ++++++++++++++++++++++++++++++
>  gdb/testsuite/gdb.python/py-value.exp | 14 ++++++++++++++
>  4 files changed, 52 insertions(+)

Thanks, the documentation parts are OK.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index d97e3c15a87..4c4df060b62 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -209,6 +209,8 @@  info main
      "unset_env".  These can be used to modify the inferior's
      environment before it is started.
 
+  ** gdb.Value now has the 'assign' method.
+
 *** Changes in GDB 13
 
 * MI version 1 is deprecated, and will be removed in GDB 14.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 69755e96143..64ddbe21fc5 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -947,6 +947,12 @@  If @var{type} is @code{None} then this version of @code{__init__}
 behaves as though @var{type} was not passed at all.
 @end defun
 
+@defun Value.assign (rhs)
+Assign @var{rhs} to this value, and return @code{None}.  If this value
+cannot be assigned to, or if the assignment is invalid for some reason
+(for example a type-checking failure), an exception will be thrown.
+@end defun
+
 @defun Value.cast (type)
 Return a new instance of @code{gdb.Value} that is the result of
 casting this instance to the type described by @var{type}, which must
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 6c62820c63b..2be223da4ea 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -856,6 +856,33 @@  valpy_reinterpret_cast (PyObject *self, PyObject *args)
   return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
 }
 
+/* Implementation of the "assign" method.  */
+
+static PyObject *
+valpy_assign (PyObject *self_obj, PyObject *args)
+{
+  PyObject *val_obj;
+
+  if (! PyArg_ParseTuple (args, "O", &val_obj))
+    return nullptr;
+
+  struct value *val = convert_value_from_python (val_obj);
+  if (val == nullptr)
+    return nullptr;
+
+  try
+    {
+      value_object *self = (value_object *) self_obj;
+      value_assign (self->value, val);
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+
+  Py_RETURN_NONE;
+}
+
 static Py_ssize_t
 valpy_length (PyObject *self)
 {
@@ -2121,6 +2148,9 @@  Return Unicode string representation of the value." },
     "format_string (...) -> string\n\
 Return a string representation of the value using the specified\n\
 formatting options" },
+  { "assign", (PyCFunction) valpy_assign, METH_VARARGS,
+    "assign (VAL) -> None\n\
+Assign VAL to this value." },
   {NULL}  /* Sentinel */
 };
 
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 9fc25814721..cdfcd414cd4 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -633,6 +633,19 @@  proc test_history_count {} {
     }
 }
 
+# Test Value.assign.
+proc test_assign {} {
+    gdb_test_no_output "python i_value = gdb.parse_and_eval('i')" \
+	"evaluate i"
+    gdb_test_no_output "python i_value.assign(27)" \
+	"set i to 27"
+    gdb_test "print i" " = 27"
+    gdb_test_no_output "python i_value = gdb.Value(27)" \
+	"reset i_value"
+    gdb_test "python i_value.assign(89)" "not an lvalue.*" \
+	"cannot assign to integer"
+}
+
 # Build C version of executable.  C++ is built later.
 if { [build_inferior "${binfile}" "c"] < 0 } {
     return -1
@@ -663,6 +676,7 @@  test_value_in_inferior
 test_value_from_buffer
 test_value_sub_classes
 test_inferior_function_call
+test_assign
 test_value_after_death
 
 # Test either C or C++ values.