[Python-Dev] unicode hell/mixing str and unicode as dictionary keys

Jim Jewett jimjjewett at gmail.com
Thu Aug 3 23:10:43 CEST 2006


http://mail.python.org/pipermail/python-dev/2006-August/067934.html
M.-A. Lemburg mal at egenix.com

> Ralf Schmitt wrote:
>> Still trying to port our software. here's another thing I noticed:

>> d = {}
>> d[u'm\xe1s'] = 1
>> d['m\xe1s'] = 1
>> print d

(a 2-element dictionary, because they are not equal)

>> With python 2.5 I get: [ a traceback ending in ]

>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 1:
>> ordinal not in range(128)

> Let's put it this way: Python 2.5 uncovered a bug in your
> application that has always been there.

No; he application would only have a bug if he expected those two
objects to compare equal.  Trying to stick something hashable into a
dictionary should not raise an Exception just because there is already
a similar key, (regardless of whether or not the other key is equal or
identical).

The only way this error could be the right thing is if you were trying
to suggest that he shouldn't mix unicode and bytestrings at all.
There is a command line switch for that, but it doesn't get much use.

-jJ


More information about the Python-Dev mailing list