diff options
author | R. David Murray <rdmurray@bitdance.com> | 2010-12-21 21:57:54 (GMT) |
---|---|---|
committer | R. David Murray <rdmurray@bitdance.com> | 2010-12-21 21:57:54 (GMT) |
commit | 30f9c8c68459466813a438a3511b5c9a8ba0d9a5 (patch) | |
tree | 10a0e40e41992105f51eb98abd96181fa497cff2 /Lib/zipfile.py | |
parent | 6cfc5124f2a615922d32ac19a319ffab8edaad0f (diff) | |
download | cpython-30f9c8c68459466813a438a3511b5c9a8ba0d9a5.zip cpython-30f9c8c68459466813a438a3511b5c9a8ba0d9a5.tar.gz cpython-30f9c8c68459466813a438a3511b5c9a8ba0d9a5.tar.bz2 |
Merged revisions 87430 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r87430 | r.david.murray | 2010-12-21 16:53:37 -0500 (Tue, 21 Dec 2010) | 9 lines
#4871: check that zipfile password is bytes, and give useful error message.
Previously passing a string in as the password would fail either with
an assertion error or a TypeError with a confusing error message.
Note that a string can't be accepted since zipfile has no way to
guess what encoding should be used to turn it into bytes.
Patch by Victor Stinner.
........
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r-- | Lib/zipfile.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py index c376ee4..2ec6306 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -880,8 +880,12 @@ class ZipFile: def setpassword(self, pwd): """Set default password for encrypted files.""" - assert isinstance(pwd, bytes) - self.pwd = pwd + if pwd and not isinstance(pwd, bytes): + raise TypeError("pwd: expected bytes, got %s" % type(pwd)) + if pwd: + self.pwd = pwd + else: + self.pwd = None def read(self, name, pwd=None): """Return file bytes (as a string) for name.""" @@ -891,6 +895,8 @@ class ZipFile: """Return file-like object for 'name'.""" if mode not in ("r", "U", "rU"): raise RuntimeError('open() requires mode "r", "U", or "rU"') + if pwd and not isinstance(pwd, bytes): + raise TypeError("pwd: expected bytes, got %s" % type(pwd)) if not self.fp: raise RuntimeError( "Attempt to read ZIP archive that was already closed") @@ -943,8 +949,8 @@ class ZipFile: # completely random, while the 12th contains the MSB of the CRC, # or the MSB of the file time depending on the header type # and is used to check the correctness of the password. - bytes = zef_file.read(12) - h = list(map(zd, bytes[0:12])) + header = zef_file.read(12) + h = list(map(zd, header[0:12])) if zinfo.flag_bits & 0x8: # compare against the file type from extended local headers check_byte = (zinfo._raw_time >> 8) & 0xff |