Skip to content

Commit b398d33

Browse files
Issue python#18039: dbm.dump.open() now always creates a new database when the
flag has the value 'n'. Patch by Claudiu Popa.
1 parent 4c4cde7 commit b398d33

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

Doc/library/dbm.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,18 @@ The module defines the following:
316316
dumbdbm database is created, files with :file:`.dat` and :file:`.dir` extensions
317317
are created.
318318

319-
The optional *flag* argument is currently ignored; the database is always opened
320-
for update, and will be created if it does not exist.
319+
The optional *flag* argument supports only the semantics of ``'c'``
320+
and ``'n'`` values. Other values will default to database being always
321+
opened for update, and will be created if it does not exist.
321322

322323
The optional *mode* argument is the Unix mode of the file, used only when the
323324
database has to be created. It defaults to octal ``0o666`` (and will be modified
324325
by the prevailing umask).
325326

327+
.. versionchanged:: 3.5
328+
:func:`.open` always creates a new database when the flag has the value
329+
``'n'``.
330+
326331
In addition to the methods provided by the
327332
:class:`collections.abc.MutableMapping` class, :class:`dumbdbm` objects
328333
provide the following method:

Lib/dbm/dumb.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class _Database(collections.MutableMapping):
4444
_os = _os # for _commit()
4545
_io = _io # for _commit()
4646

47-
def __init__(self, filebasename, mode):
47+
def __init__(self, filebasename, mode, flag='c'):
4848
self._mode = mode
4949

5050
# The directory file is a text file. Each line looks like
@@ -64,14 +64,24 @@ def __init__(self, filebasename, mode):
6464
# The index is an in-memory dict, mirroring the directory file.
6565
self._index = None # maps keys to (pos, siz) pairs
6666

67+
# Handle the creation
68+
self._create(flag)
69+
self._update()
70+
71+
def _create(self, flag):
72+
if flag == 'n':
73+
for filename in (self._datfile, self._bakfile, self._dirfile):
74+
try:
75+
_os.remove(filename)
76+
except OSError:
77+
pass
6778
# Mod by Jack: create data file if needed
6879
try:
6980
f = _io.open(self._datfile, 'r', encoding="Latin-1")
7081
except OSError:
7182
f = _io.open(self._datfile, 'w', encoding="Latin-1")
7283
self._chmod(self._datfile)
7384
f.close()
74-
self._update()
7585

7686
# Read directory file into the in-memory index dict.
7787
def _update(self):
@@ -266,20 +276,20 @@ def __exit__(self, *args):
266276
self.close()
267277

268278

269-
def open(file, flag=None, mode=0o666):
279+
def open(file, flag='c', mode=0o666):
270280
"""Open the database file, filename, and return corresponding object.
271281
272282
The flag argument, used to control how the database is opened in the
273-
other DBM implementations, is ignored in the dbm.dumb module; the
274-
database is always opened for update, and will be created if it does
275-
not exist.
283+
other DBM implementations, supports only the semantics of 'c' and 'n'
284+
values. Other values will default to the semantics of 'c' value:
285+
the database will always opened for update and will be created if it
286+
does not exist.
276287
277288
The optional mode argument is the UNIX mode of the file, used only when
278289
the database has to be created. It defaults to octal code 0o666 (and
279290
will be modified by the prevailing umask).
280291
281292
"""
282-
# flag argument is currently ignored
283293

284294
# Modify mode depending on the umask
285295
try:
@@ -290,5 +300,4 @@ def open(file, flag=None, mode=0o666):
290300
else:
291301
# Turn off any bits that are set in the umask
292302
mode = mode & (~um)
293-
294-
return _Database(file, mode)
303+
return _Database(file, mode, flag=flag)

Lib/test/test_dbm_dumb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ def test_check_closed(self):
217217
self.assertEqual(str(cm.exception),
218218
"DBM object has already been closed")
219219

220+
def test_create_new(self):
221+
with dumbdbm.open(_fname, 'n') as f:
222+
for k in self._dict:
223+
f[k] = self._dict[k]
224+
225+
with dumbdbm.open(_fname, 'n') as f:
226+
self.assertEqual(f.keys(), [])
227+
220228
def tearDown(self):
221229
_delete_files()
222230

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Core and Builtins
9292
Library
9393
-------
9494

95+
- Issue #18039: dbm.dump.open() now always creates a new database when the
96+
flag has the value 'n'. Patch by Claudiu Popa.
97+
9598
- Issue #21326: Add a new is_closed() method to asyncio.BaseEventLoop.
9699
run_forever() and run_until_complete() methods of asyncio.BaseEventLoop now
97100
raise an exception if the event loop was closed.

0 commit comments

Comments
 (0)