This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFC/Patch] Call overloaded operators to perform valid Python operations on struct/class values.
- From: Siva Chandra <sivachandra at google dot com>
- To: Doug Evans <xdje42 at gmail dot com>
- Cc: Tom Tromey <tromey at redhat dot com>, gdb-patches <gdb-patches at sourceware dot org>
- Date: Wed, 18 Dec 2013 15:15:13 -0800
- Subject: Re: [RFC/Patch] Call overloaded operators to perform valid Python operations on struct/class values.
- Authentication-results: sourceware.org; auth=none
- References: <CAGyQ6gxk9zcLKQ1Ef2XhgUMCyB8MB1v=6tA4jsfPyEFgxR8bNw at mail dot gmail dot com> <m31u1q1oq0 dot fsf at sspiff dot org> <871u1j2ldi dot fsf at fleche dot redhat dot com> <CAP9bCMSPpz8px2ZFvRroHcNP8PWWQV39P7GmXCmj+fdSR7NT2Q at mail dot gmail dot com> <CAGyQ6gzYE_NthBxc17VPxjgtYSqTW3HGhPVp8qvk=Nye27G8hg at mail dot gmail dot com> <CAP9bCMTthY1oJN-res0Cegd_YrMqW77ZehMjnzK+Y9681Of5bQ at mail dot gmail dot com>
On Wed, Dec 18, 2013 at 8:37 AM, Doug Evans <xdje42@gmail.com> wrote:
> Not that this could work, but for illustration's sake:
> smart_ptr = gdb.parse_and_eval ("my_smart_ptr");
> dereferenced_smart_ptr = gdb.parse_and_eval("*%V" % smart_ptr)
Should work. May be the '%' syntax would not (I am not sure). But, we
could consider other alternatives [Replace occurrences of $1, $2, etc
for example].
Is this an exhaustive solution or a cool fallback option when no other
Pythonic way works? Consider something more complicated but uses only
valid Python operators:
C++:
obj1.method1(obj2_ptr->method2(obj3 + obj4)) * obj5
This could be done in a Pythonic way as:
obj1.invoke('method1', obj2_ptr.invoke('method2', obj3 + obj4)) * obj5
If we do this in a string replacement way:
gdb.parse_and_eval('$1.method1($2->method2($3 + $4)) * $5',
obj1, obj2_ptr, obj3, obj4, obj5)
Above, the values objN in Python are equivalents of their C++ values.
Also, I have cooked up the 'invoke' method on gdb.Value objects but we
could have one for real. Lets see how your example would look like
with this 'invoke' in place:
C++:
*my_smart_ptr;
Python:
smart_ptr.invoke('operator*')
Or, one could have a debug method named "deref" (assuming debug
methods would soon be a reality :-) to be an equivalent of
"operator*". The Python would then look like this:
smart_ptr.invoke('deref')
[Side topic: Should smart_ptr.dereference() invoke 'operator*'? The
documentation says this: "... behavior of Value.dereference is
identical to applying the C unary operator * on a given value." And, I
remember writing this myself :-)]