17 """Utilities for working with pretty-printers."""
24 if sys.version_info[0] > 2:
30 """A basic pretty-printer.
33 name: A unique string among all printers for the context in which
34 it is defined (objfile, progspace, or global(gdb)), and should
35 meaningfully describe what can be pretty-printed.
36 E.g., "StringPiece" or "protobufs".
37 subprinters: An iterable object with each element having a `name'
38 attribute, and, potentially, "enabled" attribute.
39 Or this is None if there are no subprinters.
40 enabled: A boolean indicating if the printer is enabled.
42 Subprinters are for situations where "one" pretty-printer is actually a
43 collection of several printers. E.g., The libstdc++ pretty-printer has
44 a pretty-printer for each of several different types, based on regexps.
58 raise NotImplementedError(
"PrettyPrinter __call__")
62 """Baseclass for sub-pretty-printers.
64 Sub-pretty-printers needn't use this, but it formalizes what's needed.
67 name: The name of the subprinter.
68 enabled: A boolean indicating if the subprinter is enabled.
77 """Register pretty-printer PRINTER with OBJ.
79 The printer is added to the front of the search list, thus one can override
80 an existing printer if one needs to. Use a different name when overriding
81 an existing printer, otherwise an exception will be raised; multiple
82 printers with the same name are disallowed.
85 obj: Either an objfile, progspace, or None (in which case the printer
86 is registered globally).
87 printer: Either a function of one argument (old way) or any object
88 which has attributes: name, enabled, __call__.
89 replace: If True replace any existing copy of the printer.
90 Otherwise if the printer already exists raise an exception.
96 TypeError: A problem with the type of the printer.
97 ValueError: The printer's name contains a semicolon ";".
98 RuntimeError: A printer with the same name is already registered.
100 If the caller wants the printer to be listable and disableable, it must
101 follow the PrettyPrinter API. This applies to the old way (functions) too.
102 If printer is an object, __call__ is a method of two arguments:
103 self, and the value to be pretty-printed. See PrettyPrinter.
110 if not hasattr(printer,
"__name__")
and not hasattr(printer,
"name"):
111 raise TypeError(
"printer missing attribute: name")
112 if hasattr(printer,
"name")
and not hasattr(printer,
"enabled"):
113 raise TypeError(
"printer missing attribute: enabled")
114 if not hasattr(printer,
"__call__"):
115 raise TypeError(
"printer missing attribute: __call__")
117 if hasattr(printer,
"name"):
120 name = printer.__name__
121 if obj
is None or obj
is gdb:
122 if gdb.parameter(
"verbose"):
123 gdb.write(
"Registering global %s pretty-printer ...\n" % name)
126 if gdb.parameter(
"verbose"):
127 gdb.write(
"Registering %s pretty-printer for %s ...\n" % (
132 if hasattr(printer,
"name"):
133 if not isinstance(printer.name, basestring):
134 raise TypeError(
"printer name is not a string")
138 if printer.name.find(
";") >= 0:
139 raise ValueError(
"semicolon ';' in printer name")
145 for p
in obj.pretty_printers:
146 if hasattr(p,
"name")
and p.name == printer.name:
148 del obj.pretty_printers[i]
151 raise RuntimeError(
"pretty-printer already registered: %s" %
155 obj.pretty_printers.insert(0, printer)
159 """Class for implementing a collection of regular-expression based pretty-printers.
163 pretty_printer = RegexpCollectionPrettyPrinter("my_library")
164 pretty_printer.add_printer("myclass1", "^myclass1$", MyClass1Printer)
166 pretty_printer.add_printer("myclassN", "^myclassN$", MyClassNPrinter)
167 register_pretty_printer(obj, pretty_printer)
178 super(RegexpCollectionPrettyPrinter, self).
__init__(name, [])
181 """Add a printer to the list.
183 The printer is added to the end of the list.
186 name: The name of the subprinter.
187 regexp: The regular expression, as a string.
188 gen_printer: A function/method that given a value returns an
189 object to pretty-print it.
204 """Lookup the pretty-printer for the provided value."""
209 typename = val.type.name
217 if printer.enabled
and printer.compiled_re.search(typename):
218 return printer.gen_printer(val)
236 flag_list.append(e_name)
239 if not any_found
or v != 0:
241 flag_list.append(
'<unknown: 0x%x>' % v)
242 return "0x%x [%s]" % (self.
val,
" | ".join(flag_list))
245 """A pretty-printer which can be used to print a flag-style enumeration.
246 A flag-style enumeration is one where the enumerators are or'd
247 together to create values. The new printer will print these
248 symbolically using '|' notation. The printer must be registered
249 manually. This printer is most useful when an enum is flag-like,
250 but has some overlap. GDB's built-in printing will not handle
251 this case, but this printer will attempt to."""
254 super(FlagEnumerationPrinter, self).
__init__(enum_type)
260 flags = gdb.lookup_type(self.
name)
262 for field
in flags.fields():
263 self.enumerators.append((field.name, field.enumval))
266 self.enumerators.sort(key =
lambda x: x.enumval)
285 _builtin_pretty_printers.add_printer(name, regexp, printer)
def get_basic_type(type_)
def __init__(self, name, regexp, gen_printer)
def __init__(self, enum_type)
def add_printer(self, name, regexp, gen_printer)
def add_builtin_pretty_printer(name, regexp, printer)
def register_pretty_printer
def __init__(self, enumerators, val)