[ZODB-Dev] PersistentMapping with loops/list comps

mmillikan at vfa.com mmillikan at vfa.com
Tue Mar 16 11:42:40 EST 2004


in ZODB 3.3a2:

The following code works on non-empty UserDicts but not on empty
ones. 

emptydict=UserDict()
somedict=UserDict({'fred':123,'barney':'a fool'})

for i in somedict: #--> ok
    print 1

for i in emptydict:
    print i

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\Python23\lib\UserDict.py", line 19, in __getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: 0

print [i for i in somedict] #--> ok

print [i for i in emptydict]

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\Python23\lib\UserDict.py", line 19, in __getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: 0

PersistentMapping subclasses UserDict and Persistent, and presents the
same problem. One can't replace Userdict with the newstyle dict
because of layout conflicts between the two super classes.

UserDict.IterableUserDict is identical to UserDict.UserDict except
that it wraps the __iter__ method of the contained dict. Replacing
UserDict by IterableUserDict as a base class in PersistentMapping
fixes this particular problem.

I noticed that about eight months ago the iter method was first added
to UserDict for python 2.3 and then removed (with the new
IterableUserDict class added). Notes on the cvs checkin indicated that
there were some edge cases where the iter method broke existing uses
of UserDict.

Is anyone aware of documented problems with using IterableUserDict as
a base class for PersistentMapping?

Mark Millikan







More information about the ZODB-Dev mailing list