Skip to content

gh-84481: Add ZipFile.data_offset attribute #132165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 6, 2025

Conversation

emmatyping
Copy link
Member

@emmatyping emmatyping commented Apr 6, 2025

This attribute provides the offset to zip data from the start of the file, when available.

I chose data_offset to avoid confusion with the _offset property that is unrelated.


📚 Documentation preview 📚: https://cpython-previews--132165.org.readthedocs.build/

This attribute provides the offset to zip data from the start of the file, when available.
@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@emmatyping
Copy link
Member Author

I have signed the CLA with my current email. (I had already signed it under another email anyway)

@gpshead gpshead merged commit 0788948 into python:main Apr 6, 2025
43 checks passed
except (AttributeError, OSError):
self.fp = _Tellable(self.fp)
self.start_dir = 0
self._seekable = False
self._data_offset = None
Copy link
Member

@picnixz picnixz Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be set if mode == 'a' and if BadZipFile is raised. In this case, we should still have a None according to the docs (otherwise an AttributeError will be raised).

For mode 'r', it should be noted that failing _RealGetContents() means that the object will never be initialized so we don't care about the lack of attribute.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that in this case, since mode 'a' calls tell() unconditionally, we can set the data_offset to the result of that, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though honestly the defensive thing to do here is to initialize it to None early on then change it later when possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though honestly the defensive thing to do here is to initialize it to None early on then change it later when possible.

Yes, that's what I thought.

seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
* Add ZipFile.data_offset attribute

This attribute provides the offset to zip data from the start of the file, when available.

* Add blurb-it

* Try fixing class ref in NEWS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants