[RFA,v2,2/2] Fix PR python/17386 - add __index__ method to gdb.Value
Commit Message
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-23 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-23 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.
---
gdb/ChangeLog | 6 ++++++
gdb/python/py-value.c | 8 +++++++-
gdb/testsuite/ChangeLog | 6 ++++++
gdb/testsuite/gdb.python/py-value.exp | 7 +++++++
4 files changed, 26 insertions(+), 1 deletion(-)
Comments
On 05/23/2016 06:25 PM, Tom Tromey wrote:
> - valpy_divide /* nb_true_divide */
> + valpy_divide, /* nb_true_divide */
> + NULL, /* nb_inplace_floor_divide */
> + NULL /* nb_inplace_true_divide */
> +#ifndef HAVE_LIBPYTHON_2_4
> + /* This was added in Python 2.5. */
> + , valpy_long /* nb_index */
> +#endif /* HAVE_LIBPYTHON_2_4 */
> };
Are you trying to avoid a trailing comma? We already use trailing commas
in many places in common code (and have been for a long while),
so the fact that it was valid C89/C++03 doesn't really matter in practice.
(It was a common extension that later became valid in C99/C++11.)
IOW, this can be:
+ valpy_divide, /* nb_true_divide */
+ NULL, /* nb_inplace_floor_divide */
+ NULL, /* nb_inplace_true_divide */
+#ifndef HAVE_LIBPYTHON_2_4
+ /* This was added in Python 2.5. */
+ valpy_long, /* nb_index */
+#endif /* HAVE_LIBPYTHON_2_4 */
Thanks,
Pedro Alves
>> +#ifndef HAVE_LIBPYTHON_2_4
>> + /* This was added in Python 2.5. */
>> + , valpy_long /* nb_index */
>> +#endif /* HAVE_LIBPYTHON_2_4 */
Pedro> Are you trying to avoid a trailing comma?
Yep.
Pedro> IOW, this can be:
I fixed it locally, thanks.
Tom
Tom Tromey <tom@tromey.com> writes:
> 2016-05-23 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-23 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.
Looks good to me with the trailing comma fixed.
@@ -1,5 +1,11 @@
2016-05-23 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-23 Tom Tromey <tom@tromey.com>
+
* python/py-value.c (value_object_as_number): Add
nb_inplace_divide for Python 2.
@@ -1838,7 +1838,13 @@ static PyNumberMethods value_object_as_number = {
NULL, /* nb_inplace_xor */
NULL, /* nb_inplace_or */
NULL, /* nb_floor_divide */
- valpy_divide /* nb_true_divide */
+ valpy_divide, /* nb_true_divide */
+ NULL, /* nb_inplace_floor_divide */
+ NULL /* nb_inplace_true_divide */
+#ifndef HAVE_LIBPYTHON_2_4
+ /* This was added in Python 2.5. */
+ , valpy_long /* nb_index */
+#endif /* HAVE_LIBPYTHON_2_4 */
};
static PyMappingMethods value_object_as_mapping = {
@@ -1,5 +1,11 @@
2016-05-23 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.
+
+2016-05-23 Tom Tromey <tom@tromey.com>
+
PR python/19438, PR python/18393:
* gdb.python/py-progspace.exp: Add "dir" test.
* gdb.python/py-objfile.exp: Add "dir" test.
@@ -124,6 +124,13 @@ proc test_value_numeric_ops {} {
gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value"
gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values"
+ gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \
+ "result = r" "use value as string index"
+ gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \
+ "result = 1" "use value as tuple index"
+ gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
+ "result = 1" "use value as array index"
+
# Test some invalid operations.
gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {