我有一个方便的类,我可以使用它轻松地将一组“summariser”函数添加到 GDB 漂亮的打印机(例如,一个Rect
类可以有一个 [ Area
] 字段,由 Python 计算)。然后它也会打印所有现有的孩子,所以你可以一次看到所有东西。
class SummaryAndFieldIterator:
"""
Iterator to first go through a list of summariser functions,
then display all the fields in the object in order
"""
def __init__ (self, obj, summaries):
self.count = 0
self.obj = obj;
self.summaries = summaries;
self.keys = sorted(obj.type.iterkeys())
def __iter__(self):
return self
def __next__(self):
if (self.count >= len(self.keys) + len(self.summaries)):
raise StopIteration
elif self.count < len(self.summaries):
name, retVal = self.summaries[self.count](self.obj)
# FIXME: this doesn't seem to work when a string is returned
# in retVal?
result = "[%s]" % name, retVal
else:
field = self.count - len(self.summaries)
result = self.keys[field], self.obj[self.keys[field]]
self.count += 1
return result
next = __next__
class MyObjectPrinter:
def __init__(self, val):
self.val = val
def get_int(self):
return "meaning", 42
def get_string(self):
return "hoopiness", "Forty-two"
def children(self):
return SummaryAndFieldIterator(self.val, [self.get_string])
这对于返回数值的汇总器非常有效,但对于字符串,它最终显示为一个数组,所以我得到
NAME VALUE
myobj {..}
|-->[meaning] 42
|-->[hoopiness]
|-->[0] 'F'
|-->[1] 'o'
.....
|-->real_field 34234
这大概是因为来自的字符串
name, retVal = self.summaries[self.count](self.obj)
gdb.Value
当它由SummaryAndFieldIterator
's__next__
方法返回时,不会生成足够“字符串”的对象。调整的display_hint()
方法MyObjectPrinter
似乎没有任何效果(但我怀疑它会,因为这是孩子,而不是对象)。
任何人都知道如何从children()
迭代器返回字符串并将其显示为字符串?