10000 bpo-43651: Fix EncodingWarning in zipfile (GH-25650) · python/cpython@caae717 · GitHub
[go: up one dir, main page]

Skip to content

Commit caae717

Browse files
authored
bpo-43651: Fix EncodingWarning in zipfile (GH-25650)
1 parent 5987b8c commit caae717

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

Lib/test/test_zipfile.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def test_append_to_concatenated_zip_file(self):
548548
def test_ignores_newline_at_end(self):
549549
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
550550
zipfp.write(TESTFN, TESTFN)
551-
with open(TESTFN2, 'a') as f:
551+
with open(TESTFN2, 'a', encoding='utf-8') as f:
552552
f.write("\r\n\00\00\00")
553553
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
554554
self.assertIsInstance(zipfp, zipfile.ZipFile)
@@ -557,7 +557,7 @@ def test_ignores_stuff_appended_past_comments(self):
557557
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
558558
zipfp.comment = b"this is a comment"
559559
zipfp.write(TESTFN, TESTFN)
560-
with open(TESTFN2, 'a') as f:
560+
with open(TESTFN2, 'a', encoding='utf-8') as f:
561561
f.write("abcdef\r\n")
562562
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
563563
self.assertIsInstance(zipfp, zipfile.ZipFile)
@@ -1245,13 +1245,13 @@ def test_write_with_optimization(self):
12451245
def test_write_python_directory(self):
12461246
os.mkdir(TESTFN2)
12471247
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:
12491249
fp.write("print(42)\n")
12501250

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:
12521252
fp.write("print(42 * 42)\n")
12531253

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:
12551255
fp.write("bla bla bla\n")
12561256

12571257
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
@@ -1268,10 +1268,10 @@ def test_write_python_directory(self):
12681268
def test_write_python_directory_filtered(self):
12691269
os.mkdir(TESTFN2)
12701270
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:
12721272
fp.write("print(42)\n")
12731273

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:
12751275
fp.write("print(42 * 42)\n")
12761276

12771277
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
@@ -1287,15 +1287,15 @@ def test_write_python_directory_filtered(self):
12871287

12881288
def test_write_non_pyfile(self):
12891289
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:
12911291
f.write('most definitely not a python file')
12921292
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
12931293
unlink(TESTFN)
12941294

12951295
def test_write_pyfile_bad_syntax(self):
12961296
os.mkdir(TESTFN2)
12971297
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:
12991299
fp.write("Bad syntax in python file\n")
13001300

13011301
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
@@ -1317,7 +1317,7 @@ def test_write_pyfile_bad_syntax(self):
13171317
def test_write_pathlike(self):
13181318
os.mkdir(TESTFN2)
13191319
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:
13211321
fp.write("print(42)\n")
13221322

13231323
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
@@ -1646,7 +1646,7 @@ def test_close_erroneous_file(self):
16461646
# On Windows, this causes the os.unlink() call to fail because the
16471647
# underlying file is still open. This is SF bug #412214.
16481648
#
1649-
with open(TESTFN, "w") as fp:
1649+
with open(TESTFN, "w", encoding="utf-8") as fp:
16501650
fp.write("this is not a legal zip file\n")
16511651
try:
16521652
zf = zipfile.ZipFile(TESTFN)
@@ -1656,7 +1656,7 @@ def test_close_erroneous_file(self):
16561656
def test_is_zip_erroneous_file(self):
16571657
"""Check that is_zipfile() correctly identifies non-zip files."""
16581658
# - passing a filename
1659-
with open(TESTFN, "w") as fp:
1659+
with open(TESTFN, "w", encoding='utf-8') as fp:
16601660
fp.write("this is not a legal zip file\n")
16611661
self.assertFalse(zipfile.is_zipfile(TESTFN))
16621662
# - passing a path-like object
@@ -1719,11 +1719,11 @@ def test_non_existent_file_raises_OSError(self):
17191719
self.assertRaises(OSError, zipfile.ZipFile, TESTFN)
17201720

17211721
def test_empty_file_raises_BadZipFile(self):
1722-
f = open(TESTFN, 'w')
1722+
f = open(TESTFN, 'w', encoding='utf-8')
17231723
f.close()
17241724
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
17251725

1726-
with open(TESTFN, 'w') as fp:
1726+
with open(TESTFN, 'w', encoding='utf-8') as fp:
17271727
fp.write("short file")
17281728
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
17291729

17411741
self.assertRaises(ValueError, zipf.open, "foo.txt")
17421742
self.assertRaises(ValueError, zipf.testzip)
17431743
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:
17451745
f.write('zipfile test data')
17461746
self.assertRaises(ValueError, zipf.write, TESTFN)
17471747

@@ -1911,7 +1911,7 @@ def test_open_empty_file(self):
19111911
# Issue 1710703: Check that opening a file with less than 22 bytes
19121912
# raises a BadZipFile exception (rather than the previously unhelpful
19131913
# OSError)
1914-
f = open(TESTFN, 'w')
1914+
f = open(TESTFN, 'w', encoding='utf-8')
19151915
f.close()
19161916
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
19171917

@@ -2532,7 +2532,7 @@ def test_many_opens(self):
25322532
zipf.read('ones')
25332533
with zipf.open('ones') as zopen1:
25342534
pass
2535-
with open(os.devnull) as f:
2535+
with open(os.devnull, "rb") as f:
25362536
self.assertLess(f.fileno(), 100)
25372537

25382538
def test_write_while_reading(self):
@@ -2695,11 +2695,11 @@ def test_list_command(self):
26952695
@requires_zlib()
26962696
def test_create_command(self):
26972697
self.addCleanup(unlink, TESTFN)
2698-
with open(TESTFN, 'w') as f:
2698+
with open(TESTFN, 'w', encoding='utf-8') as f:
26992699
f.write('test 1')
27002700
os.mkdir(TESTFNDIR)
27012701
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:
27032703
f.write('test 2')
27042704
files = [TESTFN, TESTFNDIR]
27052705
namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
@@ -2911,7 +2911,7 @@ def test_subdir_is_dir(self, alpharep):
29112911
def test_open(self, alpharep):
29122912
root = zipfile.Path(alpharep)
29132913
a, b, g = root.iterdir()
2914-
with a.open() as strm:
2914+
with a.open(encoding="utf-8") as strm:
29152915
data = strm.read()
29162916
assert data == "content of a"
29172917

@@ -2923,7 +2923,7 @@ def test_open_write(self):
29232923
zf = zipfile.Path(zipfile.ZipFile(io.BytesIO(), mode='w'))
29242924
with zf.joinpath('file.bin').open('wb') as strm:
29252925
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:
29272927
strm.write('text file')
29282928

29292929
def test_open_extant_directory(self):
@@ -2954,7 +2954,7 @@ def test_open_missing_directory(self):
29542954
def test_read(self, alpharep):
29552955
root = zipfile.Path(alpharep)
29562956
a, b, g = root.iterdir()
2957-
assert a.read_text() == "content of a"
2957+
assert a.read_text(encoding="utf-8") == "content of a"
29582958
assert a.read_bytes() == b"content of a"
29592959

29602960
@pass_alpharep
@@ -2963,21 +2963,21 @@ def test_joinpath(self, alpharep):
29632963
a = root.joinpath("a.txt")
29642964
assert a.is_file()
29652965
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"
29672967

29682968
@pass_alpharep
29692969
def test_joinpath_multiple(self, alpharep):
29702970
root = zipfile.Path(alpharep)
29712971
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"
29732973

29742974
@pass_alpharep
29752975
def test_traverse_truediv(self, alpharep):
29762976
root = zipfile.Path(alpharep)
29772977
a = root / "a.txt"
29782978
assert a.is_file()
29792979
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"
29812981

29822982
@pass_alpharep
29832983
def test_traverse_simplediv(self, alpharep):
@@ -3034,9 +3034,9 @@ def test_mutability(self, alpharep):
30343034
alpharep.writestr('foo.txt', 'foo')
30353035
alpharep.writestr('bar/baz.txt', 'baz')
30363036
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'
30383038
(baz,) = (root / 'bar').iterdir()
3039-
assert baz.read_text() == 'baz'
3039+
assert baz.read_text(encoding="utf-8") == 'baz'
30403040

30413041
HUGE_ZIPFILE_NUM_ENTRIES = 2 ** 13
30423042

@@ -3070,7 +3070,7 @@ def test_read_does_not_close(self, alpharep):
30703070
alpharep = self.zipfile_ondisk(alpharep)
30713071
with zipfile.ZipFile(alpharep) as file:
30723072
for rep in range(2):
3073-
zipfile.Path(file, 'a.txt').read_text()
3073+
zipfile.Path(file, 'a.txt').read_text(encoding="utf-8")
30743074

30753075
@pass_alpharep
30763076
def test_subclass(self, alpharep):

Lib/zipfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,8 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
23342334
if args or kwargs:
23352335
raise ValueError("encoding args invalid for binary operation")
23362336
return stream
2337+
else:
2338+
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
23372339
return io.TextIOWrapper(stream, *args, **kwargs)
23382340

23392341
@property
@@ -2345,6 +2347,7 @@ def filename(self):
23452347
return pathlib.Path(self.root.filename).joinpath(self.at)
23462348

23472349
def read_text(self, *args, **kwargs):
2350+
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
23482351
with self.open('r', *args, **kwargs) as strm:
23492352
return strm.read()
23502353

0 commit comments

Comments
 (0)
0