[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-04-21 Thread Dave Opstad

New submission from Dave Opstad :

Python 2.7.1 doesn't appear to do the usual implicit call to str() for 
subclasses of unicode. In the following snippet, I would have expected print 
myTest and print str(myTest) to behave the same:

>>> class Test(unicode):
...   def __str__(self):
... print "In __str__"
... return (u"*** " + self + u" ***").encode('utf-8')
...   def __unicode__(self):
... print "In __unicode__"
... return u"*** " + self + u" ***"
... 
>>> myTest = Test(u"abc")
>>> print myTest
abc
>>> print str(myTest)
In __str__
*** abc ***
>>> print unicode(myTest)
In __unicode__
*** abc ***

--
components: Unicode
messages: 134231
nosy: opstad
priority: normal
severity: normal
status: open
title: 2.7.1 unicode subclasses not calling __str__() for print statement
type: behavior
versions: Python 2.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-04-21 Thread R. David Murray

R. David Murray  added the comment:

You subclassed unicode.  So print printed the value of your unicode object, 
which didn't need coercion.

--
nosy: +r.david.murray
resolution:  -> invalid
stage:  -> committed/rejected
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-04-21 Thread R. David Murray

R. David Murray  added the comment:

For the record, this isn't as simple as I made it sound.  See, for example, 
issue 9196.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-04-21 Thread Dave Opstad

Dave Opstad  added the comment:

I guess I was confused by the inconsistency with Python 3, which *does* call 
the __str__ method, even though, again, no coercion is needed:

Python 3.2 (r32:88452, Feb 20 2011, 10:19:59) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class X(str):
...   def __str__(self):
... print("In __str__")
... return "*** " + self + " ***"
... 
>>> x = X("abcde")
>>> print(x)
In __str__
*** abcde ***
>>> print(str(x))
In __str__
*** abcde ***

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-04-21 Thread R. David Murray

R. David Murray  added the comment:

Well, it's possible I'm wrong and you've found a bug.  There are numerous 
differences between 2 and 3 in both string handling and special method 
handling, though, so it may be hard to pin down.  If you poke around a bit more 
and still think it is a bug, please reopen.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-12-15 Thread John Nagle

John Nagle  added the comment:

This has nothing to do with Python 3.  There's a difference in __str__ handling 
between Python 2.6 and Python 2.7.2.  It's enough to crash BeautifulSoup:

[Thread-8] Unexpected EXCEPTION while processing page 
"http://www.verisign.com": global name '__str__' is not defined
[Thread-8] Traceback (most recent call last):
...
[Thread-8]   File "C:\projects\sitetruth\BeautifulSoup.py", line 646, in 
prettify
[Thread-8] return self.__str__(encoding, True)
[Thread-8]   File "C:\projects\sitetruth\BeautifulSoup.py", line 621, in __str__
[Thread-8] contents = self.renderContents(encoding, prettyPrint, 
indentContents)
[Thread-8]   File "C:\projects\sitetruth\BeautifulSoup.py", line 656, in 
renderContents
[Thread-8] text = c.__str__(encoding)
[Thread-8]   File "C:\projects\sitetruth\BeautifulSoup.py", line 415, in __str__
[Thread-8] return "" % NavigableString.__str__(self, encoding)
[Thread-8]   File "C:\projects\sitetruth\BeautifulSoup.py", line 393, in 
__unicode__
[Thread-8] return __str__(self, None)
[Thread-8] NameError: global name '__str__' is not defined

The class method that's failing is simply

class NavigableString(unicode, PageElement):
...
def __unicode__(self):
return __str__(self, None)    EXCEPTION RAISED HERE 

def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING):
if encoding:
return self.encode(encoding)
else:
return self

Using __str__ in the global namespace is probably wrong, and in a later version 
of BeautifulSoup, that code is changed to

def __unicode__(self):
return str(self).decode(DEFAULT_OUTPUT_ENCODING)

which seems to work.  However, it is a real change from 2.6 to 2.7 that breaks 
code.

--
nosy: +nagle

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11900] 2.7.1 unicode subclasses not calling __str__() for print statement

2011-12-16 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc  added the comment:

> However, it is a real change from 2.6 to 2.7 that breaks code.

John, this issue is not the same as the one above.  The difference between 
Python 2.6 and Python 2.7.2 you mention only applies to % formatting.
The change is clearly documented in http://docs.python.org/dev/whatsnew/2.7.html
"""It’s now possible for a subclass of the built-in unicode type to override 
the __unicode__() method."""

This is clearly a bug in the application.  There are many ways to break 
compatibility with bogus code...

--
nosy: +amaury.forgeotdarc

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com