]> granicus.if.org Git - python/commitdiff
bpo-30017: Allowed calling the close() method of the zip entry writer object (#1041)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 12 Apr 2017 13:03:23 +0000 (16:03 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Apr 2017 13:03:23 +0000 (16:03 +0300)
multiple times.  Writing to closed zip entry writer object now always produce
a ValueError.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 46a67d5428c3efbfa9de795824cb01b68b7a52a2..ff55e94e47616e26906b42fdc0bd4ec2bf15df98 100644 (file)
@@ -734,6 +734,48 @@ class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
     compression = zipfile.ZIP_LZMA
 
 
+class AbstractWriterTests:
+
+    def tearDown(self):
+        unlink(TESTFN2)
+
+    def test_close_after_close(self):
+        data = b'content'
+        with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
+            w = zipf.open('test', 'w')
+            w.write(data)
+            w.close()
+            self.assertTrue(w.closed)
+            w.close()
+            self.assertTrue(w.closed)
+            self.assertEqual(zipf.read('test'), data)
+
+    def test_write_after_close(self):
+        data = b'content'
+        with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
+            w = zipf.open('test', 'w')
+            w.write(data)
+            w.close()
+            self.assertTrue(w.closed)
+            self.assertRaises(ValueError, w.write, b'')
+            self.assertEqual(zipf.read('test'), data)
+
+class StoredWriterTests(AbstractWriterTests, unittest.TestCase):
+    compression = zipfile.ZIP_STORED
+
+@requires_zlib
+class DeflateWriterTests(AbstractWriterTests, unittest.TestCase):
+    compression = zipfile.ZIP_DEFLATED
+
+@requires_bz2
+class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase):
+    compression = zipfile.ZIP_BZIP2
+
+@requires_lzma
+class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
+    compression = zipfile.ZIP_LZMA
+
+
 class PyZipFileTests(unittest.TestCase):
     def assertCompiledIn(self, name, namelist):
         if name + 'o' not in namelist:
index 550e64fd1a29e5a0d999ee2e53dde06a449934ac..988f39ed1b13d07dcc0f7fbdbc98edc831dec700 100644 (file)
@@ -980,6 +980,8 @@ class _ZipWriteFile(io.BufferedIOBase):
         return True
 
     def write(self, data):
+        if self.closed:
+            raise ValueError('I/O operation on closed file.')
         nbytes = len(data)
         self._file_size += nbytes
         self._crc = crc32(data, self._crc)
@@ -990,6 +992,8 @@ class _ZipWriteFile(io.BufferedIOBase):
         return nbytes
 
     def close(self):
+        if self.closed:
+            return
         super().close()
         # Flush any data from the compressor, and update header info
         if self._compressor:
index d8b1ccb43a20597bedd0b8a0dcf75a17b2faaef9..b6f8c7094c9ef5db138d59ede2483d64a18c90cd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -314,6 +314,9 @@ Library
   times when schema is changing.  Indirectly fixed by switching to 
   use sqlite3_prepare_v2() in bpo-9303.  Patch by Aviv Palivoda.
 
+- bpo-30017: Allowed calling the close() method of the zip entry writer object
+  multiple times.  Writing to a closed writer now always produces a ValueError.
+
 - bpo-29998: Pickling and copying ImportError now preserves name and path
   attributes.