New submission from Ezio Melotti <ezio.melo...@gmail.com>: On Python 2.5 str(exception) and unicode(exception) return the same text: >>> err UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, 'ordinal not in range(128)') >>> str(err) "'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)" >>> unicode(err) u"'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)"
On Python 2.6 unicode(exception) returns unicode(exception.args): >>> err UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, 'ordinal not in range(128)') >>> str(err) "'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)" >>> unicode(err) u"('ascii', '\\xc3\\xa0', 0, 1, 'ordinal not in range(128)')" This seems to affect only exceptions with more than 1 arg (e.g. UnicodeErrors and SyntaxErrors). KeyError is also different (the '' are missing with unicode()). Note that when an exception like ValueError() is instantiated with more than 1 arg even str() returns str(exception.args) on both Py2.5 and Py2.6. Probably __str__() checks the number of args before returning a specific message and if it doesn't match it returns str(self.args). __unicode__() instead seems to always return unicode(self.args) on Py2.6. Attached there's a script that prints the repr(), str() and unicode() of some exceptions, run it on Py2.5 and Py2.6 to see the differences. ---------- components: Interpreter Core files: unicode_exceptions.py messages: 88330 nosy: ezio.melotti severity: normal status: open title: unicode(exception) behaves differently on Py2.6 when len(exception.args) > 1 type: behavior versions: Python 2.6 Added file: http://bugs.python.org/file14071/unicode_exceptions.py _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue6108> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com