@@ -5766,6 +5766,8 @@ def test_read_with_unsuitable_metadata_encoding(self):
57665766 zipfile .ZipFile (TESTFN , "a" , metadata_encoding = 'utf-8' )
57675767
57685768 def test_read_after_append (self ):
5769+ """Files loaded from an archive should keep original filenames and encodings
5770+ when rewritten to central directory in append mode."""
57695771 newname = '\u56db ' # Han 'four'
57705772 newname2 = 'fünf' # representable in cp437, but still stored as UTF-8
57715773 expected_names = [* self .file_names , newname , newname2 ]
@@ -5789,7 +5791,7 @@ def test_read_after_append(self):
57895791 with zipfile .ZipFile (TESTFN , "a" , metadata_encoding = 'shift_jis' ) as zipfp :
57905792 self ._test_read (zipfp , expected_names , expected_content )
57915793
5792- def test_append_keep_efs_flag (self ):
5794+ def test_append_keep_efs (self ):
57935795 """Files loaded from an archive should keep original EFS flags when
57945796 rewritten to central directory in append mode."""
57955797 names = ['file1' , 'file2' , 'file3' , 'file4' ]
@@ -5808,45 +5810,41 @@ def mock_encode(self):
58085810 zinfo = zipfile .ZipInfo (name )
58095811 zinfo .comment = comments [i ]
58105812 zipfp .writestr (zinfo , contents [i ])
5813+ self .assertEqual (zipfp .namelist (), names )
58115814
58125815 with zipfile .ZipFile (TESTFN , "a" ) as zipfp :
5816+ self ._test_read (zipfp , names , contents , comments , efs_flags )
58135817 # trigger archive rewriting
58145818 zipfp .comment = b'comment'
58155819
58165820 with zipfile .ZipFile (TESTFN , "r" ) as zipfp :
5817- self .assertEqual (zipfp .comment , b'comment' )
58185821 self ._test_read (zipfp , names , contents , comments , efs_flags )
58195822
5820- def test_write_enforce_efs_flag (self ):
5821- """New files should enforce EFS flag if filename or comment is not ASCII."""
5823+ def test_write_enforce_efs_on_demand (self ):
5824+ """New files should enforce EFS flag if filename or comment is not ASCII,
5825+ regardless of mode or metadata_encoding."""
58225826 names = ['\u4e00 ' , '\u4e8c ' , 'file3' , 'file4' ]
58235827 contents = [b'content1' , b'content2' , b'content3' , b'content4' ]
58245828 comments = ['\u4e00 ' .encode ('utf-8' ), b'foo' , '\u4e8c ' .encode ('utf-8' ), b'bar' ]
58255829 expected_efs_flags = [True , True , True , False ]
58265830
5827- with zipfile .ZipFile (TESTFN , "w" ) as zipfp :
5828- for i , name in enumerate (names ):
5829- zinfo = zipfile .ZipInfo (name )
5830- zinfo .comment = comments [i ]
5831- zipfp .writestr (zinfo , contents [i ])
5832- self .assertEqual (zipfp .namelist (), names )
5833-
5834- with zipfile .ZipFile (TESTFN , "r" ) as zipfp :
5835- self ._test_read (zipfp , names , contents , comments , expected_efs_flags )
5831+ def sub_test ():
5832+ with zipfile .ZipFile (TESTFN , mode = mode ,
5833+ metadata_encoding = encoding ) as zipfp :
5834+ for i , name in enumerate (names ):
5835+ zinfo = zipfile .ZipInfo (name )
5836+ zinfo .comment = comments [i ]
5837+ zipfp .writestr (zinfo , contents [i ])
5838+ self .assertEqual (zipfp .namelist (), names )
58365839
5837- def test_write_with_metadata_encoding (self ):
5838- """metadata_encoding should not affect the encoding of new files."""
5839- names = ['\u4e00 ' , 'file2' ]
5840- contents = ['\u4e00 ' .encode ('utf-8' ), '\u4e8c ' .encode ('utf-8' )]
5841- expected_efs_flags = [True , False ]
5840+ with zipfile .ZipFile (TESTFN , "r" ) as zipfp :
5841+ self ._test_read (zipfp , names , contents , comments , expected_efs_flags )
58425842
58435843 for mode in ("w" , "x" , "a" ):
5844- unlink (TESTFN )
5845- with zipfile .ZipFile (TESTFN , mode , metadata_encoding = 'shift_jis' ) as zipfp :
5846- for i , name in enumerate (names ):
5847- zipfp .writestr (name , contents [i ])
5848- with zipfile .ZipFile (TESTFN , 'r' ) as zipfp :
5849- self ._test_read (zipfp , names , contents , None , expected_efs_flags )
5844+ for encoding in (None , 'shift_jis' ):
5845+ unlink (TESTFN )
5846+ with self .subTest (mode = mode , metadata_encoding = encoding ):
5847+ sub_test ()
58505848
58515849 def test_add_comment (self ):
58525850 with zipfile .ZipFile (TESTFN , "r" ) as zipfp :
0 commit comments