tarfile: TarFile.offset attribute is not updated with the remainder when closing a TarFile
オープン
まだ誰も着手していません。
stdlib
type-bug
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
The TarFile.offset attribute is not updated with the remainder when closing the tar.
import io
import pathlib
import tarfile
tar = tarfile.open("test.tar", "w")
t = tarfile.TarInfo("foo")
t.size = 123
tar.addfile(t, io.BytesIO(b"a" * t.size))
tar.close()
assert len(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):
CPython versions tested on:
3.14, 3.13
Operating systems tested on:
Linux
Linked PRs
- gh-133919
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
関連するファイルは Lib/tarfile.py と Lib/test/test_tarfile.py です。まず TarFile.close と既存の EOF-marker テストを読み、その後 tarfile のテストスイートを実行してください。完了とは、offset の動作が明示的にカバーされ、クローズ後に書き込まれる意図されたバイト数と一致していることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100