Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Paul Koning <paulkoning@comcast.net>
To: gdb-patches@sourceware.org
Subject: [Python] Allow attribute references to gdb.Value objects
Date: Fri, 12 Aug 2011 19:33:00 -0000	[thread overview]
Message-ID: <3A56CC74-0A48-47E8-BBA2-6E2BEB2FB588@comcast.net> (raw)

It would be more natural to be able to reference fields of a gdb.Value by the standard field (attribute) syntax, e.g., "val1.field2" as opposed to "val1['field2']".  The attached patch does this.  It acts like the Python standard method __getattr__ in that it first looks for a predefined attribute (such as "type"), and only if that fails will it look for a value field with the given name.  So val1.type means what it always did (and if you want the "type" field of some structure value, you'd need to use val1['type'] as before).

I don't have write privs, but I do have a copyright assignment in place.

	paul

2011-08-12  Paul Koning  <ni1d@arrl.net>

	* python/py-value.c (valpy_getattr): New function.

Index: python/py-value.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-value.c,v
retrieving revision 1.25
diff -u -r1.25 py-value.c
--- python/py-value.c	27 Jun 2011 19:21:51 -0000	1.25
+++ python/py-value.c	12 Aug 2011 19:21:26 -0000
@@ -492,6 +492,36 @@
   return res_val ? value_to_value_object (res_val) : NULL;
 }
 
+/* Given string name of an element inside structure, return its value
+   object.  Used for attribute style references.  This one looks for
+   an object element only if the name given isn't a predefined attribute
+   such as "type" or a method such as "dereference".  */
+static PyObject *
+valpy_getattr (PyObject *self, PyObject *key)
+{
+  PyObject *retval;
+  volatile struct gdb_exception except;
+  char *field = NULL;
+
+  retval = PyObject_GenericGetAttr (self, key);
+  if (retval == NULL && PyErr_ExceptionMatches (PyExc_AttributeError))
+    {
+      /* Not a defined attribute, see if it's a value element.  */
+      PyErr_Clear ();
+      retval = valpy_getitem (self, key);
+      if (retval == NULL)
+	{
+	  if (gdbpy_is_string (key))
+	    field = python_string_to_host_string (key);
+	  if (field == NULL)
+	    field = "(none)";
+	  PyErr_Format (PyExc_AttributeError,
+			"'gdb.Value' object has no attribute '%s'", field);
+	}
+    }
+  return retval;
+}
+
 static int
 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
 {
@@ -1307,7 +1337,7 @@
   valpy_hash,		          /*tp_hash*/
   valpy_call,	                  /*tp_call*/


             reply	other threads:[~2011-08-12 19:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-12 19:33 Paul Koning [this message]
2011-08-15 19:09 ` Tom Tromey
2011-08-15 20:30   ` Paul_Koning
2011-08-15 20:43     ` Tom Tromey
2011-08-15 20:54       ` Paul_Koning
2011-08-16  1:12         ` Daniel Jacobowitz
2011-08-16 10:48           ` Paul_Koning

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=3A56CC74-0A48-47E8-BBA2-6E2BEB2FB588@comcast.net \
    --to=paulkoning@comcast.net \
    --cc=gdb-patches@sourceware.org \
    /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