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) 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..dfc573857498ca --- /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``