* python/lib/gdb/printing.py (register_pretty_printer): New argument
authorDoug Evans <dje@google.com>
Fri, 7 Oct 2011 22:46:15 +0000 (22:46 +0000)
committerDoug Evans <dje@google.com>
Fri, 7 Oct 2011 22:46:15 +0000 (22:46 +0000)
`replace'.

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

gdb/ChangeLog
gdb/python/lib/gdb/printing.py
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-pp-maint.py

index 0a0a57f3f06a97eb628ff79d5614751064c9e569..16f654b4074f3811ab86ea1534670f9a1c3463e2 100644 (file)
@@ -1,5 +1,8 @@
 2011-10-07  Doug Evans  <dje@google.com>
 
+       * 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
index 46ffccab43da6a13307a2b820bc84a83601eaf3d..a0308276af26e05697355eb56ba48f41337f8777 100644 (file)
@@ -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 78599ade65dbc842a7cf2f2d13e73e32ef02940d..89ba7cb07e574f9e959e1c7b31291ef032306d90 100644 (file)
@@ -1,3 +1,7 @@
+2011-10-07  Doug Evans  <dje@google.com>
+
+       * gdb.python/py-pp-maint.py: Add tests for `replace' arg.
+
 2011-10-07  Kevin Pouget  <kevin.pouget@st.com>
 
        Allow Python notification of new object-file loadings.
index 779b1a90f66334027769d1af150d639f3b2060b5..c35d1dcce1e546ee943b4190a48f625b49077866 100644 (file)
@@ -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)