]> granicus.if.org Git - python/commitdiff
Merged revisions 78141-78142 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Thu, 11 Feb 2010 02:01:02 +0000 (02:01 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Thu, 11 Feb 2010 02:01:02 +0000 (02:01 +0000)
svn+ssh://pythondev@svn.python.org/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.
........

Doc/library/shelve.rst
Lib/shelve.py
Lib/test/test_shelve.py
Misc/ACKS

index 5d82dc42d54912b02e2cd53a507df3d895f91bc1..c0bcb80f19090a1257ee9adfbb6c2b241ebf338a 100644 (file)
@@ -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
    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::
 
index 8055f42e2291fb4ffa4fac27ab7feb566d0ce4a4..c8cba8582d6ff1ed7a4c7af6e42492fdfca3d13d 100644 (file)
@@ -145,11 +145,12 @@ class Shelf(UserDict.DictMixin):
             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'):
index ffcc98da56e367c79f74d7d296be572b2427098c..f03457f97828c2305798adda78ccdd1b3e9fe654 100644 (file)
@@ -88,6 +88,17 @@ class TestCase(unittest.TestCase):
         self.assertEqual(len(d1), 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
 
index 6562b7e75a3ee0d1a9ae0a8b9a91bdbb1bff9ab6..1d2daac96b4d6d7775ab974ee09a1a7a75775341 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -418,6 +418,7 @@ Luc Lefebvre
 Vincent Legoll
 Kip Lehman
 Joerg Lehmann
+Robert Lehmann
 Luke Kenneth Casson Leighton
 Marc-Andre Lemburg
 John Lenton