This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Python] Allow attribute references to gdb.Value objects


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]