classification
Title: ZipFile.comment expects bytes
Type: enhancement Stage: needs patch
Components: Documentation Versions: Python 3.2, Python 3.3, Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, serhiy.storchaka, swamiyeswanth, vstinner, xuanji
Priority: normal Keywords:

Created on 2011-02-09 15:09 by xuanji, last changed 2012-10-25 16:17 by serhiy.storchaka.

Messages (5)
msg128217 - (view) Author: Xuanji Li (xuanji) * Date: 2011-02-09 15:09
The documentation for zipfile describes ZipFile.comment as "The comment text associated with the ZIP file." From reading this I expect that setting it to a string is ok; however ZipFile.comment must actually be set to bytes (or a bytes-like object, I am not very sure).

This may also unexpectedly affect old code because I saw one patch on the bug tracker that was written just last year that set ZipFile.comment to a string. 

IMO there are 2 ways to fix:

1) Change docs to mention that ZipFile.comment only accepts bytes
2) Patch zipfile.py to accept string and try to convert, throwing an error if the conversion fails
msg128478 - (view) Author: yeswanth (swamiyeswanth) Date: 2011-02-13 04:36
IMO the Zipfile.comment should accept strings too instead of just accepting bytes , so patching should help i guess
msg128479 - (view) Author: yeswanth (swamiyeswanth) Date: 2011-02-13 04:41
can we use str.encode() function to convert string into bytes ?
msg128484 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-02-13 09:59
> can we use str.encode() function to convert string into bytes ?

Can you try different ZIP archivers to check which encoding is expected? WinZip, WinRAR, 7-zip, "zip" command line program on Linux, etc.

And do you have any reference into a ZIP documentation?
msg173765 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-25 16:17
The ZIP specification says:

"""
If general purpose bit 11 is unset, the file name and comment should conform 
to the original ZIP character encoding.  If general purpose bit 11 is set, the 
filename and comment must support The Unicode Standard, Version 4.1.0 or 
greater using the character encoding form defined by the UTF-8 storage 
specification.  The Unicode Standard is published by the The Unicode
Consortium (www.unicode.org).  UTF-8 encoded data stored within ZIP files 
is expected to not include a byte order mark (BOM). 
"""

Also there is extension for UTF-8 encoded file comment.  All this means the file comment should be interpreted as an unicode string.

However the specification says nothing about .ZIP file comment (except that encryption or data authentication is applied to it).

Since changeset 4186f20d9fa4 ZipFile.comment raises TypeError on try to assign non-bytes. I think the documentation should be clarified.
History
Date User Action Args
2012-10-25 16:17:30serhiy.storchakasettype: enhancement
components: - Library (Lib)
versions: + Python 3.3, Python 3.4
nosy: + serhiy.storchaka

messages: + msg173765
stage: needs patch
2011-02-13 09:59:45vstinnersetnosy: vstinner, docs@python, xuanji, swamiyeswanth
messages: + msg128484
2011-02-13 04:41:51swamiyeswanthsetnosy: vstinner, docs@python, xuanji, swamiyeswanth
messages: + msg128479
2011-02-13 04:36:40swamiyeswanthsetnosy: + swamiyeswanth
messages: + msg128478
2011-02-09 15:09:00xuanjicreate