diff options
author | Raymond Hettinger <python@rcn.com> | 2003-09-12 06:33:37 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-09-12 06:33:37 (GMT) |
commit | deadbf50e4cc3c541e706d5bf1aa73624bed36a6 (patch) | |
tree | 407b9e5f2aa93866cdda5fb5b70fee3d49d2de0e /Lib | |
parent | 74c8e55f3b64b8819a1950be4ac1323c74931cc2 (diff) | |
download | cpython-deadbf50e4cc3c541e706d5bf1aa73624bed36a6.zip cpython-deadbf50e4cc3c541e706d5bf1aa73624bed36a6.tar.gz cpython-deadbf50e4cc3c541e706d5bf1aa73624bed36a6.tar.bz2 |
SF #662923
Add support for the iterator and mapping protocols.
For Py2.3, this was done for shelve, dumbdbm and other mapping objects, but
not for bsddb and dbhash which were inadvertently missed.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/bsddb/__init__.py | 11 | ||||
-rwxr-xr-x | Lib/test/test_bsddb.py | 24 |
2 files changed, 28 insertions, 7 deletions
diff --git a/Lib/bsddb/__init__.py b/Lib/bsddb/__init__.py index 1ec6adc..5fc8a38 100644 --- a/Lib/bsddb/__init__.py +++ b/Lib/bsddb/__init__.py @@ -52,8 +52,9 @@ error = db.DBError # So bsddb.error will mean something... #---------------------------------------------------------------------- +import UserDict -class _DBWithCursor: +class _DBWithCursor(UserDict.DictMixin): """ A simple wrapper around DB that makes it look like the bsddbobject in the old module. It uses a cursor as needed to provide DB traversal. @@ -144,6 +145,14 @@ class _DBWithCursor: self._checkOpen() return self.db.sync() + def __iter__(self): + try: + yield self.first()[0] + next = self.next + while 1: + yield next()[0] + except _bsddb.DBNotFoundError: + return #---------------------------------------------------------------------- # Compatibility object factory functions diff --git a/Lib/test/test_bsddb.py b/Lib/test/test_bsddb.py index aa58ef8..87145fb 100755 --- a/Lib/test/test_bsddb.py +++ b/Lib/test/test_bsddb.py @@ -20,12 +20,24 @@ def test(openmethod, what, ondisk=1): verify(f.keys() == []) if verbose: print 'creation...' - f['0'] = '' - f['a'] = 'Guido' - f['b'] = 'van' - f['c'] = 'Rossum' - f['d'] = 'invented' - f['f'] = 'Python' + keys = ['0', 'a', 'b', 'c', 'd', 'e', 'f'] + values = ['', 'Guido', 'van', 'Rossum', 'invented', 'Python'] + items = zip(keys, values) + for k, v in items: + f[k] = v + + # test mapping iteration methods + from sets import Set + def verifyset(s1, s2): + verify(Set(s1) == Set(s2)) + verify(keys, f.keys()) + verify(values, f.values()) + verify(items, f.items()) + verify(keys, f) + verify(keys, f.iterkeys()) + verify(values, f.itervalues()) + verify(items, f.iteritems()) + if verbose: print '%s %s %s' % (f['a'], f['b'], f['c']) |