mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.11] gh-101144: Allow zipfile.Path .open & .read_text encoding to be positional (#101179)
The zipfile.Path open() and read_text() encoding parameter can be supplied as a positional argument without causing a TypeError again. 3.10.0b1 included a regression that made it keyword only.
Documentation update included as users writing code to be compatible with a wide range of versions will need to consider this for some time..
(cherry picked from commit 5927013e47
)
Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google]
This commit is contained in:
parent
1998ea69c7
commit
efe3a389ca
4 changed files with 91 additions and 6 deletions
|
@ -2287,6 +2287,11 @@ class FastLookup(CompleteDirs):
|
|||
return self.__lookup
|
||||
|
||||
|
||||
def _extract_text_encoding(encoding=None, *args, **kwargs):
|
||||
# stacklevel=3 so that the caller of the caller see any warning.
|
||||
return io.text_encoding(encoding, 3), args, kwargs
|
||||
|
||||
|
||||
class Path:
|
||||
"""
|
||||
A pathlib-compatible interface for zip files.
|
||||
|
@ -2396,9 +2401,9 @@ class Path:
|
|||
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, args, kwargs = _extract_text_encoding(*args, **kwargs)
|
||||
return io.TextIOWrapper(stream, encoding, *args, **kwargs)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -2421,8 +2426,8 @@ class Path:
|
|||
return pathlib.Path(self.root.filename).joinpath(self.at)
|
||||
|
||||
def read_text(self, *args, **kwargs):
|
||||
kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))
|
||||
with self.open('r', *args, **kwargs) as strm:
|
||||
encoding, args, kwargs = _extract_text_encoding(*args, **kwargs)
|
||||
with self.open('r', encoding, *args, **kwargs) as strm:
|
||||
return strm.read()
|
||||
|
||||
def read_bytes(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue