diff options
author | Gregory P. Smith <greg@krypto.org> | 2023-01-20 07:04:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-20 07:04:30 (GMT) |
commit | 5927013e47a8c63b70e104152351f3447baa819c (patch) | |
tree | 277eabd54cc4d1755178ff3ba3a8ea779d25528a /Lib/zipfile | |
parent | 9e025d305f159aebf01775ad1dc2817679f01aa9 (diff) | |
download | cpython-5927013e47a8c63b70e104152351f3447baa819c.zip cpython-5927013e47a8c63b70e104152351f3447baa819c.tar.gz cpython-5927013e47a8c63b70e104152351f3447baa819c.tar.bz2 |
gh-101144: Allow open and read_text encoding to be positional. (#101145)
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.
Diffstat (limited to 'Lib/zipfile')
-rw-r--r-- | Lib/zipfile/_path.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/zipfile/_path.py b/Lib/zipfile/_path.py index aea17b6..7c7a6a0 100644 --- a/Lib/zipfile/_path.py +++ b/Lib/zipfile/_path.py @@ -148,6 +148,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. @@ -257,9 +262,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): @@ -282,8 +287,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): |