sys.exc_info() different between python 2.5.4 and 2.6.1?

Vlastimil Brom vlastimil.brom at gmail.com
Fri Feb 6 21:45:10 EST 2009


Hi all,
I just noticed a changed behaviour of sys.exc_info() between python
2.5.4 and 2.6.1 and wanted to ask, wheter it was intended, and how to
deal with the new state.

Some code triggers an error while opening a text file (windows 1250 -
with non-ascii characters) wrongly as utf-8,
this gets catched by a bare except clause.
Further the mentioned versions of python differ in handling sys.exc_info()
The following lines:
print sys.exc_info()[1]
print repr(sys.exc_info()[1])
print str(sys.exc_info()[1])
print unicode(sys.exc_info()[1])

result in python 2.5 in:

'utf8' codec can't decode byte 0x9a in position 2: unexpected code byte
UnicodeDecodeError('utf8', 'ab\x9acd', 2, 3, 'unexpected code byte')
'utf8' codec can't decode byte 0x9a in position 2: unexpected code byte
'utf8' codec can't decode byte 0x9a in position 2: unexpected code byte

in python 2.6 it is:
'utf8' codec can't decode byte 0x9a in position 2: unexpected code byte
UnicodeDecodeError('utf8', 'ab\x9acd', 2, 3, 'unexpected code byte')
'utf8' codec can't decode byte 0x9a in position 2: unexpected code byte
('utf8', 'ab\x9acd', 2, 3, 'unexpected code byte')

Which is kind of confusing as my the excepthook function uses the
unicode() form of sys.exc_info()[1]
(The second part "ab\x9acd" is the whole content of the file being
read - which is normally quite a bit longer than this sample...)

Is this the expected behaviour? If so, what might be the reason for
differently outputting str() and unicode()?
How can this be dealt with, are the standard error values somehow
expected to be ascii, so that str() can be used reasonably?

Any hints are much appreciated,
  Vlasta



More information about the Python-list mailing list