exec "\nclass _iter_mixin(UserDict.DictMixin):\n def _make_iter_cursor(self):\n cur = self.db.cursor()\n key = id(cur)\n self._cursor_refs[key] = ref(cur, self._gen_cref_cleaner(key))\n return cur\n\n def _gen_cref_cleaner(self, key):\n # use generate the function for the weakref callback here\n # to ensure that we do not hold a strict reference to cur\n # in the callback.\n return lambda ref: self._cursor_refs.pop(key, None)\n\n def __iter__(self):\n try:\n cur = self._make_iter_cursor()\n\n # FIXME-20031102-greg: race condition. cursor could\n # be closed by another thread before this call.\n\n # since we're only returning keys, we call the cursor\n # methods with flags=0, dlen=0, dofs=0\n key = cur.first(0,0,0)[0]\n yield key\n\n next = cur.next\n while 1:\n try:\n key = next(0,0,0)[0]\n yield key\n except _bsddb.DBCursorClosedError:\n cur = self._make_iter_cursor()\n # FIXME-20031101-greg: race condition. cursor could\n # be closed by another thread before this call.\n cur.set(key,0,0,0)\n next = cur.next\n except _bsddb.DBNotFoundError:\n return\n except _bsddb.DBCursorClosedError:\n # the database was modified during iteration. abort.\n return\n\n def iteritems(self):\n try:\n try:\n cur = self._make_iter_cursor()\n except AttributeError:\n return\n\n # FIXME-20031102-greg: race condition. cursor could\n # be closed by another thread before this call.\n\n kv = cur.first()\n key = kv[0]\n yield kv\n\n next = cur.next\n while 1:\n try:\n kv = next()\n key = kv[0]\n yield kv\n except _bsddb.DBCursorClosedError:\n cur = self._make_iter_cursor()\n # FIXME-20031101-greg: race condition. cursor could\n # be closed by another thread before this call.\n cur.set(key,0,0,0)\n next = cur.next\n except _bsddb.DBNotFoundError:\n return\n except _bsddb.DBCursorClosedError:\n # the database was modified during iteration. abort.\n return\n"
else:
class _iter_mixin:
pass
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.
'''
def __init__(self, db):
self.db = db
self.db.set_get_returns_none(0)
self.dbc = None
self.saved_dbc_key = None
self._cursor_refs = { }
def __del__(self):
self.close()
def _checkCursor(self):
if self.dbc is None:
self.dbc = self.db.cursor()
if self.saved_dbc_key is not None:
self.dbc.set(self.saved_dbc_key)
self.saved_dbc_key = None
def _closeCursors(self, save = 1):
if self.dbc:
c = self.dbc
self.dbc = None
if save:
try:
self.saved_dbc_key = c.current(0, 0, 0)[0]
except db.DBError:
pass
except:
None<EXCEPTION MATCH>db.DBError
None<EXCEPTION MATCH>db.DBError
c.close()
del c
for cref in self._cursor_refs.values():
c = cref()
if c is not None:
c.close()
continue
def _checkOpen(self):
if self.db is None:
raise error, 'BSDDB object has already been closed'