Skip to content

Commit 1295364

Browse files
committed
Fix unconditional usage of requests import
On systems that don't have the requests python we cannot catch this exception.
1 parent 5f9ee69 commit 1295364

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tools/ports/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,27 @@ def fetch_project(name, url, sha512hash=None):
296296
def retrieve():
297297
# retrieve from remote server
298298
logger.info(f'retrieving port: {name} from {url}')
299+
300+
# Attempt to use the `requests` module rather `urllib`.
301+
# The main difference here is that `requests` will use the `certifi`
302+
# certificate chain whereas `urllib` will use the system openssl
303+
# certificate chain, which can be out-of-date on some macOS system.
304+
# TODO(sbc): Perhaps we can remove this at some point when we no
305+
# longer support such out-of-date systems.
299306
try:
300307
import requests
301-
response = requests.get(url)
302-
data = response.content
303-
except (ImportError, requests.exceptions.InvalidSchema):
304-
# requests does not support 'file://' protocol and raises InvalidSchema
308+
try:
309+
response = requests.get(url)
310+
data = response.content
311+
except requests.exceptions.InvalidSchema:
312+
# requests does not support 'file://' protocol and raises InvalidSchema
313+
pass
314+
except ImportError:
315+
pass
316+
317+
# If we don't have `requests` or if we got InvalidSchema then fall
318+
# back to `urllib`.
319+
if not data:
305320
from urllib.request import urlopen
306321
f = urlopen(url)
307322
data = f.read()

0 commit comments

Comments
 (0)