Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [Python] Allow attribute references to gdb.Value objects
@ 2011-08-12 19:33 Paul Koning
  2011-08-15 19:09 ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Koning @ 2011-08-12 19:33 UTC (permalink / raw)
  To: gdb-patches

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*/


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-08-16 10:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-12 19:33 [Python] Allow attribute references to gdb.Value objects Paul Koning
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox