Re: [RFC][patch 2/9] export values mechanism to Python

Thiago Jung Bauermann
2008-07-07T06:03:40Z

On Mon, 2008-06-02 at 18:18 -0600, Tom Tromey wrote:
> >>>>> "Daniel" == Daniel Jacobowitz <drow@false.org> writes:
>
> Daniel> Would it be helpful or confusing to automatically expose this
> Daniel> as [] in Python, __getitem__?
>
> Good question.
>
> In general I've been using explicit method names, like get_* and
> set_*. But perhaps we should be using attributes and various
> "intrinsic" names... I'm not enough of a Python expert to know what is
> preferred.

In the GDB BoF we decided to use Python __magic__ for gdb.Values, so I
started working on it. My idea is to make them behave as much as the
corresponding native Python types as possible, so a gdb.Value with an
int type would behave like a Python int.

There's one case which I'd like to discuss, and about which I talked to
Tromey on IRC:

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.
This looks a bit strange at least. 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.

The downside is that it means that value objects couldn't have methods
since they would be interpreted as struct elements. If Python supports
class methods (I'm not sure of that, but I think it does), then we could
have things like gdb.Values.some_method (value_object, other, args).
Also, Python's own attributes and methods (like __doc__) would clash
with structure elements.

What do you prefer?
--
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center