Skip to content

Commit 9786959

Browse files
[3.12] gh-110715: Add missing import in zipfile (gh-110822) (gh-110861)
gh-110715: Add missing import in zipfile (gh-110822) (cherry picked from commit 4110cfe) Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent 7c3e8e5 commit 9786959

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Lib/test/test_zipfile/test_core.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1769,13 +1769,9 @@ def test_write_unicode_filenames(self):
17691769
self.assertEqual(zf.filelist[0].filename, "foo.txt")
17701770
self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
17711771

1772-
@requires_zlib()
1773-
def test_read_zipfile_containing_unicode_path_extra_field(self):
1772+
def create_zipfile_with_extra_data(self, filename, extra_data_name):
17741773
with zipfile.ZipFile(TESTFN, mode='w') as zf:
1775-
# create a file with a non-ASCII name
1776-
filename = '이름.txt'
1777-
filename_encoded = filename.encode('utf-8')
1778-
1774+
filename_encoded = filename.encode("utf-8")
17791775
# create a ZipInfo object with Unicode path extra field
17801776
zip_info = zipfile.ZipInfo(filename)
17811777

@@ -1785,17 +1781,32 @@ def test_read_zipfile_containing_unicode_path_extra_field(self):
17851781
import zlib
17861782
filename_crc = struct.pack('<L', zlib.crc32(filename_encoded))
17871783

1788-
extra_data = version_of_unicode_path + filename_crc + filename_encoded
1784+
extra_data = version_of_unicode_path + filename_crc + extra_data_name
17891785
tsize = len(extra_data).to_bytes(2, 'little')
17901786

17911787
zip_info.extra = tag_for_unicode_path + tsize + extra_data
17921788

17931789
# add the file to the ZIP archive
17941790
zf.writestr(zip_info, b'Hello World!')
17951791

1792+
@requires_zlib()
1793+
def test_read_zipfile_containing_unicode_path_extra_field(self):
1794+
self.create_zipfile_with_extra_data("이름.txt", "이름.txt".encode("utf-8"))
17961795
with zipfile.ZipFile(TESTFN, "r") as zf:
17971796
self.assertEqual(zf.filelist[0].filename, "이름.txt")
17981797

1798+
@requires_zlib()
1799+
def test_read_zipfile_warning(self):
1800+
self.create_zipfile_with_extra_data("이름.txt", b"")
1801+
with self.assertWarns(UserWarning):
1802+
zipfile.ZipFile(TESTFN, "r").close()
1803+
1804+
@requires_zlib()
1805+
def test_read_zipfile_error(self):
1806+
self.create_zipfile_with_extra_data("이름.txt", b"\xff")
1807+
with self.assertRaises(zipfile.BadZipfile):
1808+
zipfile.ZipFile(TESTFN, "r").close()
1809+
17991810
def test_read_after_write_unicode_filenames(self):
18001811
with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
18011812
zipfp.writestr('приклад', b'sample')

Lib/zipfile/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ def _decodeExtra(self, filename_crc):
531531
if up_unicode_name:
532532
self.filename = _sanitize_filename(up_unicode_name)
533533
else:
534+
import warnings
534535
warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
535536
except struct.error as e:
536537
raise BadZipFile("Corrupt unicode path extra field (0x7075)") from e

0 commit comments

Comments
 (0)