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] A case for "mixed" pretty-printers


Greetings,

I have a use-case for mixing Python pretty printers with "native"
ones. I don't know whether this has been considered before.

Consider:

--- nested.cc ---
#include <list>
#include <vector>

struct Foo
{
  Foo (const char *);
  const void *a; // imagine complicated internal state
  const char *to_string () const; // Pretty printer
};

int main(int argc, char *argv[])
{
  std::vector<Foo> foo_vector;
  std::list<Foo> foo_list;

  for (int i = 0; i < argc; ++i)
    {
      Foo foo(argv[i]);
      foo_vector.push_back(foo);
      foo_list.push_back(foo);
    }
  // force instantiation of operator[]
  Foo &foo = foo_vector[0];

  return 0; // break here
}


////// elsewhere in the code ////
Foo::Foo(const char *s) : a(s) { }
const char* Foo::to_string() const { return static_cast<const char *>(a); }
--- nested.cc ---


Imagine that the internal state of Foo is somewhat complicated,
and that to_string() method takes significant C++ code to implement
(not something easily re-coded in Python) and already exists.

Currently, I can somewhat easily print individual vector elements:

  (gdb) run fee foe fi fum

  Breakpoint 1, main (argc=5, argv=0x7fffffffe738) at nested.cc:25
  25        return 0; // break here

  (gdb) p foo_vector[1].to_string()
  $1 = 0x7fffffffea37 "fee"

This is considerably harder to do for list elements:

  (gdb) p (('std::_List_node<Foo>' *)foo_list._M_impl._M_node._M_next._M_next)._M_data.to_string()
  $2 = 0x7fffffffea37 "fee"

I can also print the list and vector using Tom's Python pretty printers:

  (gdb) p foo_vector
  $3 = std::vector of length 5, capacity 8
       [0] = {
	 a = 0x7fffffffea16
       }
       [1] = {
	 a = 0x7fffffffea37
       }
       [2] = {
	 a = 0x7fffffffea3b
       }
       [3] = {
	 a = 0x7fffffffea3f
       }
       [4] = {
	 a = 0x7fffffffea42
       }
  (gdb) p foo_list  
  $4 = std::list
       [0] = {
	 a = 0x7fffffffea16
       }
       [1] = {
	 a = 0x7fffffffea37
       }
       [2] = {
	 a = 0x7fffffffea3b
       }
       [3] = {
	 a = 0x7fffffffea3f
       }
       [4] = {
	 a = 0x7fffffffea42
       }

But what I really want is a mixture, in which Python pretty
printer calls the application one [1]:

  (gdb) p foo_vector
  $3 = std::vector of length 5, capacity 8
       [0] = "./nested"
       [1] = "fee"
       [2] = "foe"
       [3] = "fi"
       [4] = "fum"
  (gdb) p foo_list  
  $4 = std::list
       [0] = "./nested"
       [1] = "fee"
       [2] = "foe"
       [3] = "fi"
       [4] = "fum"

How difficult would this be to achieve?

Thanks,

[1] I do understand that this will only work for "live" application
debugging.

--
Paul Pluzhnikov


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