changeset: 105503:2f59be67830c parent: 105500:04f096b3ac9c parent: 105502:0c532bd28539 user: Serhiy Storchaka date: Wed Dec 07 11:02:18 2016 +0200 files: Lib/test/support/__init__.py Misc/NEWS description: Issue #28847: dbm.dumb now supports reading read-only files and no longer writes the index file when it is not changed. diff -r 04f096b3ac9c -r 2f59be67830c Lib/dbm/dumb.py --- a/Lib/dbm/dumb.py Wed Dec 07 02:02:48 2016 -0500 +++ b/Lib/dbm/dumb.py Wed Dec 07 11:02:18 2016 +0200 @@ -97,8 +97,9 @@ try: f = _io.open(self._dirfile, 'r', encoding="Latin-1") except OSError: - pass + self._modified = not self._readonly else: + self._modified = False with f: for line in f: line = line.rstrip() @@ -113,7 +114,7 @@ # CAUTION: It's vital that _commit() succeed, and _commit() can # be called from __del__(). Therefore we must never reference a # global in this routine. - if self._index is None: + if self._index is None or not self._modified: return # nothing to do try: @@ -197,6 +198,7 @@ elif not isinstance(val, (bytes, bytearray)): raise TypeError("values must be bytes or strings") self._verify_open() + self._modified = True if key not in self._index: self._addkey(key, self._addval(val)) else: @@ -229,6 +231,7 @@ if isinstance(key, str): key = key.encode('utf-8') self._verify_open() + self._modified = True # The blocks used by the associated value are lost. del self._index[key] # XXX It's unclear why we do a _commit() here (the code always diff -r 04f096b3ac9c -r 2f59be67830c Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Wed Dec 07 02:02:48 2016 -0500 +++ b/Lib/test/support/__init__.py Wed Dec 07 11:02:18 2016 +0200 @@ -360,9 +360,9 @@ mode = 0 if stat.S_ISDIR(mode): _waitfor(_rmtree_inner, fullname, waitall=True) - _force_run(path, os.rmdir, fullname) + _force_run(fullname, os.rmdir, fullname) else: - _force_run(path, os.unlink, fullname) + _force_run(fullname, os.unlink, fullname) _waitfor(_rmtree_inner, path, waitall=True) _waitfor(lambda p: _force_run(p, os.rmdir, p), path) else: diff -r 04f096b3ac9c -r 2f59be67830c Lib/test/test_dbm_dumb.py --- a/Lib/test/test_dbm_dumb.py Wed Dec 07 02:02:48 2016 -0500 +++ b/Lib/test/test_dbm_dumb.py Wed Dec 07 11:02:18 2016 +0200 @@ -5,6 +5,7 @@ import io import operator import os +import stat import unittest import warnings import dbm.dumb as dumbdbm @@ -259,6 +260,21 @@ f = dumbdbm.open(_fname, flag) f.close() + @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') + def test_readonly_files(self): + with support.temp_dir() as dir: + fname = os.path.join(dir, 'db') + with dumbdbm.open(fname, 'n') as f: + self.assertEqual(list(f.keys()), []) + for key in self._dict: + f[key] = self._dict[key] + os.chmod(fname + ".dir", stat.S_IRUSR) + os.chmod(fname + ".dat", stat.S_IRUSR) + os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR) + with dumbdbm.open(fname, 'r') as f: + self.assertEqual(sorted(f.keys()), sorted(self._dict)) + f.close() # don't write + def tearDown(self): _delete_files() diff -r 04f096b3ac9c -r 2f59be67830c Misc/NEWS --- a/Misc/NEWS Wed Dec 07 02:02:48 2016 -0500 +++ b/Misc/NEWS Wed Dec 07 11:02:18 2016 +0200 @@ -165,6 +165,9 @@ Library ------- +- Issue #28847: dbm.dumb now supports reading read-only files and no longer + writes the index file when it is not changed. + - Issue #27030: Unknown escapes consisting of ``'\'`` and an ASCII letter in re.sub() replacement templates regular expressions now are errors.