public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
* [python] Pretty-printers and addressprint
@ 2009-11-10  2:12 Paul Pluzhnikov
  2009-11-10  7:25 ` Elmenthaler, Jens
  2009-11-10 15:12 ` Tom Tromey
  0 siblings, 2 replies; 11+ messages in thread
From: Paul Pluzhnikov @ 2009-11-10  2:12 UTC (permalink / raw)
  To: gdb, archer; +Cc: dje, ppluzhnikov

Greetings,

Consider the gdb.python/py-prettyprint.exp test case.
It has:

typedef struct string_repr
{
  struct whybother
  {
    const char *contents;
  } whybother;
} string;

and a prettyprinter for it:

# Test returning a Value from a printer.
class string_print:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val['whybother']['contents']


Which currently produces:

$4 = 0x4007e0 "this is x"^M


The issue I am having is there is no apparent way to get rid of the
address from python side (address is not printed when the printer
returns a python string instead of a value), whereas if the
printer really wants to print the address, it can trivally add
it back by returning appropriate python string.

Printing addresses inside of a container seems to be especially
"not wanted".

Should the decision to print addresses be deferred to the
pretty-printer? Is the patch below reasonable?

Thanks,
--
Paul Pluzhnikov

2009-11-09  Paul Pluzhnikov  <ppluzhnikov@google.com>

	* gdb/python/py-prettyprint.c (print_string_repr): Don't
	print value address.



--- gdb/python/py-prettyprint.c#1    2009-11-09 17:58:39.000000000 -0800
+++ gdb/python/py-prettyprint.c     2009-11-09 16:51:16.862840000 -0800
@@ -209,7 +209,12 @@ print_string_repr (PyObject *printer, co
       Py_DECREF (py_str);
     }
   else if (replacement)
-    common_val_print (replacement, stream, recurse, options, language);
+    {
+      struct value_print_options opts = *options;
+
+      opts.addressprint = 0;
+      common_val_print (replacement, stream, recurse, &opts, language);
+    }
   else
     gdbpy_print_stack ();
 }

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [python] Pretty-printers and addressprint
  2009-11-10  2:12 [python] Pretty-printers and addressprint Paul Pluzhnikov
@ 2009-11-10  7:25 ` Elmenthaler, Jens
  2009-11-10  8:06   ` Paul Pluzhnikov
  2009-11-10 15:12 ` Tom Tromey
  1 sibling, 1 reply; 11+ messages in thread
From: Elmenthaler, Jens @ 2009-11-10  7:25 UTC (permalink / raw)
  To: Paul Pluzhnikov, gdb, archer; +Cc: dje

Hi Paul,

The gdb.Value class has a string() method, you could try this in your to_string method:
	return self.val['whybother']['contents'].string()

Greetings, Jens.

-----Original Message-----
From: gdb-owner@sourceware.org [mailto:gdb-owner@sourceware.org] On Behalf Of Paul Pluzhnikov
Sent: Dienstag, 10. November 2009 03:12
To: gdb@sourceware.org; archer@sourceware.org
Cc: dje@google.com; ppluzhnikov@google.com
Subject: [python] Pretty-printers and addressprint

Greetings,

Consider the gdb.python/py-prettyprint.exp test case.
It has:

typedef struct string_repr
{
  struct whybother
  {
    const char *contents;
  } whybother;
} string;

and a prettyprinter for it:

# Test returning a Value from a printer.
class string_print:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val['whybother']['contents']


Which currently produces:

$4 = 0x4007e0 "this is x"^M


The issue I am having is there is no apparent way to get rid of the
address from python side (address is not printed when the printer
returns a python string instead of a value), whereas if the
printer really wants to print the address, it can trivally add
it back by returning appropriate python string.

Printing addresses inside of a container seems to be especially
"not wanted".

Should the decision to print addresses be deferred to the
pretty-printer? Is the patch below reasonable?

Thanks,
--
Paul Pluzhnikov

2009-11-09  Paul Pluzhnikov  <ppluzhnikov@google.com>

	* gdb/python/py-prettyprint.c (print_string_repr): Don't
	print value address.



--- gdb/python/py-prettyprint.c#1    2009-11-09 17:58:39.000000000 -0800
+++ gdb/python/py-prettyprint.c     2009-11-09 16:51:16.862840000 -0800
@@ -209,7 +209,12 @@ print_string_repr (PyObject *printer, co
       Py_DECREF (py_str);
     }
   else if (replacement)
-    common_val_print (replacement, stream, recurse, options, language);
+    {
+      struct value_print_options opts = *options;
+
+      opts.addressprint = 0;
+      common_val_print (replacement, stream, recurse, &opts, language);
+    }
   else
     gdbpy_print_stack ();
 }

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10  7:25 ` Elmenthaler, Jens
@ 2009-11-10  8:06   ` Paul Pluzhnikov
  2009-11-10  8:37     ` Phil Muldoon
  2009-11-10 11:53     ` Paul Koning
  0 siblings, 2 replies; 11+ messages in thread
From: Paul Pluzhnikov @ 2009-11-10  8:06 UTC (permalink / raw)
  To: Elmenthaler, Jens; +Cc: gdb, archer, dje

On Mon, Nov 9, 2009 at 11:25 PM, Elmenthaler, Jens
<jens.elmenthaler@verigy.com> wrote:

> The gdb.Value class has a string() method, you could try this in your to_string method:
>        return self.val['whybother']['contents'].string()

That doesn't quite do what I want, but this almost does:

        return '"' + self.val['whybother']['contents'].string() + '"'

However, consider a modification of the original test:

-  string x = make_string ("this is x");
+  string x = make_string ("this is x\201\202\203\204");


With the original pretty-printer, this produces:

  $4 = 0x4007e0 "this is x\201\202\203\204"

which is exactly what I want, if only it didn't have the address. With
proposed patch, it produces exactly what I want:

  $4 = "this is x\201\202\203\204"

But this value can't be converted to a python string in ASCII charset:
the modified printer produces this:

  $4 = Traceback (most recent call last):
    File "../../../src/gdb/testsuite/gdb.python/py-prettyprint.py",
line 27, in to_string
      return self.val['whybother']['contents'].string()
  UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position
9: ordinal not in range(128)

Unfortunately I frequently deal with non-ascii strings, and the
problem just wouldn't go away :-(

Previous threads on this subject, e.g.
http://sourceware.org/ml/archer/2008-q4/msg00180.html didn't appear to
have reached a satisfactory conclusion.


Thanks,
-- 
Paul Pluzhnikov

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10  8:06   ` Paul Pluzhnikov
@ 2009-11-10  8:37     ` Phil Muldoon
  2009-11-10 11:53     ` Paul Koning
  1 sibling, 0 replies; 11+ messages in thread
From: Phil Muldoon @ 2009-11-10  8:37 UTC (permalink / raw)
  To: Paul Pluzhnikov; +Cc: Elmenthaler, Jens, gdb, archer, dje

On 11/10/2009 08:06 AM, Paul Pluzhnikov wrote:
>
> Unfortunately I frequently deal with non-ascii strings, and the
> problem just wouldn't go away :-(
>
> Previous threads on this subject, e.g.
> http://sourceware.org/ml/archer/2008-q4/msg00180.html didn't appear to
> have reached a satisfactory conclusion.
>
>    

There is a bug that tracks this now:

http://sourceware.org/bugzilla/show_bug.cgi?id=10705

But I think (also) what you are asking for is a way to get at the 
underlying data without passing it through an encoding?

Cheers,

Phil

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [python] Pretty-printers and addressprint
  2009-11-10  8:06   ` Paul Pluzhnikov
  2009-11-10  8:37     ` Phil Muldoon
@ 2009-11-10 11:53     ` Paul Koning
  2009-11-10 15:45       ` Paul Pluzhnikov
  1 sibling, 1 reply; 11+ messages in thread
From: Paul Koning @ 2009-11-10 11:53 UTC (permalink / raw)
  To: Paul Pluzhnikov, Elmenthaler, Jens; +Cc: gdb, archer, dje

> On Mon, Nov 9, 2009 at 11:25 PM, Elmenthaler, Jens
> <jens.elmenthaler@verigy.com> wrote:
> 
> > The gdb.Value class has a string() method, you could try this in your
> to_string method:
> >        return self.val['whybother']['contents'].string()
> 
> That doesn't quite do what I want, but this almost does:
> 
>         return '"' + self.val['whybother']['contents'].string() + '"'

