[Python-checkins] Add support for .parent and .joinpath in zipfile.Path (#13213)

Barry Warsaw webhook-mailer at python.org
Thu May 9 11:34:42 EDT 2019


https://github.com/python/cpython/commit/33e067d6a237ced8fd2ead70a461025fd91239be
commit: 33e067d6a237ced8fd2ead70a461025fd91239be
branch: master
author: Jason R. Coombs <jaraco at jaraco.com>
committer: Barry Warsaw <barry at python.org>
date: 2019年05月09日T11:34:35-04:00
summary:
Add support for .parent and .joinpath in zipfile.Path (#13213)
files:
M Lib/test/test_zipfile.py
M Lib/zipfile.py
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 538d4ee55dfb..bf5bb4d0f13e 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -2481,6 +2481,14 @@ def test_read(self):
 assert a.read_text() == "content of a"
 assert a.read_bytes() == b"content of a"
 
+ def test_joinpath(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ a = root.joinpath("a")
+ assert a.is_file()
+ e = root.joinpath("b").joinpath("d").joinpath("e.txt")
+ assert e.read_text() == "content of e"
+
 def test_traverse_truediv(self):
 for zipfile_abcde in self.zipfile_abcde():
 root = zipfile.Path(zipfile_abcde)
@@ -2502,5 +2510,11 @@ def test_traverse_pathlike(self):
 root = zipfile.Path(zipfile_abcde)
 root / pathlib.Path("a")
 
+ def test_parent(self):
+ for zipfile_abcde in self.zipfile_abcde():
+ root = zipfile.Path(zipfile_abcde)
+ assert (root / 'a').parent.at == ''
+ assert (root / 'a' / 'b').parent.at == 'a/'
+
 if __name__ == "__main__":
 unittest.main()
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 62475c701f50..8f8cb863b003 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -2218,12 +2218,14 @@ def __str__(self):
 def __repr__(self):
 return self.__repr.format(self=self)
 
- def __truediv__(self, add):
+ def joinpath(self, add):
 next = posixpath.join(self.at, add)
 next_dir = posixpath.join(self.at, add, "")
 names = self._names()
 return self._next(next_dir if next not in names and next_dir in names else next)
 
+ __truediv__ = joinpath
+
 @staticmethod
 def _add_implied_dirs(names):
 return names + [
@@ -2232,6 +2234,13 @@ def _add_implied_dirs(names):
 if name and name + "/" not in names
 ]
 
+ @property
+ def parent(self):
+ parent_at = posixpath.dirname(self.at)
+ if parent_at:
+ parent_at += '/'
+ return self._next(parent_at)
+
 def _names(self):
 return self._add_implied_dirs(self.root.namelist())
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /