我有一个方便的类,它允许我轻松地将一组“summariser”函数添加到 GDB pretty-print (例如,一个 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)
当SummaryAndFieldIterator
的__next__
方法返回时,
不会生成足够“字符串”的gdb.Value
对象。调整 MyObjectPrinter
的 display_hint()
方法似乎没有任何效果(但我怀疑它会不会,因为这是 child ,而不是对象)。
有人知道如何从 children()
迭代器返回一个字符串并将其显示为字符串吗?
最佳答案
好吧,显然这可能是一个与 GDB/MI 与 pretty-print 通信方式相关的错误,Bugzilla 在此处创建:https://sourceware.org/bugzilla/show_bug.cgi?id=18282
关于python - GDB pretty-print : returning string from a children()'s iterator, 但显示为 char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26472066/