Skip to content

Commit d729fcf

Browse files
[3.14] pythongh-137589: Zipfile tests: close file objects (pythonGH-138080) (python#139766)
pythongh-137589: Zipfile tests: close file objects (pythonGH-138080) Zipfile tests: close file objects (cherry picked from commit 5cea843) Co-authored-by: Rogdham <[email protected]>
1 parent 7e23d36 commit d729fcf

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def test_pathlike_construction(self, alpharep):
274274
"""
275275
zipfile_ondisk = self.zipfile_ondisk(alpharep)
276276
pathlike = FakePath(str(zipfile_ondisk))
277-
zipfile.Path(pathlike)
277+
root = zipfile.Path(pathlike)
278+
root.root.close()
278279

279280
@pass_alpharep
280281
def test_traverse_pathlike(self, alpharep):
@@ -373,6 +374,7 @@ def test_root_on_disk(self, alpharep):
373374
root = zipfile.Path(self.zipfile_ondisk(alpharep))
374375
assert root.name == 'alpharep.zip' == root.filename.name
375376
assert root.stem == 'alpharep' == root.filename.stem
377+
root.root.close()
376378

377379
@pass_alpharep
378380
def test_suffix(self, alpharep):
@@ -574,11 +576,13 @@ def test_inheritance(self, alpharep):
574576
)
575577
def test_pickle(self, alpharep, path_type, subpath):
576578
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep)))
577-
578-
saved_1 = pickle.dumps(zipfile.Path(zipfile_ondisk, at=subpath))
579+
root = zipfile.Path(zipfile_ondisk, at=subpath)
580+
saved_1 = pickle.dumps(root)
581+
root.root.close()
579582
restored_1 = pickle.loads(saved_1)
580583
first, *rest = restored_1.iterdir()
581584
assert first.read_text(encoding='utf-8').startswith('content of ')
585+
restored_1.root.close()
582586

583587
@pass_alpharep
584588
def test_extract_orig_with_implied_dirs(self, alpharep):
@@ -590,6 +594,7 @@ def test_extract_orig_with_implied_dirs(self, alpharep):
590594
# wrap the zipfile for its side effect
591595
zipfile.Path(zf)
592596
zf.extractall(source_path.parent)
597+
zf.close()
593598

594599
@pass_alpharep
595600
def test_getinfo_missing(self, alpharep):

Lib/test/test_zipfile/test_core.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,26 +312,26 @@ def test_low_compression(self):
312312
self.assertEqual(openobj.read(1), b'2')
313313

314314
def test_writestr_compression(self):
315-
zipfp = zipfile.ZipFile(TESTFN2, "w")
316-
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
317-
info = zipfp.getinfo('b.txt')
318-
self.assertEqual(info.compress_type, self.compression)
315+
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
316+
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
317+
info = zipfp.getinfo('b.txt')
318+
self.assertEqual(info.compress_type, self.compression)
319319

320320
def test_writestr_compresslevel(self):
321-
zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1)
322-
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
323-
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
324-
compresslevel=2)
321+
with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
322+
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
323+
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
324+
compresslevel=2)
325325

326-
# Compression level follows the constructor.
327-
a_info = zipfp.getinfo('a.txt')
328-
self.assertEqual(a_info.compress_type, self.compression)
329-
self.assertEqual(a_info.compress_level, 1)
326+
# Compression level follows the constructor.
327+
a_info = zipfp.getinfo('a.txt')
328+
self.assertEqual(a_info.compress_type, self.compression)
329+
self.assertEqual(a_info.compress_level, 1)
330330

331-
# Compression level is overridden.
332-
b_info = zipfp.getinfo('b.txt')
333-
self.assertEqual(b_info.compress_type, self.compression)
334-
self.assertEqual(b_info._compresslevel, 2)
331+
# Compression level is overridden.
332+
b_info = zipfp.getinfo('b.txt')
333+
self.assertEqual(b_info.compress_type, self.compression)
334+
self.assertEqual(b_info._compresslevel, 2)
335335

336336
def test_read_return_size(self):
337337
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
@@ -2330,13 +2330,15 @@ def test_empty_zipfile(self):
23302330
zipf = zipfile.ZipFile(TESTFN, mode="r")
23312331
except zipfile.BadZipFile:
23322332
self.fail("Unable to create empty ZIP file in 'w' mode")
2333+
zipf.close()
23332334

23342335
zipf = zipfile.ZipFile(TESTFN, mode="a")
23352336
zipf.close()
23362337
try:
23372338
zipf = zipfile.ZipFile(TESTFN, mode="r")
23382339
except:
23392340
self.fail("Unable to create empty ZIP file in 'a' mode")
2341+
zipf.close()
23402342

23412343
def test_open_empty_file(self):
23422344
# Issue 1710703: Check that opening a file with less than 22 bytes

0 commit comments

Comments
 (0)