mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
[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 4110cfec12
)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
This commit is contained in:
parent
7c3e8e5af0
commit
9786959a11
2 changed files with 19 additions and 7 deletions
|
@ -1769,13 +1769,9 @@ class OtherTests(unittest.TestCase):
|
||||||
self.assertEqual(zf.filelist[0].filename, "foo.txt")
|
self.assertEqual(zf.filelist[0].filename, "foo.txt")
|
||||||
self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
|
self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
|
||||||
|
|
||||||
@requires_zlib()
|
def create_zipfile_with_extra_data(self, filename, extra_data_name):
|
||||||
def test_read_zipfile_containing_unicode_path_extra_field(self):
|
|
||||||
with zipfile.ZipFile(TESTFN, mode='w') as zf:
|
with zipfile.ZipFile(TESTFN, mode='w') as zf:
|
||||||
# create a file with a non-ASCII name
|
filename_encoded = filename.encode("utf-8")
|
||||||
filename = '이름.txt'
|
|
||||||
filename_encoded = filename.encode('utf-8')
|
|
||||||
|
|
||||||
# create a ZipInfo object with Unicode path extra field
|
# create a ZipInfo object with Unicode path extra field
|
||||||
zip_info = zipfile.ZipInfo(filename)
|
zip_info = zipfile.ZipInfo(filename)
|
||||||
|
|
||||||
|
@ -1785,7 +1781,7 @@ class OtherTests(unittest.TestCase):
|
||||||
import zlib
|
import zlib
|
||||||
filename_crc = struct.pack('<L', zlib.crc32(filename_encoded))
|
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')
|
tsize = len(extra_data).to_bytes(2, 'little')
|
||||||
|
|
||||||
zip_info.extra = tag_for_unicode_path + tsize + extra_data
|
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
|
# add the file to the ZIP archive
|
||||||
zf.writestr(zip_info, b'Hello World!')
|
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:
|
with zipfile.ZipFile(TESTFN, "r") as zf:
|
||||||
self.assertEqual(zf.filelist[0].filename, "이름.txt")
|
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):
|
def test_read_after_write_unicode_filenames(self):
|
||||||
with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
|
with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
|
||||||
zipfp.writestr('приклад', b'sample')
|
zipfp.writestr('приклад', b'sample')
|
||||||
|
|
|
@ -531,6 +531,7 @@ class ZipInfo (object):
|
||||||
if up_unicode_name:
|
if up_unicode_name:
|
||||||
self.filename = _sanitize_filename(up_unicode_name)
|
self.filename = _sanitize_filename(up_unicode_name)
|
||||||
else:
|
else:
|
||||||
|
import warnings
|
||||||
warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
|
warnings.warn("Empty unicode path extra field (0x7075)", stacklevel=2)
|
||||||
except struct.error as e:
|
except struct.error as e:
|
||||||
raise BadZipFile("Corrupt unicode path extra field (0x7075)") from e
|
raise BadZipFile("Corrupt unicode path extra field (0x7075)") from e
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue