From 928dbfc16c9c4715459c80fe551c74702480db8b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 15 Dec 2020 21:12:54 -0500 Subject: [PATCH] bpo-42090: zipfile.Path.joinpath now accepts multiple arguments (GH-22976) Automerge-Triggered-By: GH:jaraco --- Doc/library/zipfile.rst | 15 ++++++++++++++- Lib/test/test_zipfile.py | 6 ++++++ Lib/zipfile.py | 4 ++-- .../2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst | 2 ++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-10-25-14-48-57.bpo-42090.Ubuc0j.rst diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index 7126d8bd703..3db55e646c4 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: diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index b3c24213f34..7c09e2f51b0 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -2965,6 +2965,12 @@ class TestPath(unittest.TestCase): 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 e1a50a3eb51..0eed4ce9a63 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -2379,8 +2379,8 @@ class Path: 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 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 00000000000..72f6853b505 --- /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``.