[ZODB-Dev] OOBTree: Persistent Objects as keys

Toby Dickenson tdickenson@geminidataloggers.com
Tue, 19 Feb 2002 10:31:36 +0000


On Mon, 11 Feb 2002 16:00:52 -0500, Jim Fulton <jim@zope.com> wrote:

>> Your proposed
>> comparison function would raise an exception when comparing 1.0 and =
"hello",
>> right?=20
>
>Right.
>
>> That sounds good.
>
>OK, I'll get some help from the PythonLabs dudes on this.

Below is a script which demonstrates the unicode problem. It needs the
BTree exception patch from http://collector.zope.org/Zope/231

Its unpleasant, but I think the only safe option is for Jim's proposed
new comparison function (for Object-keyed BTrees) to always raise an
exception if either of its two parameters are unicode (but not if they
are both unicode).

Is that reasonable?



def test():
    t =3D BTrees.OOBTree.OOBTree()

    t['ma']=3D1    # First we add some keys to the BTree.
    t['mb']=3D1
    t['mc'+chr(200)]=3D1
    t['md']=3D1
    t['mf']=3D1
    t['mg']=3D1
    t['mh'+unichr(200)]=3D1

    check(t)     # Everything is fine at this point

    del t['mf']  # Now we delete some keys
    del t['md']

    # Everything seems fine. We have had no exceptions,
    # and therefore might reasonably commit the
    # transaction.

    check(t)     # the exception in here is unexpected

def check(t):
    for key in t.keys():
        try:
            if not t.has_key(key):
                print 'key %r curiously not present' % key
        except UnicodeError:
            print 'unicode error looking up key %r' % key
            raise

test()



Toby Dickenson
tdickenson@geminidataloggers.com