tarfile: addfile() doesn't set member offsets
Open
Nobody has claimed this yet.
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
It appears that tarfile.TarFile.addfile() sets neither offset nor offset_data attributes in the TarInfo added to self.members, when it's done adding the file. Members in a reopened TAR have correct values.
Reproducer
from io import BytesIO
from tarfile import TarFile, TarInfo
with TarFile("test.tar", "w") as tar:
data = b"data"
tarinfo = TarInfo("test1.txt")
tarinfo.size = len(data)
tar.addfile(tarinfo, BytesIO(data))
tarinfo = TarInfo("test2.txt")
tarinfo.size = len(data)
tar.addfile(tarinfo, BytesIO(data))
for member in tar.getmembers():
print(member.name, member.offset, member.offset_data)
with TarFile("test.tar", "r") as tar:
for member in tar.getmembers():
print(member.name, member.offset, member.offset_data)
Expected output
test1.txt 0 512
test2.txt 1024 1536
test1.txt 0 512
test2.txt 1024 1536
Actual output
test1.txt 0 0
test2.txt 0 0
test1.txt 0 512
test2.txt 1024 1536
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Linked PRs
- gh-150128
- gh-150131
- gh-150278
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at tarfile.TarFile.addfile() and compare the in-memory TarInfo offsets with the values produced when the archive is reopened. Run the supplied BytesIO reproducer and verify that getmembers() reports the expected offset and offset_data values for both members; review linked PRs gh-150128, gh-150131, and gh-150278 before starting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100