This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: filemode in repr of ZipInfo, but is not a ZipInfo attribute
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: G.Rakosy, iritkatriel, ronaldoussoren
Priority: normal Keywords:

Created on 2021-08-05 13:59 by G.Rakosy, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test-zip.zip G.Rakosy, 2021-09-28 10:15
Messages (5)
msg399006 - (view) Author: Gabor Rakosy (G.Rakosy) Date: 2021-08-05 13:59
"""
ZipInfo crashes on filemode

In file /usr/lib/python3.7/zipfile.py | class ZipInfo.__slots__
Does not contain keyword 'filemode'.
"""

import zipfile

file_zip = zipfile.ZipFile("test-one-dir.zip", mode='r')

res = []

info = file_zip.infolist()
print("info[0]", type(info[0]), info[0])

print("\n# ## Good")
for inf in info:
    print("\ninf", type(inf), inf)
    res.append((
        inf.filename,
##        inf.filemode,
        inf.compress_type,
        inf.compress_size,
        inf.file_size))

for fileinfo in res:
    print("\n", fileinfo)

print("\n# ## Bad")
for inf in info:
    print("\ninf", type(inf), inf)
    res.append((
        inf.filename,
        inf.filemode,
        inf.compress_type,
        inf.compress_size,
        inf.file_size))

for fileinfo in res:
    print("\n", fileinfo)
msg401511 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-09 18:51
Crash typically means segfault or the like, not an exception. 

Can you provide code to create the file "test-one-dir.zip" so that we can reproduce the issue?
msg402147 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-09-19 08:48
I'm not sure what the issue is here. ZipInfo does not have a filemode attribute, that's why it is not in ZipInfo.__slots__.

That said, it might be interesting to add a property to ZipInfo that extracts the file mode from ZipInfo.external_attr.
msg402765 - (view) Author: Gabor Rakosy (G.Rakosy) Date: 2021-09-28 10:15
Like Ronald Oussoren wrote "ZipInfo does not have a filemode attribute, that's why it is not in ZipInfo.__slots__."

----------------
import zipfile

file_zip = zipfile.ZipFile("test-one-dir.zip", mode='r')
for info in file_zip.infolist():
    print(info)
---------------

In the print, it shows the option 'filemode'. And that option is not accessible. That's the reason why i made this issue.
msg402977 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-09-30 18:50
The repr() output of a ZipInfo contains filemode, but it is not an attribute of those objects, see <https://docs.python.org/3/library/zipfile.html#zipinfo-objects> for a list of attributes and methods.

Adding an @property for mode that extracts the mode (as an integer) from the external_attr could be useful, that avoids having to know about that encoding in users of the library.

You currently have to extract the mode yourself. Luckily that isn't too hard, basically: ```mode = info.external_attr >> 16```. Only use this when the value is not 0, as it will be zero when the archive was created by software that doesn't store a mode.   You can convert this integer to a human readable string using the function stat.filemode(). 

I've changed the version to 3.11 because adding a mode attribute to ZipInfo would be a new feature and is therefore only possible in a new feature release.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89004
2021-09-30 18:50:44ronaldoussorensettitle: ZipInfo crashes on filemode -> filemode in repr of ZipInfo, but is not a ZipInfo attribute
messages: + msg402977
versions: + Python 3.11, - Python 3.7
2021-09-28 10:15:16G.Rakosysetfiles: + test-zip.zip

messages: + msg402765
2021-09-19 08:48:54ronaldoussorensetnosy: + ronaldoussoren
messages: + msg402147
2021-09-09 18:51:30iritkatrielsettype: crash -> behavior

messages: + msg401511
nosy: + iritkatriel
2021-08-05 13:59:45G.Rakosycreate