Skip to content

gh-91356: Add ZipInfo.mode attribute #32252

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add documentation, write tests
  • Loading branch information
dignissimus committed Apr 2, 2022
commit 232ab1f1a03f1b05a82b0cf175767dba4a552ed5
3 changes: 3 additions & 0 deletions Doc/library/zipfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,9 @@ Instances have the following methods and attributes:

Size of the uncompressed file.

.. attribute:: ZipInfo.mode

The mode of the file (permissions)

.. _zipfile-commandline:
.. program:: zipfile
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,21 @@ def test_create_empty_zipinfo_default_attributes(self):
self.assertEqual(zi.file_size, 0)
self.assertEqual(zi.compress_size, 0)

def test_zipinfo_mode(self):
zinfo = zipfile.ZipInfo()
self.assertEqual(zinfo.mode, 0)

zinfo.mode = 0o777
self.assertEqual(zinfo.mode, 0o777)

zinfo.mode = 0o700
self.assertEqual(zinfo.mode, 0o700)

# Setting the file mode should not overwrite the file type
zinfo.mode = 0xFFFF
self.assertLessEqual(zinfo.mode, 0o777)
self.assertEqual(zinfo.external_attr >> 16 >> 9, 0)

def test_zipfile_with_short_extra_field(self):
"""If an extra field in the header is less than 4 bytes, skip it."""
zipdata = (
Expand Down
4 changes: 2 additions & 2 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,11 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True):

@property
def mode(self):
return (self.external_attr >> 16) & (0o777)
return (self.external_attr >> 16) & 0o777

@mode.setter
def mode(self, value):
self.external_attr = (self.external_attr & ~(0o777 << 16)) | (value << 16 )
self.external_attr = self.external_attr & ~(0o777 << 16) | ((value & 0o777) << 16)

def is_dir(self):
"""Return True if this archive member is a directory."""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy