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
41 changes: 28 additions & 13 deletions src/urllib3/contrib/_securetransport/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from __future__ import absolute_import

import platform
from ctypes.util import find_library
from ctypes import (
c_void_p,
c_int32,
Expand All @@ -45,28 +44,44 @@
c_bool,
)
from ctypes import CDLL, POINTER, CFUNCTYPE
from ctypes.util import find_library


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")
from urllib3.packages.six import raise_from


version = platform.mac_ver()[0]
pquentin marked this conversation as resolved.
Show resolved Hide resolved
version_info = tuple(map(int, version.split(".")))
if version_info < (10, 8):
if platform.system() != "Darwin" or version_info < (10, 8):
sheagcraig marked this conversation as resolved.
Show resolved Hide resolved
raise OSError(
"Only OS X 10.8 and newer are supported, not %s.%s"
% (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
24 changes: 24 additions & 0 deletions test/contrib/test_securetransport_big_sur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
import mock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this test script now :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIth the changes to retain the pre-Big Sur behavior, do it still make sense to take the test out?



class TestSecureTransportBigSur(object):
"""
Tests for macOS Big Sur's dynamic linker
"""

@mock.patch("platform.mac_ver", return_value=("10.16", ("", "", ""), "x86_64"))
def test_import_current_version(self, mock_mac_ver):
try:
import urllib3.contrib.securetransport as securetransport
except ImportError:
securetransport = None
assert securetransport

@mock.patch("platform.mac_ver", return_value=("11.0", ("", "", ""), "x86_64"))
def test_import_future_version(self, mock_mac_ver):
try:
import urllib3.contrib.securetransport as securetransport
except ImportError:
securetransport = None
assert securetransport