summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbriancurtin <brian.curtin@gmail.com>2011-03-14 20:03:54 (GMT)
committerbriancurtin <brian.curtin@gmail.com>2011-03-14 20:03:54 (GMT)
commit525c25d42f8740111d14b9f8ca56cc2762175eaf (patch)
treec5ba27fca894c9a4fb455c0ada32eb1d004cc950
parentd9a7c4b8e56ddf8be878de284b8a29d4346ec0f8 (diff)
downloadcpython-525c25d42f8740111d14b9f8ca56cc2762175eaf.zip
cpython-525c25d42f8740111d14b9f8ca56cc2762175eaf.tar.gz
cpython-525c25d42f8740111d14b9f8ca56cc2762175eaf.tar.bz2
Fix #11491. When dbm.open was called with a file which already exists and
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.
-rw-r--r--Lib/dbm/__init__.py6
-rw-r--r--Lib/test/test_dbm.py8
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 16 insertions, 3 deletions
diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py
index b2a254a..6e890f3 100644
--- a/Lib/dbm/__init__.py
+++ b/Lib/dbm/__init__.py
@@ -67,10 +67,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
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py
index 74c9c44..26d4c14 100644
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm.py
@@ -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')
diff --git a/Misc/ACKS b/Misc/ACKS
index d507ade..a11bcf0 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -169,6 +169,7 @@ Benjamin Collar
Jeffery Collins
Robert Collins
Paul Colomiets
+Denver Coneybeare
Geremy Condra
Juan José Conti
Matt Conway
diff --git a/Misc/NEWS b/Misc/NEWS
index bcda530..f3442a5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,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.