]> granicus.if.org Git - python/commitdiff
Make close() identical to __del__() for a dumbdbm database. Make
authorTim Peters <tim.peters@gmail.com>
Sun, 13 Jul 2003 17:21:10 +0000 (17:21 +0000)
committerTim Peters <tim.peters@gmail.com>
Sun, 13 Jul 2003 17:21:10 +0000 (17:21 +0000)
closing idempotent (it used to raise a nuisance exception on the 2nd
close attempt).

Bugfix candidate?  Probably, but arguable.

Lib/dumbdbm.py
Lib/test/test_dumbdbm.py
Misc/NEWS

index 1475361f2f924204a4e55b1c4a5e8808b683bb27..7b4ddb02fb035bb723b0abe44db090bfd037ca57 100644 (file)
@@ -92,6 +92,9 @@ class _Database(UserDict.DictMixin):
         # CAUTION:  It's vital that _commit() succeed, and _commit() can
         # be called from __del__().  Therefore we must never reference a
         # global in this routine.
+        if self._index is None:
+            return  # nothing to do
+
         try:
             self._os.unlink(self._bakfile)
         except self._os.error:
@@ -204,12 +207,9 @@ class _Database(UserDict.DictMixin):
 
     def close(self):
         self._commit()
-        self._index = None
-        self._datfile = self._dirfile = self._bakfile = None
+        self._index = self._datfile = self._dirfile = self._bakfile = None
 
-    def __del__(self):
-        if self._index is not None:
-            self._commit()
+    __del__ = close
 
 
 
index 08474f7353112512f6edb8b43bce078981395340..12df673eaa485956cd6f69d1f36f2ad4ca0ae776 100644 (file)
@@ -38,6 +38,13 @@ class DumbDBMTestCase(unittest.TestCase):
         self.read_helper(f)
         f.close()
 
+    def test_close_twice(self):
+        f = dumbdbm.open(_fname)
+        f['a'] = 'b'
+        self.assertEqual(f['a'], 'b')
+        f.close()
+        f.close()
+
     def test_dumbdbm_modification(self):
         self.init_db()
         f = dumbdbm.open(_fname, 'w')
index 0f329dfe3e399e64d81ad819259d1f88d2722db2..9b9b567101d0c4ef7b39a850d332c430567687c0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,9 @@ Extension modules
 Library
 -------
 
+- Closing a dumbdbm database more than once is now harmless (it used to
+  raise a nuisance exception on the second close).
+
 - It's vital that a dumbdbm database be closed properly, else the
   on-disk data and directory files can be left in mutually inconsistent
   states.  dumbdbm.py's _Database.__del__() method attempted to close