summaryrefslogtreecommitdiffstats
path: root/Lib/whichdb.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-06-08 13:13:16 (GMT)
committerGuido van Rossum <guido@python.org>1999-06-08 13:13:16 (GMT)
commitcf09a3924f80ec7dfc706c49f8b7c60c990e594b (patch)
tree22ccd34be2ee4fccb587606b7f142dd8448a0800 /Lib/whichdb.py
parentab6a08a4b6e4a12339cb33e4cd14fdf8426c346b (diff)
downloadcpython-cf09a3924f80ec7dfc706c49f8b7c60c990e594b.zip
cpython-cf09a3924f80ec7dfc706c49f8b7c60c990e594b.tar.gz
cpython-cf09a3924f80ec7dfc706c49f8b7c60c990e594b.tar.bz2
Skip Montanaro:
I guess in 1.5.2 a new module, whichdb, was added that attempts to divine the nature of a database file. This module doesn't know anything about Berkeley DB v2 files. In v2, Sleepycat added a 12-byte null pad in front of the old magic numbers (at least for hash and btree files). I've been using v2 for awhile and upgrading to 1.5.2 broke all my anydbm.open calls. I believe the following patch corrects the problem.
Diffstat (limited to 'Lib/whichdb.py')
-rw-r--r--Lib/whichdb.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/whichdb.py b/Lib/whichdb.py
index c072e5b..aa1761c 100644
--- a/Lib/whichdb.py
+++ b/Lib/whichdb.py
@@ -31,9 +31,10 @@ def whichdb(filename):
except IOError:
return None
- # Read the first 4 bytes of the file -- the magic number
- s = f.read(4)
+ # Read the start of the file -- the magic number
+ s16 = f.read(16)
f.close()
+ s = s16[0:4]
# Return "" if not at least 4 bytes
if len(s) != 4:
@@ -53,5 +54,15 @@ def whichdb(filename):
if magic in (0x00061561, 0x61150600):
return "dbhash"
+ # BSD hash v2 has a 12-byte NULL pad in front of the file type
+ try:
+ (magic,) = struct.unpack("=l", s16[-4:])
+ except struct.error:
+ return ""
+
+ # Check for BSD hash
+ if magic in (0x00061561, 0x61150600):
+ return "dbhash"
+
# Unknown
return ""