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
1 change: 1 addition & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
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