[RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value

Tom Tromey-2
This is version 2 of the patch to fix PR python/17386.
I believe it addresses Yao's review comments.

Tom

Reply | Threaded
Open this post in threaded view
|

[RFA v2 1/2] add nb_inplace_divide for python 2

Tom Tromey-2
Python 2's PyNumberMethods has nb_inplace_divide, but Python 3 does
not.  This patch adds it for Python 2.

This buglet didn't cause much fallout because the only non-NULL entry
in value_object_as_number after this is for valpy_divide; and the
missing slot caused it to slide up to nb_floor_divide (where
nb_true_divide was intended).

2016-05-23  Tom Tromey  <[hidden email]>

        * python/py-value.c (value_object_as_number): Add
        nb_inplace_divide for Python 2.
---
 gdb/ChangeLog         | 5 +++++
 gdb/python/py-value.c | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 07bc5d2..5142429 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2016-05-23  Tom Tromey  <[hidden email]>
 
+ * python/py-value.c (value_object_as_number): Add
+ nb_inplace_divide for Python 2.
+
+2016-05-23  Tom Tromey  <[hidden email]>
+
  PR python/19438, PR python/18393:
  * python/py-objfile.c (objfpy_initialize): Initialize self->dict.
  * python/py-progspace.c (pspy_initialize): Initialize self->dict.
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 7dba0ad..268f6c8 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1827,6 +1827,9 @@ static PyNumberMethods value_object_as_number = {
   NULL,                       /* nb_inplace_add */
   NULL,                       /* nb_inplace_subtract */
   NULL,                       /* nb_inplace_multiply */
+#ifndef IS_PY3K
+  NULL,                       /* nb_inplace_divide */
+#endif
   NULL,                       /* nb_inplace_remainder */
   NULL,                       /* nb_inplace_power */
   NULL,                       /* nb_inplace_lshift */
--
2.5.5

Reply | Threaded
Open this post in threaded view
|

[RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Tom Tromey-2
In reply to this post by Tom Tromey-2
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  <[hidden email]>

        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  <[hidden email]>

        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(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 5142429..a848c14 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2016-05-23  Tom Tromey  <[hidden email]>
 
+ 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  <[hidden email]>
+
  * python/py-value.c (value_object_as_number): Add
  nb_inplace_divide for Python 2.
 
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 268f6c8..e0b017a 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -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 = {
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 1f0e3f9..edcf3b2 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,11 @@
 2016-05-23  Tom Tromey  <[hidden email]>
 
+ 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  <[hidden email]>
+
  PR python/19438, PR python/18393:
  * gdb.python/py-progspace.exp: Add "dir" test.
  * gdb.python/py-objfile.exp: Add "dir" test.
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index a9dbe97..57a9ba1 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -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" {
--
2.5.5

Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Pedro Alves-7
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

Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Tom Tromey-2
>> +#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
Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 1/2] add nb_inplace_divide for python 2

Yao Qi
In reply to this post by Tom Tromey-2
Tom Tromey <[hidden email]> writes:

> 2016-05-23  Tom Tromey  <[hidden email]>
>
> * python/py-value.c (value_object_as_number): Add
> nb_inplace_divide for Python 2.

Patch is good to me.

--
Yao (齐尧)
Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Yao Qi
In reply to this post by Tom Tromey-2
Tom Tromey <[hidden email]> writes:

> 2016-05-23  Tom Tromey  <[hidden email]>
>
> 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  <[hidden email]>
>
> 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.

--
Yao (齐尧)
Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Ulrich Weigand
In reply to this post by Tom Tromey-2
Tom Tromey wrote:

> PR python/17386:
> * python/py-value.c (value_object_as_number): Add
> nb_inplace_floor_divide, nb_inplace_true_divide, nb_index.

This causes a failure on my RHEL5 test system (using Python 2.4):
gdb/python/py-value.c:1848: error: too many initializers for ‘PyNumberMethods’

> +#ifndef HAVE_LIBPYTHON_2_4
> +  /* This was added in Python 2.5.  */
> +  , valpy_long      /* nb_index */
> +#endif /* HAVE_LIBPYTHON_2_4 */

Nothing defines HAVE_LIBPYTHON_2_4 on my machine; instead, we seem to have

/* Define if Python 2.4 is being used. */
#define HAVE_LIBPYTHON2_4 1

in config.h ...  Typo?

Bye,
Ulrich

--
  Dr. Ulrich Weigand
  GNU/Linux compilers and toolchain
  [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Tom Tromey-2
Ulrich> Nothing defines HAVE_LIBPYTHON_2_4 on my machine; instead, we
Ulrich> seem to have
Ulrich> /* Define if Python 2.4 is being used. */
Ulrich> #define HAVE_LIBPYTHON2_4 1
Ulrich> in config.h ...  Typo?

Yes, sorry about that.
I'm fixing it as appended.

Tom

commit 7bd787e8774f96712d2e15a4094f094e00ff45ba
Author: Tom Tromey <[hidden email]>
Date:   Wed May 25 07:54:44 2016 -0600

    fix spelling of HAVE_LIBPYTHON2_4 in py-value.c
   
    Ulrich pointed out that an earlier patch had misspelled
    HAVE_LIBPYTHON2_4, adding an extra "_".  This caused a build failure.
    This patch fixes the bug.
   
    2016-05-25  Tom Tromey  <[hidden email]>
   
    * python/py-value.c (value_object_as_number): Use correct spelling
    of HAVE_LIBPYTHON2_4.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d99ea83..6609f9c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2016-05-25  Tom Tromey  <[hidden email]>
+
+ * python/py-value.c (value_object_as_number): Use correct spelling
+ of HAVE_LIBPYTHON2_4.
+
 2016-05-25  Bernhard Heckel  <[hidden email]>
 
  * f-typeprint.c (f_type_print_base): Replace 0 by show.
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 7a2a235..21e9247 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1841,10 +1841,10 @@ static PyNumberMethods value_object_as_number = {
   valpy_divide,               /* nb_true_divide */
   NULL,      /* nb_inplace_floor_divide */
   NULL,      /* nb_inplace_true_divide */
-#ifndef HAVE_LIBPYTHON_2_4
+#ifndef HAVE_LIBPYTHON2_4
   /* This was added in Python 2.5.  */
   valpy_long,      /* nb_index */
-#endif /* HAVE_LIBPYTHON_2_4 */
+#endif /* HAVE_LIBPYTHON2_4 */
 };
 
 static PyMappingMethods value_object_as_mapping = {
Reply | Threaded
Open this post in threaded view
|

Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value

Ulrich Weigand
Tom Tromey wrote:

> Ulrich> Nothing defines HAVE_LIBPYTHON_2_4 on my machine; instead, we
> Ulrich> seem to have
> Ulrich> /* Define if Python 2.4 is being used. */
> Ulrich> #define HAVE_LIBPYTHON2_4 1
> Ulrich> in config.h ...  Typo?
>
> Yes, sorry about that.
> I'm fixing it as appended.

Thanks, Tom!

Bye,
Ulrich

--
  Dr. Ulrich Weigand
  GNU/Linux compilers and toolchain
  [hidden email]