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

Add hardcoded paths to macOS frameworks for Big Sur compatibility. #1905

Merged
merged 9 commits into from
Jul 19, 2020
39 changes: 28 additions & 11 deletions src/urllib3/contrib/_securetransport/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,11 @@
c_bool,
)
from ctypes import CDLL, POINTER, CFUNCTYPE
from urllib3.packages.six import raise_from


security_path = find_library("Security")
sheagcraig marked this conversation as resolved.
Show resolved Hide resolved
if not security_path:
raise ImportError("The library Security could not be found")


core_foundation_path = find_library("CoreFoundation")
if not core_foundation_path:
raise ImportError("The library CoreFoundation could not be found")

if platform.system() != "Darwin":
raise ImportError("Only macOS is supported")

version = platform.mac_ver()[0]
pquentin marked this conversation as resolved.
Show resolved Hide resolved
version_info = tuple(map(int, version.split(".")))
Expand All @@ -65,8 +59,31 @@
% (version_info[0], version_info[1])
)

Security = CDLL(security_path, use_errno=True)
CoreFoundation = CDLL(core_foundation_path, use_errno=True)

def load_cdll(name, macos10_16_path):
"""Loads a CDLL by name, falling back to known path on 10.16+"""
try:
# Big Sur is technically 11 but we use 10.16 due to the Big Sur
# beta being labeled as 10.16.
if version_info >= (10, 16):
path = macos10_16_path
else:
path = find_library(name)
if not path:
raise OSError # Caught and reraised as 'ImportError'
return CDLL(path, use_errno=True)
except OSError:
raise_from(ImportError("The library %s failed to load" % name), None)


Security = load_cdll(
"Security", "/System/Library/Frameworks/Security.framework/Security"
)
CoreFoundation = load_cdll(
"CoreFoundation",
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
)


Boolean = c_bool
CFIndex = c_long
Expand Down