Commit 78f5f8aa authored by Tim Peters's avatar Tim Peters

Merge rev 38076 from 3.4 branch.

Gave PersistentMapping an __iter__ method.

Also gave it some tests (it was woefully untested).
parent cdcfa60e
...@@ -104,6 +104,17 @@ Multi-database ...@@ -104,6 +104,17 @@ Multi-database
been added. See ``ZODB/cross-database-references.txt`` for an been added. See ``ZODB/cross-database-references.txt`` for an
introduction. introduction.
PersistentMapping
-----------------
- (3.5a8) 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.
Tools Tools
----- -----
...@@ -218,6 +229,17 @@ Savepoints ...@@ -218,6 +229,17 @@ Savepoints
happen, and stopped happening for subtransaction commits too. Making a happen, and stopped happening for subtransaction commits too. Making a
savepoint (or doing a subtransaction commit) does invoke cache gc now. savepoint (or doing a subtransaction commit) does invoke cache gc now.
PersistentMapping
-----------------
- (3.4.2a1) 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.4.1? What's new in ZODB3 3.4.1?
========================== ==========================
......
...@@ -95,6 +95,74 @@ class PMTests(unittest.TestCase): ...@@ -95,6 +95,74 @@ class PMTests(unittest.TestCase):
self.assert_(oldPath is newPath) 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): def find_global(modulename, classname):
"""Helper for this test suite to get special PersistentMapping""" """Helper for this test suite to get special PersistentMapping"""
......
...@@ -75,6 +75,14 @@ class PersistentMapping(UserDict, persistent.Persistent): ...@@ -75,6 +75,14 @@ class PersistentMapping(UserDict, persistent.Persistent):
self._p_changed = 1 self._p_changed = 1
return self.__super_popitem() 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, # If the internal representation of PersistentMapping changes,
# it causes compatibility problems for pickles generated by # it causes compatibility problems for pickles generated by
# different versions of the code. Compatibility works in both # different versions of the code. Compatibility works in both
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment