]> granicus.if.org Git - python/commitdiff
Fix SF bug # 897820 - we can no longer use the DB_TRUNCATE flag when
authorGregory P. Smith <greg@mad-scientist.com>
Sun, 27 Jun 2004 22:56:42 +0000 (22:56 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Sun, 27 Jun 2004 22:56:42 +0000 (22:56 +0000)
opening the DB to implement legacy interface flag='n' support as
BerkeleyDB 4.2.52 no longer allows it in transaction protected
environments.  Do the os.unlink ourselves.

Lib/bsddb/__init__.py

index 90bf92bc5e591e1593d093d07d807ae178bf7155..3ee34416ddbe32f51beff115ce2de04a2cfcb2f2 100644 (file)
@@ -59,7 +59,7 @@ error = db.DBError  # So bsddb.error will mean something...
 
 #----------------------------------------------------------------------
 
-import sys
+import sys, os
 
 # for backwards compatibility with python versions older than 2.3, the
 # iterator interface is dynamically defined and added using a mixin
@@ -281,7 +281,7 @@ class _DBWithCursor(_iter_mixin):
 def hashopen(file, flag='c', mode=0666, pgsize=None, ffactor=None, nelem=None,
             cachesize=None, lorder=None, hflags=0):
 
-    flags = _checkflag(flag)
+    flags = _checkflag(flag, file)
     e = _openDBEnv()
     d = db.DB(e)
     d.set_flags(hflags)
@@ -299,7 +299,7 @@ def btopen(file, flag='c', mode=0666,
             btflags=0, cachesize=None, maxkeypage=None, minkeypage=None,
             pgsize=None, lorder=None):
 
-    flags = _checkflag(flag)
+    flags = _checkflag(flag, file)
     e = _openDBEnv()
     d = db.DB(e)
     if cachesize is not None: d.set_cachesize(0, cachesize)
@@ -318,7 +318,7 @@ def rnopen(file, flag='c', mode=0666,
             rnflags=0, cachesize=None, pgsize=None, lorder=None,
             rlen=None, delim=None, source=None, pad=None):
 
-    flags = _checkflag(flag)
+    flags = _checkflag(flag, file)
     e = _openDBEnv()
     d = db.DB(e)
     if cachesize is not None: d.set_cachesize(0, cachesize)
@@ -339,7 +339,7 @@ def _openDBEnv():
     e.open('.', db.DB_PRIVATE | db.DB_CREATE | db.DB_THREAD | db.DB_INIT_LOCK | db.DB_INIT_MPOOL)
     return e
 
-def _checkflag(flag):
+def _checkflag(flag, file):
     if flag == 'r':
         flags = db.DB_RDONLY
     elif flag == 'rw':
@@ -349,7 +349,12 @@ def _checkflag(flag):
     elif flag == 'c':
         flags =  db.DB_CREATE
     elif flag == 'n':
-        flags = db.DB_CREATE | db.DB_TRUNCATE
+        flags = db.DB_CREATE
+        #flags = db.DB_CREATE | db.DB_TRUNCATE
+        # we used db.DB_TRUNCATE flag for this before but BerkeleyDB
+        # 4.2.52 changed to disallowed truncate with txn environments.
+        if os.path.isfile(file):
+            os.unlink(file)
     else:
         raise error, "flags should be one of 'r', 'w', 'c' or 'n'"
     return flags | db.DB_THREAD