[Python-checkins] cpython (merge 3.2 -> default): Fix #11491. When dbm.open was called with a file which already exists and
brian.curtin
python-checkins at python.org
Mon Mar 14 21:36:56 CET 2011
http://hg.python.org/cpython/rev/0515206e36ed
changeset: 68468:0515206e36ed
parent: 68465:bffe5809bfda
parent: 68467:ec8d64396be9
user: briancurtin <brian.curtin at gmail.com>
date: Mon Mar 14 16:36:31 2011 -0400
summary:
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.
files:
Misc/ACKS
Misc/NEWS
diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py
--- a/Lib/dbm/__init__.py
+++ b/Lib/dbm/__init__.py
@@ -67,10 +67,10 @@
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
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm.py
@@ -70,6 +70,14 @@
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
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -171,6 +171,7 @@
Jeffery Collins
Robert Collins
Paul Colomiets
+Denver Coneybeare
Geremy Condra
Juan José Conti
Matt Conway
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,10 @@
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 #1162477: Postel Principal adjustment to email date parsing: handle the
fact that some non-compliant MUAs use '.' instead of ':' in time specs.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list