From 009fcee154f96239f323a398b7f5d928f02062bb Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 27 Apr 2021 15:45:31 +0900 Subject: [PATCH] bpo-43651: Fix EncodingWarning in zipfile (GH-25650) Backport of python/cpython#25650. Ref #84. --- test_zipp.py | 18 +++++++++--------- zipp.py | 3 +++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/test_zipp.py b/test_zipp.py index 77716a0..8b700ce 100644 --- a/test_zipp.py +++ b/test_zipp.py @@ -145,7 +145,7 @@ def test_subdir_is_dir(self, alpharep): def test_open(self, alpharep): root = zipp.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" @@ -157,7 +157,7 @@ def test_open_write(self): zf = zipp.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): @@ -188,7 +188,7 @@ def test_open_missing_directory(self): def test_read(self, alpharep): root = zipp.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 @@ -197,13 +197,13 @@ 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 = zipp.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): @@ -211,7 +211,7 @@ def test_traverse_truediv(self, 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): @@ -268,9 +268,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 @@ -304,7 +304,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): - zipp.Path(file, 'a.txt').read_text() + zipp.Path(file, 'a.txt').read_text(encoding="utf-8") @pass_alpharep def test_subclass(self, alpharep): diff --git a/zipp.py b/zipp.py index 52c82a0..bf21cae 100644 --- a/zipp.py +++ b/zipp.py @@ -239,6 +239,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 @@ -262,6 +264,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()