Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* should gdb.Value implement __hash__()?
@ 2010-11-02 12:32 Joachim Protze
  2010-11-02 20:59 ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Joachim Protze @ 2010-11-02 12:32 UTC (permalink / raw)
  To: gdb

Hi,

i have a problem with gdb.Value. I'm working on formating buffers, that
are usually given as pointers. The format, how to interprete the data
behind the pointer is given in a tree-like structure - inner nodes save
offsets, leafs save the datatype for an special address (buffer+offsets).
From inferior context i know, how to format the buffers address - and
following the tree how to format the whole buffer - but from context i
don't know how to format buffer+offset.
for example:
format: {
         +0x12:{int},
         +0x36:{double},
         +0x5a:{ +0x00:{int},
                 +0x08:{double}}}
buffer=0x1000
should print as:
(gdb) p buffer
$1 = { *((int*) 0x1000+0x12),
       *((double*) 0x1000+0x36),
       { *((int*) 0x1000+0x5a+0x00),
         *((double*) 0x1000+0x5a+0x08)}}


As you see, the way i want to display the content of this buffer is this
tree-like structure as given in format. The print-handler would return
"array" as display_hint and the address+offsets as children -- or casted
value for leafs. To know how to format the children i would create a
print-handler for this new address and collect it like this:
dict[address]=pointer_printer(address, format)
The intention is to lookup the address in the dict and return the listed
printer as handler.

The problem is, that gdb.Value is hashable, but the hash does not depend
on the value of gdb.Value. Also repr(gdb.Value) is not usable as key.
str(gdb.Value) triggers the lookup_function, which tries to find
str(gdb.Value) in dict - resulting in endless recursion.

Is there a way to get a stringrepresentation of the content of gdb.Value
bypassing the prettyprinting mechanism? Or is it possible to provide a
__hash__, that represents the content of the value? Or to provide the raw
string as __repr__()?

Thanks
- Joachim


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: should gdb.Value implement __hash__()?
  2010-11-02 12:32 should gdb.Value implement __hash__()? Joachim Protze
@ 2010-11-02 20:59 ` Tom Tromey
  2010-11-02 21:10   ` Paul Koning
  2010-11-02 22:01   ` Joachim Protze
  0 siblings, 2 replies; 4+ messages in thread
From: Tom Tromey @ 2010-11-02 20:59 UTC (permalink / raw)
  To: Joachim Protze; +Cc: gdb

>>>>> "Joachim" == Joachim Protze <joachim.protze@wh2.tu-dresden.de> writes:

Joachim> The problem is, that gdb.Value is hashable, but the hash does
Joachim> not depend on the value of gdb.Value. Also repr(gdb.Value) is
Joachim> not usable as key.  str(gdb.Value) triggers the
Joachim> lookup_function, which tries to find str(gdb.Value) in dict -
Joachim> resulting in endless recursion.

Joachim> Is there a way to get a stringrepresentation of the content of
Joachim> gdb.Value bypassing the prettyprinting mechanism? Or is it
Joachim> possible to provide a __hash__, that represents the content of
Joachim> the value? Or to provide the raw string as __repr__()?

Right now I think there isn't a good way.

We want to add a method to Value to show the contents as a python
buffer.  But nobody has done that yet.  (And, Value is not exactly like
a python buffer, since some parts can be optimized out...)

I think the current hash is based on the idea that a struct value's
contents can change.  But I am not completely sure this is the case.
If it is in fact immutable, we could change the hash.

In your case if you know that you only want your hash to hold pointer
values, you can use long(value) as the key.

Tom


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: should gdb.Value implement __hash__()?
  2010-11-02 20:59 ` Tom Tromey
@ 2010-11-02 21:10   ` Paul Koning
  2010-11-02 22:01   ` Joachim Protze
  1 sibling, 0 replies; 4+ messages in thread
From: Paul Koning @ 2010-11-02 21:10 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Joachim Protze, gdb

The Python rule is that mutable objects aren't hashable, so if gdb.Value can change, then it should not have a __hash__ method (and calling hash() on it should fail).

	paul

On Nov 2, 2010, at 5:03 PM, Tom Tromey wrote:

>>>>>> "Joachim" == Joachim Protze <joachim.protze@wh2.tu-dresden.de> writes:
> 
> Joachim> The problem is, that gdb.Value is hashable, but the hash does
> Joachim> not depend on the value of gdb.Value. Also repr(gdb.Value) is
> Joachim> not usable as key.  str(gdb.Value) triggers the
> Joachim> lookup_function, which tries to find str(gdb.Value) in dict -
> Joachim> resulting in endless recursion.
> 
> Joachim> Is there a way to get a stringrepresentation of the content of
> Joachim> gdb.Value bypassing the prettyprinting mechanism? Or is it
> Joachim> possible to provide a __hash__, that represents the content of
> Joachim> the value? Or to provide the raw string as __repr__()?
> 
> Right now I think there isn't a good way.
> 
> We want to add a method to Value to show the contents as a python
> buffer.  But nobody has done that yet.  (And, Value is not exactly like
> a python buffer, since some parts can be optimized out...)
> 
> I think the current hash is based on the idea that a struct value's
> contents can change.  But I am not completely sure this is the case.
> If it is in fact immutable, we could change the hash.
> 
> In your case if you know that you only want your hash to hold pointer
> values, you can use long(value) as the key.
> 
> Tom


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: should gdb.Value implement __hash__()?
  2010-11-02 20:59 ` Tom Tromey
  2010-11-02 21:10   ` Paul Koning
@ 2010-11-02 22:01   ` Joachim Protze
  1 sibling, 0 replies; 4+ messages in thread
From: Joachim Protze @ 2010-11-02 22:01 UTC (permalink / raw)
  To: gdb

On 02.11.2010 22:03, Tom Tromey wrote:
>
> In your case if you know that you only want your hash to hold pointer
> values, you can use long(value) as the key.
>
>    
Thanks for this hint, i tried int(value) and got an error, but i did not 
try long(value).

Joachim


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-11-02 22:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-02 12:32 should gdb.Value implement __hash__()? Joachim Protze
2010-11-02 20:59 ` Tom Tromey
2010-11-02 21:10   ` Paul Koning
2010-11-02 22:01   ` Joachim Protze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox