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
14 changes: 7 additions & 7 deletions Lib/zipfile/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def __init__(self, root, at=""):
self.root = FastLookup.make(root)
self.at = at

def open(self, mode='r', *args, pwd=None, **kwargs):
def open(self, mode='r', encoding=None, *args, pwd=None, **kwargs):
"""
Open this entry as text or binary following the semantics
of ``pathlib.Path.open()`` by passing arguments through
Expand All @@ -254,12 +254,12 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
raise FileNotFoundError(self)
stream = self.root.open(self.at, zip_mode, pwd=pwd)
if 'b' in mode:
if args or kwargs:
if encoding is not None or args or kwargs:
gpshead marked this conversation as resolved.
Show resolved Hide resolved
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)
encoding = io.text_encoding(encoding)
return io.TextIOWrapper(stream, *args, encoding=encoding, **kwargs)
methane marked this conversation as resolved.
Show resolved Hide resolved

@property
def name(self):
Expand All @@ -281,9 +281,9 @@ def stem(self):
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
with self.open('r', *args, **kwargs) as strm:
def read_text(self, encoding=None, *args, **kwargs):
encoding = io.text_encoding(encoding)
gpshead marked this conversation as resolved.
Show resolved Hide resolved
with self.open('r', *args, encoding=encoding, **kwargs) as strm:
return strm.read()

def read_bytes(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Make :func:`zipfile.Path.open` and :func:`zipfile.Path.read_text` also 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`.