gh-137589: Zipfile tests: close file objects (GH-138080)
Zipfile tests: close file objects
This commit is contained in:
@@ -274,7 +274,8 @@ class TestPath(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
zipfile_ondisk = self.zipfile_ondisk(alpharep)
|
zipfile_ondisk = self.zipfile_ondisk(alpharep)
|
||||||
pathlike = FakePath(str(zipfile_ondisk))
|
pathlike = FakePath(str(zipfile_ondisk))
|
||||||
zipfile.Path(pathlike)
|
root = zipfile.Path(pathlike)
|
||||||
|
root.root.close()
|
||||||
|
|
||||||
@pass_alpharep
|
@pass_alpharep
|
||||||
def test_traverse_pathlike(self, alpharep):
|
def test_traverse_pathlike(self, alpharep):
|
||||||
@@ -373,6 +374,7 @@ class TestPath(unittest.TestCase):
|
|||||||
root = zipfile.Path(self.zipfile_ondisk(alpharep))
|
root = zipfile.Path(self.zipfile_ondisk(alpharep))
|
||||||
assert root.name == 'alpharep.zip' == root.filename.name
|
assert root.name == 'alpharep.zip' == root.filename.name
|
||||||
assert root.stem == 'alpharep' == root.filename.stem
|
assert root.stem == 'alpharep' == root.filename.stem
|
||||||
|
root.root.close()
|
||||||
|
|
||||||
@pass_alpharep
|
@pass_alpharep
|
||||||
def test_suffix(self, alpharep):
|
def test_suffix(self, alpharep):
|
||||||
@@ -574,11 +576,13 @@ class TestPath(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
def test_pickle(self, alpharep, path_type, subpath):
|
def test_pickle(self, alpharep, path_type, subpath):
|
||||||
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep)))
|
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep)))
|
||||||
|
root = zipfile.Path(zipfile_ondisk, at=subpath)
|
||||||
saved_1 = pickle.dumps(zipfile.Path(zipfile_ondisk, at=subpath))
|
saved_1 = pickle.dumps(root)
|
||||||
|
root.root.close()
|
||||||
restored_1 = pickle.loads(saved_1)
|
restored_1 = pickle.loads(saved_1)
|
||||||
first, *rest = restored_1.iterdir()
|
first, *rest = restored_1.iterdir()
|
||||||
assert first.read_text(encoding='utf-8').startswith('content of ')
|
assert first.read_text(encoding='utf-8').startswith('content of ')
|
||||||
|
restored_1.root.close()
|
||||||
|
|
||||||
@pass_alpharep
|
@pass_alpharep
|
||||||
def test_extract_orig_with_implied_dirs(self, alpharep):
|
def test_extract_orig_with_implied_dirs(self, alpharep):
|
||||||
@@ -590,6 +594,7 @@ class TestPath(unittest.TestCase):
|
|||||||
# wrap the zipfile for its side effect
|
# wrap the zipfile for its side effect
|
||||||
zipfile.Path(zf)
|
zipfile.Path(zf)
|
||||||
zf.extractall(source_path.parent)
|
zf.extractall(source_path.parent)
|
||||||
|
zf.close()
|
||||||
|
|
||||||
@pass_alpharep
|
@pass_alpharep
|
||||||
def test_getinfo_missing(self, alpharep):
|
def test_getinfo_missing(self, alpharep):
|
||||||
|
|||||||
@@ -312,13 +312,13 @@ class AbstractTestsWithSourceFile:
|
|||||||
self.assertEqual(openobj.read(1), b'2')
|
self.assertEqual(openobj.read(1), b'2')
|
||||||
|
|
||||||
def test_writestr_compression(self):
|
def test_writestr_compression(self):
|
||||||
zipfp = zipfile.ZipFile(TESTFN2, "w")
|
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
||||||
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
|
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
|
||||||
info = zipfp.getinfo('b.txt')
|
info = zipfp.getinfo('b.txt')
|
||||||
self.assertEqual(info.compress_type, self.compression)
|
self.assertEqual(info.compress_type, self.compression)
|
||||||
|
|
||||||
def test_writestr_compresslevel(self):
|
def test_writestr_compresslevel(self):
|
||||||
zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1)
|
with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
|
||||||
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
|
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
|
||||||
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
|
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
|
||||||
compresslevel=2)
|
compresslevel=2)
|
||||||
@@ -2330,6 +2330,7 @@ class OtherTests(unittest.TestCase):
|
|||||||
zipf = zipfile.ZipFile(TESTFN, mode="r")
|
zipf = zipfile.ZipFile(TESTFN, mode="r")
|
||||||
except zipfile.BadZipFile:
|
except zipfile.BadZipFile:
|
||||||
self.fail("Unable to create empty ZIP file in 'w' mode")
|
self.fail("Unable to create empty ZIP file in 'w' mode")
|
||||||
|
zipf.close()
|
||||||
|
|
||||||
zipf = zipfile.ZipFile(TESTFN, mode="a")
|
zipf = zipfile.ZipFile(TESTFN, mode="a")
|
||||||
zipf.close()
|
zipf.close()
|
||||||
@@ -2337,6 +2338,7 @@ class OtherTests(unittest.TestCase):
|
|||||||
zipf = zipfile.ZipFile(TESTFN, mode="r")
|
zipf = zipfile.ZipFile(TESTFN, mode="r")
|
||||||
except:
|
except:
|
||||||
self.fail("Unable to create empty ZIP file in 'a' mode")
|
self.fail("Unable to create empty ZIP file in 'a' mode")
|
||||||
|
zipf.close()
|
||||||
|
|
||||||
def test_open_empty_file(self):
|
def test_open_empty_file(self):
|
||||||
# Issue 1710703: Check that opening a file with less than 22 bytes
|
# Issue 1710703: Check that opening a file with less than 22 bytes
|
||||||
|
|||||||
Reference in New Issue
Block a user