From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 40635 invoked by alias); 20 Feb 2019 03:43:18 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 40606 invoked by uid 89); 20 Feb 2019 03:43:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: smtp.polymtl.ca Received: from smtp.polymtl.ca (HELO smtp.polymtl.ca) (132.207.4.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 20 Feb 2019 03:43:16 +0000 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id x1K3hAGg021831 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 19 Feb 2019 22:43:15 -0500 Received: by simark.ca (Postfix, from userid 112) id 904BA1E654; Tue, 19 Feb 2019 22:43:10 -0500 (EST) Received: from simark.ca (localhost [127.0.0.1]) by simark.ca (Postfix) with ESMTP id 02CD41E152; Tue, 19 Feb 2019 22:43:10 -0500 (EST) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 20 Feb 2019 03:43:00 -0000 From: Simon Marchi To: Kevin Buettner Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2 2/4] Define gdb.Value(bufobj, type) constructor In-Reply-To: <20190219144158.7df6921b@f29-4.lan> References: <20190219143356.1576e67f@f29-4.lan> <20190219144158.7df6921b@f29-4.lan> Message-ID: <6e92c59bf470368dea2842e48254b8f8@polymtl.ca> X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.3.6 X-IsSubscribed: yes X-SW-Source: 2019-02/txt/msg00324.txt.bz2 On 2019-02-19 16:41, Kevin Buettner wrote: > Provided a buffer BUFOBJ and a type TYPE, construct a gdb.Value object > with type TYPE, where the value's contents are taken from BUFOBJ. > > E.g... > > (gdb) python import struct > (gdb) python unsigned_int_type=gdb.lookup_type('unsigned int') > (gdb) python b=struct.pack('=I',0xdeadbeef) > (gdb) python v=gdb.Value(b, unsigned_int_type) ; print("%#x" % v) > 0xdeadbeef > > This two argument form of the gdb.Value constructor may also be used > to obtain gdb values from selected portions of buffers read with > Inferior.read_memory(). The test case (which is in a separate patch) > demonstrates this use case. > > gdb/ChangeLog: > > * python/py-value.c (convert_buffer_and_type_to_value): New > function. > (valpy_new): Parse arguments via gdb_PyArg_ParseTupleAndKeywords. > Add support for handling an optional second argument. Call > convert_buffer_and_type_to_value as appropriate. > --- > gdb/python/py-value.c | 72 > ++++++++++++++++++++++++++++++++++++++++++++------- > 1 file changed, 62 insertions(+), 10 deletions(-) > > diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c > index 20ef5822f8..af5a75a4a4 100644 > --- a/gdb/python/py-value.c > +++ b/gdb/python/py-value.c > @@ -107,22 +107,68 @@ note_value (value_object *value_obj) > values_in_python = value_obj; > } > > +/* Convert a python object OBJ with type TYPE to a gdb value. The > + python object in question must conform to the python buffer > + protocol. On success, return the converted value, otherwise > + nullptr. */ > + > +static struct value * > +convert_buffer_and_type_to_value (PyObject *obj, struct type *type) > +{ > + Py_buffer_up buffer_up; > + Py_buffer py_buf; > + > + if (PyObject_CheckBuffer (obj) > + && PyObject_GetBuffer (obj, &py_buf, PyBUF_SIMPLE) == 0) > + { > + /* Got a buffer, py_buf, out of obj. Cause it to be released > + when it goes out of scope. */ > + buffer_up.reset (&py_buf); > + } > + else > + { > + PyErr_SetString (PyExc_TypeError, > + _("Object must support the python buffer protocol.")); > + return nullptr; > + } > + > + if (TYPE_LENGTH (type) > py_buf.len) > + { > + PyErr_SetString (PyExc_TypeError, > + _("Size of type is larger than that of buffer object.")); > + return nullptr; > + } Another small thing I didn't spot when reading v1: I think it would be more appropriate to raise a ValueError in this last case. TypeError would be if the arguments were of the wrong Python type, which is not the case here. It just happens that the value we handle is a (GDB) type, but that's not the same kind of type. If you do that change, don't forget to update the tests as well. Otherwise, the whole series still LGTM. Simon