From a6b3caad417c0472b611be251d623331744079a5 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 27 Jun 2004 22:56:42 +0000 Subject: [PATCH] Fix SF bug # 897820 - we can no longer use the DB_TRUNCATE flag when 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 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Lib/bsddb/__init__.py b/Lib/bsddb/__init__.py index 90bf92bc5e5..3ee34416ddb 100644 --- a/Lib/bsddb/__init__.py +++ b/Lib/bsddb/__init__.py @@ -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