Skip to content

Commit

Permalink
bpo-43651: Fix EncodingWarning in zipfile (GH-25650)
Browse files Browse the repository at this point in the history
  • Loading branch information
methane authored Apr 27, 2021
1 parent 5987b8c commit caae717
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
56 changes: 28 additions & 28 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def test_append_to_concatenated_zip_file(self):
def test_ignores_newline_at_end(self):
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
zipfp.write(TESTFN, TESTFN)
with open(TESTFN2, 'a') as f:
with open(TESTFN2, 'a', encoding='utf-8') as f:
f.write("\r\n\00\00\00")
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
self.assertIsInstance(zipfp, zipfile.ZipFile)
Expand All @@ -557,7 +557,7 @@ def test_ignores_stuff_appended_past_comments(self):
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
zipfp.comment = b"this is a comment"
zipfp.write(TESTFN, TESTFN)
with open(TESTFN2, 'a') as f:
with open(TESTFN2, 'a', encoding='utf-8') as f:
f.write("abcdef\r\n")
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
self.assertIsInstance(zipfp, zipfile.ZipFile)
Expand Down Expand Up @@ -1245,13 +1245,13 @@ def test_write_with_optimization(self):
def test_write_python_directory(self):
os.mkdir(TESTFN2)
try:
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
fp.write("print(42)\n")

with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
fp.write("print(42 * 42)\n")

with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
with open(os.path.join(TESTFN2, "mod2.txt"), "w", encoding='utf-8') as fp:
fp.write("bla bla bla\n")

with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Expand All @@ -1268,10 +1268,10 @@ def test_write_python_directory(self):
def test_write_python_directory_filtered(self):
os.mkdir(TESTFN2)
try:
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
fp.write("print(42)\n")

with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
fp.write("print(42 * 42)\n")

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

def test_write_non_pyfile(self):
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
with open(TESTFN, 'w') as f:
with open(TESTFN, 'w', encoding='utf-8') as f:
f.write('most definitely not a python file')
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
unlink(TESTFN)

def test_write_pyfile_bad_syntax(self):
os.mkdir(TESTFN2)
try:
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
fp.write("Bad syntax in python file\n")

with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Expand All @@ -1317,7 +1317,7 @@ def test_write_pyfile_bad_syntax(self):
def test_write_pathlike(self):
os.mkdir(TESTFN2)
try:
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
fp.write("print(42)\n")

with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
Expand Down Expand Up @@ -1646,7 +1646,7 @@ def test_close_erroneous_file(self):
# On Windows, this causes the os.unlink() call to fail because the
# underlying file is still open. This is SF bug #412214.
#
with open(TESTFN, "w") as fp:
with open(TESTFN, "w", encoding="utf-8") as fp:
fp.write("this is not a legal zip file\n")
try:
zf = zipfile.ZipFile(TESTFN)
Expand All @@ -1656,7 +1656,7 @@ def test_close_erroneous_file(self):
def test_is_zip_erroneous_file(self):
"""Check that is_zipfile() correctly identifies non-zip files."""
# - passing a filename
with open(TESTFN, "w") as fp:
with open(TESTFN, "w", encoding='utf-8') as fp:
fp.write("this is not a legal zip file\n")
self.assertFalse(zipfile.is_zipfile(TESTFN))
# - passing a path-like object
Expand Down Expand Up @@ -1719,11 +1719,11 @@ def test_non_existent_file_raises_OSError(self):
self.assertRaises(OSError, zipfile.ZipFile, TESTFN)

def test_empty_file_raises_BadZipFile(self):
f = open(TESTFN, 'w')
f = open(TESTFN, 'w', encoding='utf-8')
f.close()
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)

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

Expand All @@ -1741,7 +1741,7 @@ def test_closed_zip_raises_ValueError(self):
self.assertRaises(ValueError, zipf.open, "foo.txt")
self.assertRaises(ValueError, zipf.testzip)
self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus")
with open(TESTFN, 'w') as f:
with open(TESTFN, 'w', encoding='utf-8') as f:
f.write('zipfile test data')
self.assertRaises(ValueError, zipf.write, TESTFN)

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

