Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_zipfile/_path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ class Subclass(zipfile.Path):
root = Subclass(alpharep)
assert isinstance(root / 'b', Subclass)

def test_at(self):
zip_file = self.fixtures.enter_context(zipfile.ZipFile(io.BytesIO(), 'w'))
root = zipfile.Path(zip_file)
assert root.at == ''
assert (root / 'foo' / 'bar').at == 'foo/bar'

def test_str(self):
"""
An unnamed zip file is indicated by ":zipfile:"
"""
zip_file = self.fixtures.enter_context(zipfile.ZipFile(io.BytesIO(), 'w'))
root = zipfile.Path(zip_file)
assert str(root) == ':zipfile:'
assert str(root / 'foo' / 'bar') == ':zipfile:/foo/bar'

@pass_alpharep
def test_filename(self, alpharep):
root = zipfile.Path(alpharep)
Expand Down
7 changes: 5 additions & 2 deletions Lib/zipfile/_path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Path:
resolve to the zipfile.

>>> str(path)
'mem/abcde.zip/'
'mem/abcde.zip'
>>> path.name
'abcde.zip'
>>> path.filename == pathlib.Path('mem/abcde.zip')
Expand Down Expand Up @@ -431,7 +431,10 @@ 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)
root = self.root.filename
if root is None:
root = ':zipfile:'
return posixpath.join(root, self.at) if self.at else root

def __repr__(self):
return self.__repr.format(self=self)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``str()`` of a :class:`zipfile.Path` whose underlying zipfile has no
filename, such as one constructed from an :class:`io.BytesIO`, now renders
that filename as ``:zipfile:`` instead of raising :exc:`TypeError`. The root
of a zipfile is also no longer rendered with a trailing slash. Synchronized
with :pypi:`zipp` 3.23.1.
Loading