[Zodb-checkins] SVN: ZODB/branches/3.5/ merging revision 38076 from
the 3.4 branch:
Thomas Lotze
tl at gocept.com
Tue Nov 22 17:32:20 EST 2005
Log message for revision 40334:
merging revision 38076 from the 3.4 branch:
Gave PersistentMapping an __iter__ method.
Also gave it some tests (it was woefully untested).
Changed:
U ZODB/branches/3.5/NEWS.txt
U ZODB/branches/3.5/src/ZODB/tests/testPersistentMapping.py
U ZODB/branches/3.5/src/persistent/mapping.py
-=-
Modified: ZODB/branches/3.5/NEWS.txt
===================================================================
--- ZODB/branches/3.5/NEWS.txt 2005-11-22 22:20:28 UTC (rev 40333)
+++ ZODB/branches/3.5/NEWS.txt 2005-11-22 22:32:19 UTC (rev 40334)
@@ -24,7 +24,18 @@
left the commit lock in the acquired state, causing any later attempt
to commit changes hang.
+PersistentMapping
+-----------------
+- (3.5.2b1) The ``PersistentMapping`` class has an ``__iter__()`` method
+ now, so that objects of this type work well with Python's iteration
+ protocol. For example, if ``x`` is a ``PersistentMapping`` (or
+ Python dictionary, or BTree, or ``PersistentDict``, ...), then
+ ``for key in x:`` iterates over the keys of ``x``, ``list(x)`` creates
+ a list containing ``x``'s keys, ``iter(x)`` creates an iterator for
+ ``x``'s keys, and so on.
+
+
What's new in ZODB3 3.5.1?
==========================
Release date: 26-Sep-2005
Modified: ZODB/branches/3.5/src/ZODB/tests/testPersistentMapping.py
===================================================================
--- ZODB/branches/3.5/src/ZODB/tests/testPersistentMapping.py 2005-11-22 22:20:28 UTC (rev 40333)
+++ ZODB/branches/3.5/src/ZODB/tests/testPersistentMapping.py 2005-11-22 22:32:19 UTC (rev 40334)
@@ -95,6 +95,74 @@
self.assert_(oldPath is newPath)
+ def checkBasicOps(self):
+ from persistent.mapping import PersistentMapping
+ m = PersistentMapping({'x': 1}, a=2, b=3)
+ m['name'] = 'bob'
+ self.assertEqual(m['name'], "bob")
+ self.assertEqual(m.get('name', 42), "bob")
+ self.assert_('name' in m)
+
+ try:
+ m['fred']
+ except KeyError:
+ pass
+ else:
+ self.fail("expected KeyError")
+ self.assert_('fred' not in m)
+ self.assertEqual(m.get('fred'), None)
+ self.assertEqual(m.get('fred', 42), 42)
+
+ keys = m.keys()
+ keys.sort()
+ self.assertEqual(keys, ['a', 'b', 'name', 'x'])
+
+ values = m.values()
+ values.sort()
+ self.assertEqual(values, [1, 2, 3, 'bob'])
+
+ items = m.items()
+ items.sort()
+ self.assertEqual(items,
+ [('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
+
+ keys = list(m.iterkeys())
+ keys.sort()
+ self.assertEqual(keys, ['a', 'b', 'name', 'x'])
+
+ values = list(m.itervalues())
+ values.sort()
+ self.assertEqual(values, [1, 2, 3, 'bob'])
+
+ items = list(m.iteritems())
+ items.sort()
+ self.assertEqual(items,
+ [('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
+
+ # PersistentMapping didn't have an __iter__ method before ZODB 3.4.2.
+ # Check that it plays well now with the Python iteration protocol.
+ def checkIteration(self):
+ from persistent.mapping import PersistentMapping
+ m = PersistentMapping({'x': 1}, a=2, b=3)
+ m['name'] = 'bob'
+
+ def check(keylist):
+ keylist.sort()
+ self.assertEqual(keylist, ['a', 'b', 'name', 'x'])
+
+ check(list(m))
+ check([key for key in m])
+
+ i = iter(m)
+ keylist = []
+ while 1:
+ try:
+ key = i.next()
+ except StopIteration:
+ break
+ keylist.append(key)
+ check(keylist)
+
def find_global(modulename, classname):
"""Helper for this test suite to get special PersistentMapping"""
Modified: ZODB/branches/3.5/src/persistent/mapping.py
===================================================================
--- ZODB/branches/3.5/src/persistent/mapping.py 2005-11-22 22:20:28 UTC (rev 40333)
+++ ZODB/branches/3.5/src/persistent/mapping.py 2005-11-22 22:32:19 UTC (rev 40334)
@@ -75,6 +75,14 @@
self._p_changed = 1
return self.__super_popitem()
+ # __iter__ was added in ZODB 3.4.2, but should have been added long
+ # before. We could inherit from Python's IterableUserDict instead
+ # (which just adds __iter__ to Python's UserDict), but that class isn't
+ # documented, and it would add another level of lookup for all the
+ # other methods.
+ def __iter__(self):
+ return iter(self.data)
+
# If the internal representation of PersistentMapping changes,
# it causes compatibility problems for pickles generated by
# different versions of the code. Compatibility works in both
More information about the Zodb-checkins
mailing list