Skip to content

gh-137589: Zipfile tests: close file objects #138080

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions Lib/test/test_zipfile/_path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def test_pathlike_construction(self, alpharep):
"""
zipfile_ondisk = self.zipfile_ondisk(alpharep)
pathlike = FakePath(str(zipfile_ondisk))
zipfile.Path(pathlike)
root = zipfile.Path(pathlike)
root.root.close()

@pass_alpharep
def test_traverse_pathlike(self, alpharep):
Expand Down Expand Up @@ -373,6 +374,7 @@ def test_root_on_disk(self, alpharep):
root = zipfile.Path(self.zipfile_ondisk(alpharep))
assert root.name == 'alpharep.zip' == root.filename.name
assert root.stem == 'alpharep' == root.filename.stem
root.root.close()

@pass_alpharep
def test_suffix(self, alpharep):
Expand Down Expand Up @@ -574,11 +576,13 @@ def test_inheritance(self, alpharep):
)
def test_pickle(self, alpharep, path_type, subpath):
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep)))

saved_1 = pickle.dumps(zipfile.Path(zipfile_ondisk, at=subpath))
root = zipfile.Path(zipfile_ondisk, at=subpath)
saved_1 = pickle.dumps(root)
root.root.close()
restored_1 = pickle.loads(saved_1)
first, *rest = restored_1.iterdir()
assert first.read_text(encoding='utf-8').startswith('content of ')
restored_1.root.close()

@pass_alpharep
def test_extract_orig_with_implied_dirs(self, alpharep):
Expand All @@ -590,6 +594,7 @@ def test_extract_orig_with_implied_dirs(self, alpharep):
# wrap the zipfile for its side effect
zipfile.Path(zf)
zf.extractall(source_path.parent)
zf.close()

@pass_alpharep
def test_getinfo_missing(self, alpharep):
Expand Down
34 changes: 18 additions & 16 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,26 @@ def test_low_compression(self):
self.assertEqual(openobj.read(1), b'2')

def test_writestr_compression(self):
zipfp = zipfile.ZipFile(TESTFN2, "w")
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
info = zipfp.getinfo('b.txt')
self.assertEqual(info.compress_type, self.compression)
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
info = zipfp.getinfo('b.txt')
self.assertEqual(info.compress_type, self.compression)

def test_writestr_compresslevel(self):
zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1)
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
compresslevel=2)
with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
compresslevel=2)

# Compression level follows the constructor.
a_info = zipfp.getinfo('a.txt')
self.assertEqual(a_info.compress_type, self.compression)
self.assertEqual(a_info.compress_level, 1)
# Compression level follows the constructor.
a_info = zipfp.getinfo('a.txt')
self.assertEqual(a_info.compress_type, self.compression)
self.assertEqual(a_info.compress_level, 1)

# Compression level is overridden.
b_info = zipfp.getinfo('b.txt')
self.assertEqual(b_info.compress_type, self.compression)
self.assertEqual(b_info._compresslevel, 2)
# Compression level is overridden.
b_info = zipfp.getinfo('b.txt')
self.assertEqual(b_info.compress_type, self.compression)
self.assertEqual(b_info._compresslevel, 2)

def test_read_return_size(self):
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
Expand Down Expand Up @@ -2256,13 +2256,15 @@ def test_empty_zipfile(self):
zipf = zipfile.ZipFile(TESTFN, mode="r")
except zipfile.BadZipFile:
self.fail("Unable to create empty ZIP file in 'w' mode")
zipf.close()

zipf = zipfile.ZipFile(TESTFN, mode="a")
zipf.close()
try:
zipf = zipfile.ZipFile(TESTFN, mode="r")
except:
self.fail("Unable to create empty ZIP file in 'a' mode")
zipf.close()

def test_open_empty_file(self):
# Issue 1710703: Check that opening a file with less than 22 bytes
Expand Down
Loading