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.

classification
Title: sys.exc_info()[1] - different handling from str() and unicode() - py 2.6
Type: behavior Stage:
Components: Unicode Versions: Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: unicode(exception) and str(exception) should return the same message on Py2.6
View: 6108
Assigned To: Nosy List: georg.brandl, vbr
Priority: normal Keywords:

Created on 2009-02-15 21:50 by vbr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg82172 - (view) Author: Vlastimil Brom (vbr) Date: 2009-02-15 21:50
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
msg86031 - (view) Author: Vlastimil Brom (vbr) Date: 2009-04-16 13:34
I just want to confirm, that the reported issue is the same in python 
2.6.2,
is it really the intended behaviour in python 2.6 (as opposed to 2.5)?

vbr
msg88500 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-05-29 07:37
Duplicate of #6108.
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49524
2009-05-29 07:37:10georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg88500

superseder: unicode(exception) and str(exception) should return the same message on Py2.6
resolution: duplicate
2009-04-16 13:34:09vbrsetmessages: + msg86031
components: + Unicode
2009-02-15 21:50:35vbrcreate