diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2003-09-20 23:51:34 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2003-09-20 23:51:34 (GMT) |
commit | cec1b3f6a7e899179c6463145ceff47a6c5bc805 (patch) | |
tree | 26502414f2ceca83454c34b5c367b12ce61dc33e | |
parent | 0aab0020573771e224ebc0e94b2c5adf565fb4f6 (diff) | |
download | cpython-cec1b3f6a7e899179c6463145ceff47a6c5bc805.zip cpython-cec1b3f6a7e899179c6463145ceff47a6c5bc805.tar.gz cpython-cec1b3f6a7e899179c6463145ceff47a6c5bc805.tar.bz2 |
Maintain backwards compatibility with python < 2.3 by dynamically
adding the iterator interface for python >= 2.3.
-rw-r--r-- | Lib/bsddb/__init__.py | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/Lib/bsddb/__init__.py b/Lib/bsddb/__init__.py index 2910236..6b4f965 100644 --- a/Lib/bsddb/__init__.py +++ b/Lib/bsddb/__init__.py @@ -52,9 +52,38 @@ error = db.DBError # So bsddb.error will mean something... #---------------------------------------------------------------------- +import sys + +# for backwards compatibility with python versions older than 2.3, the +# iterator interface is dynamically defined and added using a mixin +# class. old python can't tokenize it due to the yield keyword. +if sys.version >= '2.3': + exec """ import UserDict +class _iter_mixin(UserDict.DictMixin): + def __iter__(self): + try: + yield self.first()[0] + next = self.next + while 1: + yield next()[0] + except _bsddb.DBNotFoundError: + return + + def iteritems(self): + try: + yield self.first() + next = self.next + while 1: + yield next() + except _bsddb.DBNotFoundError: + return +""" +else: + class _iter_mixin: pass -class _DBWithCursor(UserDict.DictMixin): + +class _DBWithCursor(_iter_mixin): """ 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. @@ -145,23 +174,6 @@ class _DBWithCursor(UserDict.DictMixin): 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 - - def iteritems(self): - try: - yield self.first() - next = self.next - while 1: - yield next() - except _bsddb.DBNotFoundError: - return #---------------------------------------------------------------------- # Compatibility object factory functions |