Skip to content

Commit 4c35eb2

Browse files
committed
Check testzip() after zip file closed
1 parent 8a448e4 commit 4c35eb2

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,8 @@ def test_remove_by_name(self):
14151415
with self.assertRaises(KeyError):
14161416
zh.getinfo(self.test_files[i][0])
14171417

1418-
# make sure the zip file is still valid
1418+
# make sure the zip file is still valid
1419+
with zipfile.ZipFile(TESTFN) as zh:
14191420
self.assertIsNone(zh.testzip())
14201421

14211422
def test_remove_by_zinfo(self):
@@ -1435,7 +1436,8 @@ def test_remove_by_zinfo(self):
14351436
with self.assertRaises(KeyError):
14361437
zh.getinfo(self.test_files[i][0])
14371438

1438-
# make sure the zip file is still valid
1439+
# make sure the zip file is still valid
1440+
with zipfile.ZipFile(TESTFN) as zh:
14391441
self.assertIsNone(zh.testzip())
14401442

14411443
def test_remove_by_name_nonexist(self):
@@ -1477,7 +1479,8 @@ def test_remove_by_name_duplicated(self):
14771479
zinfos[0],
14781480
)
14791481

1480-
# make sure the zip file is still valid
1482+
# make sure the zip file is still valid
1483+
with zipfile.ZipFile(TESTFN) as zh:
14811484
self.assertIsNone(zh.testzip())
14821485

14831486
zinfos = self._prepare_zip_from_test_files(TESTFN, test_files)
@@ -1495,7 +1498,8 @@ def test_remove_by_name_duplicated(self):
14951498
with self.assertRaises(KeyError):
14961499
zh.getinfo('file.txt')
14971500

1498-
# make sure the zip file is still valid
1501+
# make sure the zip file is still valid
1502+
with zipfile.ZipFile(TESTFN) as zh:
14991503
self.assertIsNone(zh.testzip())
15001504

15011505
def test_remove_by_zinfo_duplicated(self):
@@ -1525,7 +1529,8 @@ def test_remove_by_zinfo_duplicated(self):
15251529
zinfos[1],
15261530
)
15271531

1528-
# make sure the zip file is still valid
1532+
# make sure the zip file is still valid
1533+
with zipfile.ZipFile(TESTFN) as zh:
15291534
self.assertIsNone(zh.testzip())
15301535

15311536
zinfos = self._prepare_zip_from_test_files(TESTFN, test_files)
@@ -1544,7 +1549,8 @@ def test_remove_by_zinfo_duplicated(self):
15441549
zinfos[0],
15451550
)
15461551

1547-
# make sure the zip file is still valid
1552+
# make sure the zip file is still valid
1553+
with zipfile.ZipFile(TESTFN) as zh:
15481554
self.assertIsNone(zh.testzip())
15491555

15501556
zinfos = self._prepare_zip_from_test_files(TESTFN, test_files)
@@ -1563,7 +1569,8 @@ def test_remove_by_zinfo_duplicated(self):
15631569
with self.assertRaises(KeyError):
15641570
zh.getinfo('file.txt')
15651571

1566-
# make sure the zip file is still valid
1572+
# make sure the zip file is still valid
1573+
with zipfile.ZipFile(TESTFN) as zh:
15671574
self.assertIsNone(zh.testzip())
15681575

15691576
def test_remove_zip64(self):
@@ -1583,8 +1590,9 @@ def test_remove_zip64(self):
15831590
with self.assertRaises(KeyError):
15841591
zh.getinfo(self.test_files[i][0])
15851592

1586-
# make sure the zip file is still valid
1587-
self.assertIsNone(zh.testzip())
1593+
# make sure the zip file is still valid
1594+
with zipfile.ZipFile(TESTFN) as zh:
1595+
self.assertIsNone(zh.testzip())
15881596

15891597
def test_remove_closed(self):
15901598
self._prepare_zip_from_test_files(TESTFN, self.test_files)
@@ -1624,7 +1632,8 @@ def test_remove_mode_w(self):
16241632
with self.assertRaises(KeyError):
16251633
zh.getinfo(self.test_files[0][0])
16261634

1627-
# make sure the zip file is still valid
1635+
# make sure the zip file is still valid
1636+
with zipfile.ZipFile(TESTFN) as zh:
16281637
self.assertIsNone(zh.testzip())
16291638

16301639
def test_remove_mode_x(self):
@@ -1646,7 +1655,8 @@ def test_remove_mode_x(self):
16461655
with self.assertRaises(KeyError):
16471656
zh.getinfo(self.test_files[0][0])
16481657

1649-
# make sure the zip file is still valid
1658+
# make sure the zip file is still valid
1659+
with zipfile.ZipFile(TESTFN) as zh:
16501660
self.assertIsNone(zh.testzip())
16511661

16521662
class StoredRemoveTests(AbstractRemoveTests, unittest.TestCase):
@@ -1701,12 +1711,13 @@ def test_repack_basic(self):
17011711
expected_zinfos,
17021712
)
17031713

1704-
# make sure the zip file is still valid
1705-
self.assertIsNone(zh.testzip())
1706-
17071714
# check file size
17081715
self.assertEqual(os.path.getsize(TESTFN), expected_size)
17091716

1717+
# make sure the zip file is still valid
1718+
with zipfile.ZipFile(TESTFN) as zh:
1719+
self.assertIsNone(zh.testzip())
1720+
17101721
def test_repack_bytes_before_first_file(self):
17111722
"""Should preserve random bytes before the first recorded local file entry."""
17121723
for ii in ([], [0], [0, 1], [0, 1, 2]):
@@ -1733,12 +1744,13 @@ def test_repack_bytes_before_first_file(self):
17331744
expected_zinfos,
17341745
)
17351746

1736-
# make sure the zip file is still valid
1737-
self.assertIsNone(zh.testzip())
1738-
17391747
# check file size
17401748
self.assertEqual(os.path.getsize(TESTFN), expected_size)
17411749

1750+
# make sure the zip file is still valid
1751+
with zipfile.ZipFile(TESTFN) as zh:
1752+
self.assertIsNone(zh.testzip())
1753+
17421754
def test_repack_magic_before_first_file(self):
17431755
"""Should preserve random signature bytes not forming a valid file entry
17441756
before the first recorded local file entry."""
@@ -1766,12 +1778,13 @@ def test_repack_magic_before_first_file(self):
17661778
expected_zinfos,
17671779
)
17681780

1769-
# make sure the zip file is still valid
1770-
self.assertIsNone(zh.testzip())
1771-
17721781
# check file size
17731782
self.assertEqual(os.path.getsize(TESTFN), expected_size)
17741783

1784+
# make sure the zip file is still valid
1785+
with zipfile.ZipFile(TESTFN) as zh:
1786+
self.assertIsNone(zh.testzip())
1787+
17751788
def test_repack_file_entry_before_first_file(self):
17761789
"""Should preserve seemingly valid file entries not forming consecutive
17771790
valid file entries until the first recorded local file entry.
@@ -1811,12 +1824,13 @@ def test_repack_file_entry_before_first_file(self):
18111824
expected_zinfos,
18121825
)
18131826

1814-
# make sure the zip file is still valid
1815-
self.assertIsNone(zh.testzip())
1816-
18171827
# check file size
18181828
self.assertEqual(os.path.getsize(TESTFN), expected_size)
18191829

1830+
# make sure the zip file is still valid
1831+
with zipfile.ZipFile(TESTFN) as zh:
1832+
self.assertIsNone(zh.testzip())
1833+
18201834
def test_repack_bytes_between_files(self):
18211835
"""Should remove bytes between local file entries."""
18221836
for ii in ([1], [1, 2], [2]):
@@ -1843,12 +1857,13 @@ def test_repack_bytes_between_files(self):
18431857
expected_zinfos,
18441858
)
18451859

1846-
# make sure the zip file is still valid
1847-
self.assertIsNone(zh.testzip())
1848-
18491860
# check file size
18501861
self.assertEqual(os.path.getsize(TESTFN), expected_size)
18511862

1863+
# make sure the zip file is still valid
1864+
with zipfile.ZipFile(TESTFN) as zh:
1865+
self.assertIsNone(zh.testzip())
1866+
18521867
def test_repack_zip64(self):
18531868
"""Should correctly handle file entries with zip64."""
18541869
for ii in ([0], [0, 1], [1], [2]):
@@ -1871,12 +1886,13 @@ def test_repack_zip64(self):
18711886
expected_zinfos,
18721887
)
18731888

1874-
# make sure the zip file is still valid
1875-
self.assertIsNone(zh.testzip())
1876-
18771889
# check file size
18781890
self.assertEqual(os.path.getsize(TESTFN), expected_size)
18791891

1892+
# make sure the zip file is still valid
1893+
with zipfile.ZipFile(TESTFN) as zh:
1894+
self.assertIsNone(zh.testzip())
1895+
18801896
def test_repack_data_descriptor(self):
18811897
"""Should correctly handle file entries using data descriptor."""
18821898
for ii in ([0], [0, 1], [1], [2]):
@@ -1905,12 +1921,13 @@ def test_repack_data_descriptor(self):
19051921
expected_zinfos,
19061922
)
19071923

1908-
# make sure the zip file is still valid
1909-
self.assertIsNone(zh.testzip())
1910-
19111924
# check file size
19121925
self.assertEqual(os.path.getsize(TESTFN), expected_size)
19131926

1927+
# make sure the zip file is still valid
1928+
with zipfile.ZipFile(TESTFN) as zh:
1929+
self.assertIsNone(zh.testzip())
1930+
19141931
def test_repack_data_descriptor_and_zip64(self):
19151932
"""Should correctly handle file entries using data descriptor and zip64."""
19161933
for ii in ([0], [0, 1], [1], [2]):
@@ -1939,12 +1956,13 @@ def test_repack_data_descriptor_and_zip64(self):
19391956
expected_zinfos,
19401957
)
19411958

1942-
# make sure the zip file is still valid
1943-
self.assertIsNone(zh.testzip())
1944-
19451959
# check file size
19461960
self.assertEqual(os.path.getsize(TESTFN), expected_size)
19471961

1962+
# make sure the zip file is still valid
1963+
with zipfile.ZipFile(TESTFN) as zh:
1964+
self.assertIsNone(zh.testzip())
1965+
19481966
def test_repack_data_descriptor_no_sig(self):
19491967
"""Should correctly handle file entries using data descriptor without signature."""
19501968
for ii in ([0], [0, 1], [1], [2]):
@@ -1975,12 +1993,13 @@ def test_repack_data_descriptor_no_sig(self):
19751993
expected_zinfos,
19761994
)
19771995

1978-
# make sure the zip file is still valid
1979-
self.assertIsNone(zh.testzip())
1980-
19811996
# check file size
19821997
self.assertEqual(os.path.getsize(TESTFN), expected_size)
19831998

1999+
# make sure the zip file is still valid
2000+
with zipfile.ZipFile(TESTFN) as zh:
2001+
self.assertIsNone(zh.testzip())
2002+
19842003
def test_repack_data_descriptor_no_sig_strict(self):
19852004
"""Should skip data descriptor without signature when `strict_descriptor` is set."""
19862005
for ii in ([0], [0, 1]):
@@ -2014,12 +2033,13 @@ def test_repack_data_descriptor_no_sig_strict(self):
20142033
expected_zinfos,
20152034
)
20162035

2017-
# make sure the zip file is still valid
2018-
self.assertIsNone(zh.testzip())
2019-
20202036
# check file size
20212037
self.assertEqual(os.path.getsize(TESTFN), expected_size)
20222038

2039+
# make sure the zip file is still valid
2040+
with zipfile.ZipFile(TESTFN) as zh:
2041+
self.assertIsNone(zh.testzip())
2042+
20232043
def test_repack_data_descriptor_no_sig_and_zip64(self):
20242044
"""Should correctly handle file entries using data descriptor without signature and zip64."""
20252045
for ii in ([0], [0, 1], [1], [2]):
@@ -2050,12 +2070,13 @@ def test_repack_data_descriptor_no_sig_and_zip64(self):
20502070
expected_zinfos,
20512071
)
20522072

2053-
# make sure the zip file is still valid
2054-
self.assertIsNone(zh.testzip())
2055-
20562073
# check file size
20572074
self.assertEqual(os.path.getsize(TESTFN), expected_size)
20582075

2076+
# make sure the zip file is still valid
2077+
with zipfile.ZipFile(TESTFN) as zh:
2078+
self.assertIsNone(zh.testzip())
2079+
20592080
def test_repack_overlapping_blocks(self):
20602081
for ii in ([0], [1], [2]):
20612082
with self.subTest(remove=ii):

0 commit comments

Comments
 (0)