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 <eliz@gnu.org>
This commit is contained in:
Tom Tromey 2023-05-12 14:10:56 -06:00
parent 510586589e
commit ed80156930
4 changed files with 52 additions and 0 deletions

View file

@ -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.

View file

@ -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

View file

@ -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 */
};

View file

@ -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.