This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] A case for "mixed" pretty-printers
- From: ppluzhnikov at google dot com (Paul Pluzhnikov)
- To: archer at sourceware dot org
- Cc: ppluzhnikov at google dot com
- Date: Mon, 10 Nov 2008 10:17:40 -0800 (PST)
- Subject: [python] A case for "mixed" pretty-printers
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=google.com; s=beta;t=1226341065; bh=Rq1dXGgcVtcbBz8kG0rAf/acGHo=;h=DomainKey-Signature:To:Cc:Subject:Message-Id:Date:From; b=Jky8IHHzBuMUVv2MOFM1rNNuteo3BuOoehcbUD867hwLsAyXz0JmcrySEcpDCEQrjexBelBRenaj1nGK391SZA==
- Domainkey-signature: a=rsa-sha1; s=beta; d=google.com; c=nofws; q=dns;h=to:cc:subject:message-id:date:from;b=ZzL0BEq8S3pfGdeJqrshd3d9GIWRw18riH0fJabYlxSd9smd8UAluYeG+jGzexaSvIsZke5R3wWd3aouVBOKqQ==
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