8000 Explicitly handle encoding as an arg. · gpshead/cpython@e304094 · GitHub
[go: up one dir, main page]

Skip to content

Commit e304094

Browse files
committed
Explicitly handle encoding as an arg.
This preserves the intent of python#87817 while restoring the ability to pass `encoding` as a positional argument.
1 parent b495172 commit e304094

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Lib/zipfile/_path.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def __init__(self, root, at=""):
241241
self.root = FastLookup.make(root)
242242
self.at = at
243243

244-
def open(self, mode='r', *args, pwd=None, **kwargs):
244+
def open(self, mode='r', encoding=None, *args, pwd=None, **kwargs):
245245
"""
246246
Open this entry as text or binary following the semantics
247247
of ``pathlib.Path.open()`` by passing arguments through
@@ -254,13 +254,12 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
254254
raise FileNotFoundError(self)
255255
stream = self.root.open(self.at, zip_mode, pwd=pwd)
256256
if 'b' in mode:
257-
if args or kwargs:
257+
if encoding is not None or args or kwargs:
258258
raise ValueError("encoding args invalid for binary operation")
259259
return stream
260260
else:
261-
if "encoding" in kwargs:
262-
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
263-
return io.TextIOWrapper(stream, *args, **kwargs)
261+
encoding = io.text_encoding(encoding)
262+
return io.TextIOWrapper(stream, *args, encoding=encoding, **kwargs)
264263

265264
@property
266265
def name(self):
@@ -282,10 +281,9 @@ def stem(self):
282281
def filename(self):
283282
return pathlib.Path(self.root.filename).joinpath(self.at)
284283

285-
def read_text(self, *args, **kwargs):
286-
if "encoding" in kwargs:
287-
kwargs["encoding"] = io.text_encoding(kwargs["encoding"])
288-
with self.open('r', *args, **kwargs) as strm:
284+
def read_text(self, encoding=None, *args, **kwargs):
285+
encoding = io.text_encoding(encoding)
286+
with self.open('r', *args, encoding=encoding, **kwargs) as strm:
289287
return strm.read()
290288

291289
def read_bytes(self):

0 commit comments

Comments
 (0)
0