summaryrefslogtreecommitdiffstats
path: root/Lib/zipfile.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-01-20 08:31:15 (GMT)
committerGitHub <noreply@github.com>2023-01-20 08:31:15 (GMT)
commitb2a662fc6b391f176bbf9da45657d21ef5b363ac (patch)
treebceca26823f948445aa0e900b1971a371d3cea0a /Lib/zipfile.py
parent10c61301240374701279aa8f87bf9f1caf1e9c9a (diff)
downloadcpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.zip
cpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.tar.gz
cpython-b2a662fc6b391f176bbf9da45657d21ef5b363ac.tar.bz2
[3.10] gh-101144: Allow zipfile.Path .open & .read_text encoding to be positional (GH-101179) (GH-101182)
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 5927013e47a8c63b70e104152351f3447baa819c) (cherry picked from commit efe3a389cabd7295e6e0938767cdc4055c871e3c) Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google] Automerge-Triggered-By: GH:gpshead
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r--Lib/zipfile.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index eee1f47..dc4eb38 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -2236,6 +2236,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.
@@ -2345,9 +2350,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):
@@ -2358,8 +2363,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):