From 2988a36005f2821cee6744473ad8a3ba7638c212 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Wed, 17 Nov 2021 11:55:37 +0000 Subject: gdb/python: Use tp_init instead of tp_new to setup gdb.Value The documentation suggests that we implement gdb.Value.__init__, however, this is not currently true, we really implement gdb.Value.__new__. This will cause confusion if a user tries to sub-class gdb.Value. They might write: class MyVal (gdb.Value): def __init__ (self, val): gdb.Value.__init__(self, val) obj = MyVal(123) print ("Got: %s" % obj) But, when they source this code they'll see: (gdb) source ~/tmp/value-test.py Traceback (most recent call last): File "/home/andrew/tmp/value-test.py", line 7, in obj = MyVal(123) File "/home/andrew/tmp/value-test.py", line 5, in __init__ gdb.Value.__init__(self, val) TypeError: object.__init__() takes exactly one argument (the instance to initialize) (gdb) The reason for this is that, as we don't implement __init__ for gdb.Value, Python ends up calling object.__init__ instead, which doesn't expect any arguments. The Python docs suggest that the reason why we might take this approach is because we want gdb.Value to be immutable: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_new But I don't see any reason why we should require gdb.Value to be immutable when other types defined in GDB are not. This current immutability can be seen in this code: obj = gdb.Value(1234) print("Got: %s" % obj) obj.__init__ (5678) print("Got: %s" % obj) Which currently runs without error, but prints: Got: 1234 Got: 1234 In this commit I propose that we switch to using __init__ to initialize gdb.Value objects. This does introduce some additional complexity, during the __init__ call a gdb.Value might already be associated with a gdb value object, in which case we need to cleanly break that association before installing the new gdb value object. However, the cost of doing this is not great, and the benefit - being able to easily sub-class gdb.Value seems worth it. After this commit the first example above works without error, while the second example now prints: Got: 1234 Got: 5678 In order to make it easier to override the gdb.Value.__init__ method, I have tweaked the definition of gdb.Value.__init__. The second, optional argument to __init__ is a gdb.Type, if this argument is not present then GDB figures out a suitable type. However, if we want to override the __init__ method in a sub-class, and still support the default argument, it is easier to write: class MyVal (gdb.Value): def __init__ (self, val, type=None): gdb.Value.__init__(self, val, type) Currently, passing None for the Type will result in an error: TypeError: type argument must be a gdb.Type. After this commit I now allow the type argument to be None, in which case GDB figures out a suitable type just as if the type had not been passed at all. Unless a user is trying to reinitialize a value, or create sub-classes of gdb.Value, there should be no user visible changes after this commit. --- gdb/doc/python.texi | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gdb/doc') diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4a66c11..568aabc 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -827,6 +827,9 @@ This second form of the @code{gdb.Value} constructor returns a from the Python buffer object specified by @var{val}. The number of bytes in the Python buffer object must be greater than or equal to the size of @var{type}. + +If @var{type} is @code{None} then this version of @code{__init__} +behaves as though @var{type} was not passed at all. @end defun @defun Value.cast (type) -- cgit v1.1