summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2002-08-02 17:12:15 (GMT)
committerSkip Montanaro <skip@pobox.com>2002-08-02 17:12:15 (GMT)
commit404378f83432651429634d7e2a66d2b079397099 (patch)
tree2b320aab0d8c32cbfe6828dc80ef652e032db993
parent13a5678a5152b019300baeb5dcfe5bbc780e84d9 (diff)
downloadcpython-404378f83432651429634d7e2a66d2b079397099.zip
cpython-404378f83432651429634d7e2a66d2b079397099.tar.gz
cpython-404378f83432651429634d7e2a66d2b079397099.tar.bz2
catch the situation where Berkeley DB is used to emulate dbm(3) library
functions. In this case, calling dbm.open("foo", "c") actually creates a file named "foo.db".
-rw-r--r--Lib/whichdb.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/Lib/whichdb.py b/Lib/whichdb.py
index 8b31003..cc95ed5 100644
--- a/Lib/whichdb.py
+++ b/Lib/whichdb.py
@@ -1,6 +1,16 @@
"""Guess which db package to use to open a db file."""
import os
+import struct
+
+try:
+ import dbm
+ _dbmerror = dbm.error
+except ImportError:
+ dbm = None
+ # just some sort of valid exception which might be raised in the
+ # dbm test
+ _dbmerror = IOError
def whichdb(filename):
"""Guess which db package to use to open a db file.
@@ -15,8 +25,6 @@ def whichdb(filename):
database using that module may still fail.
"""
- import struct
-
# Check for dbm first -- this has a .pag and a .dir file
try:
f = open(filename + os.extsep + "pag", "rb")
@@ -25,7 +33,20 @@ def whichdb(filename):
f.close()
return "dbm"
except IOError:
- pass
+ # some dbm emulations based on Berkeley DB generate a .db file
+ # some do not, but they should be caught by the dbhash checks
+ try:
+ f = open(filename + os.extsep + "db", "rb")
+ f.close()
+ # guarantee we can actually open the file using dbm
+ # kind of overkill, but since we are dealing with emulations
+ # it seems like a prudent step
+ if dbm is not None:
+ d = dbm.open(filename)
+ d.close()
+ return "dbm"
+ except (IOError, _dbmerror):
+ pass
# Check for dumbdbm next -- this has a .dir and and a .dat file
try: