Skip to content

gh-137589: Close file objects in test_zipfile/test_core.py #138099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def test_write_to_readonly(self):

with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
with self.assertRaises(ValueError):
zipfp.open(TESTFN, mode='w')
zipfp.open(TESTFN, mode='w').close()

def test_add_file_before_1980(self):
# Set atime and mtime to 1970-01-01
Expand Down Expand Up @@ -1015,7 +1015,7 @@ def test_bad_zip64_extra(self):
file_size_64_set=True,
)
with self.assertRaises(zipfile.BadZipFile) as e:
zipfile.ZipFile(io.BytesIO(missing_file_size_extra))
zipfile.ZipFile(io.BytesIO(missing_file_size_extra)).close()
self.assertIn('file size', str(e.exception).lower())

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

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

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

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

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

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

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

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

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

self.assertRaises(zipfile.LargeZipFile, make_zip, io.BytesIO())
self.assertRaises(zipfile.LargeZipFile, make_zip, Unseekable(io.BytesIO()))
Expand Down Expand Up @@ -1936,7 +1936,7 @@ def test_exclusive_create_zip_file(self):
with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp:
zipfp.writestr(filename, content)
with self.assertRaises(FileExistsError):
zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED)
zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED).close()
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
self.assertEqual(zipfp.namelist(), [filename])
self.assertEqual(zipfp.read(filename), content)
Expand Down Expand Up @@ -1970,7 +1970,8 @@ def test_close_erroneous_file(self):
with open(TESTFN, "w", encoding="utf-8") as fp:
fp.write("this is not a legal zip file\n")
try:
zf = zipfile.ZipFile(TESTFN)
with zipfile.ZipFile(TESTFN) as zf:
pass
except zipfile.BadZipFile:
pass

Expand Down Expand Up @@ -2253,7 +2254,8 @@ def test_empty_zipfile(self):
zipf = zipfile.ZipFile(TESTFN, mode="w")
zipf.close()
try:
zipf = zipfile.ZipFile(TESTFN, mode="r")
with zipfile.ZipFile(TESTFN, mode="r") as zipf:
pass
except zipfile.BadZipFile:
self.fail("Unable to create empty ZIP file in 'w' mode")

Expand Down Expand Up @@ -3293,10 +3295,10 @@ def test_root_folder_in_zipfile(self):
the zip file, this is a strange behavior, but we should support it.
"""
in_memory_file = io.BytesIO()
zf = zipfile.ZipFile(in_memory_file, "w")
zf.mkdir('/')
zf.writestr('./a.txt', 'aaa')
zf.extractall(TESTFN2)
with zipfile.ZipFile(in_memory_file, "w") as zf:
zf.mkdir('/')
zf.writestr('./a.txt', 'aaa')
zf.extractall(TESTFN2)

def tearDown(self):
rmtree(TESTFN2)
Expand Down Expand Up @@ -3540,9 +3542,9 @@ def test_read_with_unsuitable_metadata_encoding(self):
# Read the ZIP archive with metadata_encoding unsuitable for
# decoding metadata
with self.assertRaises(UnicodeDecodeError):
zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii')
zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii').close()
with self.assertRaises(UnicodeDecodeError):
zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8')
zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8').close()

def test_read_after_append(self):
newname = '\u56db' # Han 'four'
Expand Down Expand Up @@ -3575,7 +3577,7 @@ def test_write_with_metadata_encoding(self):
for mode in ("w", "x", "a"):
with self.assertRaisesRegex(ValueError,
"^metadata_encoding is only"):
ZF("nonesuch.zip", mode, metadata_encoding="shift_jis")
ZF("nonesuch.zip", mode, metadata_encoding="shift_jis").close()

def test_cli_with_metadata_encoding(self):
errmsg = "Non-conforming encodings not supported with -c."
Expand Down
Loading