Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: string interpolation mystery in Python 2.6

Reply
Thread Tools

Re: string interpolation mystery in Python 2.6

 
 
Alan G Isaac
Guest
Posts: n/a
 
      09-11-2009
Michael Foord came up with a much simpler
illustration. With Python 2.6::

>>> try:

... open('flooble')
... except Exception as e:
... pass
...
>>> e

IOError(2, 'No such file or directory')
>>> unicode(e)

u"(2, 'No such file or directory')"
>>> str(e)

"[Errno 2] No such file or directory: 'flooble'"
>>> u'%s' % e

u"(2, 'No such file or directory')"
>>> '%s' % e

"[Errno 2] No such file or directory: 'flooble'"

Cheers,
Alan Isaac

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      09-11-2009
On Fri, Sep 11, 2009 at 3:12 PM, Alan G Isaac <(E-Mail Removed)> wrote:
> Michael Foord came up with a much simpler
> illustration. ¬*With Python 2.6::
>
> ¬* ¬* ¬* ¬*>>> try:
> ¬* ¬* ¬* ¬*... ¬*open('flooble')
> ¬* ¬* ¬* ¬*... except Exception as e:
> ¬* ¬* ¬* ¬*... ¬*pass
> ¬* ¬* ¬* ¬*...
> ¬* ¬* ¬* ¬*>>> e
> ¬* ¬* ¬* ¬*IOError(2, 'No such file or directory')
> ¬* ¬* ¬* ¬*>>> unicode(e)
> ¬* ¬* ¬* ¬*u"(2, 'No such file or directory')"
> ¬* ¬* ¬* ¬*>>> str(e)
> ¬* ¬* ¬* ¬*"[Errno 2] No such file or directory: 'flooble'"
> ¬* ¬* ¬* ¬*>>> u'%s' % e
> ¬* ¬* ¬* ¬*u"(2, 'No such file or directory')"
> ¬* ¬* ¬* ¬*>>> '%s' % e
> ¬* ¬* ¬* ¬*"[Errno 2] No such file or directory: 'flooble'"


Sounds like IOError or one of its ancestors defines both __str__() and
__unicode__ () special methods but has them produce different output.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-12-2009
On Fri, 11 Sep 2009 15:19:05 -0700, Chris Rebert wrote:

> On Fri, Sep 11, 2009 at 3:12 PM, Alan G Isaac <(E-Mail Removed)>
> wrote:
>> Michael Foord came up with a much simpler illustration. ¬*With Python
>> 2.6::


[snip]

> Sounds like IOError or one of its ancestors defines both __str__() and
> __unicode__ () special methods but has them produce different output.



That's what it looks like to me too, which I wouldn't call either a bug
or a feature. I don't think Python makes any promises regarding exception
messages.

However, I must admit I'm perplexed why the original example is calling
__unicode__() in the first place! Given the line:

raise self.severe('Problems with "%s" directive path:\n%s: %s.'
% (self.name, error.__class__.__name__, error))

it looks to me like it should be calling error.__str__() not
error.__unicode(). Making the suggested edit:

raise self.severe('Problems with "%s" directive path:\n%s: %s.'
% (self.name, error.__class__.__name__, str(error)))

should have no effect. But it (apparently) does. This brings us back to
Alan's original question:

"MYSTERY: how can "%s"%error be different from "%s"%str(error) in Python
2.6?"



--
Steven
 
Reply With Quote
 
Vlastimil Brom
Guest
Posts: n/a
 
      09-12-2009
2009/9/12 Steven D'Aprano <(E-Mail Removed)>:
> On Fri, 11 Sep 2009 15:19:05 -0700, Chris Rebert wrote:
>
>> Sounds like IOError or one of its ancestors defines both __str__() and
>> __unicode__ () special methods but has them produce different output.

>
>
> That's what it looks like to me too, which I wouldn't call either a bug
> or a feature. I don't think Python makes any promises regarding exception
> messages.
>
>


There are some report about the apparently same issue in the bug tracker.
http://bugs.python.org/issue5274
http://bugs.python.org/issue6108

vbr
 
Reply With Quote
 
Alan G Isaac
Guest
Posts: n/a
 
      09-12-2009
On 9/11/2009 9:42 PM, Steven D'Aprano wrote:
> However, I must admit I'm perplexed why the original example is calling
> __unicode__() in the first place! Given the line:
>
> raise self.severe('Problems with "%s" directive path:\n%s: %s.'
> % (self.name, error.__class__.__name__, error))
>
> it looks to me like it should be calling error.__str__() not
> error.__unicode(). Making the suggested edit:
>
> raise self.severe('Problems with "%s" directive path:\n%s: %s.'
> % (self.name, error.__class__.__name__, str(error)))
>
> should have no effect. But it (apparently) does. This brings us back to
> Alan's original question:
>
> "MYSTERY: how can "%s"%error be different from "%s"%str(error) in Python
> 2.6?"



George Brandl explained it to me this way:

It's probably best explained with a bit of code:

>>> >>> class C(object):

... def __str__(self): return '[str]'
... def __unicode__(self): return '[unicode]'
...
>>> "%s %s" % ('foo', C())

'foo [str]'
>>> "%s %s" % (u'foo', C())

u'foo [unicode]'

I.e., as soon as a Unicode element is interpolated into a string, further
interpolations automatically request Unicode via __unicode__, if it exists.

Pretty subtle ...

Cheers,
Alan Isaac

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: string interpolation for python Yingjie Lan Python 25 04-03-2012 05:47 AM
modifying html input date for mysql, reg ex or string interpolation? Kun Python 2 04-11-2006 11:17 PM
Perl/Ruby string interpolation Aiden Humphreys Java 5 05-22-2005 06:37 PM
yet another recipe on string interpolation Michele Simionato Python 8 11-08-2004 08:11 PM
YA string interpolation and printing idea Paul Rubin Python 4 01-19-2004 04:52 PM



Advertisments