Skip to content

Commit cc8c391

Browse files
committed
Simplify and optimize EncodedMetadataTests setUp
Write the specific encoded filename and EFS flag bit directly via mocking.
1 parent 31d5fc9 commit cc8c391

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

Lib/test/test_zipfile/test_core.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5674,6 +5674,7 @@ def test_execute_zip64(self):
56745674

56755675
class EncodedMetadataTests(unittest.TestCase):
56765676
file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
5677+
file_codecs = ['shift_jis', 'shift_jis', 'utf-8']
56775678
file_content = [
56785679
"This is pure ASCII.\n".encode('ascii'),
56795680
# This is modern Japanese. (UTF-8)
@@ -5689,18 +5690,14 @@ def setUp(self):
56895690
# The ASCII names are arbitrary as long as they are length 2 and
56905691
# not otherwise contained in the zip file.
56915692
# Data elements are encoded bytes (ascii, utf-8, shift_jis).
5692-
placeholders = ["n1", "n2"] + self.file_names[2:]
5693-
with zipfile.ZipFile(TESTFN, mode="w") as tf:
5694-
for temp, content in zip(placeholders, self.file_content):
5695-
tf.writestr(temp, content, zipfile.ZIP_STORED)
5696-
# Hack in the Shift JIS names with flag bit 11 (UTF-8) unset.
5697-
with open(TESTFN, "rb") as tf:
5698-
data = tf.read()
5699-
for name, temp in zip(self.file_names, placeholders[:2]):
5700-
data = data.replace(temp.encode('ascii'),
5701-
name.encode('shift_jis'))
5702-
with open(TESTFN, "wb") as tf:
5703-
tf.write(data)
5693+
def mock_encode(zi):
5694+
idx = self.file_names.index(zi.filename)
5695+
return (zi.filename.encode(self.file_codecs[idx]), zi.flag_bits | (
5696+
zipfile._MASK_UTF_FILENAME if self.file_codecs[idx] == 'utf-8' else 0))
5697+
with mock.patch('zipfile.ZipInfo._encodeFilenameFlags', mock_encode), \
5698+
zipfile.ZipFile(TESTFN, mode="w") as tf:
5699+
for name, content in zip(self.file_names, self.file_content):
5700+
tf.writestr(name, content, zipfile.ZIP_STORED)
57045701

57055702
def _test_read(self, zipfp, expected_names, expected_content,
57065703
expected_comments=None, expected_efs_flags=None):

0 commit comments

Comments
 (0)