Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Andrew Oakley <andrew@ado.is-a-geek.net>
To: <gdb@sourceware.org>
Subject: Simpler pretty printing API (gdb.printing helpers)
Date: Thu, 25 Aug 2011 22:25:00 -0000	[thread overview]
Message-ID: <20110825232515.1707ea1a@ado-gentoo> (raw)

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


             reply	other threads:[~2011-08-25 22:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-25 22:25 Andrew Oakley [this message]
2011-08-30 20:23 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110825232515.1707ea1a@ado-gentoo \
    --to=andrew@ado.is-a-geek.net \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox