diff options
-rw-r--r-- | Doc/library/shelve.rst | 17 | ||||
-rw-r--r-- | Lib/shelve.py | 9 | ||||
-rw-r--r-- | Lib/test/test_shelve.py | 13 |
3 files changed, 27 insertions, 12 deletions
diff --git a/Doc/library/shelve.rst b/Doc/library/shelve.rst index 659811a..0252597 100644 --- a/Doc/library/shelve.rst +++ b/Doc/library/shelve.rst @@ -27,14 +27,15 @@ lots of shared sub-objects. The keys are ordinary strings. Because of Python semantics, a shelf cannot know when a mutable persistent-dictionary entry is modified. By default modified objects are - written only when assigned to the shelf (see :ref:`shelve-example`). If the - optional *writeback* parameter is set to *True*, all entries accessed are - cached in memory, and written back on :meth:`sync` and :meth:`close`; this - can make it handier to mutate mutable entries in the persistent dictionary, - but, if many entries are accessed, it can consume vast amounts of memory for - the cache, and it can make the close operation very slow since all accessed - entries are written back (there is no way to determine which accessed entries - are mutable, nor which ones were actually mutated). + written *only* when assigned to the shelf (see :ref:`shelve-example`). If the + optional *writeback* parameter is set to *True*, all entries accessed are also + cached in memory, and written back on :meth:`~Shelf.sync` and + :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in + the persistent dictionary, but, if many entries are accessed, it can consume + vast amounts of memory for the cache, and it can make the close operation + very slow since all accessed entries are written back (there is no way to + determine which accessed entries are mutable, nor which ones were actually + mutated). .. note:: diff --git a/Lib/shelve.py b/Lib/shelve.py index 8271dfe..52e471a 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -136,11 +136,12 @@ class Shelf(collections.MutableMapping): self.dict.close() except AttributeError: pass - # _ClosedDict can be None when close is called from __del__ during shutdown - if _ClosedDict is None: - self.dict = None - else: + # Catch errors that may happen when close is called from __del__ + # because CPython is in interpreter shutdown. + try: self.dict = _ClosedDict() + except (NameError, TypeError): + self.dict = None def __del__(self): if not hasattr(self, 'writeback'): diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py index 9699043..066208a 100644 --- a/Lib/test/test_shelve.py +++ b/Lib/test/test_shelve.py @@ -122,6 +122,19 @@ class TestCase(unittest.TestCase): self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1) + def test_writeback_also_writes_immediately(self): + # Issue 5754 + d = {} + key = 'key' + encodedkey = key.encode('utf-8') + s = shelve.Shelf(d, writeback=True) + s[key] = [1] + p1 = d[encodedkey] # Will give a KeyError if backing store not updated + s['key'].append(2) + s.close() + p2 = d[encodedkey] + self.assertNotEqual(p1, p2) # Write creates new object in store + from test import mapping_tests |