mirror of
https://github.com/python/cpython.git
synced 2025-08-08 02:48:55 +00:00
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:
parent
9e025d305f
commit
5927013e47
4 changed files with 95 additions and 10 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue