summaryrefslogtreecommitdiffstats
path: root/Lib/dbm
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-05-28 08:43:17 (GMT)
committerGeorg Brandl <georg@python.org>2008-05-28 08:43:17 (GMT)
commitb17acad68ea21c60dbc2088644f2934032304628 (patch)
treecc040cbefab321dd34a06599f9a49a83a37977c1 /Lib/dbm
parente81f5ef1ebf57e9df2cfefabb5c5fa67defa6d2f (diff)
downloadcpython-b17acad68ea21c60dbc2088644f2934032304628.zip
cpython-b17acad68ea21c60dbc2088644f2934032304628.tar.gz
cpython-b17acad68ea21c60dbc2088644f2934032304628.tar.bz2
Make db modules' error classes inherit IOError.
Stop dbm from importing every dbm module when imported.
Diffstat (limited to 'Lib/dbm')
-rw-r--r--Lib/dbm/__init__.py42
-rw-r--r--Lib/dbm/bsd.py3
2 files changed, 20 insertions, 25 deletions
diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py
index 9fdd414..2082e07 100644
--- a/Lib/dbm/__init__.py
+++ b/Lib/dbm/__init__.py
@@ -48,27 +48,26 @@ class error(Exception):
pass
_names = ['dbm.bsd', 'dbm.gnu', 'dbm.ndbm', 'dbm.dumb']
-_errors = [error]
_defaultmod = None
_modules = {}
-for _name in _names:
- try:
- _mod = __import__(_name, fromlist=['open'])
- except ImportError:
- continue
- if not _defaultmod:
- _defaultmod = _mod
- _modules[_name] = _mod
- _errors.append(_mod.error)
-
-if not _defaultmod:
- raise ImportError("no dbm clone found; tried %s" % _names)
-
-error = tuple(_errors)
+error = (error, IOError)
def open(file, flag = 'r', mode = 0o666):
+ global _defaultmod
+ if _defaultmod is None:
+ for name in _names:
+ try:
+ mod = __import__(name, fromlist=['open'])
+ except ImportError:
+ continue
+ if not _defaultmod:
+ _defaultmod = mod
+ _modules[name] = mod
+ if not _defaultmod:
+ raise ImportError("no dbm clone found; tried %s" % _names)
+
# guess the type of an existing database
result = whichdb(file)
if result is None:
@@ -81,19 +80,14 @@ def open(file, flag = 'r', mode = 0o666):
elif result == "":
# db type cannot be determined
raise error("db type could not be determined")
+ elif result not in _modules:
+ raise error("db type is {0}, but the module is not "
+ "available".format(result))
else:
mod = _modules[result]
return mod.open(file, flag, mode)
-try:
- from dbm import ndbm
- _dbmerror = ndbm.error
-except ImportError:
- ndbm = None
- # just some sort of valid exception which might be raised in the ndbm test
- _dbmerror = IOError
-
def whichdb(filename):
"""Guess which db package to use to open a db file.
@@ -129,7 +123,7 @@ def whichdb(filename):
d = ndbm.open(filename)
d.close()
return "dbm.ndbm"
- except (IOError, _dbmerror):
+ except IOError:
pass
# Check for dumbdbm next -- this has a .dir and a .dat file
diff --git a/Lib/dbm/bsd.py b/Lib/dbm/bsd.py
index 8353f50..2dccadb 100644
--- a/Lib/dbm/bsd.py
+++ b/Lib/dbm/bsd.py
@@ -4,7 +4,8 @@ import bsddb
__all__ = ["error", "open"]
-error = bsddb.error
+class error(bsddb.error, IOError):
+ pass
def open(file, flag = 'r', mode=0o666):
return bsddb.hashopen(file, flag, mode)