summaryrefslogtreecommitdiffstats
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2010-03-03 12:08:54 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2010-03-03 12:08:54 (GMT)
commit0138581c43c9812d9bd415d95523e65fd72e6a34 (patch)
tree1c31df1fa26e3dab3e8db24708016d01e81d2f4d /Lib/tarfile.py
parent5749e85b53618d9e8c0fa5ee2f4f796b0a4ca6e2 (diff)
downloadcpython-0138581c43c9812d9bd415d95523e65fd72e6a34.zip
cpython-0138581c43c9812d9bd415d95523e65fd72e6a34.tar.gz
cpython-0138581c43c9812d9bd415d95523e65fd72e6a34.tar.bz2
Merged revisions 78623 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r78623 | lars.gustaebel | 2010-03-03 12:55:48 +0100 (Wed, 03 Mar 2010) | 3 lines Issue #7232: Add support for the context manager protocol to the TarFile class. ........
Diffstat (limited to 'Lib/tarfile.py')
-rw-r--r--Lib/tarfile.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 7f08a98..e28b1c7 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2391,6 +2391,20 @@ class TarFile(object):
"""
if level <= self.debug:
print(msg, file=sys.stderr)
+
+ def __enter__(self):
+ self._check()
+ return self
+
+ def __exit__(self, type, value, traceback):
+ if type is None:
+ self.close()
+ else:
+ # An exception occurred. We must not call close() because
+ # it would try to write end-of-archive blocks and padding.
+ if not self._extfileobj:
+ self.fileobj.close()
+ self.closed = True
# class TarFile
class TarIter: