This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[python] acessing struct elements
- From: Thiago Jung Bauermann <bauerman at br dot ibm dot com>
- To: gdb-patches at sources dot redhat dot com
- Date: Wed, 13 Aug 2008 01:46:53 -0300
- Subject: [python] acessing struct elements
- References: <20080429155212.444237503@br.ibm.com> <20080429155304.466637516@br.ibm.com> <20080528212451.GB2969@caradoc.them.org> <m3k5h78j64.fsf@fleche.redhat.com> <1215410598.1795.58.camel@localhost.localdomain>
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