Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101144: Allow open and read_text encoding to be positional. #101145

Merged
Merged
6 changes: 5 additions & 1 deletion Lib/test/test_zipfile/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def test_open(self, alpharep):
a, b, g = root.iterdir()
with a.open(encoding="utf-8") as strm:
data = strm.read()
assert data == "content of a"
self.assertEqual(data, "content of a")
with a.open('r', "utf-8") as strm: # No gh-101144 TypeError
data = strm.read()
self.assertEqual(data, "content of a")

def test_open_write(self):
"""
Expand Down Expand Up @@ -187,6 +190,7 @@ def test_read(self, alpharep):
root = zipfile.Path(alpharep)
a, b, g = root.iterdir()
assert a.read_text(encoding="utf-8") == "content of a"
a.read_text("utf-8") # No TypeError per gh-101144.
assert a.read_bytes() == b"content of a"

@pass_alpharep
Expand Down
6 changes: 4 additions & 2 deletions Lib/zipfile/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
raise ValueError("encoding args invalid for binary operation")
return stream
else:
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
if "encoding" in kwargs:
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
gpshead marked this conversation as resolved.
Show resolved Hide resolved
return io.TextIOWrapper(stream, *args, **kwargs)

@property
Expand All @@ -282,7 +283,8 @@ 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"))
gpshead marked this conversation as resolved.
Show resolved Hide resolved
if "encoding" in kwargs:
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
with self.open('r', *args, **kwargs) as strm:
gpshead marked this conversation as resolved.
Show resolved Hide resolved
return strm.read()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:func:`zipfile.Path.open` and :func:`zipfile.Path.read_text` accept
``encoding`` as a positional argument. This was the behavior in Python 3.9 and
earlier. 3.10 introduced a regression where supplying it as a positional
argument would lead to a :exc:`TypeError`.