From 1fa57852d6d79171d8246736a2f8a3679c0b291f Mon Sep 17 00:00:00 2001 From: Doug Evans Date: Fri, 7 Oct 2011 22:46:15 +0000 Subject: [PATCH] * python/lib/gdb/printing.py (register_pretty_printer): New argument `replace'. testsuite/ * gdb.python/py-pp-maint.py: Add tests for `replace' arg. --- gdb/ChangeLog | 3 +++ gdb/python/lib/gdb/printing.py | 18 +++++++++++++----- gdb/testsuite/ChangeLog | 4 ++++ gdb/testsuite/gdb.python/py-pp-maint.py | 14 +++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0a0a57f3f06..16f654b4074 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,8 @@ 2011-10-07 Doug Evans + * python/lib/gdb/printing.py (register_pretty_printer): New argument + `replace'. + * python/lib/gdb/printing.py: Whitespace cleanup. * python/py-value.c (valpy_call): Initialize ftype to avoid compiler diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py index 46ffccab43d..a0308276af2 100644 --- a/gdb/python/lib/gdb/printing.py +++ b/gdb/python/lib/gdb/printing.py @@ -68,7 +68,7 @@ class SubPrettyPrinter(object): self.enabled = True -def register_pretty_printer(obj, printer): +def register_pretty_printer(obj, printer, replace=False): """Register pretty-printer PRINTER with OBJ. The printer is added to the front of the search list, thus one can override @@ -81,6 +81,8 @@ def register_pretty_printer(obj, printer): is registered globally). printer: Either a function of one argument (old way) or any object which has attributes: name, enabled, __call__. + replace: If True replace any existing copy of the printer. + Otherwise if the printer already exists raise an exception. Returns: Nothing. @@ -128,10 +130,16 @@ def register_pretty_printer(obj, printer): # Alas, we can't do the same for functions and __name__, they could # all have a canonical name like "lookup_function". # PERF: gdb records printers in a list, making this inefficient. - if (printer.name in - [p.name for p in obj.pretty_printers if hasattr(p, "name")]): - raise RuntimeError("pretty-printer already registered: %s" % - printer.name) + i = 0 + for p in obj.pretty_printers: + if hasattr(p, "name") and p.name == printer.name: + if replace: + del obj.pretty_printers[i] + break + else: + raise RuntimeError("pretty-printer already registered: %s" % + printer.name) + i = i + 1 obj.pretty_printers.insert(0, printer) diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 78599ade65d..89ba7cb07e5 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2011-10-07 Doug Evans + + * gdb.python/py-pp-maint.py: Add tests for `replace' arg. + 2011-10-07 Kevin Pouget Allow Python notification of new object-file loadings. diff --git a/gdb/testsuite/gdb.python/py-pp-maint.py b/gdb/testsuite/gdb.python/py-pp-maint.py index 779b1a90f66..c35d1dcce1e 100644 --- a/gdb/testsuite/gdb.python/py-pp-maint.py +++ b/gdb/testsuite/gdb.python/py-pp-maint.py @@ -71,4 +71,16 @@ def build_pretty_printer(): gdb.printing.register_pretty_printer(gdb, lookup_function_lookup_test) -gdb.printing.register_pretty_printer(gdb, build_pretty_printer()) +my_pretty_printer = build_pretty_printer() +gdb.printing.register_pretty_printer(gdb, my_pretty_printer) + +# Exercise the "replace" argument to register pretty_printer. +saw_runtime_error = False +try: + gdb.printing.register_pretty_printer(gdb, my_pretty_printer, replace=False) +except RuntimeError: + saw_runtime_error = True + pass +if not saw_runtime_error: + raise RuntimeError("Missing RuntimeError from register_pretty_printer") +gdb.printing.register_pretty_printer(gdb, my_pretty_printer, replace=True) -- 2.30.2