From 647bb667a7dda52b7e23f95a8b91b93d2f2f5a5a Mon Sep 17 00:00:00 2001 From: Yngve Mardal Moe Date: Thu, 20 Feb 2025 21:13:14 +0000 Subject: [PATCH 1/3] Prevent __str__ and __repr__ from raising errors when zipfile.Path.root.filename is None --- Lib/test/test_zipfile/_path/test_path.py | 8 +++++++- Lib/zipfile/_path/__init__.py | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zipfile/_path/test_path.py b/Lib/test/test_zipfile/_path/test_path.py index 1ee45f5fc57104..01721f802b3186 100644 --- a/Lib/test/test_zipfile/_path/test_path.py +++ b/Lib/test/test_zipfile/_path/test_path.py @@ -454,9 +454,15 @@ def test_root_unnamed(self, alpharep): # .name and .parent should still work on subs sub = root / "b" - assert sub.name == "b" + self.assertEqual(sub.name, "b") assert sub.parent + # It should be possible to get the repr and str + self.assertEqual(str(root), ":memory:/") + self.assertEqual(repr(root), "Path(None, '')") + self.assertEqual(str(sub), ":memory:/b/") + self.assertEqual(repr(sub), "Path(None, 'b/')") + @pass_alpharep def test_match_and_glob(self, alpharep): root = zipfile.Path(alpharep) diff --git a/Lib/zipfile/_path/__init__.py b/Lib/zipfile/_path/__init__.py index 5ae16ec970dda4..f6164432de9503 100644 --- a/Lib/zipfile/_path/__init__.py +++ b/Lib/zipfile/_path/__init__.py @@ -430,7 +430,9 @@ def relative_to(self, other, *extra): return posixpath.relpath(str(self), str(other.joinpath(*extra))) def __str__(self): - return posixpath.join(self.root.filename, self.at) + if (filename := self.root.filename) is None: + filename = ":memory:" + return posixpath.join(filename, self.at) def __repr__(self): return self.__repr.format(self=self) From 09b0db5d8942c32fd6795cce7f66c770422c4495 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:30:43 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst diff --git a/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst b/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst new file mode 100644 index 00000000000000..46eb498048f813 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst @@ -0,0 +1 @@ +Fix bug where `str(zipfile.Path(zipfile.ZipFile(io.BytesIO(data))))` would raise a `TypeError` From 3918b910e0c71fb8fef03ad79cb22416a7b40426 Mon Sep 17 00:00:00 2001 From: Yngve Mardal Moe Date: Thu, 20 Feb 2025 21:34:49 +0000 Subject: [PATCH 3/3] Fix blurb --- .../next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst b/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst index 46eb498048f813..dfc573857498ca 100644 --- a/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst +++ b/Misc/NEWS.d/next/Library/2025-02-20-21-30-41.gh-issue-130120.-O2xJr.rst @@ -1 +1 @@ -Fix bug where `str(zipfile.Path(zipfile.ZipFile(io.BytesIO(data))))` would raise a `TypeError` +Fix bug where ``str(zipfile.Path(zipfile.ZipFile(io.BytesIO(data))))`` would raise a ``TypeError``