[Python-3000] bytes and dicts (was: PEP 3137: Immutable Bytesand Mutable Buffer)

Jim Jewett jimjjewett at gmail.com
Sun Sep 30 18:31:23 CEST 2007


At 10:26 AM 9/29/2007 -0500, Michael Urman wrote:
> This isn't just a matter of dicts; any collection type can be susceptible.

The reason that dicts (and sets) are even worse is that the comparison
could be delayed.  If

    b"bytes" in [...]

raises an exception, it happens while b"bytes" is still in the
traceback context.  With a dictionary, the problem comparison could be
delayed until the next resize.  Even if the TypeError did tell you
which dict and (pair of pre-existing) keys were a problem, you still
wouldn't know how those keys got there.

Example data flow:

    insert string1 with hash X
    insert string2 with hash X -- collision, so it moves to the next slot
    del string1
    insert bytes with hash X -- replaces the dummy entry, so nothing raised yet
    ...
    insert something utterly unrelated, such as an integer.  This
causes a resize, so that now string2 and bytes do collide and raise a
TypeError complaining about strings and bytes -- even though the key
you added is neither.

-jJ


More information about the Python-3000 mailing list