summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiguel Brito <5544985+miguendes@users.noreply.github.com>2021-05-14 17:57:36 (GMT)
committerGitHub <noreply@github.com>2021-05-14 17:57:36 (GMT)
commitdc0b364de46cec104dae62282174b6c8a1cdb475 (patch)
tree8709a8ec9e07ef69dbea195e55d65d919966f781
parent2918846a4ff394237ccd7c6d5cdf5655e2ddd726 (diff)
downloadcpython-dc0b364de46cec104dae62282174b6c8a1cdb475.zip
cpython-dc0b364de46cec104dae62282174b6c8a1cdb475.tar.gz
cpython-dc0b364de46cec104dae62282174b6c8a1cdb475.tar.bz2
bpo-44095: Add suffix, stem and suffixes to zipfile.Path (GH-26129)
-rw-r--r--Doc/library/zipfile.rst21
-rw-r--r--Lib/test/test_zipfile.py58
-rw-r--r--Lib/zipfile.py12
-rw-r--r--Misc/NEWS.d/next/Library/2021-05-14-16-06-02.bpo-44095.v_pLwY.rst2
4 files changed, 93 insertions, 0 deletions
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst
index 3db55e6..42fe27b 100644
--- a/Doc/library/zipfile.rst
+++ b/Doc/library/zipfile.rst
@@ -521,6 +521,27 @@ Path objects are traversable using the ``/`` operator or ``joinpath``.
Return ``True`` if the current context references a file or
directory in the zip file.
+.. data:: Path.suffix
+
+ The file extension of the final component.
+
+ .. versionadded:: 3.11
+ Added :data:`Path.suffix` property.
+
+.. data:: Path.stem
+
+ The final path component, without its suffix.
+
+ .. versionadded:: 3.11
+ Added :data:`Path.stem` property.
+
+.. data:: Path.suffixes
+
+ A list of the path’s file extensions.
+
+ .. versionadded:: 3.11
+ Added :data:`Path.suffixes` property.
+
.. method:: Path.read_text(*, **)
Read the current file as unicode text. Positional and
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index bfc981c..f559be7 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -3094,6 +3094,64 @@ class TestPath(unittest.TestCase):
assert root.name == 'alpharep.zip' == root.filename.name
@pass_alpharep
+ def test_suffix(self, alpharep):
+ """
+ The suffix of the root should be the suffix of the zipfile.
+ The suffix of each nested file is the final component's last suffix, if any.
+ Includes the leading period, just like pathlib.Path.
+ """
+ root = zipfile.Path(alpharep)
+ assert root.suffix == '.zip' == root.filename.suffix
+
+ b = root / "b.txt"
+ assert b.suffix == ".txt"
+
+ c = root / "c" / "filename.tar.gz"
+ assert c.suffix == ".gz"
+
+ d = root / "d"
+ assert d.suffix == ""
+
+ @pass_alpharep
+ def test_suffixes(self, alpharep):
+ """
+ The suffix of the root should be the suffix of the zipfile.
+ The suffix of each nested file is the final component's last suffix, if any.
+ Includes the leading period, just like pathlib.Path.
+ """
+ root = zipfile.Path(alpharep)
+ assert root.suffixes == ['.zip'] == root.filename.suffixes
+
+ b = root / 'b.txt'
+ assert b.suffixes == ['.txt']
+
+ c = root / 'c' / 'filename.tar.gz'
+ assert c.suffixes == ['.tar', '.gz']
+
+ d = root / 'd'
+ assert d.suffixes == []
+
+ e = root / '.hgrc'
+ assert e.suffixes == []
+
+ @pass_alpharep
+ def test_stem(self, alpharep):
+ """
+ The final path component, without its suffix
+ """
+ root = zipfile.Path(alpharep)
+ assert root.stem == 'alpharep' == root.filename.stem
+
+ b = root / "b.txt"
+ assert b.stem == "b"
+
+ c = root / "c" / "filename.tar.gz"
+ assert c.stem == "filename.tar"
+
+ d = root / "d"
+ assert d.stem == "d"
+
+ @pass_alpharep
def test_root_parent(self, alpharep):
root = zipfile.Path(alpharep)
assert root.parent == pathlib.Path('.')
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index d99c0d7..b83e2c1 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -2343,6 +2343,18 @@ class Path:
return pathlib.Path(self.at).name or self.filename.name
@property
+ def suffix(self):
+ return pathlib.Path(self.at).suffix or self.filename.suffix
+
+ @property
+ def suffixes(self):
+ return pathlib.Path(self.at).suffixes or self.filename.suffixes
+
+ @property
+ def stem(self):
+ return pathlib.Path(self.at).stem or self.filename.stem
+
+ @property
def filename(self):
return pathlib.Path(self.root.filename).joinpath(self.at)
diff --git a/Misc/NEWS.d/next/Library/2021-05-14-16-06-02.bpo-44095.v_pLwY.rst b/Misc/NEWS.d/next/Library/2021-05-14-16-06-02.bpo-44095.v_pLwY.rst
new file mode 100644
index 0000000..ee03e93
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-05-14-16-06-02.bpo-44095.v_pLwY.rst
@@ -0,0 +1,2 @@
+:class:`zipfile.Path` now supports :attr:`zipfile.Path.stem`,
+:attr:`zipfile.Path.suffixes`, and :attr:`zipfile.Path.suffix` attributes.