Try instead 
    return repr (self.val...)

rather than manually supplying the quotes.  That will do the right thing even if the contents has things like quotes in it.
 
> However, consider a modification of the original test:
> 
> -  string x = make_string ("this is x");
> +  string x = make_string ("this is x\201\202\203\204");
> ... 
> But this value can't be converted to a python string in ASCII charset:
> the modified printer produces this:
> 
>   $4 = Traceback (most recent call last):
>     File "../../../src/gdb/testsuite/gdb.python/py-prettyprint.py",
> line 27, in to_string
>       return self.val['whybother']['contents'].string()
>   UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position
> 9: ordinal not in range(128)
> 
> Unfortunately I frequently deal with non-ascii strings, and the
> problem just wouldn't go away :-(

My test (without gdb, I don't have that installed yet) is inconclusive but I think using repr() will cure that too.

	paul

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10  2:12 [python] Pretty-printers and addressprint Paul Pluzhnikov
  2009-11-10  7:25 ` Elmenthaler, Jens
@ 2009-11-10 15:12 ` Tom Tromey
  1 sibling, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2009-11-10 15:12 UTC (permalink / raw)
  To: Paul Pluzhnikov; +Cc: gdb, archer, dje

>>>>> "Paul" == Paul Pluzhnikov <ppluzhnikov@google.com> writes:

Paul> The issue I am having is there is no apparent way to get rid of the
Paul> address from python side (address is not printed when the printer
Paul> returns a python string instead of a value), whereas if the
Paul> printer really wants to print the address, it can trivally add
Paul> it back by returning appropriate python string.

Adding it manually like that will also affect MI output, which may not
be what you really want.

Paul> Should the decision to print addresses be deferred to the
Paul> pretty-printer? Is the patch below reasonable?

I'm ok with it.  If nobody complains this week, check it in.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10 11:53     ` Paul Koning
@ 2009-11-10 15:45       ` Paul Pluzhnikov
  2009-11-10 15:53         ` Paul Koning
  2009-11-10 16:53         ` Tom Tromey
  0 siblings, 2 replies; 11+ messages in thread
From: Paul Pluzhnikov @ 2009-11-10 15:45 UTC (permalink / raw)
  To: Paul Koning; +Cc: Elmenthaler, Jens, gdb, archer, dje

On Tue, Nov 10, 2009 at 3:53 AM, Paul Koning <Paul_Koning@dell.com> wrote:

>> Unfortunately I frequently deal with non-ascii strings, and the
>> problem just wouldn't go away :-(
>
> My test (without gdb, I don't have that installed yet) is inconclusive
> but I think using repr() will cure that too.

AFAICT, in GDB repr(value) is a no-op, and the end result of returning
repr(value) from the pretty-printer is exactly the same as that of
returning the value itself:

  $4 = 0x4007e0 "this is x\201\202\203\204"

Should repr(value) do something different?

Thanks,
-- 
Paul Pluzhnikov

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [python] Pretty-printers and addressprint
  2009-11-10 15:45       ` Paul Pluzhnikov
@ 2009-11-10 15:53         ` Paul Koning
  2009-11-10 16:53         ` Tom Tromey
  1 sibling, 0 replies; 11+ messages in thread
From: Paul Koning @ 2009-11-10 15:53 UTC (permalink / raw)
  To: Paul Pluzhnikov; +Cc: Elmenthaler, Jens, gdb, archer, dje

repr() is a Python builtin function that returns the "representation" of
an object -- a string that matches the representation of that object in
Python.  Contrast that with str() which prints it the user-friendly way
which may be less complete.

For example:

python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
>>> foo="abc\1"
>>> print foo
abc
>>> print repr(foo)
'abc\x01'
>>> print str(foo)
abc
>>>  

Note that "repr" puts quotes around the string, and escapes the
non-printable characters.  It also escapes quotes, which simply wrapping
a quote around the raw string won't cover.

If in gdb repr() doesn't work as in standard Python that would be a
problem...

	paul

> -----Original Message-----
> From: Paul Pluzhnikov [mailto:ppluzhnikov@google.com]
> Sent: Tuesday, November 10, 2009 10:45 AM
> To: Paul Koning
> Cc: Elmenthaler, Jens; gdb@sourceware.org; archer@sourceware.org;
> dje@google.com
> Subject: Re: [python] Pretty-printers and addressprint
> 
> On Tue, Nov 10, 2009 at 3:53 AM, Paul Koning <Paul_Koning@dell.com>
> wrote:
> 
> >> Unfortunately I frequently deal with non-ascii strings, and the
> >> problem just wouldn't go away :-(
> >
> > My test (without gdb, I don't have that installed yet) is
> inconclusive
> > but I think using repr() will cure that too.
> 
> AFAICT, in GDB repr(value) is a no-op, and the end result of returning
> repr(value) from the pretty-printer is exactly the same as that of
> returning the value itself:
> 
>   $4 = 0x4007e0 "this is x\201\202\203\204"
> 
> Should repr(value) do something different?
> 
> Thanks,
> --
> Paul Pluzhnikov

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10 15:45       ` Paul Pluzhnikov
  2009-11-10 15:53         ` Paul Koning
@ 2009-11-10 16:53         ` Tom Tromey
  2009-11-10 17:03           ` Paul Pluzhnikov
  1 sibling, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2009-11-10 16:53 UTC (permalink / raw)
  To: Paul Pluzhnikov; +Cc: Paul Koning, Elmenthaler, Jens, gdb, archer, dje

>>>>> "Paul" == Paul Pluzhnikov <ppluzhnikov@google.com> writes:

Paul> AFAICT, in GDB repr(value) is a no-op, and the end result of returning
Paul> repr(value) from the pretty-printer is exactly the same as that of
Paul> returning the value itself:

That is because repr(value) ends up in valpy_str, which calls
common_val_print.

What you want instead is repr(value.string()), at least if you want to
try this route.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10 16:53         ` Tom Tromey
@ 2009-11-10 17:03           ` Paul Pluzhnikov
  2009-11-10 20:49             ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Paul Pluzhnikov @ 2009-11-10 17:03 UTC (permalink / raw)
  To: tromey; +Cc: Paul Koning, Elmenthaler, Jens, gdb, archer, dje

On Tue, Nov 10, 2009 at 8:53 AM, Tom Tromey <tromey@redhat.com> wrote:

> What you want instead is repr(value.string()), at least if you want to
> try this route.

That of course can't work, since it's the value.string() which
raises exception. Here is the output:

$4 = Traceback (most recent call last):
  File "../../../src/gdb/testsuite/gdb.python/py-prettyprint.py", line
27, in to_string
    return repr(self.val['whybother']['contents'].string())
UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position
9: ordinal not in range(128)


It also changes other output in the way I didn't expect:

print cstring
$5 = u'const string'

print c
$6 = container u'container' with 2 elements = {
  [0] = 23,
  [1] = 72
}

Thanks,
-- 
Paul Pluzhnikov

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [python] Pretty-printers and addressprint
  2009-11-10 17:03           ` Paul Pluzhnikov
@ 2009-11-10 20:49             ` Tom Tromey
  0 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2009-11-10 20:49 UTC (permalink / raw)
  To: Paul Pluzhnikov; +Cc: Paul Koning, Elmenthaler, Jens, gdb, archer, dje

>>>>> "Paul" == Paul Pluzhnikov <ppluzhnikov@google.com> writes:

>> What you want instead is repr(value.string()), at least if you want to
>> try this route.

Paul> That of course can't work, since it's the value.string() which
Paul> raises exception.

Yeah, oops on me.

I think your patch was fine.  Or, if people don't like that, we can
always have an "addressprint for pretty-printing" option.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-11-10 20:49 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-10  2:12 [python] Pretty-printers and addressprint Paul Pluzhnikov
2009-11-10  7:25 ` Elmenthaler, Jens
2009-11-10  8:06   ` Paul Pluzhnikov
2009-11-10  8:37     ` Phil Muldoon
2009-11-10 11:53     ` Paul Koning
2009-11-10 15:45       ` Paul Pluzhnikov
2009-11-10 15:53         ` Paul Koning
2009-11-10 16:53         ` Tom Tromey
2009-11-10 17:03           ` Paul Pluzhnikov
2009-11-10 20:49             ` Tom Tromey
2009-11-10 15:12 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).