Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* Simpler pretty printing API (gdb.printing helpers)
@ 2011-08-25 22:25 Andrew Oakley
  2011-08-30 20:23 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Oakley @ 2011-08-25 22:25 UTC (permalink / raw)
  To: gdb

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Simpler pretty printing API (gdb.printing helpers)
  2011-08-25 22:25 Simpler pretty printing API (gdb.printing helpers) Andrew Oakley
@ 2011-08-30 20:23 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2011-08-30 20:23 UTC (permalink / raw)
  To: Andrew Oakley; +Cc: gdb

>>>>> "Andrew" == Andrew Oakley <andrew@ado.is-a-geek.net> writes:

Andrew> I've got a couple of bits of code that I thought might be useful to add
Andrew> to gdb.printing.  If there is agreement I can tidy up a bit for
Andrew> submission.

Andrew> I found myself writing lots of pretty printers that only wanted to
Andrew> accept one type of object, so I wrote a little function to register
Andrew> them:
[...]

Yeah, I think this sort of thing would be handy in gdb.printing.

Tom


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-08-30 20:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-25 22:25 Simpler pretty printing API (gdb.printing helpers) Andrew Oakley
2011-08-30 20:23 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox