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

Fix unconditional usage of requests import #21523

Merged
merged 1 commit into from
Mar 13, 2024
Merged
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
23 changes: 19 additions & 4 deletions tools/ports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,27 @@ def fetch_project(name, url, sha512hash=None):
def retrieve():
# retrieve from remote server
logger.info(f'retrieving port: {name} from {url}')

# Attempt to use the `requests` module rather `urllib`.
# The main difference here is that `requests` will use the `certifi`
# certificate chain whereas `urllib` will use the system openssl
# certificate chain, which can be out-of-date on some macOS systems.
# TODO(sbc): Perhaps we can remove this at some point when we no
# longer support such out-of-date systems.
try:
import requests
response = requests.get(url)
data = response.content
except (ImportError, requests.exceptions.InvalidSchema):
# requests does not support 'file://' protocol and raises InvalidSchema
try:
response = requests.get(url)
data = response.content
except requests.exceptions.InvalidSchema:
# requests does not support 'file://' protocol and raises InvalidSchema
pass
except ImportError:
pass

# If we don't have `requests` or if we got InvalidSchema then fall
# back to `urllib`.
if not data:
from urllib.request import urlopen
f = urlopen(url)
data = f.read()
Expand Down
Loading