summaryrefslogtreecommitdiffstats
path: root/Lib/zipfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r--Lib/zipfile.py69
1 files changed, 8 insertions, 61 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index cbac8d9..9b66a9f 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -2152,7 +2152,7 @@ def _parents(path):
def _ancestry(path):
"""
Given a path with elements separated by
- posixpath.sep, generate all elements of that path
+ posixpath.sep, generate all elements of that path.
>>> list(_ancestry('b/d'))
['b/d', 'b']
@@ -2164,9 +2164,14 @@ def _ancestry(path):
['b']
>>> list(_ancestry(''))
[]
+
+ Multiple separators are treated like a single.
+
+ >>> list(_ancestry('//b//d///f//'))
+ ['//b//d///f', '//b//d', '//b']
"""
path = path.rstrip(posixpath.sep)
- while path and path != posixpath.sep:
+ while path.rstrip(posixpath.sep):
yield path
path, tail = posixpath.split(path)
@@ -2183,65 +2188,7 @@ def _difference(minuend, subtrahend):
return itertools.filterfalse(set(subtrahend).__contains__, minuend)
-class SanitizedNames:
- """
- ZipFile mix-in to ensure names are sanitized.
- """
-
- def namelist(self):
- return list(map(self._sanitize, super().namelist()))
-
- @staticmethod
- def _sanitize(name):
- r"""
- Ensure a relative path with posix separators and no dot names.
- Modeled after
- https://github.com/python/cpython/blob/bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c/Lib/zipfile/__init__.py#L1799-L1813
- but provides consistent cross-platform behavior.
- >>> san = SanitizedNames._sanitize
- >>> san('/foo/bar')
- 'foo/bar'
- >>> san('//foo.txt')
- 'foo.txt'
- >>> san('foo/.././bar.txt')
- 'foo/bar.txt'
- >>> san('foo../.bar.txt')
- 'foo../.bar.txt'
- >>> san('\\foo\\bar.txt')
- 'foo/bar.txt'
- >>> san('D:\\foo.txt')
- 'D/foo.txt'
- >>> san('\\\\server\\share\\file.txt')
- 'server/share/file.txt'
- >>> san('\\\\?\\GLOBALROOT\\Volume3')
- '?/GLOBALROOT/Volume3'
- >>> san('\\\\.\\PhysicalDrive1\\root')
- 'PhysicalDrive1/root'
- Retain any trailing slash.
- >>> san('abc/')
- 'abc/'
- Raises a ValueError if the result is empty.
- >>> san('../..')
- Traceback (most recent call last):
- ...
- ValueError: Empty filename
- """
-
- def allowed(part):
- return part and part not in {'..', '.'}
-
- # Remove the drive letter.
- # Don't use ntpath.splitdrive, because that also strips UNC paths
- bare = re.sub('^([A-Z]):', r'\1', name, flags=re.IGNORECASE)
- clean = bare.replace('\\', '/')
- parts = clean.split('/')
- joined = '/'.join(filter(allowed, parts))
- if not joined:
- raise ValueError("Empty filename")
- return joined + '/' * name.endswith('/')
-
-
-class CompleteDirs(SanitizedNames, ZipFile):
+class CompleteDirs(ZipFile):
"""
A ZipFile subclass that ensures that implied directories
are always included in the namelist.