Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement heuristic to get non-ASCII ZIP entries #8684

Merged
merged 3 commits into from
Aug 4, 2020
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
2 changes: 2 additions & 0 deletions news/8684.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use UTF-8 to handle ZIP archive entries on Python 2 according to PEP 427, so
non-ASCII paths can be resolved as expected.
15 changes: 13 additions & 2 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
Union,
cast,
)
from zipfile import ZipInfo

from pip._vendor.pkg_resources import Distribution

Expand Down Expand Up @@ -420,6 +421,15 @@ def __init__(self, src_record_path, dest_path, zip_file):
self._zip_file = zip_file
self.changed = False

def _getinfo(self):
# type: () -> ZipInfo
if not PY2:
return self._zip_file.getinfo(self.src_record_path)
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
# Python 2 does not expose a way to detect a ZIP's encoding, but the
# wheel specification (PEP 427) explicitly mandates that paths should
# use UTF-8, so we assume it is true.
return self._zip_file.getinfo(self.src_record_path.encode("utf-8"))

def save(self):
# type: () -> None
# directory creation is lazy and after file filtering
Expand All @@ -439,11 +449,12 @@ def save(self):
if os.path.exists(self.dest_path):
os.unlink(self.dest_path)

with self._zip_file.open(self.src_record_path) as f:
zipinfo = self._getinfo()

with self._zip_file.open(zipinfo) as f:
with open(self.dest_path, "wb") as dest:
shutil.copyfileobj(f, dest)

zipinfo = self._zip_file.getinfo(self.src_record_path)
if zip_item_is_executable(zipinfo):
set_extracted_file_to_default_mode_plus_executable(self.dest_path)

Expand Down