From ed80156930339ee8e68a5341ec3d7f06fab6d02c Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 12 May 2023 14:10:56 -0600 Subject: [PATCH] Add gdb.Value.assign method This adds an 'assign' method to gdb.Value. This allows for assignment without requiring the use of parse_and_eval. Reviewed-By: Eli Zaretskii --- 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(+) diff --git a/gdb/NEWS b/gdb/NEWS index 649a3a9824a..937e5cdf573 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -220,6 +220,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 02cb9ad4bc0..efef4de3fd0 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 933af92a664..069560742cf 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -853,6 +853,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) { @@ -2119,6 +2146,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. -- 2.30.2