Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45711a7

Browse files
committed
Merged revisions 76453 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r76453 | lars.gustaebel | 2009-11-23 16:48:33 +0100 (Mon, 23 Nov 2009) | 10 lines Merged revisions 76452 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r76452 | lars.gustaebel | 2009-11-23 16:46:19 +0100 (Mon, 23 Nov 2009) | 3 lines Add a testcase that checks if the TarFile constructor successfully closes the internal file object in case of an error (issue #7341). ........ ................
1 parent 4c38059 commit 45711a7

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/test/test_tarfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ def test_extractall(self):
263263
self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
264264
tar.close()
265265

266+
def test_init_close_fobj(self):
267+
# Issue #7341: Close the internal file object in the TarFile
268+
# constructor in case of an error. For the test we rely on
269+
# the fact that opening an invalid file raises a ReadError.
270+
invalid = os.path.join(TEMPDIR, "invalid")
271+
open(invalid, "wb").write(b"foo")
272+
273+
try:
274+
tar = object.__new__(tarfile.TarFile)
275+
try:
276+
tar.__init__(invalid)
277+
except tarfile.ReadError:
278+
self.assertTrue(tar.fileobj.closed)
279+
else:
280+
self.fail("ReadError not raised")
281+
finally:
282+
os.remove(invalid)
283+
266284

267285
class StreamReadTest(ReadTest):
268286

0 commit comments

Comments
 (0)