This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Simpler pretty printing API (gdb.printing helpers)
- From: Andrew Oakley <andrew at ado dot is-a-geek dot net>
- To: <gdb at sourceware dot org>
- Date: Thu, 25 Aug 2011 23:25:15 +0100
- Subject: Simpler pretty printing API (gdb.printing helpers)
I've got a couple of bits of code that I thought might be useful to add
to gdb.printing. If there is agreement I can tidy up a bit for
submission.
I found myself writing lots of pretty printers that only wanted to
accept one type of object, so I wrote a little function to register
them:
def register_pretty_printer(type_code, type_tag):
"""Register the pretty printer class for types with the given
code and tag."""
def decorator(printer):
def pretty_printer(val):
type = gdb.types.get_basic_type(val.type)
if type.code == type_code and type.tag == type_tag:
return printer(val)
else:
return None
gdb.pretty_printers.append(pretty_printer)
return printer
return decorator
This can be used as a decorator like this:
@register_pretty_printer(gdb.TYPE_CODE_STRUCT, 'type_name')
class TypeNamePrinter:
def __init__(self, val):
....
def children(self):
....
def to_string(self):
...
I then found myself wanting to write a few really simple pretty
printers that just had a to_string and nothing else so I added this:
def register_simple_pretty_printer(type_code, type_tag):
"""Register the pretty printer to_string function for types with
the given code and tag."""
def decorator(func):
@register_pretty_printer(type_code, type_tag)
class PrettyPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return func(self.val)
return func
return decorator
Known problems (will fix if this is wanted):
* the name register_pretty_printer is already taken
* can only register globally
* should probably inherit from classes in gdb.printing
* want non-decorator versions
* documentation strings could be better
--
Andrew Oakley