From 41a6e3c82a61051244c12b6cc4eb2df977b218c4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 25 Oct 2020 14:48:18 -0400 Subject: [PATCH 1/3] bpo-42090: Allow zipfile.Path.joinpath to accept arbitrary arguments, matching pathlib.Path. --- Lib/test/test_zipfile.py | 6 ++++++ Lib/zipfile.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index b3c24213f3474d..7c09e2f51b005a 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -2965,6 +2965,12 @@ def test_joinpath(self, alpharep): e = root.joinpath("b").joinpath("d").joinpath("e.txt") assert e.read_text() == "content of e" + @pass_alpharep + def test_joinpath_multiple(self, alpharep): + root = zipfile.Path(alpharep) + e = root.joinpath("b", "d", "e.txt") + assert e.read_text() == "content of e" + @pass_alpharep def test_traverse_truediv(self, alpharep): root = zipfile.Path(alpharep) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index e1a50a3eb51d95..0eed4ce9a63441 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -2379,8 +2379,8 @@ def __str__(self): def __repr__(self): return self.__repr.format(self=self) - def joinpath(self, add): - next = posixpath.join(self.at, add) + def joinpath(self, *other): + next = posixpath.join(self.at, *other) return self._next(self.root.resolve_dir(next)) __truediv__ = joinpath From 5eba02fc1c85f56fa8047f0b37a5c777c1f920b9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 25 Oct 2020 14:49:02 -0400 Subject: [PATCH 2/3] Update changelog. --- .../next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst b/Misc/NEWS.d/next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst new file mode 100644 index 00000000000000..72f6853b505044 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst @@ -0,0 +1,2 @@ +``zipfile.Path.joinpath`` now accepts arbitrary arguments, same as +``pathlib.Path.joinpath``. From 2804512c31a62ccbad1a9be9cf809eea4bf18569 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 25 Oct 2020 19:01:55 -0400 Subject: [PATCH 3/3] Update documentation to include joinpath. --- Doc/library/zipfile.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index 7126d8bd703f62..3db55e646c47cc 100644 --- a/Doc/library/zipfile.rst +++ b/Doc/library/zipfile.rst @@ -483,7 +483,7 @@ Path Objects Path objects expose the following features of :mod:`pathlib.Path` objects: -Path objects are traversable using the ``/`` operator. +Path objects are traversable using the ``/`` operator or ``joinpath``. .. attribute:: Path.name @@ -532,6 +532,19 @@ Path objects are traversable using the ``/`` operator. Read the current file as bytes. +.. method:: Path.joinpath(*other) + + Return a new Path object with each of the *other* arguments + joined. The following are equivalent:: + + >>> Path(...).joinpath('child').joinpath('grandchild') + >>> Path(...).joinpath('child', 'grandchild') + >>> Path(...) / 'child' / 'grandchild' + + .. versionchanged:: 3.10 + Prior to 3.10, ``joinpath`` was undocumented and accepted + exactly one parameter. + .. _pyzipfile-objects: