Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ReadableZipPath: handle leading slashes slightly more robustly
  • Loading branch information
barneygale committed Feb 27, 2025
commit 0601addd366e09c67c1ce3105dfb3ed47b00bb9c
18 changes: 14 additions & 4 deletions Lib/test/test_pathlib/support/zip_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ class ZipFileList:
`resolve()` method fetches an entry from the tree.
"""

__slots__ = ('_root_info', '_items')
__slots__ = ('_roots', '_items')

def __init__(self, items):
self._root_info = ZipPathInfo()
self._roots = {
'': ZipPathInfo(),
'/': ZipPathInfo(),
'//': ZipPathInfo(),
}
self._items = []
for item in items:
self.append(item)
Expand All @@ -80,7 +84,8 @@ def resolve(self, path, create=False):
"""
Returns a PathInfo object for the given path by walking the tree.
"""
path_info = self._root_info
_drive, root, path = posixpath.splitroot(path)
path_info = self._roots[root]
for name in path.split('/'):
if not name or name == '.':
pass
Expand Down Expand Up @@ -127,7 +132,12 @@ def with_segments(self, *pathsegments):
return type(self)(*pathsegments, zip_file=self.zip_file)

def __open_rb__(self, buffering=-1):
return self.zip_file.open(str(self), 'r')
info = self.info
if not info.exists():
raise FileNotFoundError(errno.ENOENT, "File not found", self)
elif info.is_dir():
raise IsADirectoryError(errno.EISDIR, "Is a directory", self)
return self.zip_file.open(info.zip_info, 'r')

def iterdir(self):
info = self.info
Expand Down