[Python-checkins] bpo-39359: [zipfile] add missing "pwd: expected bytes, got str" exception (GH-18031)
ambv
webhook-mailer at python.org
Thu Sep 23 17:37:58 EDT 2021
https://github.com/python/cpython/commit/91099e25446b214725f8e2ffffbec6f90553260c
commit: 91099e25446b214725f8e2ffffbec6f90553260c
branch: main
author: Daniel Hillier <daniel.hillier at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-09-23T23:37:53+02:00
summary:
bpo-39359: [zipfile] add missing "pwd: expected bytes, got str" exception (GH-18031)
files:
A Misc/NEWS.d/next/Library/2020-01-16-13-54-28.bpo-39359.hzTu0h.rst
M Lib/test/test_zipfile.py
M Lib/zipfile.py
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 325491fd80976..df48fabff951d 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -2190,10 +2190,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")
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 3efeecb13bd17..8e9325b934326 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1508,8 +1508,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:
@@ -1577,6 +1575,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)
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
index 0000000000000..ed4eb0c873d86
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-16-13-54-28.bpo-39359.hzTu0h.rst
@@ -0,0 +1 @@
+Add one missing check that the password is a bytes object for an encrypted zipfile.
\ No newline at end of file
More information about the Python-checkins
mailing list