From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29725 invoked by alias); 12 Aug 2011 19:33:03 -0000 Received: (qmail 29705 invoked by uid 22791); 12 Aug 2011 19:33:02 -0000 X-SWARE-Spam-Status: No, hits=-2.7 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from qmta12.westchester.pa.mail.comcast.net (HELO qmta12.westchester.pa.mail.comcast.net) (76.96.59.227) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 12 Aug 2011 19:32:47 +0000 Received: from omta14.westchester.pa.mail.comcast.net ([76.96.62.60]) by qmta12.westchester.pa.mail.comcast.net with comcast id KXYX1h0031HzFnQ5CXYnXa; Fri, 12 Aug 2011 19:32:47 +0000 Received: from [10.127.238.91] ([65.206.2.68]) by omta14.westchester.pa.mail.comcast.net with comcast id KXYe1h01R1U2a2h3aXYgbo; Fri, 12 Aug 2011 19:32:45 +0000 From: Paul Koning Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: [Python] Allow attribute references to gdb.Value objects Date: Fri, 12 Aug 2011 19:33:00 -0000 Message-Id: <3A56CC74-0A48-47E8-BBA2-6E2BEB2FB588@comcast.net> To: gdb-patches@sourceware.org Mime-Version: 1.0 (Apple Message framework v1084) 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 X-SW-Source: 2011-08/txt/msg00261.txt.bz2 It would be more natural to be able to reference fields of a gdb.Value by t= he standard field (attribute) syntax, e.g., "val1.field2" as opposed to "va= l1['field2']". The attached patch does this. It acts like the Python stan= dard 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 th= e "type" field of some structure value, you'd need to use val1['type'] as b= efore). I don't have write privs, but I do have a copyright assignment in place. paul 2011-08-12 Paul Koning * python/py-value.c (valpy_getattr): New function. Index: python/py-value.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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; } =20 +/* 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 =3D NULL; + + retval =3D PyObject_GenericGetAttr (self, key); + if (retval =3D=3D NULL && PyErr_ExceptionMatches (PyExc_AttributeError)) + { + /* Not a defined attribute, see if it's a value element. */ + PyErr_Clear (); + retval =3D valpy_getitem (self, key); + if (retval =3D=3D NULL) + { + if (gdbpy_is_string (key)) + field =3D python_string_to_host_string (key); + if (field =3D=3D NULL) + field =3D "(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*/