diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/shelve.py | 9 | ||||
-rw-r--r-- | Lib/test/test_shelve.py | 11 |
2 files changed, 16 insertions, 4 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py index 8055f42..c8cba85 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -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'): diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py index ffcc98d..f03457f 100644 --- a/Lib/test/test_shelve.py +++ b/Lib/test/test_shelve.py @@ -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 |