summaryrefslogtreecommitdiffstats
path: root/Lib/shelve.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2012-10-06 10:52:19 (GMT)
committerAndrew Svetlov <andrew.svetlov@gmail.com>2012-10-06 10:52:19 (GMT)
commitef08fb1f040cb51e752c6b1322008714262fbf3e (patch)
treea4617bbf91a1a2ca34304e63824838de777fca2e /Lib/shelve.py
parentdc22587df2a577cdcdd21f3aec629e22b07f4263 (diff)
downloadcpython-ef08fb1f040cb51e752c6b1322008714262fbf3e.zip
cpython-ef08fb1f040cb51e752c6b1322008714262fbf3e.tar.gz
cpython-ef08fb1f040cb51e752c6b1322008714262fbf3e.tar.bz2
Issue #13896: Make shelf instances work with 'with' as context managers.
Original patch by Filip GruszczyƄski.
Diffstat (limited to 'Lib/shelve.py')
-rw-r--r--Lib/shelve.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py
index cc1815e..cfb6863 100644
--- a/Lib/shelve.py
+++ b/Lib/shelve.py
@@ -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()