[ZODB-Dev] odd KeyError in OOSets

Tim Peters tim_one at email.msn.com
Fri Apr 18 19:52:53 EDT 2003


[Nicholas Henke, on ClassmaskObject versus __cmp__]
> It derives from IndexedObject from IndexedCatalog -- I checked that
> class, and __cmp__ is not defined anywhere. I am not sure if it matters,
> __hash__ is defined to return the objects 'id' which is id(self) taken
> during __init__.

While __hash__ is important for Python dicts, __hash__ isn't used by ZODB
Sets, TreeSets, Buckets or BTrees.  They store stuff in sorted order, and
require a __cmp__ that defines a consistent ordering among members (for Sets
and TreeSets) or keys (for Buckets and BTrees).  Instances of a Python class
that doesn't define __cmp__ get a default __cmp_, which compares the
instances' locations in memory (it compares their memory addresses).  This
is a consistent total ordering across a single program run, but must never
be used for persistent objects -- the memory address they land at when
loaded from a database is accidental.  Unfortunately, nothing in ZODB can
detect the error when you end up using a bad comparison function, except
that the results are disastrous.

Short course:  if there isn't a carefully designed __cmp__ in this class
hierarchy, you simply can't use instances of this class as elements of
persistent Sets or TreeSets, or as keys in persistent Buckets or BTrees.




More information about the ZODB-Dev mailing list