Bug 17386 - gdb.Value should have __index__ method
Summary: gdb.Value should have __index__ method
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: unknown
: P2 normal
Target Milestone: 7.12
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-12 14:57 UTC by Tom Tromey
Modified: 2016-07-21 10:55 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2014-09-12 14:57:10 UTC
I've seen multiple reports where a pretty-printer throws an
exception like

Python Exception <class 'TypeError'> 'gdb.Value' object cannot be interpreted as an integer: 

E.g., https://bugzilla.mozilla.org/show_bug.cgi?id=1058392
... but there are many more if you search for the relevant bits of
the error message.

I think one possible fix is for gdb.Value to implement the __index__
method.
Comment 1 Sourceware Commits 2016-05-24 16:13:51 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ddae946278bf4269370f7d945732485ad13469fa

commit ddae946278bf4269370f7d945732485ad13469fa
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Sep 13 22:24:50 2014 -0600

    Fix PR python/17386 - add __index__ method to gdb.Value
    
    This patch fixes PR python/17386.
    
    The bug is that gdb.Value does not implement the Python __index__
    method.  This method is needed to convert a Python object to an index
    and is used by various operations in Python, such as indexing an
    array.
    
    The fix is to implement the nb_index method for gdb.Value.
    
    nb_index was added in Python 2.5.  I don't have a good way to test
    Python 2.4, but I made an attempt to accomodate it.
    
    I chose to use valpy_long in all cases because this simplifies porting
    to Python 3, and because there didn't seem to be any harm.
    
    Built and regtested on x86-64 Fedora 23.
    
    2016-05-24  Tom Tromey  <tom@tromey.com>
    
    	PR python/17386:
    	* python/py-value.c (value_object_as_number): Add
    	nb_inplace_floor_divide, nb_inplace_true_divide, nb_index.
    
    2016-05-24  Tom Tromey  <tom@tromey.com>
    
    	PR python/17386:
    	* gdb.python/py-value.exp (test_value_numeric_ops): Add tests that
    	use value as an index.
Comment 2 Tom Tromey 2016-05-24 16:15:46 UTC
Fixed.