]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-45108: Improve docstring and testing of ZipFile.testfile() (GH-96233)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 3 Sep 2022 05:58:25 +0000 (08:58 +0300)
committerGitHub <noreply@github.com>
Sat, 3 Sep 2022 05:58:25 +0000 (08:58 +0300)
Lib/test/test_zipfile.py
Lib/test/test_zipfile64.py
Lib/zipfile.py

index 21257785159b37a05043d19eaea8e0fa2968a81d..6f6f4bc26b0d4071e4e944162d5b251e1fd7684f 100644 (file)
@@ -124,8 +124,9 @@ class AbstractTestsWithSourceFile:
                 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 @@ class AbstractTestZip64InSmallFiles:
                 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):
index 0947013afbc6ed117578e217b0dff22f2fe8ec87..be654a8478b04b7e01d1bcf9b70be8ad784aaaaa 100644 (file)
@@ -32,10 +32,6 @@ class TestsWithSourceFile(unittest.TestCase):
         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 @@ class TestsWithSourceFile(unittest.TestCase):
                     (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 @@ class TestsWithSourceFile(unittest.TestCase):
         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):
index 903d09dc023e69ef89c8452b4ab25f062ce5a8f1..b6465373c107d50761a59db094be095f7b14f139 100644 (file)
@@ -1468,7 +1468,10 @@ class ZipFile:
                   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: