summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_zipfile.py21
-rw-r--r--Lib/zipfile.py4
2 files changed, 19 insertions, 6 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 325491f..df48fab 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -2190,10 +2190,23 @@ class DecryptionTests(unittest.TestCase):
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")
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 3efeecb..8e9325b 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1508,8 +1508,6 @@ class ZipFile:
"""
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:
@@ -1577,6 +1575,8 @@ class ZipFile:
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)