GDB prettyprinting:从children()的迭代器返回字符串,但显示为char[]

2024-10-01 07:48:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个方便的类,它允许我轻松地向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对象。调整MyObjectPrinterdisplay_hint()方法似乎没有任何效果(但我怀疑它会有任何效果,因为这是子对象,而不是对象)。在

有人知道如何从children()迭代器返回一个字符串并使其显示为字符串吗?在


Tags: 对象字符串inselfobjlenreturndef