gh-110715: Add missing import in zipfile (gh-110822)

This commit is contained in:
Kirill Podoprigora 2023-10-14 10:17:47 +03:00 committed by GitHub
parent e2b3d831fd
commit 4110cfec12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View 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')

View File

@ -545,6 +545,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