Skip to content

Commit 4c0d9ea

Browse files
bpo-30017: Allowed calling the close() method of the zip entry writer object (#1041)
multiple times. Writing to closed zip entry writer object now always produce a ValueError.
1 parent 3e0f1fc commit 4c0d9ea

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Lib/test/test_zipfile.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,48 @@ class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
734734
compression = zipfile.ZIP_LZMA
735735

736736

737+
class AbstractWriterTests:
738+
739+
def tearDown(self):
740+
unlink(TESTFN2)
741+
742+
def test_close_after_close(self):
743+
data = b'content'
744+
with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
745+
w = zipf.open('test', 'w')
746+
w.write(data)
747+
w.close()
748+
self.assertTrue(w.closed)
749+
w.close()
750+
self.assertTrue(w.closed)
751+
self.assertEqual(zipf.read('test'), data)
752+
753+
def test_write_after_close(self):
754+
data = b'content'
755+
with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
756+
w = zipf.open('test', 'w')
757+
w.write(data)
758+
w.close()
759+
self.assertTrue(w.closed)
760+
self.assertRaises(ValueError, w.write, b'')
761+
self.assertEqual(zipf.read('test'), data)
762+
763+
class StoredWriterTests(AbstractWriterTests, unittest.TestCase):
764+
compression = zipfile.ZIP_STORED
765+
766+
@requires_zlib
767+
class DeflateWriterTests(AbstractWriterTests, unittest.TestCase):
768+
compression = zipfile.ZIP_DEFLATED
769+
770+
@requires_bz2
771+
class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase):
772+
compression = zipfile.ZIP_BZIP2
773+
774+
@requires_lzma
775+
class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
776+
compression = zipfile.ZIP_LZMA
777+
778+
737779
class PyZipFileTests(unittest.TestCase):
738780
def assertCompiledIn(self, name, namelist):
739781
if name + 'o' not in namelist:

Lib/zipfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,8 @@ def writable(self):
980980
return True
981981

982982
def write(self, data):
983+
if self.closed:
984+
raise ValueError('I/O operation on closed file.')
983985
nbytes = len(data)
984986
self._file_size += nbytes
985987
self._crc = crc32(data, self._crc)
@@ -990,6 +992,8 @@ def write(self, data):
990992
return nbytes
991993

992994
def close(self):
995+
if self.closed:
996+
return
993997
super().close()
994998
# Flush any data from the compressor, and update header info
995999
if self._compressor:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ Library
314314
times when schema is changing. Indirectly fixed by switching to
315315
use sqlite3_prepare_v2() in bpo-9303. Patch by Aviv Palivoda.
316316

317+
- bpo-30017: Allowed calling the close() method of the zip entry writer object
318+
multiple times. Writing to a closed writer now always produces a ValueError.
319+
317320
- bpo-29998: Pickling and copying ImportError now preserves name and path
318321
attributes.
319322

0 commit comments

Comments
 (0)