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.
This commit is contained in:
Gregory P. Smith 2023-01-19 23:04:30 -08:00 committed by GitHub
parent 9e025d305f
commit 5927013e47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 10 deletions

View file

@ -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):