Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-23 17:25 [RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value Tom Tromey
@ 2016-05-23 17:25 ` Tom Tromey
  2016-05-23 18:01   ` Pedro Alves
                     ` (2 more replies)
  2016-05-23 17:25 ` [RFA v2 1/2] add nb_inplace_divide for python 2 Tom Tromey
  1 sibling, 3 replies; 10+ messages in thread
From: Tom Tromey @ 2016-05-23 17:25 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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

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  <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.
 
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  <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.
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


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

* [RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value
@ 2016-05-23 17:25 Tom Tromey
  2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
  2016-05-23 17:25 ` [RFA v2 1/2] add nb_inplace_divide for python 2 Tom Tromey
  0 siblings, 2 replies; 10+ messages in thread
From: Tom Tromey @ 2016-05-23 17:25 UTC (permalink / raw)
  To: gdb-patches

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

Tom


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

* [RFA v2 1/2] add nb_inplace_divide for python 2
  2016-05-23 17:25 [RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value Tom Tromey
  2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
@ 2016-05-23 17:25 ` Tom Tromey
  2016-05-24  8:50   ` Yao Qi
  1 sibling, 1 reply; 10+ messages in thread
From: Tom Tromey @ 2016-05-23 17:25 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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  <tom@tromey.com>

	* 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  <tom@tromey.com>
 
+	* python/py-value.c (value_object_as_number): Add
+	nb_inplace_divide for Python 2.
+
+2016-05-23  Tom Tromey  <tom@tromey.com>
+
 	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


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
@ 2016-05-23 18:01   ` Pedro Alves
  2016-05-23 18:40     ` Tom Tromey
  2016-05-24  8:58   ` Yao Qi
  2016-05-25 13:48   ` Ulrich Weigand
  2 siblings, 1 reply; 10+ messages in thread
From: Pedro Alves @ 2016-05-23 18:01 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

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


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-23 18:01   ` Pedro Alves
@ 2016-05-23 18:40     ` Tom Tromey
  0 siblings, 0 replies; 10+ messages in thread
From: Tom Tromey @ 2016-05-23 18:40 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Tom Tromey, gdb-patches

>> +#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


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

* Re: [RFA v2 1/2] add nb_inplace_divide for python 2
  2016-05-23 17:25 ` [RFA v2 1/2] add nb_inplace_divide for python 2 Tom Tromey
@ 2016-05-24  8:50   ` Yao Qi
  0 siblings, 0 replies; 10+ messages in thread
From: Yao Qi @ 2016-05-24  8:50 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey <tom@tromey.com> writes:

> 2016-05-23  Tom Tromey  <tom@tromey.com>
>
> 	* python/py-value.c (value_object_as_number): Add
> 	nb_inplace_divide for Python 2.

Patch is good to me.

-- 
Yao (齐尧)


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
  2016-05-23 18:01   ` Pedro Alves
@ 2016-05-24  8:58   ` Yao Qi
  2016-05-25 13:48   ` Ulrich Weigand
  2 siblings, 0 replies; 10+ messages in thread
From: Yao Qi @ 2016-05-24  8:58 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

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.

-- 
Yao (齐尧)


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
  2016-05-23 18:01   ` Pedro Alves
  2016-05-24  8:58   ` Yao Qi
@ 2016-05-25 13:48   ` Ulrich Weigand
  2016-05-25 14:04     ` Tom Tromey
  2 siblings, 1 reply; 10+ messages in thread
From: Ulrich Weigand @ 2016-05-25 13:48 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Tom Tromey

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
  Ulrich.Weigand@de.ibm.com


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-25 13:48   ` Ulrich Weigand
@ 2016-05-25 14:04     ` Tom Tromey
  2016-05-25 14:54       ` Ulrich Weigand
  0 siblings, 1 reply; 10+ messages in thread
From: Tom Tromey @ 2016-05-25 14:04 UTC (permalink / raw)
  To: Ulrich Weigand; +Cc: Tom Tromey, gdb-patches

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 <tom@tromey.com>
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  <tom@tromey.com>
    
    	* 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  <tom@tromey.com>
+
+	* python/py-value.c (value_object_as_number): Use correct spelling
+	of HAVE_LIBPYTHON2_4.
+
 2016-05-25  Bernhard Heckel  <bernhard.heckel@intel.com>
 
 	* 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 = {


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

* Re: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value
  2016-05-25 14:04     ` Tom Tromey
@ 2016-05-25 14:54       ` Ulrich Weigand
  0 siblings, 0 replies; 10+ messages in thread
From: Ulrich Weigand @ 2016-05-25 14:54 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Tom Tromey, gdb-patches

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
  Ulrich.Weigand@de.ibm.com


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

end of thread, other threads:[~2016-05-25 14:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-23 17:25 [RFA v2 0/2] Fix PR python/17386 - add __index__ method to gdb.Value Tom Tromey
2016-05-23 17:25 ` [RFA v2 2/2] " Tom Tromey
2016-05-23 18:01   ` Pedro Alves
2016-05-23 18:40     ` Tom Tromey
2016-05-24  8:58   ` Yao Qi
2016-05-25 13:48   ` Ulrich Weigand
2016-05-25 14:04     ` Tom Tromey
2016-05-25 14:54       ` Ulrich Weigand
2016-05-23 17:25 ` [RFA v2 1/2] add nb_inplace_divide for python 2 Tom Tromey
2016-05-24  8:50   ` Yao Qi

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