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
37 changes: 36 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,41 @@ 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: # not a kw, no gh-101144 TypeError
data = strm.read()
self.assertEqual(data, "content of a")

def test_open_encoding_utf16(self):
in_memory_file = io.BytesIO()
zf = zipfile.ZipFile(in_memory_file, "w")
zf.writestr("path/16.txt", "This was utf-16".encode("utf-16"))
zf.filename = "test_open_utf16.zip"
root = zipfile.Path(zf)
path, = root.iterdir()
u16 = path.joinpath("16.txt")
with u16.open('r', "utf-16") as strm:
data = strm.read()
self.assertEqual(data, "This was utf-16")
with u16.open(encoding="utf-16") as strm:
data = strm.read()
self.assertEqual(data, "This was utf-16")

def test_read_text_encoding_errors(self):
in_memory_file = io.BytesIO()
zf = zipfile.ZipFile(in_memory_file, "w")
zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.")
zf.filename = "test_read_text_encoding_errors.zip"
root = zipfile.Path(zf)
path, = root.iterdir()
u16 = path.joinpath("bad-utf8.bin")
with self.assertRaises(UnicodeDecodeError):
u16.read_text(encoding="utf-8", errors="strict") # both keywords
data = u16.read_text("utf-8", errors="ignore") # errors keyword
self.assertEqual(data, "invalid utf-8: .")
# encoding= both positional and keyword is an error; gh-101144.
with self.assertRaisesRegex(TypeError, "encoding"):
data = u16.read_text("utf-8", encoding="utf-8")

def test_open_write(self):
"""
Expand Down Expand Up @@ -187,6 +221,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 positional arg TypeError per gh-101144.
assert a.read_bytes() == b"content of a"

@pass_alpharep
Expand Down
19 changes: 15 additions & 4 deletions Lib/zipfile/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,21 @@ 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)
# Text mode:
encoding = None
if args:
# Per io.TextIOWrapper, encoding is the first positional arg.
# Our API is to pass all *args and **kwargs to TextIOWrapper.
# Extract it so we can process it.
encoding = args[0]
args = args[1:]
# We must check this manually as we extract it for processing.
if "encoding" in kwargs:
raise TypeError(
"argument ('encoding') given by name and position (1)")
encoding = kwargs.pop("encoding", encoding)
encoding = io.text_encoding(encoding)
gpshead marked this conversation as resolved.
Show resolved Hide resolved
return io.TextIOWrapper(stream, encoding, *args, **kwargs)

@property
def name(self):
Expand All @@ -282,7 +294,6 @@ 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:
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 @@
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`.