Skip to content

Commit 93fd719

Browse files
committed
Restore API compatibility while backporting performance improvements.
1 parent 8f997b5 commit 93fd719

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

Doc/library/zipfile.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -489,20 +489,16 @@ Path objects are traversable using the ``/`` operator.
489489

490490
The final path component.
491491

492-
.. method:: Path.open(mode='r', *, pwd, **)
493-
494-
Invoke :meth:`ZipFile.open` on the current path.
495-
Allows opening for read or write, text or binary
496-
through supported modes: 'r', 'w', 'rb', 'wb'.
497-
Positional and keyword arguments are passed through to
498-
:class:`io.TextIOWrapper` when opened as text and
499-
ignored otherwise.
500-
``pwd`` is the ``pwd`` parameter to
501-
:meth:`ZipFile.open`.
502-
503-
.. versionchanged:: 3.8.3
504-
Added support for text and binary modes for open. Default
505-
mode is now text.
492+
.. method:: Path.open(*, **)
493+
494+
Invoke :meth:`ZipFile.open` on the current path. Accepts
495+
the same arguments as :meth:`ZipFile.open`.
496+
497+
.. caution::
498+
499+
The signature on this function changes in an incompatible way
500+
in Python 3.9. For a future-compatible version, consider using
501+
the third-party zipp.Path package (3.0 or later).
506502

507503
.. method:: Path.iterdir()
508504

Lib/test/test_zipfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2840,7 +2840,7 @@ def test_open(self):
28402840
a, b, g = root.iterdir()
28412841
with a.open() as strm:
28422842
data = strm.read()
2843-
assert data == "content of a"
2843+
assert data == b"content of a"
28442844

28452845
def test_read(self):
28462846
for alpharep in self.zipfile_alpharep():

Lib/zipfile.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,31 +2316,20 @@ def __init__(self, root, at=""):
23162316
self.root = FastLookup.make(root)
23172317
self.at = at
23182318

2319-
def open(self, mode='r', *args, **kwargs):
2320-
"""
2321-
Open this entry as text or binary following the semantics
2322-
of ``pathlib.Path.open()`` by passing arguments through
2323-
to io.TextIOWrapper().
2324-
"""
2325-
pwd = kwargs.pop('pwd', None)
2326-
zip_mode = mode[0]
2327-
stream = self.root.open(self.at, zip_mode, pwd=pwd)
2328-
if 'b' in mode:
2329-
if args or kwargs:
2330-
raise ValueError("encoding args invalid for binary operation")
2331-
return stream
2332-
return io.TextIOWrapper(stream, *args, **kwargs)
2319+
@property
2320+
def open(self):
2321+
return functools.partial(self.root.open, self.at)
23332322

23342323
@property
23352324
def name(self):
23362325
return posixpath.basename(self.at.rstrip("/"))
23372326

23382327
def read_text(self, *args, **kwargs):
2339-
with self.open('r', *args, **kwargs) as strm:
2340-
return strm.read()
2328+
with self.open() as strm:
2329+
return io.TextIOWrapper(strm, *args, **kwargs).read()
23412330

23422331
def read_bytes(self):
2343-
with self.open('rb') as strm:
2332+
with self.open() as strm:
23442333
return strm.read()
23452334

23462335
def _is_child(self, path):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Improve pathlib.Path compatibility on zipfile.Path and correct performance degradation as found in zipp 3.0. This backward-incompatible change alters the default behavior for ``zipfile.Path.open`` to match the open behavior on ``pathlib.Path``. For compatibility across Python 3.8.0-3.8.2 and later versions, consider using ``zipp.Path`` on Python 3.8.2 and earlier. For a quick compatibility workaround, ``zipfile.Path.root.open`` provides the same interface as ``zipfile.Path.open`` on Python 3.8.2.
1+
Correct performance degradation in ``zipfile.Path`` as found in zipp 3.0. While retaining compatibility, this change discourages the use of ``zipfile.Path.open`` due to the signature change in Python 3.9. For compatibility across Python 3.8 and later versions, consider using ``zipp.Path`` on Python 3.8.x and earlier.

0 commit comments

Comments
 (0)