From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by sourceware.org (Postfix) with ESMTPS id B54A93857016 for ; Wed, 24 May 2023 16:37:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B54A93857016 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-77480507360so31076839f.3 for ; Wed, 24 May 2023 09:37:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1684946255; x=1687538255; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Z1ssdJsnFh9k6lnhH0/408C33n/dkemwodN1KR3ja4g=; b=JU/kTFK6XEqXknJhlcI/ljb6zv3Azka++Aaulf4hN/YgUKzMgrYvPlvMTGEH/81YLD sm7bc62/tAvoipgLBeM429N3BFyeZGqsLAmYWmPVkW4etcjVmCYJEc/xKLxkiXLn8jgd lNLPNZvKKRWanWidlwYnYOk4vIlCWyIBKkgQwcDnFo/ioffIw7qIVekXDIP0ewZz9DIa W5KvEmaIuRkHCeySizOyjG6gdNjjh35XdiEKvy2y5yI1qJoMb+44mfwzM22GJmBDq2fU OW6ptPyHCK+ThOm0iTry57lKepOZ6XJbOhXlZmdu82MEsVYIDDrEEPYXbT900DR9KD8s ZXZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684946255; x=1687538255; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z1ssdJsnFh9k6lnhH0/408C33n/dkemwodN1KR3ja4g=; b=mIfLgSz36gqk75hYj+b1vXNcyPr+i+t+JgYudTuhkcC0haOHVKOwkd0tw/y+fK5mF5 xILecIqO2De8C6KfXhtwr9/Ar1TM+fuBWa5dXqL9bRTNyX7wK6FvnQGGgDSb9raVfB7w ugSz5Zj4Ic2gQCP/R4LIYVud/Gsj/GU+sana8sU9rgZpoMMGTVq/ivvZR2+dGQphJo5f qkxh+fKpvxqqkRWTJy8rjWRTA7S6Whvu4CwP0GzIe1WWsgipbKwjiXMVy/SYIChJzg2B Hvf376WytNxpkGVevFmq+KEXwI0jpQA1vfruf4K2B992s+Z5Saua3VgsQ73mbckaIG0I J/Kg== X-Gm-Message-State: AC+VfDyqjPQWC4xZHsgcxEFo/SqACekhJA+/jFJcNcW1RCDCFKoPj/N1 FBuGhT81mZGIrJT7Lm8gsDkX1mB1XORvqPkHys55qA== X-Google-Smtp-Source: ACHHUZ5XCqDjZ1hKuHnHN5rBe0YuhdPjod/WgnQzV9D3g+11I5SlpN1NsVa4h7f1Wi6HSWLQqsHz/A== X-Received: by 2002:a5e:c006:0:b0:76f:e9ec:d62e with SMTP id u6-20020a5ec006000000b0076fe9ecd62emr12548881iol.5.1684946255009; Wed, 24 May 2023 09:37:35 -0700 (PDT) Received: from localhost.localdomain (71-211-130-244.hlrn.qwest.net. [71.211.130.244]) by smtp.gmail.com with ESMTPSA id o22-20020a6bcf16000000b0076c81bf2731sm3397266ioa.20.2023.05.24.09.37.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 09:37:34 -0700 (PDT) From: Tom Tromey Date: Wed, 24 May 2023 10:37:09 -0600 Subject: [PATCH 18/25] Add gdb.Value.assign method MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230427-ada-catch-exception-v1-18-947caa9905e3@adacore.com> References: <20230427-ada-catch-exception-v1-0-947caa9905e3@adacore.com> In-Reply-To: <20230427-ada-catch-exception-v1-0-947caa9905e3@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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(+) 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. -- 2.40.0