Code

fix up some pre-Python2.6 compatibility issues in the *dbm interface
authorrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sat, 10 Jul 2010 03:45:17 +0000 (03:45 +0000)
committerrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sat, 10 Jul 2010 03:45:17 +0000 (03:45 +0000)
git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4489 57a73879-2fb5-44c3-a270-3262357dd7e2

roundup/anypy/dbm_.py
roundup/backends/back_anydbm.py
roundup/backends/sessions_dbm.py

index 093a2f1fcdf0376558fab4f0d1ab2a6484ed6163..e8b8d8ce78a1892010670afaaee1589ae622c62f 100644 (file)
@@ -2,6 +2,14 @@
 # package containing the various implementations. The "wichdb" module's
 # whichdb() function was moved to the new "dbm" module.
 
+import sys
+if sys.version_info[:2] < (2, 6):
+    def key_in(db, key):
+        return db.has_key(key)
+else:
+    def key_in(db, key):
+        return key in db
+
 try:
     # old school first because <3 had a "dbm" module too...
     import anydbm
index baa389b4a5bf2efc66c82a692548a8ce471174b9..0bbba4c4dafacb6c032c5b698a1cdc7804cc7442 100644 (file)
@@ -24,7 +24,7 @@ __docformat__ = 'restructuredtext'
 
 import os, marshal, re, weakref, string, copy, time, shutil, logging
 
-from roundup.anypy.dbm_ import anydbm, whichdb
+from roundup.anypy.dbm_ import anydbm, whichdb, key_in
 
 from roundup import hyperdb, date, password, roundupdb, security, support
 from roundup.support import reversed
@@ -248,7 +248,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
         """
         # open the ids DB - create if if doesn't exist
         db = self.opendb('_ids', 'c')
-        if classname in db:
+        if key_in(db, classname):
             newid = db[classname] = str(int(db[classname]) + 1)
         else:
             # the count() bit is transitional - older dbs won't start at 1
@@ -322,7 +322,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
         # get from the database and save in the cache
         if db is None:
             db = self.getclassdb(classname)
-        if nodeid not in db:
+        if not key_in(db, nodeid):
             raise IndexError("no such %s %s"%(classname, nodeid))
 
         # check the uncommitted, destroyed nodes
@@ -435,7 +435,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
         # not in the cache - check the database
         if db is None:
             db = self.getclassdb(classname)
-        return nodeid in db
+        return key_in(db, nodeid)
 
     def countnodes(self, classname, db=None):
         count = 0
@@ -552,7 +552,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
             db_type = self.determine_db_type(path)
             db = self.opendb(db_name, 'w')
 
-            for key in db:
+            for key in db.keys():
                 # get the journal for this db entry
                 journal = marshal.loads(db[key])
                 l = []
@@ -679,7 +679,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
         db = self.getCachedJournalDB(classname)
 
         # now insert the journal entry
-        if nodeid in db:
+        if key_in(db, nodeid):
             # append to existing
             s = db[nodeid]
             l = marshal.loads(s)
@@ -704,12 +704,12 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database):
     def doDestroyNode(self, classname, nodeid):
         # delete from the class database
         db = self.getCachedClassDB(classname)
-        if nodeid in db:
+        if key_in(db, nodeid):
             del db[nodeid]
 
         # delete from the database
         db = self.getCachedJournalDB(classname)
-        if nodeid in db:
+        if key_in(db, nodeid):
             del db[nodeid]
 
     def rollback(self):
@@ -1530,12 +1530,12 @@ class Class(hyperdb.Class):
             db = self.db.getclassdb(self.classname)
             must_close = True
         try:
-            res.extend(db)
+            res.extend(db.keys())
 
             # remove the uncommitted, destroyed nodes
             if self.classname in self.db.destroyednodes:
                 for nodeid in self.db.destroyednodes[self.classname]:
-                    if nodeid in db:
+                    if key_in(db, nodeid):
                         res.remove(nodeid)
 
             # check retired flag
index c4e2be161fbdd9da6f5c63e6b82475cbcdcb6248..c08e353e2bab4c76c6fbd66cfe1895e785b7fd1e 100644 (file)
@@ -11,7 +11,7 @@ import os, marshal, time
 
 from roundup import hyperdb
 from roundup.i18n import _
-from roundup.anypy.dbm_ import anydbm, whichdb
+from roundup.anypy.dbm_ import anydbm, whichdb, key_in
 
 class BasicDatabase:
     ''' Provide a nice encapsulation of an anydbm store.
@@ -28,7 +28,7 @@ class BasicDatabase:
     def exists(self, infoid):
         db = self.opendb('c')
         try:
-            return infoid in db
+            return key_in(db, infoid)
         finally:
             db.close()
 
@@ -60,7 +60,7 @@ class BasicDatabase:
     def get(self, infoid, value, default=_marker):
         db = self.opendb('c')
         try:
-            if infoid in db:
+            if key_in(db, infoid):
                 values = marshal.loads(db[infoid])
             else:
                 if default != self._marker:
@@ -85,7 +85,7 @@ class BasicDatabase:
     def set(self, infoid, **newvalues):
         db = self.opendb('c')
         try:
-            if infoid in db:
+            if key_in(db, infoid):
                 values = marshal.loads(db[infoid])
             else:
                 values = {'__timestamp': time.time()}
@@ -104,7 +104,7 @@ class BasicDatabase:
     def destroy(self, infoid):
         db = self.opendb('c')
         try:
-            if infoid in db:
+            if key_in(db, infoid):
                 del db[infoid]
         finally:
             db.close()