summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_zipfile.py6
-rw-r--r--Lib/zipfile.py14
-rw-r--r--Misc/NEWS3
3 files changed, 19 insertions, 4 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 26bc47b..c989bf0 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -991,6 +991,12 @@ class DecryptionTests(unittest.TestCase):
self.zip2.setpassword(b"12345")
self.assertEqual(self.zip2.read("zero"), self.plain2)
+ def test_unicode_password(self):
+ self.assertRaises(TypeError, self.zip.setpassword, "unicode")
+ self.assertRaises(TypeError, self.zip.read, "test.txt", "python")
+ self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python")
+ self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python")
+
class TestsWithRandomBinaryFiles(unittest.TestCase):
def setUp(self):
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
diff --git a/Misc/NEWS b/Misc/NEWS
index 6a216edc..bd43ee7 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
Library
-------
+- Issue #4871: The zipfile module now gives a more useful error message if
+ an attempt is made to use a string to specify the archive password.
+
- Issue #10750: The ``raw`` attribute of buffered IO objects is now read-only.
- Issue #6791: Limit header line length (to 65535 bytes) in http.client