Skip to content

Commit 0601add

Browse files
committed
ReadableZipPath: handle leading slashes slightly more robustly
1 parent f7afbec commit 0601add

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Lib/test/test_pathlib/support/zip_path.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ class ZipFileList:
5858
`resolve()` method fetches an entry from the tree.
5959
"""
6060

61-
__slots__ = ('_root_info', '_items')
61+
__slots__ = ('_roots', '_items')
6262

6363
def __init__(self, items):
64-
self._root_info = ZipPathInfo()
64+
self._roots = {
65+
'': ZipPathInfo(),
66+
'/': ZipPathInfo(),
67+
'//': ZipPathInfo(),
68+
}
6569
self._items = []
6670
for item in items:
6771
self.append(item)
@@ -80,7 +84,8 @@ def resolve(self, path, create=False):
8084
"""
8185
Returns a PathInfo object for the given path by walking the tree.
8286
"""
83-
path_info = self._root_info
87+
_drive, root, path = posixpath.splitroot(path)
88+
path_info = self._roots[root]
8489
for name in path.split('/'):
8590
if not name or name == '.':
8691
pass
@@ -127,7 +132,12 @@ def with_segments(self, *pathsegments):
127132
return type(self)(*pathsegments, zip_file=self.zip_file)
128133

129134
def __open_rb__(self, buffering=-1):
130-
return self.zip_file.open(str(self), 'r')
135+
info = self.info
136+
if not info.exists():
137+
raise FileNotFoundError(errno.ENOENT, "File not found", self)
138+
elif info.is_dir():
139+
raise IsADirectoryError(errno.EISDIR, "Is a directory", self)
140+
return self.zip_file.open(info.zip_info, 'r')
131141

132142
def iterdir(self):
133143
info = self.info

0 commit comments

Comments
 (0)