Conditionally import ssl
Saves >=10ms on irrelevant platforms.
@@ -7,18 +7,30 @@ Note that we only do the injection on macOS, when the linked OpenSSL is too
old to handle TLSv1.2.
"""
+import sys
+
+
+def inject_securetransport():
+ # type: () -> None
+ # Only relevant on macOS
+ if sys.platform != "darwin":
+ return
+
try:
import ssl
except ImportError:
- pass
-else:
- import sys
+ return
+
+ # Checks for OpenSSL 1.0.1
+ if ssl.OPENSSL_VERSION_NUMBER >= 0x1000100f:
+ return
- # Checks for OpenSSL 1.0.1 on MacOS
- if sys.platform == "darwin" and ssl.OPENSSL_VERSION_NUMBER < 0x1000100f:
try:
from pip._vendor.urllib3.contrib import securetransport
except (ImportError, OSError):
- pass
- else:
+ return
+
securetransport.inject_into_urllib3()
+
+
+inject_securetransport()
