Kaydet (Commit) 7c29f071 authored tarafından R. David Murray's avatar R. David Murray

Issue 5754: tweak shelve doc wording to make it clearer that even when

writeback=True values are written to the backing store when assigned to
the shelf.  Add test to confirm that this happens.  Doc patch and added
test by Robert Lehmann.  I also fixed the cross references to the sync
and close methods.
üst 63e4fd7e
...@@ -30,14 +30,15 @@ lots of shared sub-objects. The keys are ordinary strings. ...@@ -30,14 +30,15 @@ lots of shared sub-objects. The keys are ordinary strings.
Because of Python semantics, a shelf cannot know when a mutable Because of Python semantics, a shelf cannot know when a mutable
persistent-dictionary entry is modified. By default modified objects are persistent-dictionary entry is modified. By default modified objects are
written only when assigned to the shelf (see :ref:`shelve-example`). If the written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
optional *writeback* parameter is set to *True*, all entries accessed are optional *writeback* parameter is set to *True*, all entries accessed are also
cached in memory, and written back on :meth:`sync` and :meth:`close`; this cached in memory, and written back on :meth:`~Shelf.sync` and
can make it handier to mutate mutable entries in the persistent dictionary, :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
but, if many entries are accessed, it can consume vast amounts of memory for the persistent dictionary, but, if many entries are accessed, it can consume
the cache, and it can make the close operation very slow since all accessed vast amounts of memory for the cache, and it can make the close operation
entries are written back (there is no way to determine which accessed entries very slow since all accessed entries are written back (there is no way to
are mutable, nor which ones were actually mutated). determine which accessed entries are mutable, nor which ones were actually
mutated).
.. note:: .. note::
......
...@@ -90,6 +90,17 @@ class TestCase(unittest.TestCase): ...@@ -90,6 +90,17 @@ class TestCase(unittest.TestCase):
self.assertEqual(len(d1), 1) self.assertEqual(len(d1), 1)
self.assertEqual(len(d2), 1) self.assertEqual(len(d2), 1)
def test_writeback_also_writes_immediately(self):
# Issue 5754
d = {}
s = shelve.Shelf(d, writeback=True)
s['key'] = [1]
p1 = d['key'] # Will give a KeyError if backing store not updated
s['key'].append(2)
s.close()
p2 = d['key']
self.assertNotEqual(p1, p2) # Write creates new object in store
from test import mapping_tests from test import mapping_tests
......
...@@ -444,6 +444,7 @@ Luc Lefebvre ...@@ -444,6 +444,7 @@ Luc Lefebvre
Vincent Legoll Vincent Legoll
Kip Lehman Kip Lehman
Joerg Lehmann Joerg Lehmann
Robert Lehmann
Luke Kenneth Casson Leighton Luke Kenneth Casson Leighton
Marc-Andre Lemburg Marc-Andre Lemburg
John Lenton John Lenton
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment