Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/httpx/_compat.py: 62%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2The _compat module is used for code which requires branching between different
3Python environments. It is excluded from the code coverage checks.
4"""
6import re
7import ssl
8import sys
9from types import ModuleType
10from typing import Optional
12# Brotli support is optional
13# The C bindings in `brotli` are recommended for CPython.
14# The CFFI bindings in `brotlicffi` are recommended for PyPy and everything else.
15try:
16 import brotlicffi as brotli
17except ImportError: # pragma: no cover
18 try:
19 import brotli
20 except ImportError:
21 brotli = None
23# Zstandard support is optional
24zstd: Optional[ModuleType] = None
25try:
26 import zstandard as zstd
27except (AttributeError, ImportError, ValueError): # Defensive:
28 zstd = None
29else:
30 # The package 'zstandard' added the 'eof' property starting
31 # in v0.18.0 which we require to ensure a complete and
32 # valid zstd stream was fed into the ZstdDecoder.
33 # See: https://github.com/urllib3/urllib3/pull/2624
34 _zstd_version = tuple(
35 map(int, re.search(r"^([0-9]+)\.([0-9]+)", zstd.__version__).groups()) # type: ignore[union-attr]
36 )
37 if _zstd_version < (0, 18): # Defensive:
38 zstd = None
41if sys.version_info >= (3, 10) or ssl.OPENSSL_VERSION_INFO >= (1, 1, 0, 7):
43 def set_minimum_tls_version_1_2(context: ssl.SSLContext) -> None:
44 # The OP_NO_SSL* and OP_NO_TLS* become deprecated in favor of
45 # 'SSLContext.minimum_version' from Python 3.7 onwards, however
46 # this attribute is not available unless the ssl module is compiled
47 # with OpenSSL 1.1.0g or newer.
48 # https://docs.python.org/3.10/library/ssl.html#ssl.SSLContext.minimum_version
49 # https://docs.python.org/3.7/library/ssl.html#ssl.SSLContext.minimum_version
50 context.minimum_version = ssl.TLSVersion.TLSv1_2
52else:
54 def set_minimum_tls_version_1_2(context: ssl.SSLContext) -> None:
55 # If 'minimum_version' isn't available, we configure these options with
56 # the older deprecated variants.
57 context.options |= ssl.OP_NO_SSLv2
58 context.options |= ssl.OP_NO_SSLv3
59 context.options |= ssl.OP_NO_TLSv1
60 context.options |= ssl.OP_NO_TLSv1_1
63__all__ = ["brotli", "set_minimum_tls_version_1_2"]