From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29308 invoked by alias); 13 Aug 2008 06:45:49 -0000 Received: (qmail 29125 invoked by uid 22791); 13 Aug 2008 06:45:45 -0000 X-Spam-Check-By: sourceware.org Received: from main.gmane.org (HELO ciao.gmane.org) (80.91.229.2) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 13 Aug 2008 06:45:06 +0000 Received: from root by ciao.gmane.org with local (Exim 4.43) id 1KTA6Q-0002Jz-Ub for gdb-patches@sources.redhat.com; Wed, 13 Aug 2008 06:45:03 +0000 Received: from 201.82.124.180 ([201.82.124.180]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 13 Aug 2008 06:45:02 +0000 Received: from bauerman by 201.82.124.180 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 13 Aug 2008 06:45:02 +0000 To: gdb-patches@sources.redhat.com From: Thiago Jung Bauermann Subject: [python] acessing struct elements Date: Wed, 13 Aug 2008 06:45:00 -0000 Message-ID: References: <20080429155212.444237503@br.ibm.com> <20080429155304.466637516@br.ibm.com> <20080528212451.GB2969@caradoc.them.org> <1215410598.1795.58.camel@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit User-Agent: KNode/0.10.9 X-IsSubscribed: yes 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: 2008-08/txt/msg00341.txt.bz2 Thiago Jung Bauermann wrote: > In the case of valpy_get_element (which is used to access an element in > a value representing a struct or class), using __getitem__ means that in > Python one would use a_struct["element"] to access a_struct.element. There's one unexpected consequence of this: to enable the syntax above, all gdb.Value objects need to implement the map methods, and Python considers empty maps to be False in contexts which expect a boolean. Because of this, the following will not generally work: val = frame.read_var_value (sym) if val: print "Variable value is: " + str (val) else: print "Variable not found." Instead, one needs to explicitly compare with None: val = frame.read_var_value (sym) if val != None: print "Variable value is: " + str (val) else: print "Variable not found." I believe the same problem can happen with values representing numbers, since Python also considers 0 to be False. I didn't test that, though. We can keep going with this approach, and warn users that they need to explicitly compare gdb.Value with None instead of relying on Python implicit rules for boolean evaluation. Is this acceptable? Or we can create an element of gdb.Value which would provide access to the actual value, so one would say something like a_struct.val["element"] (or do math using a_number.val, in the case of numeric values). While we are at it ... > It would be possible to make this > case work more like a real struct, by intercepting python object > accesses to attributes. Then one could have a_struct.element work for > gdb.Values. ... we can implement this alternative (e.g., a_struct.val.element), since we wouldn't have to worry anymore about confusing struct elements with gdb.Value methods. -- []'s Thiago Jung Bauermann IBM Linux Technology Center