Skip to content

Commit

Permalink
Merge commit '009fcee154'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Oct 8, 2022
2 parents 952b20e + 009fcee commit 399de70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 9 additions & 9 deletions test_zipp.py
Expand Up @@ -138,7 +138,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"

Expand All @@ -150,7 +150,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):
Expand Down Expand Up @@ -181,7 +181,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
Expand All @@ -190,21 +190,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 = 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):
root = zipp.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 @@ -261,9 +261,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 @@ -297,7 +297,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):
Expand Down
3 changes: 3 additions & 0 deletions zipp.py
Expand Up @@ -257,6 +257,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 @@ -280,6 +282,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 399de70

Please sign in to comment.