From c482e560d15f50fa2bef95ea6da7fb720dcfeb1c Mon Sep 17 00:00:00 2001 From: Miguel Mendes Date: Fri, 14 May 2021 13:00:47 +0100 Subject: [PATCH 1/4] Add suffix --- Lib/test/test_zipfile.py | 19 +++++++++++++++++++ Lib/zipfile.py | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index bfc981c0d15d37..222d835d94c1e3 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -3093,6 +3093,25 @@ def test_root_name(self, alpharep): root = zipfile.Path(alpharep) 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_root_parent(self, alpharep): root = zipfile.Path(alpharep) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index d99c0d76977750..0031f077ad587d 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -2342,6 +2342,10 @@ def open(self, mode='r', *args, pwd=None, **kwargs): def name(self): 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 filename(self): return pathlib.Path(self.root.filename).joinpath(self.at) From e25059865f40dee6bb968ffd8331b942415f810a Mon Sep 17 00:00:00 2001 From: Miguel Mendes Date: Fri, 14 May 2021 13:04:30 +0100 Subject: [PATCH 2/4] Add stem --- Lib/test/test_zipfile.py | 17 +++++++++++++++++ Lib/zipfile.py | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 222d835d94c1e3..5238f4fea6756d 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -3112,6 +3112,23 @@ def test_suffix(self, alpharep): d = root / "d" assert d.suffix == "" + @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) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 0031f077ad587d..b66c9ecf52ae33 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -2346,6 +2346,10 @@ def name(self): def suffix(self): return pathlib.Path(self.at).suffix or self.filename.suffix + @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) From 65ded7cf50bd3550a458393aa5602e0c7721ccdc Mon Sep 17 00:00:00 2001 From: Miguel Mendes Date: Fri, 14 May 2021 13:08:29 +0100 Subject: [PATCH 3/4] Add suffixes --- Lib/test/test_zipfile.py | 22 ++++++++++++++++++++++ Lib/zipfile.py | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 5238f4fea6756d..f559be790bb38e 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -3112,6 +3112,28 @@ def test_suffix(self, alpharep): 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): """ diff --git a/Lib/zipfile.py b/Lib/zipfile.py index b66c9ecf52ae33..b83e2c187eb033 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -2346,6 +2346,10 @@ def name(self): 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 From 1190276fad80a0e058a956a5a7ef114cc1ea32be Mon Sep 17 00:00:00 2001 From: Miguel Mendes Date: Fri, 14 May 2021 16:43:11 +0100 Subject: [PATCH 4/4] Add docs and news --- Doc/library/zipfile.rst | 21 +++++++++++++++++++ .../2021-05-14-16-06-02.bpo-44095.v_pLwY.rst | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-05-14-16-06-02.bpo-44095.v_pLwY.rst diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index 3db55e646c47cc..42fe27b9e1e47f 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/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 00000000000000..ee03e933f35d63 --- /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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy