Skip to content

Commit d3baa5f

Browse files
Rogdhammiss-islington
authored andcommitted
gh-137589: Zipfile tests: close file objects (GH-138080)
Zipfile tests: close file objects (cherry picked from commit 5cea843) Co-authored-by: Rogdham <[email protected]>
1 parent 0c91d68 commit d3baa5f

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
@@ -275,7 +275,8 @@ def test_pathlike_construction(self, alpharep):
275275
"""
276276
zipfile_ondisk = self.zipfile_ondisk(alpharep)
277277
pathlike = FakePath(str(zipfile_ondisk))
278-
zipfile.Path(pathlike)
278+
root = zipfile.Path(pathlike)
279+
root.root.close()
279280

280281
@pass_alpharep
281282
def test_traverse_pathlike(self, alpharep):
@@ -374,6 +375,7 @@ def test_root_on_disk(self, alpharep):
374375
root = zipfile.Path(self.zipfile_ondisk(alpharep))
375376
assert root.name == 'alpharep.zip' == root.filename.name
376377
assert root.stem == 'alpharep' == root.filename.stem
378+
root.root.close()
377379

378380
@pass_alpharep
379381
def test_suffix(self, alpharep):
@@ -575,11 +577,13 @@ def test_inheritance(self, alpharep):
575577
)
576578
def test_pickle(self, alpharep, path_type, subpath):
577579
zipfile_ondisk = path_type(str(self.zipfile_ondisk(alpharep)))
578-
579-
saved_1 = pickle.dumps(zipfile.Path(zipfile_ondisk, at=subpath))
580+
root = zipfile.Path(zipfile_ondisk, at=subpath)
581+
saved_1 = pickle.dumps(root)
582+
root.root.close()
580583
restored_1 = pickle.loads(saved_1)
581584
first, *rest = restored_1.iterdir()
582585
assert first.read_text(encoding='utf-8').startswith('content of ')
586+
restored_1.root.close()
583587

584588
@pass_alpharep
585589
def test_extract_orig_with_implied_dirs(self, alpharep):
@@ -591,6 +595,7 @@ def test_extract_orig_with_implied_dirs(self, alpharep):
591595
# wrap the zipfile for its side effect
592596
zipfile.Path(zf)
593597
zf.extractall(source_path.parent)
598+
zf.close()
594599

595600
@pass_alpharep
596601
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
@@ -302,26 +302,26 @@ def test_low_compression(self):
302302
self.assertEqual(openobj.read(1), b'2')
303303

304304
def test_writestr_compression(self):
305-
zipfp = zipfile.ZipFile(TESTFN2, "w")
306-
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
307-
info = zipfp.getinfo('b.txt')
308-
self.assertEqual(info.compress_type, self.compression)
305+
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
306+
zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
307+
info = zipfp.getinfo('b.txt')
308+
self.assertEqual(info.compress_type, self.compression)
309309

310310
def test_writestr_compresslevel(self):
311-
zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1)
312-
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
313-
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
314-
compresslevel=2)
311+
with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
312+
zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
313+
zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
314+
compresslevel=2)
315315

316-
# Compression level follows the constructor.
317-
a_info = zipfp.getinfo('a.txt')
318-
self.assertEqual(a_info.compress_type, self.compression)
319-
self.assertEqual(a_info.compress_level, 1)
316+
# Compression level follows the constructor.
317+
a_info = zipfp.getinfo('a.txt')
318+
self.assertEqual(a_info.compress_type, self.compression)
319+
self.assertEqual(a_info.compress_level, 1)
320320

321-
# Compression level is overridden.
322-
b_info = zipfp.getinfo('b.txt')
323-
self.assertEqual(b_info.compress_type, self.compression)
324-
self.assertEqual(b_info._compresslevel, 2)
321+
# Compression level is overridden.
322+
b_info = zipfp.getinfo('b.txt')
323+
self.assertEqual(b_info.compress_type, self.compression)
324+
self.assertEqual(b_info._compresslevel, 2)
325325

326326
def test_read_return_size(self):
327327
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
@@ -2255,13 +2255,15 @@ def test_empty_zipfile(self):
22552255
zipf = zipfile.ZipFile(TESTFN, mode="r")
22562256
except zipfile.BadZipFile:
22572257
self.fail("Unable to create empty ZIP file in 'w' mode")
2258+
zipf.close()
22582259

22592260
zipf = zipfile.ZipFile(TESTFN, mode="a")
22602261
zipf.close()
22612262
try:
22622263
zipf = zipfile.ZipFile(TESTFN, mode="r")
22632264
except:
22642265
self.fail("Unable to create empty ZIP file in 'a' mode")
2266+
zipf.close()
22652267

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

0 commit comments

Comments
 (0)