This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[commit] (register_pretty_printer): New argument `replace'.


Hi.

fyi, I've committed this.
It's a simple addition to the register_pretty_printer API to
let one replace any existing version of the printer.

2011-10-07  Doug Evans  <dje@google.com>

	* python/lib/gdb/printing.py (register_pretty_printer): New argument
	`replace'.

	testsuite/
	* gdb.python/py-pp-maint.py: Add tests for `replace' arg.

Index: python/lib/gdb/printing.py
===================================================================
RCS file: /cvs/src/src/gdb/python/lib/gdb/printing.py,v
retrieving revision 1.5
diff -u -p -r1.5 printing.py
--- python/lib/gdb/printing.py	7 Oct 2011 22:25:57 -0000	1.5
+++ python/lib/gdb/printing.py	7 Oct 2011 22:32:18 -0000
@@ -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)
 
Index: testsuite/gdb.python/py-pp-maint.py
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-pp-maint.py,v
retrieving revision 1.2
diff -u -p -r1.2 py-pp-maint.py
--- testsuite/gdb.python/py-pp-maint.py	1 Jan 2011 15:33:49 -0000	1.2
+++ testsuite/gdb.python/py-pp-maint.py	7 Oct 2011 22:43:52 -0000
@@ -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)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]