summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2010-03-03 11:55:48 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2010-03-03 11:55:48 (GMT)
commit6458104188813b0165f95fcc0cf48bf02a276f42 (patch)
tree759342dacad1d17021d1e2cab41c2523e4bc2570 /Lib
parent8af970ab58e371def3779f6dd2fffe0c389c79d4 (diff)
downloadcpython-6458104188813b0165f95fcc0cf48bf02a276f42.zip
cpython-6458104188813b0165f95fcc0cf48bf02a276f42.tar.gz
cpython-6458104188813b0165f95fcc0cf48bf02a276f42.tar.bz2
Issue #7232: Add support for the context manager protocol
to the TarFile class.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py14
-rw-r--r--Lib/test/test_tarfile.py60
2 files changed, 74 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index eb42055..45928fd 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2411,6 +2411,20 @@ class TarFile(object):
"""
if level <= self.debug:
print >> sys.stderr, msg
+
+ 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:
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 938e48b..353551d 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1292,6 +1292,65 @@ class LimitsTest(unittest.TestCase):
tarinfo.tobuf(tarfile.PAX_FORMAT)
+class ContextManagerTest(unittest.TestCase):
+
+ def test_basic(self):
+ with tarfile.open(tarname) as tar:
+ self.assertFalse(tar.closed, "closed inside runtime context")
+ self.assertTrue(tar.closed, "context manager failed")
+
+ def test_closed(self):
+ # The __enter__() method is supposed to raise IOError
+ # if the TarFile object is already closed.
+ tar = tarfile.open(tarname)
+ tar.close()
+ with self.assertRaises(IOError):
+ with tar:
+ pass
+
+ def test_exception(self):
+ # Test if the IOError exception is passed through properly.
+ with self.assertRaises(Exception) as exc:
+ with tarfile.open(tarname) as tar:
+ raise IOError
+ self.assertIsInstance(exc.exception, IOError,
+ "wrong exception raised in context manager")
+ self.assertTrue(tar.closed, "context manager failed")
+
+ def test_no_eof(self):
+ # __exit__() must not write end-of-archive blocks if an
+ # exception was raised.
+ try:
+ with tarfile.open(tmpname, "w") as tar:
+ raise Exception
+ except:
+ pass
+ self.assertEqual(os.path.getsize(tmpname), 0,
+ "context manager wrote an end-of-archive block")
+ self.assertTrue(tar.closed, "context manager failed")
+
+ def test_eof(self):
+ # __exit__() must write end-of-archive blocks, i.e. call
+ # TarFile.close() if there was no error.
+ with tarfile.open(tmpname, "w"):
+ pass
+ self.assertNotEqual(os.path.getsize(tmpname), 0,
+ "context manager wrote no end-of-archive block")
+
+ def test_fileobj(self):
+ # Test that __exit__() did not close the external file
+ # object.
+ fobj = open(tmpname, "wb")
+ try:
+ with tarfile.open(fileobj=fobj, mode="w") as tar:
+ raise Exception
+ except:
+ pass
+ self.assertFalse(fobj.closed, "external file object was closed")
+ self.assertTrue(tar.closed, "context manager failed")
+ fobj.close()
+
+
class GzipMiscReadTest(MiscReadTest):
tarname = gzipname
mode = "r:gz"
@@ -1371,6 +1430,7 @@ def test_main():
PaxUnicodeTest,
AppendTest,
LimitsTest,
+ ContextManagerTest,
]
if hasattr(os, "link"):