]> granicus.if.org Git - python/commitdiff
Fix #11491. When dbm.open was called with a file which already exists and
authorbriancurtin <brian.curtin@gmail.com>
Mon, 14 Mar 2011 19:35:35 +0000 (15:35 -0400)
committerbriancurtin <brian.curtin@gmail.com>
Mon, 14 Mar 2011 19:35:35 +0000 (15:35 -0400)
the "flag" argument is "n", dbm.error was being raised. As documented,
dbm.open(...,flag='n') will now "Always create a new, empty database,
open for reading and writing", regardless of a previous file existing.

Lib/dbm/__init__.py
Lib/test/test_dbm.py
Misc/ACKS
Misc/NEWS

index c224847597f0f7b04a35500c30c4249bfa2b98cd..99c16379a957e9b701232c06543a0cd78c7902ac 100644 (file)
@@ -68,10 +68,10 @@ def open(file, flag = 'r', mode = 0o666):
         if not _defaultmod:
             raise ImportError("no dbm clone found; tried %s" % _names)
 
-    # guess the type of an existing database
-    result = whichdb(file)
+    # guess the type of an existing database, if not creating a new one
+    result = whichdb(file) if 'n' not in flag else None
     if result is None:
-        # db doesn't exist
+        # db doesn't exist or 'n' flag was specified to create a new db
         if 'c' in flag or 'n' in flag:
             # file doesn't exist and the new flag was used so use default type
             mod = _defaultmod
index 818be453f47dcafea0088e229d421b98d476a9cc..ce48cfd5ced6d5af5d88f55fbfabc6f398101aa0 100644 (file)
@@ -70,6 +70,14 @@ class AnyDBMTestCase(unittest.TestCase):
         self.read_helper(f)
         f.close()
 
+    def test_anydbm_creation_n_file_exists_with_invalid_contents(self):
+        with open(_fname, "w") as w:
+            pass # create an empty file
+
+        f = dbm.open(_fname, 'n')
+        self.addCleanup(f.close)
+        self.assertEqual(len(f), 0)
+
     def test_anydbm_modification(self):
         self.init_db()
         f = dbm.open(_fname, 'c')
index a18dd18efebc5f008643ed742e1a52d438a8f8c9..c95e76dff7398288bc18f92316f3270152fbec66 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -154,6 +154,7 @@ Terrence Cole
 Benjamin Collar
 Jeffery Collins
 Paul Colomiets
+Denver Coneybeare
 Matt Conway
 David M. Cooke
 Greg Copeland
index 9eb0537dd52132bb8640dcf991a28efa0a3be397..37b247730d07b45fc0b03edb19f37a75840467e5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #11491: dbm.error is no longer raised when dbm.open is called with
+  the "n" as the flag argument and the file exists. The behavior matches
+  the documentation and general logic.
+   
 - Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
   operations when the rounding mode is ROUND_FLOOR.