This is the mail archive of the archer@sourceware.org mailing list for the Archer 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]

[python] remove 'header' method


This patch removes the 'header' method from pretty-printer objects.

I think this method was a mistake.  Its functionality is implicit in
'to_string'.  Now the printing approach is simpler:

* For the CLI, if to_string exists, call it first.
  Then, if children exists, call it.
  Concatenate all the results and print.

* For MI, to_string returns the immediate value of the varobj, and
  children returns the children of the varobj.

Tom

b/gdb/ChangeLog:
2008-10-22  Tom Tromey  <tromey@redhat.com>

	* python/python.c (_initialize_python) <_format_children>: Call
	to_string, not header.
	(pretty_print_one_value): Call _format_children if either
	'children' to 'to_string' exists.

b/gdb/doc/ChangeLog:
2008-10-22  Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Pretty Printing): Remove 'header'.

b/gdb/testsuite/ChangeLog:
2008-10-22  Tom Tromey  <tromey@redhat.com>

	* gdb.python/python-prettyprint.py (ContainerPrinter.to_string):
	Rename from 'header'.

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index edbf51e..83c6280 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -17951,17 +17951,6 @@ using Python code.  This mechanism works for both MI and the CLI.
 A pretty-printer is an object that implements a specific interface.
 There is no predefined base class for pretty-printers.
 
-@c FIXME I think perhaps we should nuke header and just use to_string
-@c Investigate.
-
-@defop Operation {pretty printer} header (self, val)
-If this method exists, @value{GDBN} may call it when printing a value
-from the CLI.  In particular, it will be called if the pretty-printer
-does not define the @code{to_string} method.  The method must return a
-string.  This string will be displayed before any children of the
-value being printed.
-@end defop
-
 @defop Operation {pretty printer} children (self, val)
 This method is used by both the MI and CLI code.  When printing a
 value, @value{GDBN} will call this method to display the children of
@@ -17992,13 +17981,11 @@ This method is used by both the MI and CLI code.  When printing a
 value, @value{GDBN} will call this method to display the string
 representation of @var{val}, an instance of @code{gdb.Value}.
 
-This method must return a string.
-
-@c FIXME -- this is where the oddity arises.  Don't we do something
-@c different for MI?
 When printing from the CLI, if the @code{to_string} method exists,
-then @value{GDBN} will print its result, and will not call
-@code{header} and @code{children}.
+then @value{GDBN} will prepend its result to the values returned by
+@code{children}.
+
+This method must return a string.
 @end defop
 
 @subsubsection Selecting CLI Pretty-Printers
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 55de351..3541712 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -687,21 +687,24 @@ pretty_print_one_value (PyObject *func, struct value *value,
 
       val_obj = value_to_value_object (value);
 
-      /* The function might be an MI-style class with a to_string
-	 method, or it might be an ordinary function.  FIXME: should
-	 also check for the 'children' method here.  */
-      if (PyObject_HasAttr (func, gdbpy_to_string_cst))
-	result = PyObject_CallMethodObjArgs (func, gdbpy_to_string_cst,
-					     val_obj, NULL);
-      else if (children
-	       && PyObject_HasAttr (func, gdbpy_children_cst)
-	       && PyObject_HasAttrString (gdb_module, "_format_children"))
+      /* The function might be an MI-style class, or it might be an
+	 ordinary function.  If CHILDREN is true, the existence of
+	 either the to_string or children methods means to call
+	 _format_children.  Otherwise, if we have to_string, call it.
+	 As a last resort, call the object as a function.  */
+      if (children
+	  && (PyObject_HasAttr (func, gdbpy_children_cst)
+	      || PyObject_HasAttr (func, gdbpy_to_string_cst))
+	  && PyObject_HasAttrString (gdb_module, "_format_children"))
 	{
 	  PyObject *fmt = PyObject_GetAttrString (gdb_module,
 						  "_format_children");
 	  result = PyObject_CallFunctionObjArgs (fmt, func, val_obj, NULL);
 	  Py_DECREF (fmt);
 	}
+      else if (PyObject_HasAttr (func, gdbpy_to_string_cst))
+	result = PyObject_CallMethodObjArgs (func, gdbpy_to_string_cst,
+					     val_obj, NULL);
       else
 	result = PyObject_CallFunctionObjArgs (func, val_obj, NULL);
       if (result)
@@ -1047,8 +1050,8 @@ sys.stdout = GdbOutputFile()\n\
   PyRun_SimpleString ("\
 def _format_children(obj, val):\n\
   result = []\n\
-  if hasattr(obj, 'header'):\n\
-    result.append(obj.header(val))\n\
+  if hasattr(obj, 'to_string'):\n\
+    result.append(obj.to_string(val))\n\
   max = gdb.get_parameter('print elements')\n\
   i = 0\n\
   for elt in obj.children(val):\n\
diff --git a/gdb/testsuite/gdb.python/python-prettyprint.py b/gdb/testsuite/gdb.python/python-prettyprint.py
index 2c30ef3..abb46d7 100644
--- a/gdb/testsuite/gdb.python/python-prettyprint.py
+++ b/gdb/testsuite/gdb.python/python-prettyprint.py
@@ -38,7 +38,7 @@ class ContainerPrinter:
             self.pointer = self.pointer + 1
             return ('[%d]' % int (result - self.start), result.dereference())
 
-    def header(self, val):
+    def to_string(self, val):
         return 'container %s with %d elements' % (val['name'], val['len'])
 
     def children(self, val):


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