summaryrefslogtreecommitdiffstats
path: root/Lib/test
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/test
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/test')
-rw-r--r--Lib/test/test_tarfile.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 792f2b9..12cf2fb 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1274,6 +1274,65 @@ class MiscTest(unittest.TestCase):
self.assertEqual(tarfile.itn(0xffffffff), b"\x80\x00\x00\x00\xff\xff\xff\xff")
+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"
@@ -1354,6 +1413,7 @@ def test_main():
AppendTest,
LimitsTest,
MiscTest,
+ ContextManagerTest,
]
if hasattr(os, "link"):