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

Skip to content

Commit 525c25d

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 d9a7c4b commit 525c25d

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
@@ -67,10 +67,10 @@ def open(file, flag = 'r', mode = 0o666):
6767
if not _defaultmod:
6868
raise ImportError("no dbm clone found; tried %s" % _names)
6969

70-
# guess the type of an existing database
71-
result = whichdb(file)
70+
# guess the type of an existing database, if not creating a new one
71+
result = whichdb(file) if 'n' not in flag else None
7272
if result is None:
73-
# db doesn't exist
73+
# db doesn't exist or 'n' flag was specified to create a new db
7474
if 'c' in flag or 'n' in flag:
7575
# file doesn't exist and the new flag was used so use default type
7676
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
@@ -169,6 +169,7 @@ Benjamin Collar
169169
Jeffery Collins
170170
Robert Collins
171171
Paul Colomiets
172+
Denver Coneybeare
172173
Geremy Condra
173174
Juan José Conti
174175
Matt Conway

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #11491: dbm.error is no longer raised when dbm.open is called with
38+
the "n" as the flag argument and the file exists. The behavior matches
39+
the documentation and general logic.
40+
3741
- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
3842
operations when the rounding mode is ROUND_FLOOR.
3943

0 commit comments

Comments
 (0)