Catch + report BadZipFile errors in get_wheel_distribution by lukasjuhrich · Pull Request #10535 · pypa/pip (original) (raw)

Catch BadZipFile errors and re-raise with some context information.
No functional change, hence no news entry.

I'm facing a few nasty CI bugs where the pip install command fails with an uncaught BadZipFile exception. A stacktrace looks like this:

  […N similar lines…]
   Building wheel for wrapt (setup.py): started
  Building wheel for wrapt (setup.py): finished with status 'done'
  Created wheel for wrapt: filename=wrapt-1.12.1-cp39-cp39-linux_x86_64.whl size=78277 sha256=337cd2663d837fd8364f46d472964afd4b05eb1fdf68361b26245a866e714227
  Stored in directory: /opt/pycroft/.cache/pip/wheels/98/23/68/efe259aaca055e93b08e74fbe512819c69a2155c11ba3c0f10
ERROR: Exception:
Traceback (most recent call last):
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 173, in _main
    status = self.run(options, args)
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 203, in wrapper
    return func(self, options, args)
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/commands/install.py", line 342, in run
    _, build_failures = build(
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/wheel_builder.py", line 336, in build
    wheel_file = _build_one(
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/wheel_builder.py", line 228, in _build_one
    _verify_one(req, wheel_path)
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/wheel_builder.py", line 177, in _verify_one
    dist = get_wheel_distribution(wheel_path, canonical_name)
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/metadata/__init__.py", line 48, in get_wheel_distribution
    return Distribution.from_wheel(wheel_path, canonical_name)
  File "/opt/pycroft/venv/lib/python3.9/site-packages/pip/_internal/metadata/pkg_resources.py", line 43, in from_wheel
    with zipfile.ZipFile(path, allowZip64=True) as zf:
  File "/usr/local/lib/python3.9/zipfile.py", line 1257, in __init__
    self._RealGetContents()
  File "/usr/local/lib/python3.9/zipfile.py", line 1324, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Since the information which file caused the error is missing, I don't have any means to further narrow down this issue (e.g. check which files are affected, compare checksums, etc). This change should help me and anyone else facing such issues.

Please note that I have no clue about PIP internals; feel free to make any changes if e.g. the error message is factually incorrect.