libdrgn/python: make Object.__index__() TypeError message clearer

Currently, we print:

>>> prog.symbol(prog['init_task'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: cannot convert 'struct task_struct' to index

It's not obvious what it means to convert to an index. Instead, let's
use the error message raised by operator.index():

TypeError: 'struct task_struct' object cannot be interpreted as an integer
This commit is contained in:
Omar Sandoval 2020-02-11 09:19:53 -08:00
parent 4adc691622
commit 9de2cc8410
2 changed files with 3 additions and 3 deletions

View File

@ -1264,7 +1264,7 @@ static PyObject *DrgnObject_index(DrgnObject *self)
underlying_type = drgn_underlying_type(self->obj.type); underlying_type = drgn_underlying_type(self->obj.type);
if (!drgn_type_is_integer(underlying_type) && if (!drgn_type_is_integer(underlying_type) &&
drgn_type_kind(underlying_type) != DRGN_TYPE_POINTER) { drgn_type_kind(underlying_type) != DRGN_TYPE_POINTER) {
return set_error_type_name("cannot convert '%s' to index", return set_error_type_name("'%s' object cannot be interpreted as an integer",
drgn_object_qualified_type(&self->obj)); drgn_object_qualified_type(&self->obj));
} }

View File

@ -701,13 +701,13 @@ class TestConversions(ObjectTestCase):
self.assertRaisesRegex( self.assertRaisesRegex(
TypeError, TypeError,
"cannot convert 'double' to index", "'double' object cannot be interpreted as an integer",
operator.index, operator.index,
Object(self.prog, "double", value=9.99), Object(self.prog, "double", value=9.99),
) )
self.assertRaisesRegex( self.assertRaisesRegex(
TypeError, TypeError,
r"cannot convert 'int \[\]' to index", r"'int \[\]' object cannot be interpreted as an integer",
operator.index, operator.index,
Object(self.prog, "int []", address=0), Object(self.prog, "int []", address=0),
) )