Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 17 additions & 4 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,10 +2165,23 @@ def test_good_password(self):
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")
expected_msg = "pwd: expected bytes, got str"

with self.assertRaisesRegex(TypeError, expected_msg):
self.zip.setpassword("unicode")

with self.assertRaisesRegex(TypeError, expected_msg):
self.zip.read("test.txt", "python")

with self.assertRaisesRegex(TypeError, expected_msg):
self.zip.open("test.txt", pwd="python")

with self.assertRaisesRegex(TypeError, expected_msg):
self.zip.extract("test.txt", pwd="python")

with self.assertRaisesRegex(TypeError, expected_msg):
self.zip.pwd = "python"
self.zip.open("test.txt")

def test_seek_tell(self):
self.zip.setpassword(b"python")
Expand Down
4 changes: 2 additions & 2 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,8 +1480,6 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
"""
if mode not in {"r", "w"}:
raise ValueError('open() requires mode "r" or "w"')
if pwd and not isinstance(pwd, bytes):
raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
if pwd and (mode == "w"):
raise ValueError("pwd is only supported for reading files")
if not self.fp:
Expand Down Expand Up @@ -1549,6 +1547,8 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
if is_encrypted:
if not pwd:
pwd = self.pwd
if pwd and not isinstance(pwd, bytes):
raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
if not pwd:
raise RuntimeError("File %r is encrypted, password "
"required for extraction" % name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add one missing check that the password is a bytes object for an encrypted zipfile.