This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vbr
Recipients vbr
Date 2009-02-15.21:50:34
SpamBayes Score 2.546686e-08
Marked as misclassified No
Message-id <1234734636.9.0.12200425592.issue5274@psf.upfronthosting.co.za>
In-reply-to
Content
Recently I noticed a bit surprising behaviour of sys.exc_info() in 
python 2.6.1 (comparing to 2.5.4). 
Some code like:
try:
    my_text = unicode(open("test_file.txt").read(), "utf-8")

triggers UnicodeDecodeError while opening a text file wrongly with 
utf-8 codec (test_file.txt contains the string "abšcd" encoded in 
windows-1250); 

this error is catched by the 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, I'd expect str() and unicode() to return 
the equivalent content, which is not the case here.
(The second part "ab\x9acd" is the whole content of the file being
read - which is normally quite a bit longer than this sample...)

Regards
  vbr
History
Date User Action Args
2009-02-15 21:50:36vbrsetrecipients: + vbr
2009-02-15 21:50:36vbrsetmessageid: <1234734636.9.0.12200425592.issue5274@psf.upfronthosting.co.za>
2009-02-15 21:50:35vbrlinkissue5274 messages
2009-02-15 21:50:34vbrcreate