Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
a4f19a4
gh-101144: Allow open and read_text encoding to be positional.
gpsheadJan 19, 2023
b495172
news wording tweak
gpsheadJan 19, 2023
e304094
Explicitly handle encoding as an arg.
gpsheadJan 19, 2023
9caec6c
wording tweak in news.
gpsheadJan 19, 2023
b4248b1
Explicitly test an unusual encoding to verify it passed through.
gpsheadJan 19, 2023
32b252b
simplify read_text, just defer encoding to open
gpsheadJan 19, 2023
d1228b5
address review comments: fixup pos+kw logic, test it.
gpsheadJan 19, 2023
8ee213e
Apply jaraco's suggestion: an encoding extraction method.
gpsheadJan 19, 2023
25dd5bd
Test EncodingWarning blame of open & read_text.
gpsheadJan 20, 2023
e9b373f
Surface the behavior in the zipfile.Path docs.
gpsheadJan 20, 2023
a12243b
simplify the doc wording.
gpsheadJan 20, 2023
35abedb
more doc wording updates based on review.
gpsheadJan 20, 2023
88ecd56
use () on single element tuple assignment for clarity.
gpsheadJan 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-101144: Allow open and read_text encoding to be positional.
As was the behavior in 3.9 and earlier.  The fix for#87817 introduced an APIregression in 3.10.0b1.
  • Loading branch information
@gpshead
gpshead committedJan 19, 2023
commita4f19a42e9a4ae3f44661ca831d2be183adb9cb3
6 changes: 5 additions & 1 deletionLib/test/test_zipfile/test_path.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsLib/zipfile/_path.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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"])
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"))
if "encoding" in kwargs:
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
with self.open('r', *args, **kwargs) as strm:
return strm.read()

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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 an unintended regression where supplying it as a
positional argument would lead to a :exc:`TypeError`.

[8]ページ先頭

©2009-2025 Movatter.jp