Skip to content

bpo-40105: Truncating file if opened in append mode. #19337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 8 additions & 4 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,6 @@ def test_struct_sizes(self):

def test_comments(self):
"""Check that comments on the archive are handled properly."""

# check default comment is empty
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
self.assertEqual(zipf.comment, b'')
Expand Down Expand Up @@ -1853,13 +1852,18 @@ def test_comments(self):
self.assertEqual(zipf.comment, b"an updated comment")

# check that comments are correctly shortened in append mode
# and the file is indeed truncated
original_comment = b"original comment that's longer"
one_byte_shorter_comment = original_comment[:-1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO extracting these to variables makes the test code harder to understand rather than easier. I suggest undoing this change.

with zipfile.ZipFile(TESTFN,mode="w") as zipf:
zipf.comment = b"original comment that's longer"
zipf.comment = original_comment
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
original_zip_size = os.path.getsize(TESTFN)
with zipfile.ZipFile(TESTFN,mode="a") as zipf:
zipf.comment = b"shorter comment"
zipf.comment = one_byte_shorter_comment
self.assertEqual(original_zip_size, os.path.getsize(TESTFN) + 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing for the precise size is too restrictive IMO. It would be better to just test that the size was reduced. That would also allow keeping the existing comments.

with zipfile.ZipFile(TESTFN,mode="r") as zipf:
self.assertEqual(zipf.comment, b"shorter comment")
self.assertEqual(zipf.comment, one_byte_shorter_comment)

def test_unicode_comment(self):
with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
Expand Down
2 changes: 2 additions & 0 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,8 @@ def _write_end_record(self):
centDirSize, centDirOffset, len(self._comment))
self.fp.write(endrec)
self.fp.write(self._comment)
if self.mode == "a":
self.fp.truncate()
self.fp.flush()

def _fpclose(self, fp):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZipFiles are truncated when shorter comment is provided in append ("a") mode.