]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-110715: Add missing import in zipfile (gh-110822) (gh-110861)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 14 Oct 2023 07:58:31 +0000 (09:58 +0200)
committerGitHub <noreply@github.com>
Sat, 14 Oct 2023 07:58:31 +0000 (07:58 +0000)
gh-110715: Add missing import in zipfile (gh-110822)
(cherry picked from commit 4110cfec1233139b4e7c63459ba465ab80554e3e)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Lib/test/test_zipfile/test_core.py
Lib/zipfile/__init__.py

index 9960259c4cde0c7afbd0504d80abc6ea6cd316ae..c2cc2c827ca69543226eb19a1d952f5cf75f49bf 100644 (file)
@@ -1769,13 +1769,9 @@ class OtherTests(unittest.TestCase):
             self.assertEqual(zf.filelist[0].filename, "foo.txt")
             self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
 
-    @requires_zlib()
-    def test_read_zipfile_containing_unicode_path_extra_field(self):
+    def create_zipfile_with_extra_data(self, filename, extra_data_name):
         with zipfile.ZipFile(TESTFN, mode='w') as zf:
-            # create a file with a non-ASCII name
-            filename = '이름.txt'
-            filename_encoded = filename.encode('utf-8')
-
+            filename_encoded = filename.encode("utf-8")
             # create a ZipInfo object with Unicode path extra field
             zip_info = zipfile.ZipInfo(filename)
 
@@ -1785,7 +1781,7 @@ class OtherTests(unittest.TestCase):
             import zlib
             filename_crc = struct.pack('<L', zlib.crc32(filename_encoded))
 
-            extra_data = version_of_unicode_path + filename_crc + filename_encoded
+            extra_data = version_of_unicode_path + filename_crc + extra_data_name
             tsize = len(extra_data).to_bytes(2, 'little')
 
             zip_info.extra = tag_for_unicode_path + tsize + extra_data
@@ -1793,9 +1789,24 @@ class OtherTests(unittest.TestCase):
             # add the file to the ZIP archive
             zf.writestr(zip_info, b'Hello World!')
 
+    @requires_zlib()
+    def test_read_zipfile_containing_unicode_path_extra_field(self):
+        self.create_zipfile_with_extra_data("이름.txt", "이름.txt".encode("utf-8"))
         with zipfile.ZipFile(TESTFN, "r") as zf:
             self.assertEqual(zf.filelist[0].filename, "이름.txt")
 
+    @requires_zlib()
+    def test_read_zipfile_warning(self):
+        self.create_zipfile_with_extra_data("이름.txt", b"")
+        with self.assertWarns(UserWarning):
+            zipfile.ZipFile(TESTFN, "r").close()
+
+    @requires_zlib()
+    def test_read_zipfile_error(self):
+        self.create_zipfile_with_extra_data("이름.txt", b"\xff")
+        with self.assertRaises(zipfile.BadZipfile):
+            zipfile.ZipFile(TESTFN, "r").close()
+
     def test_read_after_write_unicode_filenames(self):
         with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
             zipfp.writestr('приклад', b'sample')
index 9fc1840ba1e5340658832ddc30c8394f4c1ccda8..5b981d5742c12ec10f48084f8f0370bea80e7ec8 100644 (file)
@@ -531,6 +531,7 @@ class ZipInfo (object):
                         if up_unicode_name:
                             self.filename = _sanitize_filename(up_unicode_name)
                         else:
+                            import warnings
                             warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
                 except struct.error as e:
                     raise BadZipFile("Corrupt unicode path extra field (0x7075)") from e