inject_securetransport.py 810 B

123456789101112131415161718192021222324252627282930313233343536
  1. """A helper module that injects SecureTransport, on import.
  2. The import should be done as early as possible, to ensure all requests and
  3. sessions (or whatever) are created after injecting SecureTransport.
  4. Note that we only do the injection on macOS, when the linked OpenSSL is too
  5. old to handle TLSv1.2.
  6. """
  7. import sys
  8. def inject_securetransport():
  9. # type: () -> None
  10. # Only relevant on macOS
  11. if sys.platform != "darwin":
  12. return
  13. try:
  14. import ssl
  15. except ImportError:
  16. return
  17. # Checks for OpenSSL 1.0.1
  18. if ssl.OPENSSL_VERSION_NUMBER >= 0x1000100f:
  19. return
  20. try:
  21. from pip._vendor.urllib3.contrib import securetransport
  22. except (ImportError, OSError):
  23. return
  24. securetransport.inject_into_urllib3()
  25. inject_securetransport()