mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Issue #13896: Make shelf instances work with 'with' as context managers.
Original patch by Filip Gruszczyński.
This commit is contained in:
parent
dc22587df2
commit
ef08fb1f04
3 changed files with 23 additions and 0 deletions
|
@ -131,6 +131,12 @@ class Shelf(collections.MutableMapping):
|
|||
except KeyError:
|
||||
pass
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.close()
|
||||
|
||||
def close(self):
|
||||
self.sync()
|
||||
try:
|
||||
|
@ -147,6 +153,7 @@ class Shelf(collections.MutableMapping):
|
|||
def __del__(self):
|
||||
if not hasattr(self, 'writeback'):
|
||||
# __init__ didn't succeed, so don't bother closing
|
||||
# see http://bugs.python.org/issue1339007 for details
|
||||
return
|
||||
self.close()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue