From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5148 invoked by alias); 25 Aug 2011 22:25:37 -0000 Received: (qmail 5104 invoked by uid 22791); 25 Aug 2011 22:25:35 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00 X-Spam-Check-By: sourceware.org Received: from adoakley.name (HELO ado.is-a-geek.net) (46.4.104.242) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 25 Aug 2011 22:25:16 +0000 Received: from ado-gentoo.moore.slainvet.net ([2001:8b0:393:0:223:54ff:fe39:b789] helo=ado-gentoo) by ado.is-a-geek.net with esmtpa (Exim 4.76) (envelope-from ) id 1QwiK2-0002Ag-A3 for gdb@sourceware.org; Thu, 25 Aug 2011 22:22:50 +0000 Date: Thu, 25 Aug 2011 22:25:00 -0000 From: Andrew Oakley To: Subject: Simpler pretty printing API (gdb.printing helpers) Message-ID: <20110825232515.1707ea1a@ado-gentoo> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2011-08/txt/msg00118.txt.bz2 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