exceptions and unicode

Thomas Jollans thomas at jollans.com
Wed Jun 16 17:51:03 EDT 2010


On 06/16/2010 10:10 PM, Stuart McGraw wrote:
> I am having a problem with exceptions and unicode.
> 
>   try: open ('file.txt')
>   except IOError, e: pass
>   str (e)
>   => "[Errno 2] No such file or directory: 'file.txt'"
> 
> which is fine but...
> 
>   try: open (u'フィイル.txt')
>   except IOError, e: pass
>   str (e)
>   => "[Errno 2] No such file or directory: u'\\u30d5\\u30a3\\u30a4\\u30eb.txt'"
> 
> ok, I did ask for a str string so no reason I should expect a unicode
> string, but then (in Python 2.6)...
> 
>   unicode (e)
>   => u"(2, 'No such file or directory')"
> 
> i.e. no formatting at all, or in Python 2.6.5
> 
>   unicode (e)
>   => u"[Errno 2] No such file or directory: u'\\u30d5\\u30a3\\u30a4\\u30eb.txt'"

it looks like IOError simply calls repr(self.filename) to create an
error message.

e.filename is, however, still a unicode object, so you could access that
directly, and format it yourself. A simple approach to get the same
message would be
u"[Errno {0.errno}] {0.strerror}: '{0.filename}'".format(e)

but it looks like the standard exceptions use repr (barely surprising),
and in Python 2, repr is as much of a arse about unicode as the rest of
the language.

> (Python 3.x is not an option.)

If it were, it'd be a good one though ;-)

> Note that the exceptions may be anything (I just used IOError 
> as an example) and are generated in bowels of an API that I 
> can't/won't mess with.

Yeah, well, you'd have to special-case every single exception type that
gets unicode arguments, as they probably all treat them in the same
ungrateful way.


Thomas



More information about the Python-list mailing list