FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-39359, zipfile: add missing "pwd: expected bytes, got str" exception by danifus · Pull Request #18031 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 17 additions & 4 deletions Lib/test/test_zipfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Back | FazBrowse Home | New Git URL