[issue25880] u'..'.encode('idna') → UnicodeError: label empty or too long

SpaceOne report at bugs.python.org
Thu Dec 17 11:35:29 EST 2015


SpaceOne added the comment:

I know that UnicodeEncodeError is a subclass of UnicodeError. The problem here is that UnicodeError would also catch UnicodeDecodeError.
This is especially disturbing if you catch errors of a whole function.

If you e.g. use python2.7 you might want to catch only UnicodeEncodeError if you encode something and don't want to catch UnicodeDecodeError.

>>> b'\xff'.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

(Read that code carefully!!! It's not something which should ever be done but might happen in the world)
Especially if you are writing python2+3 compatible applications.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25880>
_______________________________________


More information about the Python-bugs-list mailing list