Skip to content

Commit

Permalink
pythongh-110715: Add missing import in zipfile (pythongh-110822)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclips4 authored Oct 14, 2023
1 parent e2b3d83 commit 4110cfe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,13 +1769,9 @@ def test_write_unicode_filenames(self):
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)

Expand All @@ -1785,17 +1781,32 @@ def test_read_zipfile_containing_unicode_path_extra_field(self):
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

# 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')
Expand Down
1 change: 1 addition & 0 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ def _decodeExtra(self, filename_crc):
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
Expand Down

0 comments on commit 4110cfe

Please sign in to comment.