Skip to content

Commit 03c2ea0

Browse files
committed
Improve BadZipFile reporting
1 parent b160e83 commit 03c2ea0

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

news/10535.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Catch ``BadZipFile`` errors and re-raise them as ``InvalidWheel`` which
2+
additionally reports wheel name and path.

src/pip/_internal/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ class UnsupportedWheel(InstallationError):
132132
"""Unsupported wheel."""
133133

134134

135+
class InvalidWheel(InstallationError):
136+
"""Invalid (e.g. corrupt) wheel."""
137+
138+
def __init__(self, location: str, name: str):
139+
self.location = location
140+
self.name = name
141+
142+
def __str__(self) -> str:
143+
return f"Wheel '{self.name}' located at {self.location} is invalid."
144+
145+
135146
class MetadataInconsistent(InstallationError):
136147
"""Built metadata contains inconsistent information.
137148

src/pip/_internal/metadata/pkg_resources.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import email.message
22
import logging
33
from typing import Collection, Iterable, Iterator, List, NamedTuple, Optional
4+
from zipfile import BadZipFile
45

56
from pip._vendor import pkg_resources
67
from pip._vendor.packaging.requirements import Requirement
78
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
89
from pip._vendor.packaging.version import parse as parse_version
910

11+
from pip._internal.exceptions import InvalidWheel
1012
from pip._internal.utils import misc # TODO: Move definition here.
1113
from pip._internal.utils.packaging import get_installer, get_metadata
1214
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
@@ -34,8 +36,16 @@ def __init__(self, dist: pkg_resources.Distribution) -> None:
3436

3537
@classmethod
3638
def from_wheel(cls, wheel: Wheel, name: str) -> "Distribution":
37-
with wheel.as_zipfile() as zf:
38-
dist = pkg_resources_distribution_for_wheel(zf, name, wheel.location)
39+
"""Load the distribution from a given wheel.
40+
41+
:raises InvalidWheel: Whenever loading of the wheel causes a
42+
:py:exc:`zipfile.BadZipFile` exception to be thrown.
43+
"""
44+
try:
45+
with wheel.as_zipfile() as zf:
46+
dist = pkg_resources_distribution_for_wheel(zf, name, wheel.location)
47+
except BadZipFile as e:
48+
raise InvalidWheel(wheel.location, name) from e
3949
return cls(dist)
4050

4151
@property

0 commit comments

Comments
 (0)