我有一个方便的类,它允许我轻松地向GDB漂亮的打印机添加一组“summariser”函数(例如,Rect
类可以有一个由Python计算的[Area
]字段)。然后它还会打印所有现有的子对象,这样您就可以一次看到所有内容。在
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])
对于返回数值的摘要器来说,这非常有效,但是对于字符串,它最终显示为一个数组,因此
^{pr2}$这大概是因为字符串来自
name, retVal = self.summaries[self.count](self.obj)
当SummaryAndFieldIterator
的__next__
方法返回时,不会生成足够“stringy”gdb.Value
对象。调整MyObjectPrinter
的display_hint()
方法似乎没有任何效果(但我怀疑它会有任何效果,因为这是子对象,而不是对象)。在
有人知道如何从children()
迭代器返回一个字符串并使其显示为字符串吗?在
很显然,这可能是一个与GDB/MI与漂亮的打印机通信方式有关的bug,Bugzilla在这里创建了:https://sourceware.org/bugzilla/show_bug.cgi?id=18282
相关问题 更多 >
编程相关推荐