Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,8 @@ def test_open_conflicting_handles(self):
zipf.writestr('str', 'abcde')
with self.assertRaises(ValueError):
zipf.write(__file__, 'file')
with self.assertRaises(ValueError):
zipf.mkdir('directory')
with self.assertRaises(ValueError):
zipf.close()
w1.write(msg2)
Expand Down Expand Up @@ -5518,6 +5520,9 @@ def test_mkdir(self):
zf.extractall(target)
self.assertEqual(set(os.listdir(target)), {"directory", "directory2", "directory3", "directory4"})

with self.assertRaises(ValueError):
zf.mkdir("closed")

def test_create_directory_with_write(self):
with zipfile.ZipFile(TESTFN, "w") as zf:
zf.writestr(zipfile.ZipInfo('directory/'), '')
Expand Down
8 changes: 8 additions & 0 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,14 @@ def mkdir(self, zinfo_or_directory_name, mode=511):
else:
raise TypeError("Expected type str or ZipInfo")

if not self.fp:
raise ValueError(
"Attempt to write to ZIP archive that was already closed")
if self._writing:
Comment thread
StanFromIreland marked this conversation as resolved.
raise ValueError(
"Can't write to ZIP archive while an open writing handle exists."
)

with self._lock:
if self._seekable:
self.fp.seek(self.start_dir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`zipfile.ZipFile.mkdir` to raise :exc:`ValueError` when called
while a writable member handle is open, preventing corruption of the ZIP
archive.
Loading