From 9de2cc8410223fea80f434cd6fdffc61836f49f6 Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Tue, 11 Feb 2020 09:19:53 -0800 Subject: [PATCH] libdrgn/python: make Object.__index__() TypeError message clearer Currently, we print: >>> prog.symbol(prog['init_task']) Traceback (most recent call last): File "", line 1, in 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 --- libdrgn/python/object.c | 2 +- tests/test_object.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libdrgn/python/object.c b/libdrgn/python/object.c index cb578d3c..43587d3d 100644 --- a/libdrgn/python/object.c +++ b/libdrgn/python/object.c @@ -1264,7 +1264,7 @@ static PyObject *DrgnObject_index(DrgnObject *self) underlying_type = drgn_underlying_type(self->obj.type); if (!drgn_type_is_integer(underlying_type) && 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)); } diff --git a/tests/test_object.py b/tests/test_object.py index 417adc11..5034af40 100644 --- a/tests/test_object.py +++ b/tests/test_object.py @@ -701,13 +701,13 @@ class TestConversions(ObjectTestCase): self.assertRaisesRegex( TypeError, - "cannot convert 'double' to index", + "'double' object cannot be interpreted as an integer", operator.index, Object(self.prog, "double", value=9.99), ) self.assertRaisesRegex( TypeError, - r"cannot convert 'int \[\]' to index", + r"'int \[\]' object cannot be interpreted as an integer", operator.index, Object(self.prog, "int []", address=0), )