Skip to content

Commit 8736270

Browse files
author
Richard Jones
committed
fix up some pre-Python2.6 compatibility issues in the *dbm interface
1 parent ef2509f commit 8736270

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

roundup/anypy/dbm_.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
# package containing the various implementations. The "wichdb" module's
33
# whichdb() function was moved to the new "dbm" module.
44

5+
import sys
6+
if sys.version_info[:2] < (2, 6):
7+
def key_in(db, key):
8+
return db.has_key(key)
9+
else:
10+
def key_in(db, key):
11+
return key in db
12+
513
try:
614
# old school first because <3 had a "dbm" module too...
715
import anydbm

roundup/backends/back_anydbm.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import os, marshal, re, weakref, string, copy, time, shutil, logging
2626

27-
from roundup.anypy.dbm_ import anydbm, whichdb
27+
from roundup.anypy.dbm_ import anydbm, whichdb, key_in
2828

2929
from roundup import hyperdb, date, password, roundupdb, security, support
3030
from roundup.support import reversed
@@ -248,7 +248,7 @@ def newid(self, classname):
248248
"""
249249
# open the ids DB - create if if doesn't exist
250250
db = self.opendb('_ids', 'c')
251-
if classname in db:
251+
if key_in(db, classname):
252252
newid = db[classname] = str(int(db[classname]) + 1)
253253
else:
254254
# the count() bit is transitional - older dbs won't start at 1
@@ -322,7 +322,7 @@ def getnode(self, classname, nodeid, db=None, cache=1):
322322
# get from the database and save in the cache
323323
if db is None:
324324
db = self.getclassdb(classname)
325-
if nodeid not in db:
325+
if not key_in(db, nodeid):
326326
raise IndexError("no such %s %s"%(classname, nodeid))
327327

328328
# check the uncommitted, destroyed nodes
@@ -435,7 +435,7 @@ def hasnode(self, classname, nodeid, db=None):
435435
# not in the cache - check the database
436436
if db is None:
437437
db = self.getclassdb(classname)
438-
return nodeid in db
438+
return key_in(db, nodeid)
439439

440440
def countnodes(self, classname, db=None):
441441
count = 0
@@ -552,7 +552,7 @@ def pack(self, pack_before):
552552
db_type = self.determine_db_type(path)
553553
db = self.opendb(db_name, 'w')
554554

555-
for key in db:
555+
for key in db.keys():
556556
# get the journal for this db entry
557557
journal = marshal.loads(db[key])
558558
l = []
@@ -679,7 +679,7 @@ def doSaveJournal(self, classname, nodeid, action, params, creator,
679679
db = self.getCachedJournalDB(classname)
680680

681681
# now insert the journal entry
682-
if nodeid in db:
682+
if key_in(db, nodeid):
683683
# append to existing
684684
s = db[nodeid]
685685
l = marshal.loads(s)
@@ -704,12 +704,12 @@ def doSetJournal(self, classname, nodeid, journal):
704704
def doDestroyNode(self, classname, nodeid):
705705
# delete from the class database
706706
db = self.getCachedClassDB(classname)
707-
if nodeid in db:
707+
if key_in(db, nodeid):
708708
del db[nodeid]
709709

710710
# delete from the database
711711
db = self.getCachedJournalDB(classname)
712-
if nodeid in db:
712+
if key_in(db, nodeid):
713713
del db[nodeid]
714714

715715
def rollback(self):
@@ -1530,12 +1530,12 @@ def getnodeids(self, db=None, retired=None):
15301530
db = self.db.getclassdb(self.classname)
15311531
must_close = True
15321532
try:
1533-
res.extend(db)
1533+
res.extend(db.keys())
15341534

15351535
# remove the uncommitted, destroyed nodes
15361536
if self.classname in self.db.destroyednodes:
15371537
for nodeid in self.db.destroyednodes[self.classname]:
1538-
if nodeid in db:
1538+
if key_in(db, nodeid):
15391539
res.remove(nodeid)
15401540

15411541
# check retired flag

roundup/backends/sessions_dbm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from roundup import hyperdb
1313
from roundup.i18n import _
14-
from roundup.anypy.dbm_ import anydbm, whichdb
14+
from roundup.anypy.dbm_ import anydbm, whichdb, key_in
1515

1616
class BasicDatabase:
1717
''' Provide a nice encapsulation of an anydbm store.
@@ -28,7 +28,7 @@ def __init__(self, db):
2828
def exists(self, infoid):
2929
db = self.opendb('c')
3030
try:
31-
return infoid in db
31+
return key_in(db, infoid)
3232
finally:
3333
db.close()
3434

@@ -60,7 +60,7 @@ def cache_db_type(self, path):
6060
def get(self, infoid, value, default=_marker):
6161
db = self.opendb('c')
6262
try:
63-
if infoid in db:
63+
if key_in(db, infoid):
6464
values = marshal.loads(db[infoid])
6565
else:
6666
if default != self._marker:
@@ -85,7 +85,7 @@ def getall(self, infoid):
8585
def set(self, infoid, **newvalues):
8686
db = self.opendb('c')
8787
try:
88-
if infoid in db:
88+
if key_in(db, infoid):
8989
values = marshal.loads(db[infoid])
9090
else:
9191
values = {'__timestamp': time.time()}
@@ -104,7 +104,7 @@ def list(self):
104104
def destroy(self, infoid):
105105
db = self.opendb('c')
106106
try:
107-
if infoid in db:
107+
if key_in(db, infoid):
108108
del db[infoid]
109109
finally:
110110
db.close()

0 commit comments

Comments
 (0)