Expand Down Expand Up @@ -2532,7 +2532,7 @@ def test_many_opens(self):
zipf.read('ones')
with zipf.open('ones') as zopen1:
pass
with open(os.devnull) as f:
with open(os.devnull, "rb") as f:
self.assertLess(f.fileno(), 100)

def test_write_while_reading(self):
Expand Down Expand Up @@ -2695,11 +2695,11 @@ def test_list_command(self):
@requires_zlib()
def test_create_command(self):
self.addCleanup(unlink, TESTFN)
with open(TESTFN, 'w') as f:
with open(TESTFN, 'w', encoding='utf-8') as f:
f.write('test 1')
os.mkdir(TESTFNDIR)
self.addCleanup(rmtree, TESTFNDIR)
with open(os.path.join(TESTFNDIR, 'file.txt'), 'w') as f:
with open(os.path.join(TESTFNDIR, 'file.txt'), 'w', encoding='utf-8') as f:
f.write('test 2')
files = [TESTFN, TESTFNDIR]
namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
Expand Down Expand Up @@ -2911,7 +2911,7 @@ def test_subdir_is_dir(self, alpharep):
def test_open(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g = root.iterdir()
with a.open() as strm:
with a.open(encoding="utf-8") as strm:
data = strm.read()
assert data == "content of a"

Expand All @@ -2923,7 +2923,7 @@ def test_open_write(self):
zf = zipfile.Path(zipfile.ZipFile(io.BytesIO(), mode='w'))
with zf.joinpath('file.bin').open('wb') as strm:
strm.write(b'binary contents')
with zf.joinpath('file.txt').open('w') as strm:
with zf.joinpath('file.txt').open('w', encoding="utf-8") as strm:
strm.write('text file')

def test_open_extant_directory(self):
Expand Down Expand Up @@ -2954,7 +2954,7 @@ def test_open_missing_directory(self):
def test_read(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g = root.iterdir()
assert a.read_text() == "content of a"
assert a.read_text(encoding="utf-8") == "content of a"
assert a.read_bytes() == b"content of a"

@pass_alpharep
Expand All @@ -2963,21 +2963,21 @@ def test_joinpath(self, alpharep):
a = root.joinpath("a.txt")
assert a.is_file()
e = root.joinpath("b").joinpath("d").joinpath("e.txt")
assert e.read_text() == "content of e"
assert e.read_text(encoding="utf-8") == "content of e"

@pass_alpharep
def test_joinpath_multiple(self, alpharep):
root = zipfile.Path(alpharep)
e = root.joinpath("b", "d", "e.txt")
assert e.read_text() == "content of e"
assert e.read_text(encoding="utf-8") == "content of e"

@pass_alpharep
def test_traverse_truediv(self, alpharep):
root = zipfile.Path(alpharep)
a = root / "a.txt"
assert a.is_file()
e = root / "b" / "d" / "e.txt"
assert e.read_text() == "content of e"
assert e.read_text(encoding="utf-8") == "content of e"

@pass_alpharep
def test_traverse_simplediv(self, alpharep):
Expand Down Expand Up @@ -3034,9 +3034,9 @@ def test_mutability(self, alpharep):
alpharep.writestr('foo.txt', 'foo')
alpharep.writestr('bar/baz.txt', 'baz')
assert any(child.name == 'foo.txt' for child in root.iterdir())
assert (root / 'foo.txt').read_text() == 'foo'
assert (root / 'foo.txt').read_text(encoding="utf-8") == 'foo'
(baz,) = (root / 'bar').iterdir()
assert baz.read_text() == 'baz'
assert baz.read_text(encoding="utf-8") == 'baz'

HUGE_ZIPFILE_NUM_ENTRIES = 2 ** 13

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

@pass_alpharep
def test_subclass(self, alpharep):
Expand Down
3 changes: 3 additions & 0 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,8 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
if args or kwargs:
raise ValueError("encoding args invalid for binary operation")
return stream
else:
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
return io.TextIOWrapper(stream, *args, **kwargs)

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

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

Expand Down

0 comments on commit caae717

Please sign in to comment.