Bug 14320 - Strange python script behaviour
Summary: Strange python script behaviour
Status: RESOLVED MOVED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.4
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-01 18:42 UTC by David Kozub
Modified: 2012-08-01 16:53 UTC (History)
1 user (show)

See Also:
Host: x86-linux-gnu, x86_64-linux-gnu
Target: x86-linux-gnu, x86_64-linux-gnu
Build:
Last reconfirmed:


Attachments
Standalone testcase. (727 bytes, application/x-bzip)
2012-07-01 18:42 UTC, David Kozub
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Kozub 2012-07-01 18:42:04 UTC
Created attachment 6506 [details]
Standalone testcase.

I found a strange behaviour trying to implement a pretty printer. Attached is a test case (just unpack and run make). The issue is reproducible both with 7.4.1 release and with CVS head as of now (7.4.50.20120701-cvs). (It's also present in Debian testing and Ubuntu 12.04.)

Assumming a C++ class:

struct Foo
{
	int *m_ptr;
};

the following pretty printing class:

class MyTestPrinter:
	def __init__(self, val):
		self.val = val

	def get_ptr(self):
		return self.val['m_ptr']

	def children(self):
		print "foo"

	def to_string(self):
		return "0x%08x" % self.get_ptr()

causes a strange error message:

$1 = 0x7fffffffe390Traceback (most recent call last):
  File "test-printer.py", line 11, in children
    print "foo"
  File "<string>", line 13, in write
gdb.error: Cannot convert value to int.


The body of MyTestPrinter.to_string() might be wrong (using a gdb.Value for a pointer with %08x), but the actual error message complains about the 'print "foo"'. But the print is ok, so the error message is highly confusing.

To make this matter more mysterious, doing any of the following makes the error disappear:

* remove MyTestPrinter.children()
* add a long cast to MyTestPrinter.to_string(): return "0x%08x" % long(self.get_ptr())

So, if MyTestPrinter.to_string() is wrong, it should produce an appropriate error message, and it should be consistent, no matter if there's any MyTestPrinter.children() or not. Or, if MyTestPrinter.to_string() is correct, then the print in MyTestPrinter.children() should not cause any error.

It seems that it's not caused by print (in MyTestPrinter.children()) itself, but by the string literal, as I can get the same kind of error message for other things involving string literals in MyTestPrinter.children().
Comment 1 David Kozub 2012-07-01 19:02:17 UTC
Just to clarify: MyTestPrinter.children() is wrong in not returning an iterable. But the strange error happens even before MyTestPrinter.children() returns, so I believe it's not related to this.

The attached testcase is just a bare minimum code to trigger this.
Comment 2 Tom Tromey 2012-07-31 16:50:15 UTC
I believe this is a bug in Python.
I'm going to file a bug there.

The pretty-printer does:

	def to_string(self):
		return "0x%08x" % self.get_ptr()

but in gdb, one cannot convert a pointer-valued gdb.Value
to a Python integer.

Python tries to convert it to a long:

                        iobj = PyNumber_Int(v);
                        if (iobj==NULL) iobj = PyNumber_Long(v);

... but I think it ought to be clearing the exception here
before calling PyNumber_Long.
Comment 3 Tom Tromey 2012-07-31 20:00:53 UTC
This is now http://bugs.python.org/issue15516
Comment 4 Tom Tromey 2012-08-01 16:53:55 UTC
Not a gdb bug; and I don't think there's a reasonable
workaround in gdb.