Repository: home-assistant/core
File: homeassistant/util/ssl.py (Python)

Code before:

    return sslcontext


@cache
def _client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    # Cached version of _create_client_context
    return _create_client_context(ssl_cipher_list, alpn_protocols)


# Pre-warm the cache for ALL SSL context configurations at module load time.
# This is critical because creating SSL contexts loads certificates from disk,
# which is blocking I/O that must not happen in the event loop.
_SSL_ALPN_PROTOCOLS = (SSL_ALPN_NONE, SSL_ALPN_HTTP1, SSL_ALPN_HTTP2)
for _cipher in SSLCipherList:
    for _alpn in _SSL_ALPN_PROTOCOLS:
        _client_context(_cipher, _alpn)
        _client_context_no_verify(_cipher, _alpn)


def get_default_context() -> ssl.SSLContext:
    """Return the default SSL context."""
    return _client_context(SSLCipherList.PYTHON_DEFAULT, SSL_ALPN_HTTP1)


def get_default_no_verify_context() -> ssl.SSLContext:
    """Return the default SSL context that does not verify the server certificate."""
    return _client_context_no_verify(SSLCipherList.PYTHON_DEFAULT, SSL_ALPN_HTTP1)


def client_context_no_verify(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    """Return a SSL context with no verification with a specific ssl cipher."""
    return _client_context_no_verify(ssl_cipher_list, alpn_protocols)


def client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    """Return an SSL context for making requests."""
    return _client_context(ssl_cipher_list, alpn_protocols)


def create_client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,

Review comment (performance):
`_client_context` has `@cache` so the extra dict was unneeded complexity

Code after:

    return sslcontext


@cache
def _client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    # Cached version of _create_client_context
    return _create_client_context(ssl_cipher_list, alpn_protocols)


# Pre-warm the cache for ALL SSL context configurations at module load time.
# This is critical because creating SSL contexts loads certificates from disk,
# which is blocking I/O that must not happen in the event loop.
_SSL_ALPN_PROTOCOLS = (SSL_ALPN_NONE, SSL_ALPN_HTTP11, SSL_ALPN_HTTP11_HTTP2)
for _cipher in SSLCipherList:
    for _alpn in _SSL_ALPN_PROTOCOLS:
        _client_context(_cipher, _alpn)
        _client_context_no_verify(_cipher, _alpn)


def get_default_context() -> ssl.SSLContext:
    """Return the default SSL context."""
    return _client_context(SSLCipherList.PYTHON_DEFAULT, SSL_ALPN_HTTP11)


def get_default_no_verify_context() -> ssl.SSLContext:
    """Return the default SSL context that does not verify the server certificate."""
    return _client_context_no_verify(SSLCipherList.PYTHON_DEFAULT, SSL_ALPN_HTTP11)


def client_context_no_verify(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    """Return a SSL context with no verification with a specific ssl cipher."""
    return _client_context_no_verify(ssl_cipher_list, alpn_protocols)


def client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,
    alpn_protocols: SSLALPNProtocols = SSL_ALPN_NONE,
) -> ssl.SSLContext:
    """Return an SSL context for making requests."""
    return _client_context(ssl_cipher_list, alpn_protocols)


def create_client_context(
    ssl_cipher_list: SSLCipherList = SSLCipherList.PYTHON_DEFAULT,