You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tarfile: TarFile.offset attribute is not updated with the remainder when closing a TarFile #129255
The TarFile.offset attribute is not updated with the remainder when closing the tar.
importioimportpathlibimporttarfiletar=tarfile.open("test.tar", "w")
t=tarfile.TarInfo("foo")
t.size=123tar.addfile(t, io.BytesIO(b"a"*t.size))
tar.close()
assertlen(pathlib.Path("test.tar").read_bytes()) ==tar.offset# Fails because tar.offset is 2048, although 10240 bytes have been written
If this intentional, a test case asserting the current behavior should be added.
In case this is not intentional, a suggested change to make the offset match the number of bytes written, and a test case asserting it, are included in the snippet below.
In either case, I'd be happy to submit a PR.
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index a0fab46b24e..feafb88d2d3 100644
--- a/Lib/tarfile.py+++ b/Lib/tarfile.py@@ -2027,6 +2027,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()
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 2549b6b35ad..4d1a2b2171b 100644
--- a/Lib/test/test_tarfile.py+++ b/Lib/test/test_tarfile.py@@ -1333,6 +1333,18 @@ 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):
changed the title [-]tarfile: The offset does not match the number of bytes written after closing a tarfile which has been written to[/-][+]tarfile: TarFile.offset attribute is not updated with the remainder when closing a TarFile[/+]on Jan 24, 2025
Activity
[-]tarfile: The offset does not match the number of bytes written after closing a tarfile which has been written to[/-][+]tarfile: TarFile.offset attribute is not updated with the remainder when closing a TarFile[/+]StanFromIreland commentedon Feb 26, 2025
I agree it offset should be updated. Are you still planning on opening a PR?
emontnemery commentedon Feb 28, 2025
Yes, sure, I'll open a PR 👍