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] acessing struct elements


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


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