| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,8 +97,9 @@ def _update(self): | |||
| 97 | 97 | try: | |
| 98 | 98 | f = _io.open(self._dirfile, 'r', encoding="Latin-1") | |
| 99 | 99 | except OSError: | |
| 100 | - pass | ||
| 100 | + self._modified = not self._readonly | ||
| 101 | 101 | else: | |
| 102 | + self._modified = False | ||
| 102 | 103 | with f: | |
| 103 | 104 | for line in f: | |
| 104 | 105 | line = line.rstrip() | |
@@ -113,7 +114,7 @@ def _commit(self): | |||
| 113 | 114 | # CAUTION: It's vital that _commit() succeed, and _commit() can | |
| 114 | 115 | # be called from __del__(). Therefore we must never reference a | |
| 115 | 116 | # global in this routine. | |
| 116 | - if self._index is None: | ||
| 117 | + if self._index is None or not self._modified: | ||
| 117 | 118 | return # nothing to do | |
| 118 | 119 | ||
| 119 | 120 | try: | |
@@ -197,6 +198,7 @@ def __setitem__(self, key, val): | |||
| 197 | 198 | elif not isinstance(val, (bytes, bytearray)): | |
| 198 | 199 | raise TypeError("values must be bytes or strings") | |
| 199 | 200 | self._verify_open() | |
| 201 | + self._modified = True | ||
| 200 | 202 | if key not in self._index: | |
| 201 | 203 | self._addkey(key, self._addval(val)) | |
| 202 | 204 | else: | |
@@ -229,6 +231,7 @@ def __delitem__(self, key): | |||
| 229 | 231 | if isinstance(key, str): | |
| 230 | 232 | key = key.encode('utf-8') | |
| 231 | 233 | self._verify_open() | |
| 234 | + self._modified = True | ||
| 232 | 235 | # The blocks used by the associated value are lost. | |
| 233 | 236 | del self._index[key] | |
| 234 | 237 | # XXX It's unclear why we do a _commit() here (the code always | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -360,9 +360,9 @@ def _rmtree_inner(path): | |||
| 360 | 360 | mode = 0 | |
| 361 | 361 | if stat.S_ISDIR(mode): | |
| 362 | 362 | _waitfor(_rmtree_inner, fullname, waitall=True) | |
| 363 | - _force_run(path, os.rmdir, fullname) | ||
| 363 | + _force_run(fullname, os.rmdir, fullname) | ||
| 364 | 364 | else: | |
| 365 | - _force_run(path, os.unlink, fullname) | ||
| 365 | + _force_run(fullname, os.unlink, fullname) | ||
| 366 | 366 | _waitfor(_rmtree_inner, path, waitall=True) | |
| 367 | 367 | _waitfor(lambda p: _force_run(p, os.rmdir, p), path) | |
| 368 | 368 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | import io | |
| 6 | 6 | import operator | |
| 7 | 7 | import os | |
| 8 | + import stat | ||
| 8 | 9 | import unittest | |
| 9 | 10 | import warnings | |
| 10 | 11 | import dbm.dumb as dumbdbm | |
@@ -259,6 +260,21 @@ def test_invalid_flag(self): | |||
| 259 | 260 | f = dumbdbm.open(_fname, flag) | |
| 260 | 261 | f.close() | |
| 261 | 262 | ||
| 263 | + @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') | ||
| 264 | + def test_readonly_files(self): | ||
| 265 | + with support.temp_dir() as dir: | ||
| 266 | + fname = os.path.join(dir, 'db') | ||
| 267 | + with dumbdbm.open(fname, 'n') as f: | ||
| 268 | + self.assertEqual(list(f.keys()), []) | ||
| 269 | + for key in self._dict: | ||
| 270 | + f[key] = self._dict[key] | ||
| 271 | + os.chmod(fname + ".dir", stat.S_IRUSR) | ||
| 272 | + os.chmod(fname + ".dat", stat.S_IRUSR) | ||
| 273 | + os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR) | ||
| 274 | + with dumbdbm.open(fname, 'r') as f: | ||
| 275 | + self.assertEqual(sorted(f.keys()), sorted(self._dict)) | ||
| 276 | + f.close() # don't write | ||
| 277 | + | ||
| 262 | 278 | def tearDown(self): | |
| 263 | 279 | _delete_files() | |
| 264 | 280 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,9 @@ Core and Builtins | |||
| 13 | 13 | Library | |
| 14 | 14 | ------- | |
| 15 | 15 | ||
| 16 | + - Issue #28847: dbm.dumb now supports reading read-only files and no longer | ||
| 17 | + writes the index file when it is not changed. | ||
| 18 | + | ||
| 16 | 19 | ||
| 17 | 20 | What's New in Python 3.6.0 release candidate 1 | |
| 18 | 21 | ============================================== | |
| Back | FazBrowse Home | New Git URL |
0 commit comments