_compat.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. The _compat module is used for code which requires branching between different
  3. Python environments. It is excluded from the code coverage checks.
  4. """
  5. import ssl
  6. import sys
  7. # Brotli support is optional
  8. # The C bindings in `brotli` are recommended for CPython.
  9. # The CFFI bindings in `brotlicffi` are recommended for PyPy and everything else.
  10. try:
  11. import brotlicffi as brotli
  12. except ImportError: # pragma: no cover
  13. try:
  14. import brotli
  15. except ImportError:
  16. brotli = None
  17. if sys.version_info >= (3, 10) or ssl.OPENSSL_VERSION_INFO >= (1, 1, 0, 7):
  18. def set_minimum_tls_version_1_2(context: ssl.SSLContext) -> None:
  19. # The OP_NO_SSL* and OP_NO_TLS* become deprecated in favor of
  20. # 'SSLContext.minimum_version' from Python 3.7 onwards, however
  21. # this attribute is not available unless the ssl module is compiled
  22. # with OpenSSL 1.1.0g or newer.
  23. # https://docs.python.org/3.10/library/ssl.html#ssl.SSLContext.minimum_version
  24. # https://docs.python.org/3.7/library/ssl.html#ssl.SSLContext.minimum_version
  25. context.minimum_version = ssl.TLSVersion.TLSv1_2
  26. else:
  27. def set_minimum_tls_version_1_2(context: ssl.SSLContext) -> None:
  28. # If 'minimum_version' isn't available, we configure these options with
  29. # the older deprecated variants.
  30. context.options |= ssl.OP_NO_SSLv2
  31. context.options |= ssl.OP_NO_SSLv3
  32. context.options |= ssl.OP_NO_TLSv1
  33. context.options |= ssl.OP_NO_TLSv1_1
  34. __all__ = ["brotli", "set_minimum_tls_version_1_2"]