[Python-checkins] gh-45108: Improve docstring and testing of ZipFile.testfile() (GH-96233)
serhiy-storchaka
webhook-mailer at python.org
Sat Sep 3 01:58:34 EDT 2022
https://github.com/python/cpython/commit/16c6759b3748f9b787b2fa4d5e652a8e08a17dee
commit: 16c6759b3748f9b787b2fa4d5e652a8e08a17dee
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-09-03T08:58:25+03:00
summary:
gh-45108: Improve docstring and testing of ZipFile.testfile() (GH-96233)
files:
M Lib/test/test_zipfile.py
M Lib/test/test_zipfile64.py
M Lib/zipfile.py
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 21257785159..6f6f4bc26b0 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -124,8 +124,9 @@ def zip_test(self, f, compression, compresslevel=None):
self.assertEqual(info.filename, nm)
self.assertEqual(info.file_size, len(self.data))
- # Check that testzip doesn't raise an exception
- zipfp.testzip()
+ # Check that testzip thinks the archive is ok
+ # (it returns None if all contents could be read properly)
+ self.assertIsNone(zipfp.testzip())
def test_basic(self):
for f in get_files(self):
@@ -748,8 +749,8 @@ def zip_test(self, f, compression):
self.assertEqual(info.filename, nm)
self.assertEqual(info.file_size, len(self.data))
- # Check that testzip doesn't raise an exception
- zipfp.testzip()
+ # Check that testzip thinks the archive is valid
+ self.assertIsNone(zipfp.testzip())
def test_basic(self):
for f in get_files(self):
diff --git a/Lib/test/test_zipfile64.py b/Lib/test/test_zipfile64.py
index 0947013afbc..be654a8478b 100644
--- a/Lib/test/test_zipfile64.py
+++ b/Lib/test/test_zipfile64.py
@@ -32,10 +32,6 @@ def setUp(self):
line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
self.data = '\n'.join(line_gen).encode('ascii')
- # And write it to a file.
- with open(TESTFN, "wb") as fp:
- fp.write(self.data)
-
def zipTest(self, f, compression):
# Create the ZIP archive.
with zipfile.ZipFile(f, "w", compression) as zipfp:
@@ -67,6 +63,9 @@ def zipTest(self, f, compression):
(num, filecount)), file=sys.__stdout__)
sys.__stdout__.flush()
+ # Check that testzip thinks the archive is valid
+ self.assertIsNone(zipfp.testzip())
+
def testStored(self):
# Try the temp file first. If we do TESTFN2 first, then it hogs
# gigabytes of disk space for the duration of the test.
@@ -85,9 +84,7 @@ def testDeflated(self):
self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)
def tearDown(self):
- for fname in TESTFN, TESTFN2:
- if os.path.exists(fname):
- os.remove(fname)
+ os_helper.unlink(TESTFN2)
class OtherTests(unittest.TestCase):
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 903d09dc023..b6465373c10 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1468,7 +1468,10 @@ def printdir(self, file=None):
file=file)
def testzip(self):
- """Read all the files and check the CRC."""
+ """Read all the files and check the CRC.
+
+ Return None if all files could be read successfully, or the name
+ of the offending file otherwise."""
chunk_size = 2 ** 20
for zinfo in self.filelist:
try:
More information about the Python-checkins
mailing list