Python 如何将新的漂亮打印机添加到现有的打印机定义列表中?

Python 如何将新的漂亮打印机添加到现有的打印机定义列表中?,python,gdb,pretty-print,Python,Gdb,Pretty Print,我想通过更新std::prettyprinters或boost::prettyprinters的现有printers.py文件来添加新的prettyprinters。 它们是使用以下链接进行适当设置的: 我也阅读了他们的教程来添加新的打印机,但不知何故未能获得很好的理解。也有机会研究类似的线索: 和 如果知道如何添加在myprinters.py文件中注册的上述boost::unorder_映射,那就太好了。我对boost目录中的printers.py文件做了以下修改 @_register_p

我想通过更新std::prettyprinters或boost::prettyprinters的现有printers.py文件来添加新的prettyprinters。 它们是使用以下链接进行适当设置的:

我也阅读了他们的教程来添加新的打印机,但不知何故未能获得很好的理解。也有机会研究类似的线索: 和

如果知道如何添加在myprinters.py文件中注册的上述boost::unorder_映射,那就太好了。我对boost目录中的printers.py文件做了以下修改

@_register_printer
class BoostUnorderedMapPrinter:
"Pretty printer for a boost::unordered_map"
printer_name = 'boost::unordered_map'
version = '1.40'
type_name_re = '^boost::unordered_map$'

class _iterator:
    def __init__ (self, fields):
        type_1 = fields.val.type.template_argument(0)
        type_2 = fields.val.type.template_argument(1)
        self.buckets = fields.val['table_']['buckets_']
        self.bucket_count = fields.val['table_']['bucket_count_']
        self.current_bucket = 0
        pair = "std::pair<%s const, %s>" % (type_1, type_2)
        self.pair_pointer = gdb.lookup_type(pair).pointer()
        self.base_pointer = gdb.lookup_type("boost::unordered_detail::value_base< %s >" % pair).pointer()
        self.node_pointer = gdb.lookup_type("boost::unordered_detail::hash_node<std::allocator< %s >, boost::unordered_detail::ungrouped>" % pair).pointer()
        self.node = self.buckets[self.current_bucket]['next_']

    def __iter__(self):
        return self

    def next(self):
        while not self.node:
            self.current_bucket = self.current_bucket + 1
            if self.current_bucket >= self.bucket_count:
                raise StopIteration
            self.node = self.buckets[self.current_bucket]['next_']

        iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()   
        self.node = self.node['next_']

        return ('%s' % iterator['first'], iterator['second'])

def __init__(self, val):
    self.val = val

def children(self):
    return self._iterator(self)

def to_string(self):
    return "boost::unordered_map"
不知何故,它似乎无法识别这个类


提前感谢

github上的boost printing代码使用一个装饰器来说明应该注册给定的打印机,然后类中的一些字段来控制注册:

@_register_printer
class BoostIteratorRange:
...
printer_name = 'boost::iterator_range'
version = '1.40'
type_name_re = '^boost::iterator_range<.*>$'
因此,我想您可以将这些添加到另一篇So文章中给出的示例代码中,以进行设置


或者,您也可以自己手工编写注册码。

可能不是最好的方法,但对于我来说,它适用于快速调试会话:

import gdb
import re

def lookup_type(val)
   resolved_type = str(val.type.unqualified().strip_typedefs())
   if (re.search("^boost::unordered_map>.*>$", resolved_type):
      return BoostUnorderedMapPrinter()

gdb.pretty_printers.append (lookup_type)


您需要围绕boost库创建一个python包装器,可能使用cython。不,这与在gdb中运行的python代码有关。boost不需要python包装器,也不需要cython包装器。我对boost目录中的printers.py文件进行了更改,如上面编辑的部分所示,但我的无序映射仍然无法打印可理解的结果。我做错什么了吗?