Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94eceeb

Browse files
committed
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.
1 parent 2503249 commit 94eceeb

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

Lib/dbm/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def open(file, flag = 'r', mode = 0o666):
6868
if not _defaultmod:
6969
raise ImportError("no dbm clone found; tried %s" % _names)
7070

71-
# guess the type of an existing database
72-
result = whichdb(file)
71+
# guess the type of an existing database, if not creating a new one
72+
result = whichdb(file) if 'n' not in flag else None
7373
if result is None:
74-
# db doesn't exist
74+
# db doesn't exist or 'n' flag was specified to create a new db
7575
if 'c' in flag or 'n' in flag:
7676
# file doesn't exist and the new flag was used so use default type
7777
mod = _defaultmod

Lib/test/test_dbm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def test_anydbm_creation(self):
7070
self.read_helper(f)
7171
f.close()
7272

73+
def test_anydbm_creation_n_file_exists_with_invalid_contents(self):
74+
with open(_fname, "w") as w:
75+
pass # create an empty file
76+
77+
f = dbm.open(_fname, 'n')
78+
self.addCleanup(f.close)
79+
self.assertEqual(len(f), 0)
80+
7381
def test_anydbm_modification(self):
7482
self.init_db()
7583
f = dbm.open(_fname, 'c')

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Terrence Cole
154154
Benjamin Collar
155155
Jeffery Collins
156156
Paul Colomiets
157+
Denver Coneybeare
157158
Matt Conway
158159
David M. Cooke
159160
Greg Copeland

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #11491: dbm.error is no longer raised when dbm.open is called with
44+
the "n" as the flag argument and the file exists. The behavior matches
45+
the documentation and general logic.
46+
4347
- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
4448
operations when the rounding mode is ROUND_FLOOR.
4549

0 commit comments

Comments
 (0)