Skip to content

Commit 8f244dd

Browse files
committed
Add handling for PyPI 404
Now that warehouse set cors to 404, (pypi/warehouse#16339) we need to change the checked exceptions as there is no more network errors.
1 parent c7dfa4e commit 8f244dd

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

micropip/_compat/_compat_in_pyodide.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
if TYPE_CHECKING:
66
pass
77

8+
import pyodide
9+
from packaging.version import parse
810
from pyodide._package_loader import get_dynlibs
911
from pyodide.ffi import IN_BROWSER, to_js
10-
from pyodide.http import HttpStatusError, pyfetch
12+
13+
if parse(pyodide.__version__) > parse("0.27"):
14+
from pyodide.http import HttpStatusError, pyfetch
15+
else:
16+
17+
class HttpStatusError(Exception): # type: ignore [no-redef]
18+
"""we just want this to be defined, it is never going to be raised"""
19+
20+
pass
21+
22+
from pyodide.http import pyfetch
1123

1224
from .compatibility_layer import CompatibilityLayer
1325

micropip/package_index.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from packaging.utils import InvalidWheelFilename
1111
from packaging.version import InvalidVersion, Version
1212

13-
from ._compat import fetch_string_and_headers
13+
from ._compat import HttpStatusError, fetch_string_and_headers
1414
from ._utils import is_package_compatible, parse_version
1515
from .externals.mousebender.simple import from_project_details_html
1616
from .wheelinfo import WheelInfo
@@ -276,11 +276,33 @@ async def query_package(
276276

277277
try:
278278
metadata, headers = await fetch_string_and_headers(url, _fetch_kwargs)
279+
except HttpStatusError as e:
280+
if e.status_code == 404:
281+
continue
282+
raise
279283
except OSError:
280-
continue
284+
# temporary pyodide compatibility.
285+
# pypi now set proper CORS on 404 (https://github.com/pypi/warehouse/pull/16339),
286+
# but stable pyodide (<0.27) does not yet have proper HttpStatusError exception
287+
# so when: on pyodide and 0.26.x we ignore OSError. Once we drop support for 0.26
288+
# all this OSError except clause should just be gone.
289+
try:
290+
import pyodide
291+
from packaging.version import parse
292+
293+
if parse(pyodide.__version__) > parse("0.27"):
294+
# reraise on more recent pyodide.
295+
raise
296+
continue
297+
except ImportError:
298+
# not in pyodide.
299+
raise
281300

282301
content_type = headers.get("content-type", "").lower()
283-
parser = _select_parser(content_type, name)
302+
try:
303+
parser = _select_parser(content_type, name)
304+
except ValueError as e:
305+
raise ValueError(f"Error trying to decode url: {url}") from e
284306
return parser(metadata)
285307
else:
286308
raise ValueError(

0 commit comments

Comments
 (0)