summaryrefslogtreecommitdiff
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
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 7f08a98406..e28b1c7e57 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: