Skip to content

Commit 16c6759

Browse files
gh-45108: Improve docstring and testing of ZipFile.testfile() (GH-96233)
1 parent 57b6110 commit 16c6759

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

Lib/test/test_zipfile.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ def zip_test(self, f, compression, compresslevel=None):
124124
self.assertEqual(info.filename, nm)
125125
self.assertEqual(info.file_size, len(self.data))
126126

127-
# Check that testzip doesn't raise an exception
128-
zipfp.testzip()
127+
# Check that testzip thinks the archive is ok
128+
# (it returns None if all contents could be read properly)
129+
self.assertIsNone(zipfp.testzip())
129130

130131
def test_basic(self):
131132
for f in get_files(self):
@@ -748,8 +749,8 @@ def zip_test(self, f, compression):
748749
self.assertEqual(info.filename, nm)
749750
self.assertEqual(info.file_size, len(self.data))
750751

751-
# Check that testzip doesn't raise an exception
752-
zipfp.testzip()
752+
# Check that testzip thinks the archive is valid
753+
self.assertIsNone(zipfp.testzip())
753754

754755
def test_basic(self):
755756
for f in get_files(self):

Lib/test/test_zipfile64.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ def setUp(self):
3232
line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
3333
self.data = '\n'.join(line_gen).encode('ascii')
3434

35-
# And write it to a file.
36-
with open(TESTFN, "wb") as fp:
37-
fp.write(self.data)
38-
3935
def zipTest(self, f, compression):
4036
# Create the ZIP archive.
4137
with zipfile.ZipFile(f, "w", compression) as zipfp:
@@ -67,6 +63,9 @@ def zipTest(self, f, compression):
6763
(num, filecount)), file=sys.__stdout__)
6864
sys.__stdout__.flush()
6965

66+
# Check that testzip thinks the archive is valid
67+
self.assertIsNone(zipfp.testzip())
68+
7069
def testStored(self):
7170
# Try the temp file first. If we do TESTFN2 first, then it hogs
7271
# gigabytes of disk space for the duration of the test.
@@ -85,9 +84,7 @@ def testDeflated(self):
8584
self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
8685

8786
def tearDown(self):
88-
for fname in TESTFN, TESTFN2:
89-
if os.path.exists(fname):
90-
os.remove(fname)
87+
os_helper.unlink(TESTFN2)
9188

9289

9390
class OtherTests(unittest.TestCase):

Lib/zipfile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,10 @@ def printdir(self, file=None):
14681468
file=file)
14691469

14701470
def testzip(self):
1471-
"""Read all the files and check the CRC."""
1471+
"""Read all the files and check the CRC.
1472+
1473+
Return None if all files could be read successfully, or the name
1474+
of the offending file otherwise."""
14721475
chunk_size = 2 ** 20
14731476
for zinfo in self.filelist:
14741477
try:

0 commit comments

Comments
 (0)