Skip to content

Commit 681eed8

Browse files
committed
tarfile: Update tarfile.offset with the remainder on close
1 parent 27ed645 commit 681eed8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Lib/tarfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@ def close(self):
20882088
blocks, remainder = divmod(self.offset, RECORDSIZE)
20892089
if remainder > 0:
20902090
self.fileobj.write(NUL * (RECORDSIZE - remainder))
2091+
self.offset += (RECORDSIZE - remainder)
20912092
finally:
20922093
if not self._extfileobj:
20932094
self.fileobj.close()

Lib/test/test_tarfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,17 @@ def test_eof_marker(self):
13581358
with self.open(tmpname, "rb") as fobj:
13591359
self.assertEqual(len(fobj.read()), tarfile.RECORDSIZE * 2)
13601360

1361+
def test_offset_on_close(self):
1362+
# Check the offset after calling close matches the total number of
1363+
# bytes written.
1364+
tar = tarfile.open(tmpname, self.mode)
1365+
t = tarfile.TarInfo("foo")
1366+
tar.addfile(t)
1367+
tar.close()
1368+
1369+
with self.open(tmpname, "rb") as fobj:
1370+
self.assertEqual(len(fobj.read()), tar.offset)
1371+
13611372

13621373
class WriteTest(WriteTestBase, unittest.TestCase):
13631374

0 commit comments

Comments
 (0)