Skip to content

Commit 1e5d817

Browse files
committed
Close file objects in test_core.py
1 parent 6fcac09 commit 1e5d817

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def test_write_to_readonly(self):
630630

631631
with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
632632
with self.assertRaises(ValueError):
633-
zipfp.open(TESTFN, mode='w')
633+
zipfp.open(TESTFN, mode='w').close()
634634

635635
def test_add_file_before_1980(self):
636636
# Set atime and mtime to 1970-01-01
@@ -1015,7 +1015,7 @@ def test_bad_zip64_extra(self):
10151015
file_size_64_set=True,
10161016
)
10171017
with self.assertRaises(zipfile.BadZipFile) as e:
1018-
zipfile.ZipFile(io.BytesIO(missing_file_size_extra))
1018+
zipfile.ZipFile(io.BytesIO(missing_file_size_extra)).close()
10191019
self.assertIn('file size', str(e.exception).lower())
10201020

10211021
# zip64 file size present, zip64 compress size present, one field in
@@ -1026,7 +1026,7 @@ def test_bad_zip64_extra(self):
10261026
compress_size_64_set=True,
10271027
)
10281028
with self.assertRaises(zipfile.BadZipFile) as e:
1029-
zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
1029+
zipfile.ZipFile(io.BytesIO(missing_compress_size_extra)).close()
10301030
self.assertIn('compress size', str(e.exception).lower())
10311031

10321032
# zip64 compress size present, no fields in extra, expecting one,
@@ -1035,7 +1035,7 @@ def test_bad_zip64_extra(self):
10351035
compress_size_64_set=True,
10361036
)
10371037
with self.assertRaises(zipfile.BadZipFile) as e:
1038-
zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
1038+
zipfile.ZipFile(io.BytesIO(missing_compress_size_extra)).close()
10391039
self.assertIn('compress size', str(e.exception).lower())
10401040

10411041
# zip64 file size present, zip64 compress size present, zip64 header
@@ -1049,7 +1049,7 @@ def test_bad_zip64_extra(self):
10491049
header_offset_64_set=True,
10501050
)
10511051
with self.assertRaises(zipfile.BadZipFile) as e:
1052-
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1052+
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)).close()
10531053
self.assertIn('header offset', str(e.exception).lower())
10541054

10551055
# zip64 compress size present, zip64 header offset present, one field
@@ -1061,7 +1061,7 @@ def test_bad_zip64_extra(self):
10611061
header_offset_64_set=True,
10621062
)
10631063
with self.assertRaises(zipfile.BadZipFile) as e:
1064-
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1064+
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)).close()
10651065
self.assertIn('header offset', str(e.exception).lower())
10661066

10671067
# zip64 file size present, zip64 header offset present, one field in
@@ -1073,7 +1073,7 @@ def test_bad_zip64_extra(self):
10731073
header_offset_64_set=True,
10741074
)
10751075
with self.assertRaises(zipfile.BadZipFile) as e:
1076-
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1076+
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)).close()
10771077
self.assertIn('header offset', str(e.exception).lower())
10781078

10791079
# zip64 header offset present, no fields in extra, expecting one,
@@ -1084,7 +1084,7 @@ def test_bad_zip64_extra(self):
10841084
header_offset_64_set=True,
10851085
)
10861086
with self.assertRaises(zipfile.BadZipFile) as e:
1087-
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
1087+
zipfile.ZipFile(io.BytesIO(missing_header_offset_extra)).close()
10881088
self.assertIn('header offset', str(e.exception).lower())
10891089

10901090
def test_generated_valid_zip64_extra(self):
@@ -1163,10 +1163,10 @@ def test_force_zip64(self):
11631163
self.assertEqual(ex_csize, 1) # compressed size
11641164
self.assertEqual(cd_sig, b"PK\x01\x02") # ensure the central directory header is next
11651165

1166-
z = zipfile.ZipFile(io.BytesIO(zipdata))
1167-
zinfos = z.infolist()
1168-
self.assertEqual(len(zinfos), 1)
1169-
self.assertGreaterEqual(zinfos[0].extract_version, zipfile.ZIP64_VERSION) # requires zip64 to extract
1166+
with zipfile.ZipFile(io.BytesIO(zipdata)) as z:
1167+
zinfos = z.infolist()
1168+
self.assertEqual(len(zinfos), 1)
1169+
self.assertGreaterEqual(zinfos[0].extract_version, zipfile.ZIP64_VERSION) # requires zip64 to extract
11701170

11711171
def test_unseekable_zip_unknown_filesize(self):
11721172
"""Test that creating a zip with/without seeking will raise a RuntimeError if zip64 was required but not used"""
@@ -1186,7 +1186,7 @@ def make_zip(fp):
11861186
# pretend zipfile.ZipInfo.from_file was used to get the name and filesize
11871187
info = zipfile.ZipInfo("text.txt")
11881188
info.file_size = zipfile.ZIP64_LIMIT + 1
1189-
zf.open(info, mode="w")
1189+
zf.open(info, mode="w").close()
11901190

11911191
self.assertRaises(zipfile.LargeZipFile, make_zip, io.BytesIO())
11921192
self.assertRaises(zipfile.LargeZipFile, make_zip, Unseekable(io.BytesIO()))
@@ -1936,7 +1936,7 @@ def test_exclusive_create_zip_file(self):
19361936
with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp:
19371937
zipfp.writestr(filename, content)
19381938
with self.assertRaises(FileExistsError):
1939-
zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED)
1939+
zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED).close()
19401940
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
19411941
self.assertEqual(zipfp.namelist(), [filename])
19421942
self.assertEqual(zipfp.read(filename), content)
@@ -1973,6 +1973,8 @@ def test_close_erroneous_file(self):
19731973
zf = zipfile.ZipFile(TESTFN)
19741974
except zipfile.BadZipFile:
19751975
pass
1976+
finally:
1977+
zf.close()
19761978

19771979
def test_is_zip_erroneous_file(self):
19781980
"""Check that is_zipfile() correctly identifies non-zip files."""
@@ -2256,6 +2258,8 @@ def test_empty_zipfile(self):
22562258
zipf = zipfile.ZipFile(TESTFN, mode="r")
22572259
except zipfile.BadZipFile:
22582260
self.fail("Unable to create empty ZIP file in 'w' mode")
2261+
finally:
2262+
zipf.close()
22592263

22602264
zipf = zipfile.ZipFile(TESTFN, mode="a")
22612265
zipf.close()
@@ -3293,10 +3297,10 @@ def test_root_folder_in_zipfile(self):
32933297
the zip file, this is a strange behavior, but we should support it.
32943298
"""
32953299
in_memory_file = io.BytesIO()
3296-
zf = zipfile.ZipFile(in_memory_file, "w")
3297-
zf.mkdir('/')
3298-
zf.writestr('./a.txt', 'aaa')
3299-
zf.extractall(TESTFN2)
3300+
with zipfile.ZipFile(in_memory_file, "w") as zf:
3301+
zf.mkdir('/')
3302+
zf.writestr('./a.txt', 'aaa')
3303+
zf.extractall(TESTFN2)
33003304

33013305
def tearDown(self):
33023306
rmtree(TESTFN2)
@@ -3540,9 +3544,9 @@ def test_read_with_unsuitable_metadata_encoding(self):
35403544
# Read the ZIP archive with metadata_encoding unsuitable for
35413545
# decoding metadata
35423546
with self.assertRaises(UnicodeDecodeError):
3543-
zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii')
3547+
zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii').close()
35443548
with self.assertRaises(UnicodeDecodeError):
3545-
zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8')
3549+
zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8').close()
35463550

35473551
def test_read_after_append(self):
35483552
newname = '\u56db' # Han 'four'
@@ -3575,7 +3579,7 @@ def test_write_with_metadata_encoding(self):
35753579
for mode in ("w", "x", "a"):
35763580
with self.assertRaisesRegex(ValueError,
35773581
"^metadata_encoding is only"):
3578-
ZF("nonesuch.zip", mode, metadata_encoding="shift_jis")
3582+
ZF("nonesuch.zip", mode, metadata_encoding="shift_jis").close()
35793583

35803584
def test_cli_with_metadata_encoding(self):
35813585
errmsg = "Non-conforming encodings not supported with -c."

0 commit comments

Comments
 (0)