diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-05 08:05:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-05 08:05:57 (GMT) |
commit | 9e4861f52349011cd5916eef8e8344575e8ac426 (patch) | |
tree | f959ba22afc2588dbd9eaa1b87533f194495d67d /Lib/test/test_tarfile.py | |
parent | b727239575894b060db37792e86aab818c00817a (diff) | |
download | cpython-9e4861f52349011cd5916eef8e8344575e8ac426.zip cpython-9e4861f52349011cd5916eef8e8344575e8ac426.tar.gz cpython-9e4861f52349011cd5916eef8e8344575e8ac426.tar.bz2 |
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 1). (GH-10928)
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r-- | Lib/test/test_tarfile.py | 127 |
1 files changed, 60 insertions, 67 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 5e4d75e..5e5a3c3 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -132,49 +132,47 @@ class UstarReadTest(ReadTest, unittest.TestCase): data = fobj.read() tarinfo = self.tar.getmember("ustar/regtype") - fobj = self.tar.extractfile(tarinfo) - - text = fobj.read() - fobj.seek(0) - self.assertEqual(0, fobj.tell(), - "seek() to file's start failed") - fobj.seek(2048, 0) - self.assertEqual(2048, fobj.tell(), - "seek() to absolute position failed") - fobj.seek(-1024, 1) - self.assertEqual(1024, fobj.tell(), - "seek() to negative relative position failed") - fobj.seek(1024, 1) - self.assertEqual(2048, fobj.tell(), - "seek() to positive relative position failed") - s = fobj.read(10) - self.assertEqual(s, data[2048:2058], - "read() after seek failed") - fobj.seek(0, 2) - self.assertEqual(tarinfo.size, fobj.tell(), - "seek() to file's end failed") - self.assertEqual(fobj.read(), b"", - "read() at file's end did not return empty string") - fobj.seek(-tarinfo.size, 2) - self.assertEqual(0, fobj.tell(), - "relative seek() to file's end failed") - fobj.seek(512) - s1 = fobj.readlines() - fobj.seek(512) - s2 = fobj.readlines() - self.assertEqual(s1, s2, - "readlines() after seek failed") - fobj.seek(0) - self.assertEqual(len(fobj.readline()), fobj.tell(), - "tell() after readline() failed") - fobj.seek(512) - self.assertEqual(len(fobj.readline()) + 512, fobj.tell(), - "tell() after seek() and readline() failed") - fobj.seek(0) - line = fobj.readline() - self.assertEqual(fobj.read(), data[len(line):], - "read() after readline() failed") - fobj.close() + with self.tar.extractfile(tarinfo) as fobj: + text = fobj.read() + fobj.seek(0) + self.assertEqual(0, fobj.tell(), + "seek() to file's start failed") + fobj.seek(2048, 0) + self.assertEqual(2048, fobj.tell(), + "seek() to absolute position failed") + fobj.seek(-1024, 1) + self.assertEqual(1024, fobj.tell(), + "seek() to negative relative position failed") + fobj.seek(1024, 1) + self.assertEqual(2048, fobj.tell(), + "seek() to positive relative position failed") + s = fobj.read(10) + self.assertEqual(s, data[2048:2058], + "read() after seek failed") + fobj.seek(0, 2) + self.assertEqual(tarinfo.size, fobj.tell(), + "seek() to file's end failed") + self.assertEqual(fobj.read(), b"", + "read() at file's end did not return empty string") + fobj.seek(-tarinfo.size, 2) + self.assertEqual(0, fobj.tell(), + "relative seek() to file's end failed") + fobj.seek(512) + s1 = fobj.readlines() + fobj.seek(512) + s2 = fobj.readlines() + self.assertEqual(s1, s2, + "readlines() after seek failed") + fobj.seek(0) + self.assertEqual(len(fobj.readline()), fobj.tell(), + "tell() after readline() failed") + fobj.seek(512) + self.assertEqual(len(fobj.readline()) + 512, fobj.tell(), + "tell() after seek() and readline() failed") + fobj.seek(0) + line = fobj.readline() + self.assertEqual(fobj.read(), data[len(line):], + "read() after readline() failed") def test_fileobj_text(self): with self.tar.extractfile("ustar/regtype") as fobj: @@ -486,15 +484,14 @@ class MiscReadTestBase(CommonReadTest): fobj.seek(offset) # Test if the tarfile starts with the second member. - tar = tar.open(self.tarname, mode="r:", fileobj=fobj) - t = tar.next() - self.assertEqual(t.name, name) - # Read to the end of fileobj and test if seeking back to the - # beginning works. - tar.getmembers() - self.assertEqual(tar.extractfile(t).read(), data, - "seek back did not work") - tar.close() + with tar.open(self.tarname, mode="r:", fileobj=fobj) as tar: + t = tar.next() + self.assertEqual(t.name, name) + # Read to the end of fileobj and test if seeking back to the + # beginning works. + tar.getmembers() + self.assertEqual(tar.extractfile(t).read(), data, + "seek back did not work") def test_fail_comp(self): # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. @@ -1042,9 +1039,8 @@ class WriteTestBase(TarTest): def test_fileobj_no_close(self): fobj = io.BytesIO() - tar = tarfile.open(fileobj=fobj, mode=self.mode) - tar.addfile(tarfile.TarInfo("foo")) - tar.close() + with tarfile.open(fileobj=fobj, mode=self.mode) as tar: + tar.addfile(tarfile.TarInfo("foo")) self.assertFalse(fobj.closed, "external fileobjs must never closed") # Issue #20238: Incomplete gzip output with mode="w:gz" data = fobj.getvalue() @@ -1306,19 +1302,16 @@ class WriteTest(WriteTestBase, unittest.TestCase): with open(source_file,'w') as f: f.write('something\n') os.symlink(source_file, target_file) - tar = tarfile.open(temparchive,'w') - tar.add(source_file) - tar.add(target_file) - tar.close() + with tarfile.open(temparchive, 'w') as tar: + tar.add(source_file) + tar.add(target_file) # Let's extract it to the location which contains the symlink - tar = tarfile.open(temparchive,'r') - # this should not raise OSError: [Errno 17] File exists - try: - tar.extractall(path=tempdir) - except OSError: - self.fail("extractall failed with symlinked files") - finally: - tar.close() + with tarfile.open(temparchive) as tar: + # this should not raise OSError: [Errno 17] File exists + try: + tar.extractall(path=tempdir) + except OSError: + self.fail("extractall failed with symlinked files") finally: support.unlink(temparchive) support.rmtree(tempdir) |