close a few files that are given as external fileobj to TarFile in unittests
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 19 Jul 2016 11:11:11 +0000 (13:11 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Nov 2020 14:04:34 +0000 (15:04 +0100)
testing/test_concat_compress.py

index b2539b9..729aa08 100644 (file)
@@ -90,12 +90,11 @@ class ConcatCompressTest(BaseTest):
         tarobj.close()
         os.unlink("big")
 
-        fo = open("sample.tar.gz", 'rb') # will not be released on tarfile.close()
-        fo.seek(pos)
-        tarobj = TarFile.open(mode="r#gz", fileobj=fo)
-        tarobj.extract(tarobj.next())
-        tarobj.close()
-        fo.close()
+        with open("sample.tar.gz", 'rb') as fo:
+            fo.seek(pos)
+            tarobj = TarFile.open(mode="r#gz", fileobj=fo)
+            tarobj.extract(tarobj.next())
+            tarobj.close()
         assert os.path.exists("big")
         assert hash == self.md5sum("big")
 
@@ -127,12 +126,11 @@ class ConcatCompressTest(BaseTest):
         os.unlink("small2")
 
         # extract only the "small" file
-        fo = open("sample.tar.gz", 'rb') # will not be released on tarfile.close()
-        fo.seek(pos)
-        tarobj = TarFile.open(mode="r#gz", fileobj=fo)
-        tarobj.extract(tarobj.next())
-        tarobj.close()
-        fo.close()
+        with open("sample.tar.gz", 'rb') as fo:
+            fo.seek(pos)
+            tarobj = TarFile.open(mode="r#gz", fileobj=fo)
+            tarobj.extract(tarobj.next())
+            tarobj.close()
         assert os.path.exists("small")
         assert hash['small'] == self.md5sum("small")