Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddb3ed0

Browse files
committed
Merged revisions 78141-78142 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r78141 | r.david.murray | 2010-02-10 20:38:42 -0500 (Wed, 10 Feb 2010) | 6 lines 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. ........ r78142 | r.david.murray | 2010-02-10 20:56:42 -0500 (Wed, 10 Feb 2010) | 3 lines Improve issue 7835 fix per MAL to handle the case that the module dictionary has also been cleared. ........
1 parent f8b60b2 commit ddb3ed0

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

Doc/library/shelve.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ lots of shared sub-objects. The keys are ordinary strings.
2727

2828
Because of Python semantics, a shelf cannot know when a mutable
2929
persistent-dictionary entry is modified. By default modified objects are
30-
written only when assigned to the shelf (see :ref:`shelve-example`). If the
31-
optional *writeback* parameter is set to *True*, all entries accessed are
32-
cached in memory, and written back on :meth:`sync` and :meth:`close`; this
33-
can make it handier to mutate mutable entries in the persistent dictionary,
34-
but, if many entries are accessed, it can consume vast amounts of memory for
35-
the cache, and it can make the close operation very slow since all accessed
36-
entries are written back (there is no way to determine which accessed entries
37-
are mutable, nor which ones were actually mutated).
30+
written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
31+
optional *writeback* parameter is set to *True*, all entries accessed are also
32+
cached in memory, and written back on :meth:`~Shelf.sync` and
33+
:meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
34+
the persistent dictionary, but, if many entries are accessed, it can consume
35+
vast amounts of memory for the cache, and it can make the close operation
36+
very slow since all accessed entries are written back (there is no way to
37+
determine which accessed entries are mutable, nor which ones were actually
38+
mutated).
3839

3940
.. note::
4041

Lib/shelve.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ def close(self):
136136
self.dict.close()
137137
except AttributeError:
138138
pass
139-
# _ClosedDict can be None when close is called from __del__ during shutdown
140-
if _ClosedDict is None:
141-
self.dict = None
142-
else:
139+
# Catch errors that may happen when close is called from __del__
140+
# because CPython is in interpreter shutdown.
141+
try:
143142
self.dict = _ClosedDict()
143+
except (NameError, TypeError):
144+
self.dict = None
144145

145146
def __del__(self):
146147
if not hasattr(self, 'writeback'):

Lib/test/test_shelve.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ def test_mutable_entry(self):
122122
self.assertEqual(len(d1), 1)
123123
self.assertEqual(len(d2), 1)
124124

125+
def test_writeback_also_writes_immediately(self):
126+
# Issue 5754
127+
d = {}
128+
key = 'key'
129+
encodedkey = key.encode('utf-8')
130+
s = shelve.Shelf(d, writeback=True)
131+
s[key] = [1]
132+
p1 = d[encodedkey] # Will give a KeyError if backing store not updated
133+
s['key'].append(2)
134+
s.close()
135+
p2 = d[encodedkey]
136+
self.assertNotEqual(p1, p2) # Write creates new object in store
137+
125138

126139
from test import mapping_tests
127140

0 commit comments

Comments
 (0)