@@ -548,7 +548,7 @@ def test_append_to_concatenated_zip_file(self):
548
548
def test_ignores_newline_at_end (self ):
549
549
with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ) as zipfp :
550
550
zipfp .write (TESTFN , TESTFN )
551
- with open (TESTFN2 , 'a' ) as f :
551
+ with open (TESTFN2 , 'a' , encoding = 'utf-8' ) as f :
552
552
f .write ("\r \n \00 \00 \00 " )
553
553
with zipfile .ZipFile (TESTFN2 , "r" ) as zipfp :
554
554
self .assertIsInstance (zipfp , zipfile .ZipFile )
@@ -557,7 +557,7 @@ def test_ignores_stuff_appended_past_comments(self):
557
557
with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ) as zipfp :
558
558
zipfp .comment = b"this is a comment"
559
559
zipfp .write (TESTFN , TESTFN )
560
- with open (TESTFN2 , 'a' ) as f :
560
+ with open (TESTFN2 , 'a' , encoding = 'utf-8' ) as f :
561
561
f .write ("abcdef\r \n " )
562
562
with zipfile .ZipFile (TESTFN2 , "r" ) as zipfp :
563
563
self .assertIsInstance (zipfp , zipfile .ZipFile )
@@ -1245,13 +1245,13 @@ def test_write_with_optimization(self):
1245
1245
def test_write_python_directory (self ):
1246
1246
os .mkdir (TESTFN2 )
1247
1247
try :
1248
- with open (os .path .join (TESTFN2 , "mod1.py" ), "w" ) as fp :
1248
+ with open (os .path .join (TESTFN2 , "mod1.py" ), "w" , encoding = 'utf-8' ) as fp :
1249
1249
fp .write ("print(42)\n " )
1250
1250
1251
- with open (os .path .join (TESTFN2 , "mod2.py" ), "w" ) as fp :
1251
+ with open (os .path .join (TESTFN2 , "mod2.py" ), "w" , encoding = 'utf-8' ) as fp :
1252
1252
fp .write ("print(42 * 42)\n " )
1253
1253
1254
- with open (os .path .join (TESTFN2 , "mod2.txt" ), "w" ) as fp :
1254
+ with open (os .path .join (TESTFN2 , "mod2.txt" ), "w" , encoding = 'utf-8' ) as fp :
1255
1255
fp .write ("bla bla bla\n " )
1256
1256
1257
1257
with TemporaryFile () as t , zipfile .PyZipFile (t , "w" ) as zipfp :
@@ -1268,10 +1268,10 @@ def test_write_python_directory(self):
1268
1268
def test_write_python_directory_filtered (self ):
1269
1269
os .mkdir (TESTFN2 )
1270
1270
try :
1271
- with open (os .path .join (TESTFN2 , "mod1.py" ), "w" ) as fp :
1271
+ with open (os .path .join (TESTFN2 , "mod1.py" ), "w" , encoding = 'utf-8' ) as fp :
1272
1272
fp .write ("print(42)\n " )
1273
1273
1274
- with open (os .path .join (TESTFN2 , "mod2.py" ), "w" ) as fp :
1274
+ with open (os .path .join (TESTFN2 , "mod2.py" ), "w" , encoding = 'utf-8' ) as fp :
1275
1275
fp .write ("print(42 * 42)\n " )
1276
1276
1277
1277
with TemporaryFile () as t , zipfile .PyZipFile (t , "w" ) as zipfp :
@@ -1287,15 +1287,15 @@ def test_write_python_directory_filtered(self):
1287
1287
1288
1288
def test_write_non_pyfile (self ):
1289
1289
with TemporaryFile () as t , zipfile .PyZipFile (t , "w" ) as zipfp :
1290
- with open (TESTFN , 'w' ) as f :
1290
+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as f :
1291
1291
f .write ('most definitely not a python file' )
1292
1292
self .assertRaises (RuntimeError , zipfp .writepy , TESTFN )
1293
1293
unlink (TESTFN )
1294
1294
1295
1295
def test_write_pyfile_bad_syntax (self ):
1296
1296
os .mkdir (TESTFN2 )
1297
1297
try :
1298
- with open (os .path .join (TESTFN2 , "mod1.py" ), "w" ) as fp :
1298
+ with open (os .path .join (TESTFN2 , "mod1.py" ), "w" , encoding = 'utf-8' ) as fp :
1299
1299
fp .write ("Bad syntax in python file\n " )
1300
1300
1301
1301
with TemporaryFile () as t , zipfile .PyZipFile (t , "w" ) as zipfp :
@@ -1317,7 +1317,7 @@ def test_write_pyfile_bad_syntax(self):
1317
1317
def test_write_pathlike (self ):
1318
1318
os .mkdir (TESTFN2 )
1319
1319
try :
1320
- with open (os .path .join (TESTFN2 , "mod1.py" ), "w" ) as fp :
1320
+ with open (os .path .join (TESTFN2 , "mod1.py" ), "w" , encoding = 'utf-8' ) as fp :
1321
1321
fp .write ("print(42)\n " )
1322
1322
1323
1323
with TemporaryFile () as t , zipfile .PyZipFile (t , "w" ) as zipfp :
@@ -1646,7 +1646,7 @@ def test_close_erroneous_file(self):
1646
1646
# On Windows, this causes the os.unlink() call to fail because the
1647
1647
# underlying file is still open. This is SF bug #412214.
1648
1648
#
1649
- with open (TESTFN , "w" ) as fp :
1649
+ with open (TESTFN , "w" , encoding = "utf-8" ) as fp :
1650
1650
fp .write ("this is not a legal zip file\n " )
1651
1651
try :
1652
1652
zf = zipfile .ZipFile (TESTFN )
@@ -1656,7 +1656,7 @@ def test_close_erroneous_file(self):
1656
1656
def test_is_zip_erroneous_file (self ):
1657
1657
"""Check that is_zipfile() correctly identifies non-zip files."""
1658
1658
# - passing a filename
1659
- with open (TESTFN , "w" ) as fp :
1659
+ with open (TESTFN , "w" , encoding = 'utf-8' ) as fp :
1660
1660
fp .write ("this is not a legal zip file\n " )
1661
1661
self .assertFalse (zipfile .is_zipfile (TESTFN ))
1662
1662
# - passing a path-like object
@@ -1719,11 +1719,11 @@ def test_non_existent_file_raises_OSError(self):
1719
1719
self .assertRaises (OSError , zipfile .ZipFile , TESTFN )
1720
1720
1721
1721
def test_empty_file_raises_BadZipFile (self ):
1722
- f = open (TESTFN , 'w' )
1722
+ f = open (TESTFN , 'w' , encoding = 'utf-8' )
1723
1723
f .close ()
1724
1724
self .assertRaises (zipfile .BadZipFile , zipfile .ZipFile , TESTFN )
1725
1725
1726
- with open (TESTFN , 'w' ) as fp :
1726
+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as fp :
1727
1727
fp .write ("short file" )
1728
1728
self .assertRaises (zipfile .BadZipFile , zipfile .ZipFile , TESTFN )
1729
1729
@@ -1741,7 +1741,7 @@ def test_closed_zip_raises_ValueError(self):
1741
1741
self .assertRaises (ValueError , zipf .open , "foo.txt" )
1742
1742
self .assertRaises (ValueError , zipf .testzip )
1743
1743
self .assertRaises (ValueError , zipf .writestr , "bogus.txt" , "bogus" )
1744
- with open (TESTFN , 'w' ) as f :
1744
+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as f :
1745
1745
f .write ('zipfile test data' )
1746
1746
self .assertRaises (ValueError , zipf .write , TESTFN )
1747
1747
@@ -1911,7 +1911,7 @@ def test_open_empty_file(self):
1911
1911
# Issue 1710703: Check that opening a file with less than 22 bytes
1912
1912
# raises a BadZipFile exception (rather than the previously unhelpful
1913
1913
# OSError)
1914
- f = open (TESTFN , 'w' )
1914
+ f = open (TESTFN , 'w' , encoding = 'utf-8' )
1915
1915
f .close ()
1916
1916
self .assertRaises (zipfile .BadZipFile , zipfile .ZipFile , TESTFN , 'r' )
1917
1917
@@ -2532,7 +2532,7 @@ def test_many_opens(self):
2532
2532
zipf .read ('ones' )
2533
2533
with zipf .open ('ones' ) as zopen1 :
2534
2534
pass
2535
- with open (os .devnull ) as f :
2535
+ with open (os .devnull , "rb" ) as f :
2536
2536
self .assertLess (f .fileno (), 100 )
2537
2537
2538
2538
def test_write_while_reading (self ):
@@ -2695,11 +2695,11 @@ def test_list_command(self):
2695
2695
@requires_zlib ()
2696
2696
def test_create_command (self ):
2697
2697
self .addCleanup (unlink , TESTFN )
2698
- with open (TESTFN , 'w' ) as f :
2698
+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as f :
2699
2699
f .write ('test 1' )
2700
2700
os .mkdir (TESTFNDIR )
2701
2701
self .addCleanup (rmtree , TESTFNDIR )
2702
- with open (os .path .join (TESTFNDIR , 'file.txt' ), 'w' ) as f :
2702
+ with open (os .path .join (TESTFNDIR , 'file.txt' ), 'w' , encoding = 'utf-8' ) as f :
2703
2703
f .write ('test 2' )
2704
2704
files = [TESTFN , TESTFNDIR ]
2705
2705
namelist = [TESTFN , TESTFNDIR + '/' , TESTFNDIR + '/file.txt' ]
@@ -2911,7 +2911,7 @@ def test_subdir_is_dir(self, alpharep):
2911
2911
def test_open (self , alpharep ):
2912
2912
root = zipfile .Path (alpharep )
2913
2913
a , b , g = root .iterdir ()
2914
- with a .open () as strm :
2914
+ with a .open (encoding = "utf-8" ) as strm :
2915
2915
data = strm .read ()
2916
2916
assert data == "content of a"
2917
2917
@@ -2923,7 +2923,7 @@ def test_open_write(self):
2923
2923
zf = zipfile .Path (zipfile .ZipFile (io .BytesIO (), mode = 'w' ))
2924
2924
with zf .joinpath ('file.bin' ).open ('wb' ) as strm :
2925
2925
strm .write (b'binary contents' )
2926
- with zf .joinpath ('file.txt' ).open ('w' ) as strm :
2926
+ with zf .joinpath ('file.txt' ).open ('w' , encoding = "utf-8" ) as strm :
2927
2927
strm .write ('text file' )
2928
2928
2929
2929
def test_open_extant_directory (self ):
@@ -2954,7 +2954,7 @@ def test_open_missing_directory(self):
2954
2954
def test_read (self , alpharep ):
2955
2955
root = zipfile .Path (alpharep )
2956
2956
a , b , g = root .iterdir ()
2957
- assert a .read_text () == "content of a"
2957
+ assert a .read_text (encoding = "utf-8" ) == "content of a"
2958
2958
assert a .read_bytes () == b"content of a"
2959
2959
2960
2960
@pass_alpharep
@@ -2963,21 +2963,21 @@ def test_joinpath(self, alpharep):
2963
2963
a = root .joinpath ("a.txt" )
2964
2964
assert a .is_file ()
2965
2965
e = root .joinpath ("b" ).joinpath ("d" ).joinpath ("e.txt" )
2966
- assert e .read_text () == "content of e"
2966
+ assert e .read_text (encoding = "utf-8" ) == "content of e"
2967
2967
2968
2968
@pass_alpharep
2969
2969
def test_joinpath_multiple (self , alpharep ):
2970
2970
root = zipfile .Path (alpharep )
2971
2971
e = root .joinpath ("b" , "d" , "e.txt" )
2972
- assert e .read_text () == "content of e"
2972
+ assert e .read_text (encoding = "utf-8" ) == "content of e"
2973
2973
2974
2974
@pass_alpharep
2975
2975
def test_traverse_truediv (self , alpharep ):
2976
2976
root = zipfile .Path (alpharep )
2977
2977
a = root / "a.txt"
2978
2978
assert a .is_file ()
2979
2979
e = root / "b" / "d" / "e.txt"
2980
- assert e .read_text () == "content of e"
2980
+ assert e .read_text (encoding = "utf-8" ) == "content of e"
2981
2981
2982
2982
@pass_alpharep
2983
2983
def test_traverse_simplediv (self , alpharep ):
@@ -3034,9 +3034,9 @@ def test_mutability(self, alpharep):
3034
3034
alpharep .writestr ('foo.txt' , 'foo' )
3035
3035
alpharep .writestr ('bar/baz.txt' , 'baz' )
3036
3036
assert any (child .name == 'foo.txt' for child in root .iterdir ())
3037
- assert (root / 'foo.txt' ).read_text () == 'foo'
3037
+ assert (root / 'foo.txt' ).read_text (encoding = "utf-8" ) == 'foo'
3038
3038
(baz ,) = (root / 'bar' ).iterdir ()
3039
- assert baz .read_text () == 'baz'
3039
+ assert baz .read_text (encoding = "utf-8" ) == 'baz'
3040
3040
3041
3041
HUGE_ZIPFILE_NUM_ENTRIES = 2 ** 13
3042
3042
@@ -3070,7 +3070,7 @@ def test_read_does_not_close(self, alpharep):
3070
3070
alpharep = self .zipfile_ondisk (alpharep )
3071
3071
with zipfile .ZipFile (alpharep ) as file :
3072
3072
for rep in range (2 ):
3073
- zipfile .Path (file , 'a.txt' ).read_text ()
3073
+ zipfile .Path (file , 'a.txt' ).read_text (encoding = "utf-8" )
3074
3074
3075
3075
@pass_alpharep
3076
3076
def test_subclass (self , alpharep ):
0 commit comments