]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39359: [zipfile] add missing "pwd: expected bytes, got str" exception (GH-18031)
authorDaniel Hillier <daniel.hillier@gmail.com>
Thu, 23 Sep 2021 21:37:53 +0000 (07:37 +1000)
committerGitHub <noreply@github.com>
Thu, 23 Sep 2021 21:37:53 +0000 (23:37 +0200)
Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS.d/next/Library/2020-01-16-13-54-28.bpo-39359.hzTu0h.rst [new file with mode: 0644]

index 325491fd809760fdbfac7515633ac1458bf901f1..df48fabff951d87192365dc4ff3c99c828c1af09 100644 (file)
@@ -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")
index 3efeecb13bd1798402c1de150214c99f538da8ce..8e9325b934326064256b2061f26972a3054b6188 100644 (file)
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2020-01-16-13-54-28.bpo-39359.hzTu0h.rst b/Misc/NEWS.d/next/Library/2020-01-16-13-54-28.bpo-39359.hzTu0h.rst
new file mode 100644 (file)
index 0000000..ed4eb0c
--- /dev/null
@@ -0,0 +1 @@
+Add one missing check that the password is a bytes object for an encrypted zipfile.
\ No newline at end of file