Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit e3bbfd4

Browse files
author
Anselm Kruis
committed
merge branch 3.5
2 parents fb4ed9b + 028ace1 commit e3bbfd4

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

Lib/dbm/dumb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class _Database(collections.MutableMapping):
4747

4848
def __init__(self, filebasename, mode, flag='c'):
4949
self._mode = mode
50+
self._readonly = (flag == 'r')
5051

5152
# The directory file is a text file. Each line looks like
5253
# "%r, (%d, %d)\n" % (key, pos, siz)
@@ -91,8 +92,9 @@ def _update(self):
9192
try:
9293
f = _io.open(self._dirfile, 'r', encoding="Latin-1")
9394
except OSError:
94-
pass
95+
self._modified = not self._readonly
9596
else:
97+
self._modified = False
9698
with f:
9799
for line in f:
98100
line = line.rstrip()
@@ -107,7 +109,7 @@ def _commit(self):
107109
# CAUTION: It's vital that _commit() succeed, and _commit() can
108110
# be called from __del__(). Therefore we must never reference a
109111
# global in this routine.
110-
if self._index is None:
112+
if self._index is None or not self._modified:
111113
return # nothing to do
112114

113115
try:
@@ -187,6 +189,7 @@ def __setitem__(self, key, val):
187189
elif not isinstance(val, (bytes, bytearray)):
188190
raise TypeError("values must be bytes or strings")
189191
self._verify_open()
192+
self._modified = True
190193
if key not in self._index:
191194
self._addkey(key, self._addval(val))
192195
else:
@@ -215,6 +218,7 @@ def __delitem__(self, key):
215218
if isinstance(key, str):
216219
key = key.encode('utf-8')
217220
self._verify_open()
221+
self._modified = True
218222
# The blocks used by the associated value are lost.
219223
del self._index[key]
220224
# XXX It's unclear why we do a _commit() here (the code always

Lib/test/support/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ def _rmtree_inner(path):
364364
mode = 0
365365
if stat.S_ISDIR(mode):
366366
_waitfor(_rmtree_inner, fullname, waitall=True)
367-
_force_run(path, os.rmdir, fullname)
367+
_force_run(fullname, os.rmdir, fullname)
368368
else:
369-
_force_run(path, os.unlink, fullname)
369+
_force_run(fullname, os.unlink, fullname)
370370
_waitfor(_rmtree_inner, path, waitall=True)
371371
_waitfor(lambda p: _force_run(p, os.rmdir, p), path)
372372
else:
@@ -940,7 +940,7 @@ def temp_dir(path=None, quiet=False):
940940
yield path
941941
finally:
942942
if dir_created and pid == os.getpid():
943-
shutil.rmtree(path)
943+
rmtree(path)
944944

945945
@contextlib.contextmanager
946946
def change_cwd(path, quiet=False):

Lib/test/test_dbm_dumb.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io
66
import operator
77
import os
8+
import stat
89
import unittest
910
import dbm.dumb as dumbdbm
1011
from test import support
@@ -234,6 +235,21 @@ def test_eval(self):
234235
pass
235236
self.assertEqual(stdout.getvalue(), '')
236237

238+
@unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()')
239+
def test_readonly_files(self):
240+
with support.temp_dir() as dir:
241+
fname = os.path.join(dir, 'db')
242+
with dumbdbm.open(fname, 'n') as f:
243+
self.assertEqual(list(f.keys()), [])
244+
for key in self._dict:
245+
f[key] = self._dict[key]
246+
os.chmod(fname + ".dir", stat.S_IRUSR)
247+
os.chmod(fname + ".dat", stat.S_IRUSR)
248+
os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR)
249+
with dumbdbm.open(fname, 'r') as f:
250+
self.assertEqual(sorted(f.keys()), sorted(self._dict))
251+
f.close() # don't write
252+
237253
def tearDown(self):
238254
_delete_files()
239255

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Core and Builtins
121121
Library
122122
-------
123123

124+
- Issue #28847: dbm.dumb now supports reading read-only files and no longer
125+
writes the index file when it is not changed.
126+
124127
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
125128
from_buffer_copy() methods on abstract classes like Array.
126129

0 commit comments

Comments
 (0)