Index: Doc/library/tarfile.rst =================================================================== --- Doc/library/tarfile.rst (revision 78514) +++ Doc/library/tarfile.rst (working copy) @@ -234,7 +234,16 @@ archive several times. Each archive member is represented by a :class:`TarInfo` object, see :ref:`tarinfo-objects` for details. +A :class:`TarFile` object can be used as a context manager in a :keyword:`with` +statement. It guarantees that when the code block is exited the +:class:`TarFile` object will be closed regardless of errors. Please note that +if an error occurs while writing the archive will not be finalized, only the +internal file object will be closed. See the :ref:`tar-examples` section for a +use case. +.. versionadded:: 2.7 + + .. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0) All following arguments are optional and can be accessed as instance attributes @@ -650,6 +659,13 @@ tar.add(name) tar.close() +The same example using the :keyword:`with` statement:: + + import tarfile + with tarfile.open("sample.tar", "w") as tar: + for name in ["foo", "bar", "quux"]: + tar.add(name) + How to read a gzip compressed tar archive and display some member information:: import tarfile Index: Lib/tarfile.py =================================================================== --- Lib/tarfile.py (revision 78514) +++ Lib/tarfile.py (working copy) @@ -2411,6 +2411,20 @@ """ 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: Index: Lib/test/test_tarfile.py =================================================================== --- Lib/test/test_tarfile.py (revision 78514) +++ Lib/test/test_tarfile.py (working copy) @@ -1292,6 +1292,65 @@ 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 @@ PaxUnicodeTest, AppendTest, LimitsTest, + ContextManagerTest, ] if hasattr(os, "link"):