Skip to content

gh-129255: tarfile: Update tarfile.offset with the remainder on close #133919

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant

Open
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
1 change: 1 addition & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
@@ -2088,6 +2088,7 @@ def close(self):
blocks, remainder = divmod(self.offset, RECORDSIZE)
if remainder > 0:
self.fileobj.write(NUL * (RECORDSIZE - remainder))
self.offset += (RECORDSIZE - remainder)
finally:
if not self._extfileobj:
self.fileobj.close()
11 changes: 11 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
@@ -1358,6 +1358,17 @@ def test_eof_marker(self):
with self.open(tmpname, "rb") as fobj:
self.assertEqual(len(fobj.read()), tarfile.RECORDSIZE * 2)

def test_offset_on_close(self):
# Check the offset after calling close matches the total number of
# bytes written.
tar = tarfile.open(tmpname, self.mode)
t = tarfile.TarInfo("foo")
tar.addfile(t)
tar.close()

with self.open(tmpname, "rb") as fobj:
self.assertEqual(len(fobj.read()), tar.offset)


class WriteTest(WriteTestBase, unittest.TestCase):

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the ``offset`` with the remainder when closing a :func:`tarfile.TarFile`.
Loading
Oops, something went wrong.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant