Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Kevin Buettner <kevinb@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 2/4] Define gdb.Value(bufobj, type) constructor
Date: Wed, 20 Feb 2019 03:43:00 -0000	[thread overview]
Message-ID: <6e92c59bf470368dea2842e48254b8f8@polymtl.ca> (raw)
In-Reply-To: <20190219144158.7df6921b@f29-4.lan>

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


  reply	other threads:[~2019-02-20  3:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-19 21:34 [PATCH v2 0/4] Define gdb.Value(val, " Kevin Buettner
2019-02-19 21:39 ` [PATCH v2 1/4] Define unique_ptr specialization for Py_buffer Kevin Buettner
2019-02-19 21:42 ` [PATCH v2 2/4] Define gdb.Value(bufobj, type) constructor Kevin Buettner
2019-02-20  3:43   ` Simon Marchi [this message]
2019-02-20 18:03     ` Tom Tromey
2019-02-26 17:34     ` Kevin Buettner
2019-02-19 21:43 ` [PATCH v2 3/4] Add tests for " Kevin Buettner
2019-02-19 21:46 ` [PATCH v2 4/4] Document two argument form of gdb.Value constructor Kevin Buettner
2019-02-20  3:35   ` Eli Zaretskii
2019-02-26 17:35 ` [PATCH v2 0/4] Define gdb.Value(val, type) constructor Kevin Buettner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6e92c59bf470368dea2842e48254b8f8@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=kevinb@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox