public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Enable call of overloaded subscript operator from python
       [not found] <20240530192837.4530-1-ssbssa.ref@yahoo.de>
@ 2024-05-30 19:28 ` Hannes Domani
  2024-06-03 15:02   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Hannes Domani @ 2024-05-30 19:28 UTC (permalink / raw)
  To: gdb-patches

If you try to use the overloaded subscript operator of a class
in python, it fails like this:

(gdb) py print(gdb.parse_and_eval('b')[5])
Traceback (most recent call last):
  File "<string>", line 1, in <module>
gdb.error: Cannot subscript requested type.
Error while executing Python code.

This simply checks if such an operator exists, and calls it
instead, making this possible:

(gdb) py print(gdb.parse_and_eval('b')[5])
102 'f'
---
 gdb/python/py-value.c                    | 8 +++++++-
 gdb/testsuite/gdb.python/py-value-cc.cc  | 6 ++++++
 gdb/testsuite/gdb.python/py-value-cc.exp | 1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index c844baa978b..3f28716ad8a 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1153,7 +1153,13 @@ valpy_getitem (PyObject *self, PyObject *key)
 	     type.  */
 	  struct value *idx = convert_value_from_python (key);
 
-	  if (idx != NULL)
+	  if (idx != NULL
+	      && binop_user_defined_p (BINOP_SUBSCRIPT, tmp, idx))
+	    {
+	      res_val = value_x_binop (tmp, idx, BINOP_SUBSCRIPT,
+				       OP_NULL, EVAL_NORMAL);
+	    }
+	  else if (idx != NULL)
 	    {
 	      /* Check the value's type is something that can be accessed via
 		 a subscript.  */
diff --git a/gdb/testsuite/gdb.python/py-value-cc.cc b/gdb/testsuite/gdb.python/py-value-cc.cc
index 2d38a26d948..08b99158d02 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.cc
+++ b/gdb/testsuite/gdb.python/py-value-cc.cc
@@ -42,6 +42,7 @@ class B : public A {
   int arg0_func ();
   int arg1_func (int arg1);
   int arg2_func (int arg1, int arg2);
+  char operator[] (int num);
 };
 
 int B::static_func ()
@@ -64,6 +65,11 @@ int B::arg2_func (int arg1, int arg2)
   return a * arg1 + arg2;
 }
 
+char B::operator[] (int num)
+{
+  return a + num;
+}
+
 struct X
 {
   union { int x; char y; };
diff --git a/gdb/testsuite/gdb.python/py-value-cc.exp b/gdb/testsuite/gdb.python/py-value-cc.exp
index 17a67e20c1c..b096c7538a1 100644
--- a/gdb/testsuite/gdb.python/py-value-cc.exp
+++ b/gdb/testsuite/gdb.python/py-value-cc.exp
@@ -99,6 +99,7 @@ gdb_test "python print(uu\[uu_fields\[1\]\]\['a'\])" "1000" "uu.a via field"
 # Test overloaded operators.
 gdb_test_no_output "python a = gdb.parse_and_eval('a')" "init a"
 gdb_test "python print(a + 5)" "10" "a + 5"
+gdb_test "python print(gdb.parse_and_eval('b')\[5\])" "102 'f'"
 
 # Test inferior function calls of methods.
 gdb_test "py print(b_obj\['static_func'\]())" "1111"
-- 
2.35.1


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

* Re: [PATCH] Enable call of overloaded subscript operator from python
  2024-05-30 19:28 ` [PATCH] Enable call of overloaded subscript operator from python Hannes Domani
@ 2024-06-03 15:02   ` Tom Tromey
  2024-06-03 15:24     ` Hannes Domani
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-06-03 15:02 UTC (permalink / raw)
  To: Hannes Domani; +Cc: gdb-patches

>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> If you try to use the overloaded subscript operator of a class
Hannes> in python, it fails like this:

Thanks for the patch.

Hannes> +	  if (idx != NULL
Hannes> +	      && binop_user_defined_p (BINOP_SUBSCRIPT, tmp, idx))
Hannes> +	    {
Hannes> +	      res_val = value_x_binop (tmp, idx, BINOP_SUBSCRIPT,
Hannes> +				       OP_NULL, EVAL_NORMAL);
Hannes> +	    }

The braces aren't needed here.

This is ok with this fixed up.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

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

* Re: [PATCH] Enable call of overloaded subscript operator from python
  2024-06-03 15:02   ` Tom Tromey
@ 2024-06-03 15:24     ` Hannes Domani
  0 siblings, 0 replies; 3+ messages in thread
From: Hannes Domani @ 2024-06-03 15:24 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

 Am Montag, 3. Juni 2024 um 17:02:49 MESZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> If you try to use the overloaded subscript operator of a class
> Hannes> in python, it fails like this:
>
> Thanks for the patch.
>
> Hannes> +      if (idx != NULL
> Hannes> +          && binop_user_defined_p (BINOP_SUBSCRIPT, tmp, idx))
> Hannes> +        {
> Hannes> +          res_val = value_x_binop (tmp, idx, BINOP_SUBSCRIPT,
> Hannes> +                      OP_NULL, EVAL_NORMAL);
> Hannes> +        }
>
> The braces aren't needed here.
>
> This is ok with this fixed up.
>
> Approved-By: Tom Tromey <tom@tromey.com>

Pushed with this change, thanks.


Hannes

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

end of thread, other threads:[~2024-06-03 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240530192837.4530-1-ssbssa.ref@yahoo.de>
2024-05-30 19:28 ` [PATCH] Enable call of overloaded subscript operator from python Hannes Domani
2024-06-03 15:02   ` Tom Tromey
2024-06-03 15:24     ` Hannes Domani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).