diff options
author | Tim Potter <tpot@samba.org> | 2006-04-03 22:04:33 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 14:00:13 -0500 |
commit | 6705f888d5a8a540e341ad782fa84036c400b753 (patch) | |
tree | cb4e2fab999242cb96b32056b9cb874e62df7f23 /source4/scripting | |
parent | 1ac990ddcf8501ce551c87e70cb3722ae9f4f34b (diff) | |
download | samba-6705f888d5a8a540e341ad782fa84036c400b753.tar.gz samba-6705f888d5a8a540e341ad782fa84036c400b753.tar.bz2 samba-6705f888d5a8a540e341ad782fa84036c400b753.zip |
r14897: Do more error checking of tdb function returns and raise IOError or
KeyError exceptions as appropriate.
Add a close() function to the wrapper as we can't rely on the
Python garbage collector destroying the object and closing the tdb file
at any particular time.
(This used to be commit a40d6c792257315d1eac955718db5ec1df7e07bb)
Diffstat (limited to 'source4/scripting')
-rw-r--r-- | source4/scripting/swig/Tdb.py | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/source4/scripting/swig/Tdb.py b/source4/scripting/swig/Tdb.py index 0de43688b3..ef0165ed8b 100644 --- a/source4/scripting/swig/Tdb.py +++ b/source4/scripting/swig/Tdb.py @@ -39,25 +39,33 @@ class Tdb: def __init__(self, name, hash_size = 0, flags = tdb.TDB_DEFAULT, open_flags = os.O_RDWR | os.O_CREAT, mode = 0600): self.tdb = tdb.open(name, hash_size, flags, open_flags, mode) - + if self.tdb is None: + raise IOError, tdb.errorstr(self.tdb) + def __del__(self): - if hasattr(self, 'tdb'): - tdb.close(self.tdb) + self.close() + + def close(self): + if hasattr(self, 'tdb') and self.tdb is not None: + if tdb.close(self.tdb) == -1: + raise IOError, tdb.errorstr(self.tdb) + self.tdb = None # Random access to keys, values def __getitem__(self, key): result = tdb.fetch(self.tdb, key) if result is None: - raise KeyError, key + raise KeyError, '%s: %s' % (key, tdb.errorstr(self.tdb)) return result def __setitem__(self, key, item): - tdb.store(self.tdb, key, item) + if tdb.store(self.tdb, key, item) == -1: + raise IOError, tdb.errorstr(self.tdb) def __delitem__(self, key): if not tdb.exists(self.tdb, key): - raise KeyError, key + raise KeyError, '%s: %s' % (key, tdb.errorstr(self.tdb)) tdb.delete(self.tdb, key) def has_key(self, key): |