Skip to content

Commit a4f19a4

Browse files
committed
pythongh-101144: Allow open and read_text encoding to be positional.
As was the behavior in 3.9 and earlier. The fix for python#87817 introduced an API regression in 3.10.0b1.
1 parent a1e051a commit a4f19a4

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Lib/test/test_zipfile/test_path.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ def test_open(self, alpharep):
145145
a, b, g = root.iterdir()
146146
with a.open(encoding="utf-8") as strm:
147147
data = strm.read()
148-
assert data == "content of a"
148+
self.assertEqual(data, "content of a")
149+
with a.open('r', "utf-8") as strm: # No gh-101144 TypeError
150+
data = strm.read()
151+
self.assertEqual(data, "content of a")
149152

150153
def test_open_write(self):
151154
"""
@@ -187,6 +190,7 @@ def test_read(self, alpharep):
187190
root = zipfile.Path(alpharep)
188191
a, b, g = root.iterdir()
189192
assert a.read_text(encoding="utf-8") == "content of a"
193+
a.read_text("utf-8") # No TypeError per gh-101144.
190194
assert a.read_bytes() == b"content of a"
191195

192196
@pass_alpharep

Lib/zipfile/_path.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
258258
raise ValueError("encoding args invalid for binary operation")
259259
return stream
260260
else:
261-
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
261+
if "encoding" in kwargs:
262+
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
262263
return io.TextIOWrapper(stream, *args, **kwargs)
263264

264265
@property
@@ -282,7 +283,8 @@ def filename(self):
282283
return pathlib.Path(self.root.filename).joinpath(self.at)
283284

284285
def read_text(self, *args, **kwargs):
285-
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
286+
if "encoding" in kwargs:
287+
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
286288
with self.open('r', *args, **kwargs) as strm:
287289
return strm.read()
288290

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`zipfile.Path.open` and :func:`zipfile.Path.read_text` accept
2+
``encoding`` as a positional argument. This was the behavior in Python 3.9 and
3+
earlier. 3.10 introduced an unintended regression where supplying it as a
4+
positional argument would lead to a :exc:`TypeError`.

0 commit comments

Comments
 (0)