inject_securetransport.py 795 B

1234567891011121314151617181920212223242526272829303132333435
  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() -> None:
  9. # Only relevant on macOS
  10. if sys.platform != "darwin":
  11. return
  12. try:
  13. import ssl
  14. except ImportError:
  15. return
  16. # Checks for OpenSSL 1.0.1
  17. if ssl.OPENSSL_VERSION_NUMBER >= 0x1000100F:
  18. return
  19. try:
  20. from pip._vendor.urllib3.contrib import securetransport
  21. except (ImportError, OSError):
  22. return
  23. securetransport.inject_into_urllib3()
  24. inject_securetransport()