Skip to content

gh-150075: tar.addfile() doesn't set member offsets#150131

Open
grantlouisherman wants to merge 3 commits into
python:mainfrom
grantlouisherman:fix-150075-tar-addfile-no-offsets
Open

gh-150075: tar.addfile() doesn't set member offsets#150131
grantlouisherman wants to merge 3 commits into
python:mainfrom
grantlouisherman:fix-150075-tar-addfile-no-offsets

Conversation

@grantlouisherman
Copy link
Copy Markdown

@grantlouisherman grantlouisherman commented May 19, 2026

Original bug report:

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


Fix
I added the offset info for a given file based on the current offset of the file and then after the block size is calculated. I also included a test to validate the behavior

…file

Signed-off-by: Grant Herman <grantlouisherman041@gmail.com>
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 19, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 19, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@grantlouisherman
Copy link
Copy Markdown
Author

I dont think the news is necessary it seems like a pretty minor addition

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 20, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@grantlouisherman
Copy link
Copy Markdown
Author

@ethanfurman should I squash commits?

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 20, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant