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

Skip to content

Commit 4fc7942

Browse files
Issue #28847: A deprecation warning is now emitted if the index file is missed
and recreated in the 'r' and 'w' modes (will be an error in future Python releases).
1 parent 43153e4 commit 4fc7942

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

Lib/dbm/dumb.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, filebasename, mode, flag='c'):
6868

6969
# Handle the creation
7070
self._create(flag)
71-
self._update()
71+
self._update(flag)
7272

7373
def _create(self, flag):
7474
if flag == 'n':
@@ -92,12 +92,17 @@ def _create(self, flag):
9292
f.close()
9393

9494
# Read directory file into the in-memory index dict.
95-
def _update(self):
95+
def _update(self, flag):
9696
self._index = {}
9797
try:
9898
f = _io.open(self._dirfile, 'r', encoding="Latin-1")
9999
except OSError:
100100
self._modified = not self._readonly
101+
if flag not in ('c', 'n'):
102+
import warnings
103+
warnings.warn("The index file is missing, the "
104+
"semantics of the 'c' flag will be used.",
105+
DeprecationWarning, stacklevel=4)
101106
else:
102107
self._modified = False
103108
with f:

Lib/test/test_dbm_dumb.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ def test_warn_on_ignored_flags(self):
252252
f = dumbdbm.open(_fname, value)
253253
f.close()
254254

255+
def test_missing_index(self):
256+
with dumbdbm.open(_fname, 'n') as f:
257+
pass
258+
os.unlink(_fname + '.dir')
259+
for value in ('r', 'w'):
260+
with self.assertWarnsRegex(DeprecationWarning,
261+
"The index file is missing, the "
262+
"semantics of the 'c' flag will "
263+
"be used."):
264+
f = dumbdbm.open(_fname, value)
265+
f.close()
266+
self.assertEqual(os.path.exists(_fname + '.dir'), value == 'w')
267+
self.assertFalse(os.path.exists(_fname + '.bak'))
268+
255269
def test_invalid_flag(self):
256270
for flag in ('x', 'rf', None):
257271
with self.assertWarnsRegex(DeprecationWarning,

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ Library
166166
-------
167167

168168
- Issue #28847: dbm.dumb now supports reading read-only files and no longer
169-
writes the index file when it is not changed.
169+
writes the index file when it is not changed. A deprecation warning is now
170+
emitted if the index file is missed and recreated in the 'r' and 'w' modes
171+
(will be an error in future Python releases).
170172

171173
- Issue #27030: Unknown escapes consisting of ``'\'`` and an ASCII letter in
172174
re.sub() replacement templates regular expressions now are errors.

0 commit comments

Comments
 (0)