ENH Show informative error message when fetching wheel fails (#2175)

Co-authored-by: Roman Yurchak <rth.yurchak@gmail.com>
This commit is contained in:
Gyeongjae Choi 2022-02-15 20:20:24 +09:00 committed by GitHub
parent 32f9d791be
commit 55fbd32ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -113,11 +113,16 @@ be the case if the wheels is made using standard Python tools (`pip wheel`,
All required dependencies must have been previously installed with {mod}`micropip`
or {any}`pyodide.loadPackage`.
If the file is on a remote server, the server must set Cross-Origin Resource Sharing
(CORS) headers to allow access. Otherwise, you can prepend a CORS proxy to the
```{admonition} Cross-Origin Resource Sharing (CORS)
:class: info
If the file is on a remote server, the server must set
[Cross-Origin Resource Sharing (CORS) headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
to allow access. Otherwise, you can prepend a CORS proxy to the
URL. Note however that using third-party CORS proxies has security implications,
particularly since we are not able to check the file integrity, unlike with
installs from PyPI.
```
## Example

View File

@ -295,7 +295,20 @@ class _PackageManager:
async def add_wheel(self, name, wheel, version, extras, ctx, transaction):
transaction["locked"][name] = PackageMetadata(name=name, version=version)
wheel_bytes = await fetch_bytes(wheel["url"])
try:
wheel_bytes = await fetch_bytes(wheel["url"])
except Exception as e:
if wheel["url"].startswith("https://files.pythonhosted.org/"):
raise e
else:
raise ValueError(
f"Couldn't fetch wheel from '{wheel['url']}'."
"One common reason for this is when the server blocks "
"Cross-Origin Resource Sharing (CORS)."
"Check if the server is sending the correct 'Access-Control-Allow-Origin' header."
) from e
wheel["wheel_bytes"] = wheel_bytes
with ZipFile(io.BytesIO(wheel_bytes)) as zip_file: # type: ignore

View File

@ -345,6 +345,22 @@ def test_install_keep_going(monkeypatch):
)
def test_fetch_wheel_fail(monkeypatch):
pytest.importorskip("packaging")
from micropip import _micropip
def _mock_fetch_bytes(*args, **kwargs):
raise Exception("Failed to fetch")
monkeypatch.setattr(_micropip, "fetch_bytes", _mock_fetch_bytes)
msg = "Access-Control-Allow-Origin"
with pytest.raises(ValueError, match=msg):
asyncio.get_event_loop().run_until_complete(
_micropip.install("htps://x.com/xxx-1.0.0-py3-none-any.whl")
)
def test_list_pypi_package(monkeypatch):
pytest.importorskip("packaging")
from micropip import _micropip